#include<bits/stdc++.h>
#define f first
#define s second
#define int long long
using namespace std;
const int N = 2005;
int A = 37;
int B = 1e9 + 9;
int C = 31;
int D = 1e9 + 7;
string s , t;
int pw1[27] , pw2[27];
struct Hash{
size_t operator() (const pair<int , int> &x) const{
return ((long long)x.first) ^ (((long long)x.second) << 60);
}
};
unordered_map<pair<int , int>, int , Hash> H;
main (){
ios_base::sync_with_stdio(0); cin.tie(0);
cin >> s >> t;
int n = s.size() , m = t.size();
pw1[0] = pw2[0] = 1;
for(int i = 1; i < 26; ++i) pw1[i] = pw1[i - 1] * A*(i + 1) % B;
for(int i = 1; i< 26; ++i) pw2[i] = pw2[i - 1] * C*(i + 1) % D;
for(int i = 0; i < n; ++i){
int x= 0 , y =0;
for(int j = i; j < n; ++j){
x += pw1[s[j] - 'a'];
y += pw2[s[j] - 'a'];
if(x>=B)x-=B;
if(y>=D)y-=D;
// a[j - i + 1].emplace_back(x , y);
H[{x, y}]= 1;
}
}
int ans = 0;
for(int i = 0; i < m; ++i){
int x= 0 , y =0;
for(int j = i; j < m; ++j){
x += pw1[t[j] - 'a'];
y += pw2[t[j] - 'a'];
if(x>=B)x-=B;
if(y>=D)y-=D;
if(H[{x , y}])ans = max(ans , j - i + 1);
}
}
cout << ans;
}
Compilation message
interval.cpp:19:1: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
19 | main (){
| ^~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
204 KB |
Output is correct |
2 |
Correct |
1 ms |
332 KB |
Output is correct |
3 |
Correct |
1 ms |
332 KB |
Output is correct |
4 |
Correct |
2 ms |
716 KB |
Output is correct |
5 |
Correct |
2 ms |
716 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
59 ms |
12496 KB |
Output is correct |
2 |
Correct |
39 ms |
8932 KB |
Output is correct |
3 |
Correct |
10 ms |
716 KB |
Output is correct |
4 |
Correct |
31 ms |
336 KB |
Output is correct |
5 |
Correct |
70 ms |
13548 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
463 ms |
54624 KB |
Output is correct |
2 |
Correct |
472 ms |
56292 KB |
Output is correct |
3 |
Correct |
481 ms |
55880 KB |
Output is correct |
4 |
Correct |
486 ms |
54756 KB |
Output is correct |
5 |
Correct |
465 ms |
56092 KB |
Output is correct |
6 |
Correct |
459 ms |
54968 KB |
Output is correct |
7 |
Correct |
473 ms |
56116 KB |
Output is correct |
8 |
Correct |
470 ms |
56316 KB |
Output is correct |
9 |
Correct |
484 ms |
56352 KB |
Output is correct |
10 |
Correct |
468 ms |
56000 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
168 ms |
17576 KB |
Output is correct |
2 |
Execution timed out |
1087 ms |
116552 KB |
Time limit exceeded |
3 |
Halted |
0 ms |
0 KB |
- |