BZOJ3916: [Baltic2014]friends

时间:2023-03-08 23:29:12
BZOJ3916: [Baltic2014]friends

题目:http://www.lydsy.com/JudgeOnline/problem.php?id=3916

复习一下hash(然后被傻叉错误卡了半天TAT。。。

取出一个字串:h[r]-h[l-1]*power[r-l+1]  然后匹配。。。

注意一下当前需要的是s[i]还是s[i-1],做hash数组时不要写s[i]-'A',写s[i],否则容易被卡

对于这道题,枚举一下断点,注意判重。

对于这种有断点的前面一段+后面一段等于完整一段,要注意前面那一段乘的权。

#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(){
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 ;
}