#include <bits/stdc++.h>
using namespace std;
const int maxn = 4007;
int dp[maxn][maxn], ty[maxn][maxn];
int n, m;
void dplcs(string s, string t){
memset(dp, 0, sizeof dp);
memset(ty, 0, sizeof ty);
t += t;
for(int i = 1; i <= n; i++){
for(int j = 1; j <= 2*m; j++){
dp[i][j] = dp[i-1][j];
if(s[i-1] == t[j-1] and dp[i-1][j-1]+1 > dp[i][j]) dp[i][j] = dp[i-1][j-1]+1, ty[i][j] = 1;
if(dp[i][j-1] > dp[i][j]) dp[i][j] = dp[i][j-1], ty[i][j] = 2;
}
}
}
int lcs(int a, int b){
int ret = 0;
while(a){
if(ty[a][b] == 1){
ret++;
a--;
b--;
} else if(ty[a][b] == 2) b--;
else a--;
}
return ret;
}
void res(int b){
int a = 0;
while(b < 2 * m){
if(ty[a][b+1] == 2){
ty[a][b+1] = 0;
b++;
} else {
if(a == n) break;
a++;
if(ty[a][b+1] == 1){
ty[a][b+1] = 0;
b++;
}
}
}
}
int solve(string s, string t){
dplcs(s, t);
int ret = 0;
for(int j = 0; j < m; j++){
ret = max(ret, lcs(n, m+j));
res(j);
}
return ret;
}
int main(){
ios::sync_with_stdio(false);
cin.tie(0); cout.tie(0);
string s, t; cin >> s >> t;
n = s.size(); m = t.size();
int sol = solve(s, t);
reverse(t.begin(), t.end());
sol = max(sol, solve(s, t));
cout << sol << '\n';
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
50 ms |
126032 KB |
Output is correct |
2 |
Correct |
57 ms |
126036 KB |
Output is correct |
3 |
Correct |
54 ms |
125928 KB |
Output is correct |
4 |
Correct |
40 ms |
126056 KB |
Output is correct |
5 |
Correct |
48 ms |
126120 KB |
Output is correct |
6 |
Correct |
44 ms |
126040 KB |
Output is correct |
7 |
Correct |
87 ms |
126036 KB |
Output is correct |
8 |
Correct |
82 ms |
126184 KB |
Output is correct |
9 |
Correct |
94 ms |
126000 KB |
Output is correct |
10 |
Correct |
64 ms |
126044 KB |
Output is correct |
11 |
Correct |
67 ms |
126044 KB |
Output is correct |
12 |
Correct |
71 ms |
126092 KB |
Output is correct |
13 |
Correct |
95 ms |
126288 KB |
Output is correct |
14 |
Correct |
79 ms |
126132 KB |
Output is correct |
15 |
Correct |
86 ms |
126032 KB |
Output is correct |
16 |
Correct |
85 ms |
125936 KB |
Output is correct |
17 |
Correct |
54 ms |
126048 KB |
Output is correct |
18 |
Correct |
84 ms |
126140 KB |
Output is correct |
19 |
Correct |
75 ms |
126036 KB |
Output is correct |
20 |
Correct |
77 ms |
126044 KB |
Output is correct |
21 |
Correct |
60 ms |
126084 KB |
Output is correct |
22 |
Correct |
56 ms |
126036 KB |
Output is correct |
23 |
Correct |
61 ms |
125928 KB |
Output is correct |
24 |
Correct |
58 ms |
126068 KB |
Output is correct |
25 |
Correct |
67 ms |
126036 KB |
Output is correct |