hash在acm中的运用(持续更新)
第一题
链接:UVA11475
题意:让你在字符串后面加字符,使它成为最短的回文串,如xyz, ans为xyzyx
题解:扩k,马拉车,后缀数组都可以,但是用hash直接正反hash一下判断当前hash是否一样就行,注意hash冲突可以双hash,反正时间也够
//https://vjudge.net/problem/UVA-11475
#include <bits/stdc++.h>
using namespace std;
typedef unsigned long long ULL;
const int maxn = 1e5+50;
const ULL mod = 1e9+7;
const ULL base = 2333;
int n;
char a[maxn];
int main()
{
while(scanf("%s", a) != EOF){
n = strlen(a);
ULL pow = 1, pre = 0, suf = 0, ans;
for(int i = n-1; i >=0; i--){
pre = pre*base + a[i];//从前往后hash,让最后一个字符为最高位
suf = a[i]*pow + suf;//从后往前hash,让新加入的字符为最高位
pow = pow*base;
if(suf == pre){
ans = i;
}
}
printf("%s", a);
for(int i = ans-1; i >= 0; i--){
printf("%c", a[i]);
}
printf("\n");
}
return 0;
}