/* https://codeforces.com/blog/entry/21372 */
/* https://arxiv.org/pdf/1208.0396.pdf */
/* https://oj.uz/submission/314177 */
#include <stdio.h>
#include <string.h>
#define N 2000
#define M 2000
int max(int a, int b) { return a > b ? a : b; }
int solve(char *aa, int n, char *bb, int m) {
static int dp[N * 2 + 1][M + 1], dir[N * 2 + 1][M + 1];
int l, i, j, x, ans;
for (i = 0; i <= n * 2; i++)
for (j = 0; j <= m; j++)
if (i == 0 && j == 0)
dp[i][j] = 0, dir[i][j] = -1;
else {
dp[i][j] = dir[i][j] = -1;
if (j > 0 && dp[i][j] < (x = dp[i][j - 1]))
dp[i][j] = x, dir[i][j] = 0;
if (i > 0 && j > 0 && aa[(i - 1) % n] == bb[j - 1] && dp[i][j] < (x = dp[i - 1][j - 1] + 1))
dp[i][j] = x, dir[i][j] = 1;
if (i > 0 && dp[i][j] < (x = dp[i - 1][j]))
dp[i][j] = x, dir[i][j] = 2;
}
ans = 0;
for (l = 0; l < n; l++) {
i = n + l, j = m, x = 0;
while (dir[i][j] != -1)
if (dir[i][j] == 0)
j--;
else if (dir[i][j] == 1)
i--, j--, x++;
else
i--;
ans = max(ans, x);
i = l + 1, j = 0;
dir[i][j] = -1;
while (j < m)
if (dir[i][j + 1] == 0)
j++;
else {
dir[i][j + 1] = 0;
if (i++ == n * 2)
break;
if (dir[i][j + 1] == 1)
j++;
}
}
return ans;
}
int main() {
static char aa[N + 1], bb[M + 1];
int n, m, i, j, ans, tmp;
scanf("%s%s", aa, bb), n = strlen(aa), m = strlen(bb);
ans = solve(aa, n, bb, m);
for (i = 0, j = n - 1; i < j; i++, j--)
tmp = aa[i], aa[i] = aa[j], aa[j] = tmp;
ans = max(ans, solve(aa, n, bb, m));
printf("%d\n", ans);
return 0;
}
Compilation message
rowords.c: In function 'main':
rowords.c:60:2: warning: ignoring return value of 'scanf' declared with attribute 'warn_unused_result' [-Wunused-result]
60 | scanf("%s%s", aa, bb), n = strlen(aa), m = strlen(bb);
| ^~~~~~~~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
424 KB |
Output is correct |
3 |
Correct |
1 ms |
596 KB |
Output is correct |
4 |
Correct |
1 ms |
980 KB |
Output is correct |
5 |
Correct |
1 ms |
980 KB |
Output is correct |
6 |
Correct |
17 ms |
17820 KB |
Output is correct |
7 |
Correct |
40 ms |
31624 KB |
Output is correct |
8 |
Correct |
93 ms |
31632 KB |
Output is correct |
9 |
Correct |
97 ms |
31624 KB |
Output is correct |
10 |
Correct |
86 ms |
31748 KB |
Output is correct |
11 |
Correct |
73 ms |
34764 KB |
Output is correct |
12 |
Correct |
67 ms |
37836 KB |
Output is correct |
13 |
Correct |
116 ms |
37796 KB |
Output is correct |
14 |
Correct |
105 ms |
36008 KB |
Output is correct |
15 |
Correct |
102 ms |
39020 KB |
Output is correct |
16 |
Correct |
107 ms |
34072 KB |
Output is correct |
17 |
Correct |
78 ms |
36428 KB |
Output is correct |
18 |
Correct |
120 ms |
44764 KB |
Output is correct |
19 |
Correct |
70 ms |
31632 KB |
Output is correct |
20 |
Correct |
129 ms |
39964 KB |
Output is correct |
21 |
Correct |
75 ms |
31480 KB |
Output is correct |
22 |
Correct |
103 ms |
36232 KB |
Output is correct |
23 |
Correct |
95 ms |
39116 KB |
Output is correct |
24 |
Correct |
103 ms |
41200 KB |
Output is correct |
25 |
Correct |
127 ms |
46468 KB |
Output is correct |