#include <bits/stdc++.h>
using namespace std;
//#define int long long
#define pii pair<int,int>
#define OK puts("OK");
#define NO puts("NO");
#define YES puts("YES");
#define fr first
#define sc second
#define ret return
#define scan1(a) scanf("%d",&a);
#define scan2(a,b) scanf("%d %d",&a, &b);
#define scan3(a,b,c) scanf("%d %d %d",&a,&b,&c);
#define all(s) s.begin(),s.end()
#define allr(s) s.rbegin(),s.rend()
#define pb push_back
#define sz(v) (int)v.size()
#define endi puts("");
#define eps 1e-12
const int N = 1e5+12,INF=1e9+7;
int dp[2001][2001];
main(){
string a,b;
int i,j,n,y,m;
cin>>a>>b;
n = a.size();
m = b.size();
if (n > m){
swap(n,m);
swap(a,b);
}
for (i=1;i<=n;++i){
for (j=1;j<=m;++j){
dp[i][j] = max(dp[i][j-1],dp[i-1][j]);
if (a[i-1]==b[j-1]){
dp[i][j] = max(dp[i][j],dp[i-1][j-1]+1);
}
}
}
int ans = dp[n][m];
for (y=0;y<n;++y){
a.pb(a[0]);
a.erase(a.begin());
for (i=1;i<=n;++i){
for (j=1;j<=m;++j){
dp[i][j] = max(dp[i][j-1],dp[i-1][j]);
if (a[i-1]==b[j-1]){
dp[i][j] = max(dp[i][j],dp[i-1][j-1]+1);
}
}
}
ans = max(ans,dp[n][m]);
}
reverse(all(b));
for (y=0;y<n;++y){
a.pb(a[0]);
a.erase(a.begin());
for (i=1;i<=n;++i){
for (j=1;j<=m;++j){
dp[i][j] = max(dp[i][j-1],dp[i-1][j]);
if (a[i-1]==b[j-1]){
dp[i][j] = max(dp[i][j],dp[i-1][j-1]+1);
}
}
}
ans = max(ans,dp[n][m]);
}
cout <<ans;
}
Compilation message
rowords.cpp:22:6: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
22 | main(){
| ^
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
364 KB |
Output is correct |
2 |
Correct |
1 ms |
364 KB |
Output is correct |
3 |
Correct |
1 ms |
364 KB |
Output is correct |
4 |
Correct |
1 ms |
492 KB |
Output is correct |
5 |
Correct |
1 ms |
492 KB |
Output is correct |
6 |
Correct |
82 ms |
1132 KB |
Output is correct |
7 |
Execution timed out |
2073 ms |
8172 KB |
Time limit exceeded |
8 |
Execution timed out |
2031 ms |
8172 KB |
Time limit exceeded |
9 |
Execution timed out |
2069 ms |
8172 KB |
Time limit exceeded |
10 |
Execution timed out |
2074 ms |
8172 KB |
Time limit exceeded |
11 |
Execution timed out |
2082 ms |
8172 KB |
Time limit exceeded |
12 |
Execution timed out |
2081 ms |
8684 KB |
Time limit exceeded |
13 |
Execution timed out |
2076 ms |
8684 KB |
Time limit exceeded |
14 |
Execution timed out |
2065 ms |
8172 KB |
Time limit exceeded |
15 |
Execution timed out |
2073 ms |
8940 KB |
Time limit exceeded |
16 |
Execution timed out |
2090 ms |
8300 KB |
Time limit exceeded |
17 |
Execution timed out |
2090 ms |
4844 KB |
Time limit exceeded |
18 |
Execution timed out |
2100 ms |
7148 KB |
Time limit exceeded |
19 |
Execution timed out |
2097 ms |
8172 KB |
Time limit exceeded |
20 |
Execution timed out |
2097 ms |
7148 KB |
Time limit exceeded |
21 |
Correct |
301 ms |
1516 KB |
Output is correct |
22 |
Correct |
872 ms |
2412 KB |
Output is correct |
23 |
Correct |
1693 ms |
3128 KB |
Output is correct |
24 |
Correct |
1779 ms |
3180 KB |
Output is correct |
25 |
Execution timed out |
2069 ms |
3820 KB |
Time limit exceeded |