[BZOJ3916] [Baltic2014]friends

传送门

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

题目大意

串S复制一遍接到S后面,然后任意位置插一个字符得到串U
给定串U,询问串S

题解

暴力+HASH
尼玛,给的Sample Input x:我真以为他是多组询问我还要输出Sample Output x:,WAWAW

const
    maxn=2000010;
    seed=131;
    mmod=maxlongint;
var
    hash,pow:array[0..maxn]of int64;
    x:array[0..maxn]of char;
    i,j,k,l:longint;
    n,m,ansl,ansr:longint;
    t1,t2,ans:int64;
    cha:char;
function query(l,r:longint):int64;
begin
    if l>r then exit(0);
    if l<1 then exit(0);
    if r>n then exit(0);
    exit((hash[r]-(hash[l-1]*pow[r-l+1])mod mmod+mmod)mod mmod);
end;

begin    
    ansl:=0; ansr:=0; ans:=0;
    readln(n); hash[0]:=0; pow[0]:=1;
    for i:=1 to n do
        begin
            read(x[i]);
            hash[i]:=(hash[i-1]*seed+ord(x[i]))mod mmod;
            pow[i]:=(pow[i-1]*seed)mod mmod;
        end;
    readln;
    m:=(n-1)div 2;
    if (n mod 2)<>0
    then
        for i:=1 to n do
            begin
                if i<=m+1
                then begin t1:=(query(i+1,m+1)+query(1,i-1)*pow[m-i+1])mod mmod; t2:=query(n-m+1,n); end
                else begin t1:=query(1,m); t2:=(query(i+1,n)+query(m+1,i-1)*pow[n-i])mod mmod; end;
                if t1=t2
                then
                    begin
                        if (ans<>0)and(t1<>ans) then begin ansl:=-1; break; end;
                        if i<m+1
                        then begin ansl:=n-m+1; ansr:=n; ans:=t1; end
                        else begin ansl:=1; ansr:=m; ans:=t1; end;
                    end;
                    end;
    if (ansl=0)and(ansr=0)
    then writeln('NOT POSSIBLE')
    else
        if ansl=-1
        then writeln('NOT UNIQUE')
        else
            begin
                for i:=ansl to ansr do
                    write(x[i]);
                writeln;
            end;
end.
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值