#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 = 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:36:13: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
36 | cin>> a + 1;
| ~~^~~
necklace.cpp:38:13: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
38 | cin>> b + 1;
| ~~^~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
40 ms |
71032 KB |
Output is correct |
2 |
Correct |
41 ms |
71032 KB |
Output is correct |
3 |
Correct |
40 ms |
71032 KB |
Output is correct |
4 |
Correct |
40 ms |
71032 KB |
Output is correct |
5 |
Correct |
47 ms |
71032 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
40 ms |
71032 KB |
Output is correct |
2 |
Correct |
41 ms |
71032 KB |
Output is correct |
3 |
Correct |
40 ms |
71032 KB |
Output is correct |
4 |
Correct |
40 ms |
71032 KB |
Output is correct |
5 |
Correct |
47 ms |
71032 KB |
Output is correct |
6 |
Correct |
42 ms |
71032 KB |
Output is correct |
7 |
Correct |
40 ms |
71036 KB |
Output is correct |
8 |
Correct |
43 ms |
71032 KB |
Output is correct |
9 |
Correct |
47 ms |
71032 KB |
Output is correct |
10 |
Correct |
42 ms |
71040 KB |
Output is correct |
11 |
Correct |
41 ms |
71032 KB |
Output is correct |
12 |
Correct |
42 ms |
71032 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
40 ms |
71032 KB |
Output is correct |
2 |
Correct |
41 ms |
71032 KB |
Output is correct |
3 |
Correct |
40 ms |
71032 KB |
Output is correct |
4 |
Correct |
40 ms |
71032 KB |
Output is correct |
5 |
Correct |
47 ms |
71032 KB |
Output is correct |
6 |
Correct |
42 ms |
71032 KB |
Output is correct |
7 |
Correct |
40 ms |
71036 KB |
Output is correct |
8 |
Correct |
43 ms |
71032 KB |
Output is correct |
9 |
Correct |
47 ms |
71032 KB |
Output is correct |
10 |
Correct |
42 ms |
71040 KB |
Output is correct |
11 |
Correct |
41 ms |
71032 KB |
Output is correct |
12 |
Correct |
42 ms |
71032 KB |
Output is correct |
13 |
Correct |
203 ms |
71036 KB |
Output is correct |
14 |
Correct |
147 ms |
71064 KB |
Output is correct |
15 |
Correct |
223 ms |
71060 KB |
Output is correct |
16 |
Correct |
157 ms |
71160 KB |
Output is correct |
17 |
Correct |
121 ms |
71032 KB |
Output is correct |
18 |
Correct |
123 ms |
71032 KB |
Output is correct |
19 |
Correct |
157 ms |
71032 KB |
Output is correct |
20 |
Correct |
183 ms |
71160 KB |
Output is correct |
21 |
Correct |
213 ms |
71056 KB |
Output is correct |