#include <bits/stdc++.h>
using namespace std;
const int mx = 4004;
int dp[mx][mx], dir[mx][mx], n, m;
string a, b;
int solve() {
int x, ans = 0;
for (int i = 0; i <= n * 2; i++)
for (int 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 && a[(i - 1) % n] == b[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;
}
}
for (int k=0; k<n; k++) {
int i = n + k, 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 = k + 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() {
cin >> a >> b;
n = (int)a.length(), m = (int)b.length();
int ans = solve();
for (int i=0, j=n-1; i<j; i++, j--) {
char tmp = a[i];
a[i] = a[j];
a[j] = tmp;
}
cout << max(ans, solve());
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
4440 KB |
Output is correct |
2 |
Correct |
1 ms |
4440 KB |
Output is correct |
3 |
Correct |
1 ms |
4444 KB |
Output is correct |
4 |
Correct |
1 ms |
6492 KB |
Output is correct |
5 |
Correct |
1 ms |
6492 KB |
Output is correct |
6 |
Correct |
26 ms |
66360 KB |
Output is correct |
7 |
Correct |
31 ms |
66136 KB |
Output is correct |
8 |
Correct |
69 ms |
66128 KB |
Output is correct |
9 |
Correct |
70 ms |
66180 KB |
Output is correct |
10 |
Correct |
57 ms |
66192 KB |
Output is correct |
11 |
Correct |
61 ms |
72332 KB |
Output is correct |
12 |
Correct |
81 ms |
78480 KB |
Output is correct |
13 |
Correct |
88 ms |
78420 KB |
Output is correct |
14 |
Correct |
68 ms |
74328 KB |
Output is correct |
15 |
Correct |
76 ms |
80548 KB |
Output is correct |
16 |
Correct |
80 ms |
70240 KB |
Output is correct |
17 |
Correct |
61 ms |
94884 KB |
Output is correct |
18 |
Correct |
79 ms |
96852 KB |
Output is correct |
19 |
Correct |
52 ms |
66140 KB |
Output is correct |
20 |
Correct |
86 ms |
86608 KB |
Output is correct |
21 |
Correct |
34 ms |
109244 KB |
Output is correct |
22 |
Correct |
44 ms |
115388 KB |
Output is correct |
23 |
Correct |
52 ms |
115400 KB |
Output is correct |
24 |
Correct |
60 ms |
121556 KB |
Output is correct |
25 |
Correct |
74 ms |
125588 KB |
Output is correct |