#include<bits/stdc++.h>
using namespace std;
int A = 987654323;
int B = 1e9 + 9;
int C = 985201597;
int D = 660353359;
int pol1[26];
int pol2[26];
vector<pair<int,int> > sa[1509];
vector<pair<int,int> > sb[1509];
main(){
pol1[0] = 1;
for(int i = 1; i < 26; i ++)
pol1[i] = (long long)pol1[i - 1] * (long long)(A*A) % B;
pol2[0] = 1;
for(int i = 1; i < 26; i ++)
pol2[i] = (long long)pol2[i - 1] * (long long)(C*C) % D;
//for(int i = 1; i <= 26; ++i)cout << pol1[i] << " ";cout << endl;
//for(int i = 1; i <= 26; ++i)cout << pol2[i] << " ";cout << endl;
int n, m;
string a, b;
cin >> a >> b;
n = a.size(), m = b.size();
for(int i = 0; i < n; i++){
int H1 = 0, H2 = 0;
for(int j = i; j < n; j++){
H1 += pol1[a[j] - 'a'];
H2 += pol2[a[j] - 'a'];
if(H1 >= B) H1 -= B;
if(H2 >= D) H2 -= D;
sa[j - i + 1].push_back({H1,H2});
}
}
int ans = 0;
for(int i = 0; i < m; i++){
int H1 = 0, H2 = 0;
for(int j = i; j < m; j++){
H1 += pol1[b[j] - 'a'];
H2 += pol2[b[j] - 'a'];
if(H1 >= B) H1 -= B;
if(H2 >= D) H2 -= D;
sb[j - i + 1].push_back({H1,H2});
}
}
for(int k = min(m,n); k >=0; k--){
sort(sa[k].begin(), sa[k].end());
sort(sb[k].begin(), sb[k].end());
int j = 0;
for(int i = 0 ;i < sa[k].size(); i++)
{
while(j < sb[k].size() && sb[k][j] < sa[k][i]) j++;
if(sb[k][j] == sa[k][i]){
cout<<k<<endl;
return 0;
}
}
}
cout << ans << endl;
}
Compilation message
interval.cpp:11:1: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
11 | main(){
| ^~~~
interval.cpp: In function 'int main()':
interval.cpp:49:26: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
49 | for(int i = 0 ;i < sa[k].size(); i++)
| ~~^~~~~~~~~~~~~~
interval.cpp:51:21: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
51 | while(j < sb[k].size() && sb[k][j] < sa[k][i]) j++;
| ~~^~~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
332 KB |
Output is correct |
2 |
Correct |
1 ms |
332 KB |
Output is correct |
3 |
Incorrect |
1 ms |
460 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
15 ms |
2676 KB |
Output is correct |
2 |
Incorrect |
13 ms |
2680 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
73 ms |
10956 KB |
Output is correct |
2 |
Correct |
76 ms |
11076 KB |
Output is correct |
3 |
Correct |
76 ms |
11176 KB |
Output is correct |
4 |
Correct |
76 ms |
11052 KB |
Output is correct |
5 |
Correct |
76 ms |
11128 KB |
Output is correct |
6 |
Incorrect |
76 ms |
11060 KB |
Output isn't correct |
7 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
43 ms |
16328 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |