bzoj3916: [Baltic2014]friends

链接

  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 long
using 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
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值