-
Notifications
You must be signed in to change notification settings - Fork 0
/
smart_phone.cpp
executable file
·58 lines (47 loc) · 1.19 KB
/
smart_phone.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
#include <iostream>
#include <cstring>
#define MAX_WORD_LENGTH 25+1
unsigned int countKeyPress(char wanted[], char typed[])
{
unsigned int presses = 0;
size_t wantedLength = strlen(wanted);
size_t typedLength = strlen(typed);
// Calculate number of deletions
for(unsigned int i=0; i<typedLength; ++i)
{
if(wanted[i] != typed[i])
{
presses += typedLength - i;
break;
}
if(wantedLength < i)
{
presses += typedLength - wantedLength;
}
}
// Calculate number of new characters
presses += wantedLength - (typedLength - presses);
return presses;
}
int main()
{
unsigned int testcases;
std::cin >> testcases;
for(unsigned int i=0; i<testcases; ++i)
{
// Input
static char wanted[MAX_WORD_LENGTH];
std::cin >> wanted;
static char typed[MAX_WORD_LENGTH];
std::cin >> typed;
static char suggestions[3][MAX_WORD_LENGTH];
std::cin >> suggestions[0] >> suggestions[1] >> suggestions[2];
// Calculate
unsigned int result = countKeyPress(wanted, typed);
for(unsigned int j=0; j<3; ++j)
{
result = std::min(result, 1 + countKeyPress(wanted, suggestions[j]));
}
std::cout << result << std::endl;
}
}