朋友啊!~
嗯?貌似这个题挺变态的?
首先我们知道,如果这个字符串得长度是一个偶数,那么肯定不存在S
#include<iostream>
#include<cstdio>
#include<string>
#include<cstring>
using namespace std;
typedef unsigned long long ull;
const int SIZE=400010;
const int base=131;
ull power[SIZE],hash[SIZE];
int n,mid;
string s;
ull gethash(int l,int r)//计算 区间hash值
{
return hash[r]-hash[l-1]*power[r-l+1];
}
ull get(int l,int r,int x)//在区间分割到x
{
return gethash(l,x-1)*power[r-x]+gethash(x+1,r);
}
int main()
{
cin>>n>>s;
if(n%2==0)
{
cout<<"NOT POSSIBLE\n";
return 0;
}
power[0]=1;
for(int i=1;i<=SIZE;i++) power[i]=power[i-1]*base;
s=" "+s;
hash[1]=s[1];
for(int i=2;i<=n;i++)
{
hash[i]=hash[i-1]*base+s[i];
}
mid=n/2+1;
ull a,b,answer=0;
b=gethash(mid+1,n);//分半处理
string ans,ans1,ans2;
for(int i=mid+1;i<=n;i++) ans+=s[i];//存储后半部分
for(int i=1;i<mid;i++)
{
a=get(1,mid,i);
if(a==b)
{
answer++;
ans1=ans;
break;//hash值相等,ans是答案之一
}
}
ans="";
for(int i=1;i<=mid-1;i++) ans+=s[i];
b=gethash(1,mid-1);
for(int i=mid;i<=n;i++)
{
a=get(mid,n,i);
if(a==b)
{
answer++;
ans2=ans;
break;
}
}
if (answer == 0) cout << "NOT POSSIBLE\n";
else if (answer == 1 || ans == ans2) cout << (ans1 == "" ? ans2 : ans1) << endl;
else cout << "NOT UNIQUE\n";
}