【字符串哈希】bzoj3916 [Baltic2014]friends
枚举断点,哈希判断。
#include<cstdio> using namespace std; typedef unsigned long long ull; ull hs,hs1,hs2,seeds[1000010],hsgoal; int n; int del=-1; char s[2000002],s2[2000002]; int main() { seeds[0]=1; for(int i=1;i<=1000009;++i) seeds[i]=seeds[i-1]*31; scanf("%d%s",&n,s); if(!(n&1)) {puts("NOT POSSIBLE"); return 0;} for(int i=(n>>1)+1;i<n;++i) hs=hs*31+(s[i]-64); for(int i=1;i<=(n>>1);++i) hs2=hs2*31+(s[i]-64); if(hs2==hs) { del=0; hsgoal=hs; } for(int i=1;i<=(n>>1);++i) { hs2-=((s[i]-64)*seeds[(n>>1)-i]); hs1=hs1*31+(s[i-1]-64); if(hs1*seeds[(n>>1)-i]+hs2==hs) { if(del!=-1&&hsgoal!=hs) { puts("NOT UNIQUE"); return 0; } else { del=i; hsgoal=hs; } } } hs=hs1=hs2=0; for(int i=0;i<(n>>1);++i) hs=hs*31+(s[i]-64); for(int i=(n>>1)+1;i<n;++i) hs2=hs2*31+(s[i]-64); for(int i=(n>>1)+1;i<n;++i) { hs2-=((s[i]-64)*seeds[n-1-i]); hs1=hs1*31+(s[i-1]-64); if(hs1*seeds[n-1-i]+hs2==hs) { if(del!=-1&&hsgoal!=hs) { puts("NOT UNIQUE"); return 0; } else { del=i; hsgoal=hs; } } } if(del==-1) puts("NOT POSSIBLE"); else if(del<=(n>>1)) { for(int i=0;i<=(n>>1);++i) if(i!=del) putchar(s[i]); puts(""); } else { for(int i=(n>>1);i<n;++i) if(i!=del) putchar(s[i]); puts(""); } return 0; }
——The Solution By AutSky_JadeK From UESTC
转载请注明出处:http://www.cnblogs.com/autsky-jadek/