BZOJ3916: [Baltic2014]friends
题目:http://www.lydsy.com/JudgeOnline/problem.php?id=3916
题解:随便hash.刚开始看错题WA了N发.(我连双hash都写了!)
代码:
1 #include<cstdio> 2 #include<cstdlib> 3 #include<cmath> 4 #include<cstring> 5 #include<algorithm> 6 #include<iostream> 7 #include<vector> 8 #include<map> 9 #include<set> 10 #include<queue> 11 #include<string> 12 #define inf 1000000000 13 #define maxn 2000000+5 14 #define maxm 200000+5 15 #define eps 1e-6 16 #define ll unsigned int 17 #define pa pair<int,int> 18 #define for0(i,n) for(int i=0;i<=(n);i++) 19 #define for1(i,n) for(int i=1;i<=(n);i++) 20 #define for2(i,x,y) for(int i=(x);i<=(y);i++) 21 #define for3(i,x,y) for(int i=(x);i>=(y);i--) 22 #define for4(i,x) for(int i=head[x],y=e[i].go;i;i=e[i].next,y=e[i].go) 23 #define for5(n,m) for(int i=1;i<=n;i++)for(int j=1;j<=m;j++) 24 #define lch k<<1,l,mid 25 #define rch k<<1|1,mid+1,r 26 #define sqr(x) (x)*(x) 27 #define db double 28 using namespace std; 29 inline int read() 30 { 31 int x=0,f=1;char ch=getchar(); 32 while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();} 33 while(ch>='0'&&ch<='9'){x=10*x+ch-'0';ch=getchar();} 34 return x*f; 35 } 36 int n,pos; 37 char s[maxn]; 38 ll h[maxn],p[maxn]; 39 inline ll get(int x,int y){return x>y?0:h[y]-h[x-1]*p[y-x+1];} 40 ll ans,t1,t2; 41 inline bool check(int x) 42 { 43 if(x>n>>1)t1=get(1,n>>1),t2=(get((n>>1)+1,x-1)*p[n-x]+get(x+1,n)); 44 else t1=get(n-(n>>1)+1,n),t2=(get(1,x-1)*p[(n>>1)-x+1]+get(x+1,(n>>1)+1)); 45 return t1==t2; 46 } 47 int main() 48 { 49 freopen("input.txt","r",stdin); 50 freopen("output.txt","w",stdout); 51 n=read();scanf("%s",s+1); 52 if(n%2==0){printf("NOT POSSIBLE\n");return 0;} 53 p[0]=1; 54 for1(i,n)h[i]=h[i-1]*13131+s[i]-'A',p[i]=p[i-1]*13131; 55 for1(i,n)if(check(i)) 56 { 57 pos=i; 58 if(!ans)ans=t1; 59 else if(t1!=ans){printf("NOT UNIQUE\n");return 0;}; 60 } 61 if(!pos)printf("NOT POSSIBLE\n"); 62 else 63 { 64 if(pos>n>>1)for1(i,n>>1)printf("%c",s[i]); 65 else for2(i,n-(n>>1)+1,n)printf("%c",s[i]); 66 printf("\n"); 67 } 68 return 0; 69 }
字体为什么忽然变了好鬼畜