#include<iostream>
#include<cstring>
using namespace std;
int n, m, i, j, sol, x, ii, jj, val;
int d[3005][3005], ds[3005][3005];
char a[3005], b[3005];
void solve(int t){
for(i = n; i >= 1; i--){
for(j = m; j >= 1; j--){
if(a[i] == b[j]){
d[i][j] = 1 + d[i + 1][j + 1];
x = i + d[i][j] - 1;
ds[x][j] = max(ds[x][j], d[i][j]);
}
}
}
for(i = 1; i <= n; i++){
for(j = m; j >= 1; j--){
ds[i][j] = max(ds[i][j], ds[i - 1][j + 1]);
}
}
for(i = 1; i <= n; i++){
for(j = 1; j <= m; j++){
val = d[i][j] + ds[i - 1][ j + d[i][j] ];
if(val > sol){
sol = val;
if(t == 0){
jj = j;
}
else{
jj = j + val - 1;
jj = m - jj + 1;
}
ii = i - ds[i - 1][ j + d[i][j] ];
}
sol = max(sol, d[i][j] + ds[i - 1][ j + d[i][j] ]);
}
}
}
int main(){
cin>> a + 1;
n = strlen(a + 1);
cin>> b + 1;
m = strlen(b + 1);
solve(0);
memset(d, 0, sizeof(d) );
memset(ds, 0, sizeof(ds) );
for(i = 1, j = m; i < j; i++, j--){
swap(b[i], b[j]);
}
solve(1);
cout<< sol <<"\n";
cout<< ii - 1 <<" "<< jj - 1 <<"\n";
}
Compilation message
necklace.cpp: In function 'int main()':
necklace.cpp:41:13: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
41 | cin>> a + 1;
| ~~^~~
necklace.cpp:43:13: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
43 | cin>> b + 1;
| ~~^~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
43 ms |
71032 KB |
Output is correct |
2 |
Correct |
42 ms |
71032 KB |
Output is correct |
3 |
Incorrect |
43 ms |
71032 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
43 ms |
71032 KB |
Output is correct |
2 |
Correct |
42 ms |
71032 KB |
Output is correct |
3 |
Incorrect |
43 ms |
71032 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
43 ms |
71032 KB |
Output is correct |
2 |
Correct |
42 ms |
71032 KB |
Output is correct |
3 |
Incorrect |
43 ms |
71032 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |