bzoj3916: [Baltic2014]friends

来源:互联网 发布:俄罗斯民族性格 知乎 编辑:程序博客网 时间:2024/06/04 19:17

链接

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

题解

  枚举删除位置,哈希判断
  我查了一上午的错,就因为题没读好。长得一样的串,算一个。

代码

//哈希#include <cstdio>#include <algorithm>#define maxn 3000000#define mod 1000000007#define base 4894651ll#define ll long longusing namespace std;int N, h[maxn], inv[maxn], b[maxn];char s[maxn];void init(){    int i;    scanf("%d%s",&N,s+1);    for(b[0]=i=1;i<=N;i++)b[i]=(ll)b[i-1]*base%mod;    for(inv[0]=1,i=1;i<=N;i++)inv[i]=(inv[i-1]*612582597ll)%mod;    for(i=1;i<=N;i++)h[i]=((ll)(s[i]-'A')*b[i-1]+h[i-1])%mod;}inline int geth(int l, int r){    if(l>r)return 0;    return ((ll)h[r]-h[l-1])*inv[l-1]%mod;}inline int check(int pos){    int h1, h2;    if(pos<=(N>>1))    {        h1=(geth(1,pos-1)+(ll)geth(pos+1,(N>>1)+1)*b[pos-1]%mod)%mod;        h2=geth((N>>1)+2,N);    }    else    {        h1=geth(1,N>>1);        h2=(geth((N>>1)+1,pos-1)+(ll)geth(pos+1,N)*b[pos-(N>>1)-1]%mod)%mod;    }    return (h1+mod)%mod == (h2+mod)%mod ? (h1+mod)%mod : -1;}void show(int pos){    int i;    for(i=pos;i<N;i++)s[i]=s[i+1];    for(i=1;i<=(N>>1);i++)putchar(s[i]);}int main(){    int i, pos, cnt=0, H=-1, t;    init();    if((~N&1) or N<3){printf("NOT POSSIBLE");return 0;}    for(i=1;i<=N;i++)    {        t=check(i);        if(t^-1 and t^H)        {            pos=i;cnt++;H=t;            if(cnt>1)break;        }    }    if(cnt==0){printf("NOT POSSIBLE");return 0;}    if(cnt>1){printf("NOT UNIQUE");return 0;}    show(pos);    return 0;}
0 0