bzoj3916
https://www.lydsy.com/JudgeOnline/problem.php?id=3916
一个串s2由两个s,中间夹一个字符c组成,求s。若s不存在,"NOT POSSIBLE";若s不唯一,"NOTUNIQUE";否则;输出s。
其实主要是hash+模拟。hash和power下标要好好算一下,还得分类讨论(c在前一半/c在后一半)
注意:是看s是否唯一,不是c!!!
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cmath>
#include<cstring>
#include<string>
#include<cstdlib>
#include<set>
#include<map>
#include<queue>
#include<vector>
#define p 999983
#define q 1000000007
#define ull unsigned long long
using namespace std;
char s[2000005];
ull po[2000005],qo[2000005],h[2000005],h2[2000005];
map<ull,bool> ma;
int main()
{
int n;
scanf("%d%s",&n,s+1);
if(n%2==0||n==1){
puts("NOT POSSIBLE");
return 0;
}
po[0]=qo[0]=1;
for(int i=1;i<=n;++i) po[i]=po[i-1]*p,qo[i]=qo[i-1]*q;
for(int i=1;i<=n;++i) h[i]=h[i-1]*p+s[i],h2[i]=h2[i-1]*q+s[i];
int m=n/2,ansx=0;
for(int i=1;i<m+1;++i){
ull x=h[i-1]*po[m+1-i]+h[m+1]-h[i]*po[m+1-i];
ull x2=h2[i-1]*qo[m+1-i]+h2[m+1]-h2[i]*qo[m+1-i];
ull t=h[n]-h[m+1]*po[m];
ull t2=h2[n]-h2[m+1]*qo[m];
if(x==t&&x2==t2){
if(ansx&&(!ma[t]||!ma[t2])){puts("NOT UNIQUE");return 0;}
ansx=i;ma[t]=ma[t2]=1;
}
}
ull x=h[m],x2=h2[m],t=h[n]-po[m]*h[n-m],t2=h2[n]-qo[m]*h2[n-m];
if(x==t&&x2==t2){
if(ansx&&(!ma[t]||!ma[t2])){puts("NOT UNIQUE");return 0;}
ansx=m+1;ma[t]=ma[t2]=1;
}
for(int i=m+2;i<=n;++i){
x=h[m],x2=h2[m];
t=(h[i-1]-h[m]*po[i-1-m])*po[n-i]+h[n]-h[i]*po[n-i];
t2=(h2[i-1]-h2[m]*qo[i-1-m])*qo[n-i]+h2[n]-h2[i]*qo[n-i];
if(x==t&&x2==t2){
if(ansx&&(!ma[t]||!ma[t2])){puts("NOT UNIQUE");return 0;}
ansx=i;ma[t]=ma[t2]=1;
}
}
if(ansx){
if(ansx<=m+1){
for(int i=1;i<=m+1;++i)
if(i!=ansx) printf("%c",s[i]);
}
else{
for(int i=m+1;i<=n;++i)
if(i!=ansx) printf("%c",s[i]);
}
}
else puts("NOT POSSIBLE");
}