#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
#include <limits.h>
#include <stack>
#include <queue>
#include <map>
#include <set>
#include <complex>
#include <algorithm>
#include <string>
#include <functional>
#include <vector>
#include <numeric>
#include <deque>
#include <utility>
#include <bitset>
#include <iostream>
using namespace std;
typedef long long lint;
typedef long double llf;
typedef pair<int, int> pi;
typedef complex<double> pnt;
string s1, s2;
int dp[4005][2005];
int nxt[4005][2005];
int n, m;
void reroot(int px){
int py = 1;
while(py <= m && nxt[px][py] != 2) py++;
nxt[px][py] = 1;
while(px < 2 * n && py < m){
if(nxt[px+1][py] == 3){
px++;
nxt[px][py] = 1;
}
else if(nxt[px+1][py+1] == 2){
px++;
py++;
nxt[px][py] = 1;
}
else py++;
}
while(px < 2 * n && nxt[px+1][py] == 3){
px++;
nxt[px][py] = 1;
}
}
int track(int x, int y, int e){
int ret = 0;
while(y != 0 && x != e){
if(nxt[x][y] == 1) y--;
else if(nxt[x][y] == 2) ret += (s1[x] == s2[y]), x--, y--;
else if(nxt[x][y] == 3) x--;
}
return ret;
}
int solve(string a, string b){
n = a.size(), m = b.size();
s1 = "#" + a + a;
s1 = '#' + b;
for(int i=0; i<=2*n; i++){
for(int j=0; j<=m; j++){
if(j == 0){
nxt[i][j] = 3;
continue;
}
if(i == 0){
nxt[i][j] = 1;
continue;
}
dp[i][j] = -1;
if(dp[i][j] < dp[i][j-1]){
dp[i][j] = dp[i][j-1];
nxt[i][j] = 1;
}
if(dp[i][j] < dp[i-1][j-1] + (s1[i] == s2[j])){
dp[i][j] = dp[i-1][j-1] + (s1[i] == s2[j]);
nxt[i][j] = 2;
}
if(dp[i][j] < dp[i-1][j]){
dp[i][j] = dp[i-1][j];
nxt[i][j] = 3;
}
}
}
int ret = dp[n][m];
for(int i=1; i<n; i++){
reroot(i);
ret = max(ret, track(n+i, m, i));
}
return ret;
}
int main(){
string a, b;
cin >> a >> b;
int ret = solve(a, b);
reverse(b.begin(), b.end());
ret = max(ret, solve(a, b));
cout << ret;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
2 ms |
552 KB |
Output isn't correct |
2 |
Incorrect |
2 ms |
504 KB |
Output isn't correct |
3 |
Incorrect |
2 ms |
632 KB |
Output isn't correct |
4 |
Incorrect |
3 ms |
1144 KB |
Output isn't correct |
5 |
Correct |
3 ms |
1016 KB |
Output is correct |
6 |
Incorrect |
36 ms |
17944 KB |
Output isn't correct |
7 |
Incorrect |
59 ms |
31736 KB |
Output isn't correct |
8 |
Incorrect |
58 ms |
31736 KB |
Output isn't correct |
9 |
Incorrect |
59 ms |
31736 KB |
Output isn't correct |
10 |
Incorrect |
58 ms |
31736 KB |
Output isn't correct |
11 |
Incorrect |
69 ms |
34936 KB |
Output isn't correct |
12 |
Incorrect |
79 ms |
38008 KB |
Output isn't correct |
13 |
Incorrect |
79 ms |
38008 KB |
Output isn't correct |
14 |
Incorrect |
73 ms |
36088 KB |
Output isn't correct |
15 |
Incorrect |
83 ms |
39132 KB |
Output isn't correct |
16 |
Incorrect |
66 ms |
34168 KB |
Output isn't correct |
17 |
Incorrect |
98 ms |
36572 KB |
Output isn't correct |
18 |
Incorrect |
118 ms |
44920 KB |
Output isn't correct |
19 |
Incorrect |
60 ms |
31736 KB |
Output isn't correct |
20 |
Incorrect |
96 ms |
40056 KB |
Output isn't correct |
21 |
Incorrect |
126 ms |
31736 KB |
Output isn't correct |
22 |
Incorrect |
105 ms |
36316 KB |
Output isn't correct |
23 |
Incorrect |
111 ms |
39132 KB |
Output isn't correct |
24 |
Incorrect |
125 ms |
41468 KB |
Output isn't correct |
25 |
Incorrect |
182 ms |
46616 KB |
Output isn't correct |