原题链接
#include <algorithm>
#include <iostream>
#include <cstdio>
#include <cstring>
#include <string>
#include <cmath>
#include <vector>
using namespace std;
typedef long long int LL;
const int MAX = 10010;
const LL P = 10000019, MOD = 1000000007;
typedef struct Node
{
string str;
LL hash_value;
int len;
}Node;
vector<Node> subS, subT;
string S, T;
Node MAX_LEN = {"", 0, 0};
LL powP[MAX] = {0}, HS[MAX] = {0}, HT[MAX] = {0};
void init(int len)
{
powP[0] = 1;
for(int i=1; i<=len; i++)
{
powP[i] = (powP[i-1]*P) % MOD;
}
}
void calHash(LL H[], string &str)
{
H[0] = str[0];
for(int i=1; i<(int)str.size(); i++)
{
H[i] = (H[i-1]*P + str[i]) % MOD;
}
}
LL calSingleSubHash(LL H[], int i, int j)
{
if(!i) return H[j];
return ((H[j] - H[i-1]*powP[j-i+1]) % MOD + MOD) % MOD;
}
void calSubHash(LL H[], string &str, vector<Node> &subStr)
{
for(int i=0; i<(int)str.size(); i++)
{
for(int j=i; j<(int)str.size(); j++)
{
LL temp_hash_value = calSingleSubHash(H, i, j);
Node temp_node = {str.substr(i, j), temp_hash_value, j-i+1};
subStr.push_back(temp_node);
}
}
}
int main()
{
cin>>S>>T;
init(max(S.size(), T.size()));
calHash(HS, S);
calHash(HT, T);
calSubHash(HS, S, subS);
calSubHash(HT, T, subT);
for(int i=0; i<(int)subS.size(); i++)
{
for(int j=0; j<(int)subT.size(); j++)
{
if(subS[i].hash_value == subT[j].hash_value && subS[i].len > MAX_LEN.len)
{
MAX_LEN.hash_value = subS[i].hash_value;
MAX_LEN.len = subS[i].len;
MAX_LEN.str = subS[i].str;
}
}
}
cout<<MAX_LEN.str;
return 0;
}