#include<bits/stdc++.h>
#define god dimasi5eks
#pragma GCC optimize("O3")
#define fi first
#define se second
#define pb push_back
#define pf push_front
#define mod 1000000007
#define dancila 3.14159265359
#define eps 1e-9
// #define fisier 1
using namespace std;
typedef long long ll;
string a, b;
int maxLen, startA, startB;
bool rev;
int lss[3002][3002];
int lsp[3002][3002], lps[3002][3002];
void solve()
{
memset(lss, 0, sizeof(lss));
for(int j = 0; j < b.size(); ++j)
for(int i = 0; i < a.size(); ++i)
{
if(j + i >= b.size())
break;
if(a[i] == b[j + i])
lss[i][j + i] = (i > 0 ? lss[i - 1][j + i - 1] : 0) + 1;
else
lss[i][j + i] = 0;
}
for(int j = 0; j < a.size(); ++j)
for(int i = 0; i < b.size(); ++i)
{
if(j + i >= a.size())
break;
if(a[i + j] == b[i])
lss[i + j][i] = (i > 0 ? lss[i + j - 1][i - 1] : 0) + 1;
else
lss[i + j][i] = 0;
}
memset(lsp, 0, sizeof(lsp));
memset(lps, 0, sizeof(lps));
for(int i = 0; i < a.size(); ++i)
{
for(int j = 0; j < b.size(); ++j)
{
int x = lss[i][j];
if (x == 0)
continue;
if (j - x >= 0)
lsp[i][j - x] = max(lsp[i][j - x], x);
if (i - x >= 0)
lps[i - x][j] = max(lps[i - x][j], x);
}
}
for(int i = 0; i < a.size(); ++i)
for(int j = 1; j < b.size(); ++j)
lsp[i][j] = max(lsp[i][j], lsp[i][j - 1] - 1);
for(int i = 0; i < a.size(); ++i)
for(int j = 0; j < b.size(); ++j)
{
int x = lsp[i][j];
int y = lps[i][j];
if(x + y > maxLen)
{
maxLen = x + y;
startA = i - x + 1;
startB = j - y + 1;
if(rev)
startB = b.size() - (j + x + 1);
}
}
}
int main()
{
#ifdef fisier
ifstream f("input.in");
ofstream g("output.out");
#endif
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cin >> a;
cin >> b;
solve();
reverse(b.begin(), b.end());
rev = 1;
solve();
cout << maxLen << '\n';
cout << startA << " " << startB << '\n';
return 0;
}
Compilation message
necklace.cpp: In function 'void solve()':
necklace.cpp:27:22: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for(int j = 0; j < b.size(); ++j)
~~^~~~~~~~~~
necklace.cpp:28:26: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for(int i = 0; i < a.size(); ++i)
~~^~~~~~~~~~
necklace.cpp:30:22: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
if(j + i >= b.size())
~~~~~~^~~~~~~~~~~
necklace.cpp:37:22: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for(int j = 0; j < a.size(); ++j)
~~^~~~~~~~~~
necklace.cpp:38:26: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for(int i = 0; i < b.size(); ++i)
~~^~~~~~~~~~
necklace.cpp:40:22: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
if(j + i >= a.size())
~~~~~~^~~~~~~~~~~
necklace.cpp:49:22: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for(int i = 0; i < a.size(); ++i)
~~^~~~~~~~~~
necklace.cpp:51:26: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for(int j = 0; j < b.size(); ++j)
~~^~~~~~~~~~
necklace.cpp:62:22: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for(int i = 0; i < a.size(); ++i)
~~^~~~~~~~~~
necklace.cpp:63:26: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for(int j = 1; j < b.size(); ++j)
~~^~~~~~~~~~
necklace.cpp:65:22: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for(int i = 0; i < a.size(); ++i)
~~^~~~~~~~~~
necklace.cpp:66:26: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for(int j = 0; j < b.size(); ++j)
~~^~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
75 ms |
106104 KB |
Output is correct |
2 |
Correct |
71 ms |
106232 KB |
Output is correct |
3 |
Correct |
71 ms |
106108 KB |
Output is correct |
4 |
Correct |
75 ms |
106184 KB |
Output is correct |
5 |
Correct |
72 ms |
106132 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
75 ms |
106104 KB |
Output is correct |
2 |
Correct |
71 ms |
106232 KB |
Output is correct |
3 |
Correct |
71 ms |
106108 KB |
Output is correct |
4 |
Correct |
75 ms |
106184 KB |
Output is correct |
5 |
Correct |
72 ms |
106132 KB |
Output is correct |
6 |
Correct |
78 ms |
106104 KB |
Output is correct |
7 |
Correct |
74 ms |
106104 KB |
Output is correct |
8 |
Correct |
78 ms |
106104 KB |
Output is correct |
9 |
Correct |
80 ms |
106104 KB |
Output is correct |
10 |
Correct |
76 ms |
106232 KB |
Output is correct |
11 |
Correct |
75 ms |
106104 KB |
Output is correct |
12 |
Correct |
77 ms |
106232 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
75 ms |
106104 KB |
Output is correct |
2 |
Correct |
71 ms |
106232 KB |
Output is correct |
3 |
Correct |
71 ms |
106108 KB |
Output is correct |
4 |
Correct |
75 ms |
106184 KB |
Output is correct |
5 |
Correct |
72 ms |
106132 KB |
Output is correct |
6 |
Correct |
78 ms |
106104 KB |
Output is correct |
7 |
Correct |
74 ms |
106104 KB |
Output is correct |
8 |
Correct |
78 ms |
106104 KB |
Output is correct |
9 |
Correct |
80 ms |
106104 KB |
Output is correct |
10 |
Correct |
76 ms |
106232 KB |
Output is correct |
11 |
Correct |
75 ms |
106104 KB |
Output is correct |
12 |
Correct |
77 ms |
106232 KB |
Output is correct |
13 |
Correct |
436 ms |
106360 KB |
Output is correct |
14 |
Correct |
384 ms |
106104 KB |
Output is correct |
15 |
Correct |
450 ms |
106212 KB |
Output is correct |
16 |
Correct |
393 ms |
106232 KB |
Output is correct |
17 |
Correct |
339 ms |
106232 KB |
Output is correct |
18 |
Correct |
347 ms |
106232 KB |
Output is correct |
19 |
Correct |
382 ms |
106232 KB |
Output is correct |
20 |
Correct |
398 ms |
106364 KB |
Output is correct |
21 |
Correct |
410 ms |
106232 KB |
Output is correct |