bzoj 3916 暴力哈希
暴力的哈希,注意:
将一个串当作另一个串的前缀,需要乘上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=2005000; 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-1]*p[r-l+1];} bool judge(int i){ ull x,y,la; if(i<mid+1){ x=get(1,i-1)*p[mid+1-i]+get(i+1,mid+1); y=get(mid+2,n); }else if(i==mid+1){ x=get(1,mid); y=get(mid+2,n); }else if(i>mid+1){ x=get(1,mid); y=get(mid+1,i-1)*p[n-i]+get(i+1,n); }if(x==y){ if(x==la) return 0; la=x; int top=0; if(i<=mid+1) rep(j,mid+2,n) ans[++top]=a[j]; else rep(j,1,mid) ans[++top]=a[j]; return 1; } return 0; } int main(){ scanf("%d",&n);if(n%2==0) {puts("NOT POSSIBLE");return 0;} scanf("%s",a+1);mid=n/2; p[0]=1;b=19911103; rep(i,1,n) p[i]=p[i-1]*b; rep(i,1,n) sum[i]=sum[i-1]*b+(a[i]-'A'+1); int cnt=0; rep(i,1,n){cnt+=judge(i);if(cnt>1) break;} if(!cnt) puts("NOT POSSIBLE"); else if(cnt>1) puts("NOT UNIQUE"); else puts(ans+1); return 0; }
别人的代码,侵删啊啊啊
#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=0,f=1; char ch=getchar(); while (!isdigit(ch)){ if (ch=='-') f=-1; ch=getchar(); } while (isdigit(ch)){ x=x*10+ch-'0'; ch=getchar(); } return x*f; } ll get(int l,int r){ return h[r]-h[l-1]*bin[r-l+1]; } int judge(int pos){ ll x,y,z; int flag=0; if (pos<mid){ x=get(1,pos-1)*bin[mid-pos]+get(pos+1,mid); y=get(mid+1,n); } else if (pos>mid){ x=get(1,mid-1); y=get(mid,pos-1)*bin[n-pos]+get(pos+1,n); } else if (pos==mid){ x=get(1,mid-1); y=get(mid+1,n); } if (x==y){ if (x==last) return 0; last=x; int top=0; if (pos<=mid) rep(i,mid+1,n) ans[++top]=s[i]; else rep(i,1,mid-1) ans[++top]=s[i]; return 1; } return 0; } int main(){ freopen("3916.in","r",stdin); freopen("3916.out","w",stdout); n=read(); if (n%2==0) {puts("NOT POSSIBLE"); return 0;} scanf("%s",s+1); bin[0]=1; rep(i,1,n) bin[i]=bin[i-1]*p; rep(i,1,n) h[i]=h[i-1]*p+s[i]; mid=(n/2)+1; int cnt=0; rep(i,1,n) { cnt+=judge(i); if (cnt>1) break; } if (!cnt) puts("NOT POSSIBLE"); else if (cnt>1) puts("NOT UNIQUE"); else puts(ans+1); return 0; }