#include <bits/stdc++.h>
#define endl "\n"
typedef long long ll ;
using namespace std;
string str, str2 ;
ll n , m , dp1[3001][3001] , dp2[3001][3001] , lcs[3001][3001] ;
array <ll, 3> ans = {0, 0, 0};
int main(){
ios::sync_with_stdio(0);cin.tie(0);
cin >> str >> str2;
ll n = str.size();
ll m = str2.size();
for (ll i = 0; i < n; i++){
for (ll j = 0; j < m; j++){
lcs[i][j] = str[i] == str2[j] ? ((i > 0 && j > 0 ? lcs[i - 1][j - 1] : 0) + 1) : 0;
}
}
for (ll i = 0; i < n; i++){
for (ll j = 0; j < m; j++){
if (lcs[i][j] > 0){
dp1[i][j - lcs[i][j] + 1] = max(dp1[i][j - lcs[i][j] + 1], lcs[i][j]);
dp2[i - lcs[i][j] + 1][j] = max(dp2[i - lcs[i][j] + 1][j], lcs[i][j]);
}
}
}
for (ll i = 0; i < n; i++){
for (ll j = 0; j < m; j++){
if (j > 0) dp1[i][j] = max(dp1[i][j], dp1[i][j - 1] - 1);
if (i > 0) dp2[i][j] = max(dp2[i][j], dp2[i - 1][j] - 1);
}
}
for (ll i = 0; i < n; i++){
for (ll j = 0; j < m; j++){
ll le = dp1[i][j] + (i + 1 < n && j > 0 ? dp2[i + 1][j - 1] : 0);
if (le > 0){
ans = max(ans , {le, i - dp1[i][j] + 1, j + dp1[i][j] - le}) ;
}
}
}
for(ll i = 0 ; i < 3000 ; i++){
for(ll j = 0 ; j < 3000; j++){
dp1[i][j] = dp2[i][j] = lcs[i][j] = 0 ;
}
}
reverse(begin(str2), end(str2));
for (ll i = 0; i < n; i++){
for (ll j = 0; j < m; j++){
lcs[i][j] = str[i] == str2[j] ? ((i > 0 && j > 0 ? lcs[i - 1][j - 1] : 0) + 1) : 0;
}
}
for (ll i = 0; i < n; i++){
for (ll j = 0; j < m; j++){
if (lcs[i][j] > 0){
dp1[i][j - lcs[i][j] + 1] = max(dp1[i][j - lcs[i][j] + 1], lcs[i][j]);
dp2[i - lcs[i][j] + 1][j] = max(dp2[i - lcs[i][j] + 1][j], lcs[i][j]);
}
}
}
for (ll i = 0; i < n; i++){
for (ll j = 0; j < m; j++){
if (j > 0) dp1[i][j] = max(dp1[i][j], dp1[i][j - 1] - 1);
if (i > 0) dp2[i][j] = max(dp2[i][j], dp2[i - 1][j] - 1);
}
}
for (ll i = 0; i < n; i++){
for (ll j = 0; j < m; j++){
ll le = dp1[i][j] + (i + 1 < n && j > 0 ? dp2[i + 1][j - 1] : 0);
if (le > 0){
ans = max(ans , {le, i - dp1[i][j] + 1, (str2.size()) - (j + dp1[i][j] - le) - le}) ;
}
}
}
cout << ans[0] << " " << ans[1] << " " << ans[2] << endl ;
return 0;
}
Compilation message
necklace.cpp: In function 'int main()':
necklace.cpp:70:98: warning: narrowing conversion of '((((long long unsigned int)str2.std::__cxx11::basic_string<char>::size()) - ((long long unsigned int)((j + dp1[i][j]) - le))) - ((long long unsigned int)le))' from 'long long unsigned int' to 'long long int' [-Wnarrowing]
70 | ans = max(ans , {le, i - dp1[i][j] + 1, (str2.size()) - (j + dp1[i][j] - le) - le}) ;
| ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
88 ms |
211780 KB |
Output is correct |
2 |
Correct |
77 ms |
211648 KB |
Output is correct |
3 |
Correct |
78 ms |
211604 KB |
Output is correct |
4 |
Correct |
85 ms |
211652 KB |
Output is correct |
5 |
Correct |
86 ms |
211652 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
88 ms |
211780 KB |
Output is correct |
2 |
Correct |
77 ms |
211648 KB |
Output is correct |
3 |
Correct |
78 ms |
211604 KB |
Output is correct |
4 |
Correct |
85 ms |
211652 KB |
Output is correct |
5 |
Correct |
86 ms |
211652 KB |
Output is correct |
6 |
Correct |
87 ms |
211736 KB |
Output is correct |
7 |
Correct |
83 ms |
211684 KB |
Output is correct |
8 |
Correct |
85 ms |
211684 KB |
Output is correct |
9 |
Correct |
85 ms |
211696 KB |
Output is correct |
10 |
Correct |
86 ms |
211620 KB |
Output is correct |
11 |
Correct |
83 ms |
211656 KB |
Output is correct |
12 |
Correct |
91 ms |
211652 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
88 ms |
211780 KB |
Output is correct |
2 |
Correct |
77 ms |
211648 KB |
Output is correct |
3 |
Correct |
78 ms |
211604 KB |
Output is correct |
4 |
Correct |
85 ms |
211652 KB |
Output is correct |
5 |
Correct |
86 ms |
211652 KB |
Output is correct |
6 |
Correct |
87 ms |
211736 KB |
Output is correct |
7 |
Correct |
83 ms |
211684 KB |
Output is correct |
8 |
Correct |
85 ms |
211684 KB |
Output is correct |
9 |
Correct |
85 ms |
211696 KB |
Output is correct |
10 |
Correct |
86 ms |
211620 KB |
Output is correct |
11 |
Correct |
83 ms |
211656 KB |
Output is correct |
12 |
Correct |
91 ms |
211652 KB |
Output is correct |
13 |
Correct |
329 ms |
211740 KB |
Output is correct |
14 |
Correct |
297 ms |
211652 KB |
Output is correct |
15 |
Correct |
345 ms |
211644 KB |
Output is correct |
16 |
Correct |
305 ms |
211672 KB |
Output is correct |
17 |
Correct |
292 ms |
211652 KB |
Output is correct |
18 |
Correct |
291 ms |
211728 KB |
Output is correct |
19 |
Correct |
312 ms |
211772 KB |
Output is correct |
20 |
Correct |
322 ms |
211652 KB |
Output is correct |
21 |
Correct |
335 ms |
211640 KB |
Output is correct |