哈希寻找字符串 最长对称子串
题目链接
#include<iostream>
#include<algorithm>
using namespace std;
typedef unsigned long long ull;
const int K = 131;
ull q[1010],p[1010],s[1010];
int main()
{
string str;
getline(cin,str);
p[0] = 1;
q[0] = str[0];
s[str.length()] = 0;
for(int i = 1 ; i < str.length() ; i ++){
q[i] = q[i - 1]*K + str[i];
p[i] = p[i - 1]*K;
}
for(int j = str.length() - 1 ; j >= 0 ; j --){
s[j] = s[j + 1]*K + str[j];
}
int ans = 1;
for(int i = 0 ; i < str.length() ; i ++){
for(int j = 1 ; j < str.length() ; j ++){
if(q[j] - q[i] * p[j - i] == s[i] - s[j] * p[j - i]) ans = max(ans,j - i + 1);
}
}
cout<<ans;
return 0;
}