有三个好朋友喜欢在一起玩游戏,A君写下一个字符串S,B君将其复制一遍得到T,C君在T的任意位置(包括首尾)插入一个字符得到U.
现在你得到了U,请你找出S.
Input
第一行一个数N,表示U的长度. 对于100%的数据 2<=N<=2000001
第二行一个字符串U,保证U由大写字母组成
Output
输出一行,若S不存在,输出"NOT POSSIBLE".若S不唯一,输出"NOT UNIQUE".否则输出S.
Sample Input
7
ABXCABC
Sample Output
ABC
#include<cmath>
#include<iostream>
#include<cstring>
#include<cstdio>
#include<algorithm>
const int maxn=2000000+10;
using namespace std;
int n,Hash[maxn],tmp[maxn],ans,flag=0,opt;
char str[maxn];
int main()
{
scanf("%d%s",&n,str+1);
if(!(n%2))
{
printf("NOT POSSIBLE
");
return 0;
}
tmp[0]=1;
for(int i=1;i<=n;i++)
Hash[i]=Hash[i-1]*131+str[i],tmp[i]=tmp[i-1]*131;
unsigned long long x,y;
for(int i=1;i<=n;i++) //枚举断开点
{
if(i<=n/2)
x=Hash[n/2+1]-Hash[i]*tmp[n/2+1-i]+Hash[i-1]*tmp[n/2-i+1];
//拿样例来说,abxcabc.现在枚举断开点为3.
//Hash[n/2+1]-Hash[i]*tmp[n/2+1-i],即abxc-abx=c
//Hash[i-1]*tmp[n/2-i+1]即ab
//然后两者相加
else
x=Hash[n/2];
//形如abcabxc.则x直接取前一半的字符就好了
if(i<=n/2+1)
y=Hash[n]-Hash[n/2+1]*tmp[n/2];
else
y=Hash[n]-Hash[i]*tmp[n-i]+(Hash[i-1]-Hash[n/2]*tmp[i-1-n/2])*tmp[n-i];
if(x==y)
{
if(flag && ans!=x)
{
printf("NOT UNIQUE
");
return 0;
}
flag=1;ans=x;opt=i;
}
}
if(!flag)
{
printf("NOT POSSIBLE
");
return 0;
}
int cnt=1;
for(int i=1;cnt<=n/2;i++)
if(i!=opt)
cout<<str[i],cnt++;
return 0;
}