[BZOJ3942] [Usaco2015 Feb]Censoring

传送门

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

题目大意

有一个S串和一个T串,长度均小于1,000,000,设当前串为U串,然后从前往后枚举S串一个字符一个字符往U串里添加,若U串后缀为T,则去掉这个后缀继续流程。

题解

KMP大水题

const
    maxn=1000005;
var
    u,s,t:array[0..maxn]of char;
    x,next:array[0..maxn]of longint;
    i,j,k:longint;
    n,m,len:longint;
begin
    i:=0;
    while not eoln do
        begin
            inc(i);
            read(s[i]);
        end;
    readln; m:=i;
    i:=0;
    while not eoln do
        begin
            inc(i);
            read(t[i]);
        end;
    readln; n:=i;
    next[1]:=0; j:=0;
    for i:=2 to n do
        begin
            while (j>0)and(t[j+1]<>t[i]) do j:=next[j];
            if t[j+1]=t[i] then inc(j);
            next[i]:=j;
        end;
    j:=0; i:=1; len:=0;
    while i<=m do
        begin
            while (j>0)and(t[j+1]<>s[i]) do j:=next[j];
            if t[j+1]=s[i] then inc(j);
            inc(len); u[len]:=s[i]; x[len]:=j;
            if j=n
            then begin dec(len,n); j:=x[len]; end;
            inc(i);
        end;
    for i:=1 to len do
        write(u[i]);
    writeln;
end.

你可能感兴趣的:([BZOJ3942] [Usaco2015 Feb]Censoring)