# | 제출 시각 | 아이디 | 문제 | 언어 | 결과 | 실행 시간 | 메모리 |
---|---|---|---|---|---|---|---|
340354 | beksultan04 | 원형 문자열 (IZhO13_rowords) | C++14 | 1782 ms | 8940 KiB |
이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#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);
}
}
}
if (n*m*n > 4e8){
cout <<dp[n][m];
ret 0;
}
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;
}
컴파일 시 표준 에러 (stderr) 메시지
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |