forked from sitz/UVa-Online-Judge
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy path10029.cpp
114 lines (105 loc) · 1.68 KB
/
10029.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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
#include <bits/stdc++.h>
using namespace std;
const int N = 26000;
char str[N][20];
int dp[N];
void dele(char *str, char *res, int pos)
{
int len = strlen(str), cnt = 0;
for (int i = 0; i < len; i++)
if (i != pos)
res[cnt++] = str[i];
res[cnt] = '\0';
}
void repla(char *str, char *res, int pos, char letter)
{
int len = strlen(str);
for (int i = 0; i < len; i++)
{
if (i == pos)
{
res[i] = letter;
}
else
{
res[i] = str[i];
}
}
res[len] = '\0';
}
void add(char *str, char *res, int pos, char letter)
{
int len = strlen(str), cnt = 0;
for (int i = 0; i <= len; i++)
{
if (i == pos)
res[cnt++] = letter;
if (i < len)
res[cnt++] = str[i];
}
str[cnt] = '\0';
}
int bin_srch(int start, int end, char *a)
{
while (start <= end)
{
int mid = start + (end - start) / 2;
int res = strcmp(a, str[mid]);
if (res < 0) // smaller
{
end = mid - 1;
}
else if (res == 0) // equal: found val
{
return mid;
}
else // larger
{
start = mid + 1;
}
}
return -1;
}
void fun(char *a, int pos)
{
int pre = bin_srch(0, pos - 1, a);
if (pre != -1)
{
dp[pos] = max(dp[pos], dp[pre] + 1);
}
}
int main()
{
int n = 0, imax = 0;
while (scanf("%s", str[n]) != EOF)
{
n++;
}
memset(dp, 0, sizeof(dp));
for (int i = 1; i < n; i++)
{
int len = strlen(str[i]);
for (int j = 0; j <= len; j++)
{
char temp[20] = {0};
for (char k = 'a'; k <= 'z'; (int)k++)
{
if (j < len)
{
repla(str[i], temp, j, k);
fun(temp, i);
}
add(str[i], temp, j, k);
fun(temp, i);
}
if (j < len)
{
dele(str[i], temp, j);
fun(temp, i);
}
}
imax = max(imax, dp[i]);
}
printf("%d\n", imax + 1);
return 0;
}