This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#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];
vector<pair<int ,int>> a[N] , b[N];
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);
}
}
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;
b[j - i + 1].emplace_back(x , y);
}
}
for(int i = min(n , m); ~i;--i){
sort(a[i].begin() , a[i].end());
sort(b[i].begin() , b[i].end());
int k = 0;
for(int j = 0; a[i].size(); ++j){
while(a[i][j] > b[i][k])++k;
if(a[i][j] == b[i][k]){cout << i;return 0;}
}
}
cout << 0;
}
Compilation message (stderr)
interval.cpp:14:1: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
14 | main (){
| ^~~~
interval.cpp: In function 'int main()':
interval.cpp:31:9: warning: unused variable 'ans' [-Wunused-variable]
31 | int ans = 0;
| ^~~
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |