#include <bits/stdc++.h>
#define DIM 4010
using namespace std;
char a[DIM],b[DIM],a2[DIM],b2[DIM];
int dp[DIM][DIM],t[DIM][DIM];
int n,m,i,j,sol;
void solve (char a[], char b[]){
memset (dp,0,sizeof dp);
for (i=1;i<=n;i++)
for (j=1;j<=m;j++){
if (dp[i][j-1] > dp[i][j]){
dp[i][j] = dp[i][j-1];
t[i][j] = 1;
}
if (a[i] == b[j] && dp[i-1][j-1] + 1 > dp[i][j]){
dp[i][j] = dp[i-1][j-1] + 1;
t[i][j] = 2;
}
if (dp[i-1][j] > dp[i][j]){
dp[i][j] = dp[i-1][j];
t[i][j] = 3;
}
}
for (int poz=n+1;poz<=2*n;poz++){
/// sterg - n u s t i u s a f a c a s t a a j u t o r
i = poz - n, j = 1;
for (j=1;j<=m;j++)
dp[i][j] = 0;
for (i=poz-n+1;i<poz;i++)
for (j=1;j<=m;j++){
dp[i][j] = 0;
if (dp[i][j-1] > dp[i][j]){
dp[i][j] = dp[i][j-1];
t[i][j] = 1;
}
if (a[i] == b[j] && dp[i-1][j-1] + 1 > dp[i][j]){
dp[i][j] = dp[i-1][j-1] + 1;
t[i][j] = 2;
}
if (dp[i-1][j] > dp[i][j]){
dp[i][j] = dp[i-1][j];
t[i][j] = 3;
}
}
/// adaug
i = poz;
for (j=1;j<=m;j++){
if (dp[i][j-1] > dp[i][j]){
dp[i][j] = dp[i][j-1];
t[i][j] = 1;
}
if (a[i] == b[j] && dp[i-1][j-1] + 1 > dp[i][j]){
dp[i][j] = dp[i-1][j-1] + 1;
t[i][j] = 2;
}
if (dp[i-1][j] > dp[i][j]){
dp[i][j] = dp[i-1][j];
t[i][j] = 3;
}
}
sol = max (sol,dp[poz][m]);
}
}
int main (){
//ifstream cin ("date.in");
//ofstream cout ("date.out");
cin>>a+1>>b+1;
n = strlen (a+1), m = strlen (b+1);
for (i=1;i<=n;i++)
a2[n-i+1] = a[i];
for (i=1;i<=m;i++)
b2[m-i+1] = b[i];
for (i=1;i<=n;i++)
a[i+n] = a[i];
solve (a,b);
solve (a,b2);
for (i=1;i<=n;i++)
a2[i+n] = a2[i];
solve (a2,b);
cout<<sol;
return 0;
}
Compilation message
rowords.cpp: In function 'int main()':
rowords.cpp:89:11: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
89 | cin>>a+1>>b+1;
| ~^~
rowords.cpp:89:16: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
89 | cin>>a+1>>b+1;
| ~^~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
49 ms |
63340 KB |
Output is correct |
2 |
Correct |
48 ms |
63340 KB |
Output is correct |
3 |
Correct |
53 ms |
63468 KB |
Output is correct |
4 |
Correct |
50 ms |
63340 KB |
Output is correct |
5 |
Correct |
49 ms |
63596 KB |
Output is correct |
6 |
Correct |
1470 ms |
72168 KB |
Output is correct |
7 |
Execution timed out |
2075 ms |
63212 KB |
Time limit exceeded |
8 |
Execution timed out |
2072 ms |
73520 KB |
Time limit exceeded |
9 |
Execution timed out |
2084 ms |
73864 KB |
Time limit exceeded |
10 |
Execution timed out |
2089 ms |
74348 KB |
Time limit exceeded |
11 |
Execution timed out |
2080 ms |
75056 KB |
Time limit exceeded |
12 |
Execution timed out |
2085 ms |
76908 KB |
Time limit exceeded |
13 |
Execution timed out |
2086 ms |
75480 KB |
Time limit exceeded |
14 |
Execution timed out |
2083 ms |
75288 KB |
Time limit exceeded |
15 |
Execution timed out |
2083 ms |
76272 KB |
Time limit exceeded |
16 |
Execution timed out |
2096 ms |
73964 KB |
Time limit exceeded |
17 |
Execution timed out |
2097 ms |
75032 KB |
Time limit exceeded |
18 |
Execution timed out |
2095 ms |
76664 KB |
Time limit exceeded |
19 |
Execution timed out |
2028 ms |
74504 KB |
Time limit exceeded |
20 |
Execution timed out |
2086 ms |
75364 KB |
Time limit exceeded |
21 |
Execution timed out |
2083 ms |
77484 KB |
Time limit exceeded |
22 |
Execution timed out |
2084 ms |
76012 KB |
Time limit exceeded |
23 |
Execution timed out |
2082 ms |
75828 KB |
Time limit exceeded |
24 |
Execution timed out |
2079 ms |
76332 KB |
Time limit exceeded |
25 |
Execution timed out |
2075 ms |
77068 KB |
Time limit exceeded |