暴力的哈希,注意:
将一个串当作另一个串的前缀,需要乘上p[len],len=后面串的长度
这是自己的代码,拿数据在本地测A掉了,但是bz上wa了??bz换数据了难道??
#include<cstdio>
#include<cstring>
#define rep(i,x,y) for(register int i=x;i<=y;i++)
using namespace std;
const int N=;
typedef unsigned long long ull; int n,mid;
char a[N],ans[N];
ull p[N],sum[N],b; inline ull get(int l,int r){
return sum[r]-sum[l-]*p[r-l+];}
bool judge(int i){
ull x,y,la;
if(i<mid+){
x=get(,i-)*p[mid+-i]+get(i+,mid+);
y=get(mid+,n);
}else if(i==mid+){
x=get(,mid);
y=get(mid+,n);
}else if(i>mid+){
x=get(,mid);
y=get(mid+,i-)*p[n-i]+get(i+,n);
}if(x==y){
if(x==la) return ;
la=x;
int top=;
if(i<=mid+) rep(j,mid+,n) ans[++top]=a[j];
else rep(j,,mid) ans[++top]=a[j];
return ;
}
return ;
}
int main(){
scanf("%d",&n);if(n%==) {puts("NOT POSSIBLE");return ;}
scanf("%s",a+);mid=n/; p[]=;b=;
rep(i,,n) p[i]=p[i-]*b;
rep(i,,n) sum[i]=sum[i-]*b+(a[i]-'A'+); int cnt=;
rep(i,,n){cnt+=judge(i);if(cnt>) break;} if(!cnt) puts("NOT POSSIBLE");
else if(cnt>) puts("NOT UNIQUE");
else puts(ans+); return ;
}
别人的代码,侵删啊啊啊
#include<cstring>
#include<iostream>
#include<cstdio>
#include<algorithm>
#define rep(i,l,r) for (int i=l;i<=r;i++)
#define down(i,l,r) for (int i=l;i>=r;i--)
#define clr(x,y) memset(x,y,sizeof(x))
#define maxn 2000009
#define p 23333
using namespace std;
typedef unsigned long long ll;
char ans[maxn],s[maxn];
ll h[maxn],bin[maxn],last;
int now,n,mid,anspos;
int read(){
int x=,f=; char ch=getchar();
while (!isdigit(ch)){
if (ch=='-') f=-; ch=getchar();
}
while (isdigit(ch)){
x=x*+ch-''; ch=getchar();
}
return x*f;
}
ll get(int l,int r){
return h[r]-h[l-]*bin[r-l+];
}
int judge(int pos){
ll x,y,z;
int flag=;
if (pos<mid){
x=get(,pos-)*bin[mid-pos]+get(pos+,mid);
y=get(mid+,n);
}
else if (pos>mid){
x=get(,mid-);
y=get(mid,pos-)*bin[n-pos]+get(pos+,n);
}
else if (pos==mid){
x=get(,mid-);
y=get(mid+,n);
}
if (x==y){
if (x==last) return ;
last=x;
int top=;
if (pos<=mid) rep(i,mid+,n) ans[++top]=s[i];
else rep(i,,mid-) ans[++top]=s[i];
return ;
}
return ;
}
int main(){
freopen("3916.in","r",stdin);
freopen("3916.out","w",stdout);
n=read(); if (n%==) {puts("NOT POSSIBLE"); return ;}
scanf("%s",s+);
bin[]=; rep(i,,n) bin[i]=bin[i-]*p;
rep(i,,n) h[i]=h[i-]*p+s[i];
mid=(n/)+;
int cnt=;
rep(i,,n) {
cnt+=judge(i);
if (cnt>) break;
}
if (!cnt) puts("NOT POSSIBLE");
else if (cnt>) puts("NOT UNIQUE");
else puts(ans+);
return ;
}