Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <bits/stdc++.h>
- using namespace std;
- typedef long long lli;
- const int N = 2000;
- const int PB = 1e9 + 7;
- lli anaHash[26];
- char strA[N + 1], strB[N + 1];
- int lenA, lenB;
- bool sameSubstr(int len){
- set<lli> st;
- lli hsh = 0;
- for(int i = 0; i < len; ++i){
- hsh += anaHash[strA[i] - 'A'];
- }
- st.insert(hsh);
- for(int i = len; i < lenA; ++i){
- hsh -= anaHash[strA[i - len] - 'A'];
- hsh += anaHash[strA[i] - 'A'];
- st.insert(hsh);
- }
- hsh = 0;
- for(int i = 0; i < len; ++i){
- hsh += anaHash[strB[i] - 'A'];
- }
- if(st.find(hsh) != st.end()){
- return true;
- }
- for(int i = len; i < lenB; ++i){
- hsh -= anaHash[strB[i - len] - 'A'];
- hsh += anaHash[strB[i] - 'A'];
- if(st.find(hsh) != st.end()){
- return true;
- }
- }
- return false;
- }
- int main(){
- anaHash[0] = 1;
- for(int i = 1; i < 26; ++i){
- anaHash[i] = anaHash[i - 1] * PB;
- }
- scanf(" %s %s", strA, strB);
- lenA = strlen(strA);
- lenB = strlen(strB);
- int mxLen = min(lenA, lenB);
- int mx = 0;
- for(int i = 1; i <= mxLen; ++i){
- if(sameSubstr(i)){
- mx = max(mx, i);
- }
- }
- cout << mx;
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement