[BZOJ3942] [Usaco2015 Feb]Censoring

本文介绍了一道关于字符串匹配的问题,通过使用KMP算法进行解答。问题要求在字符串S中不断添加字符并检查是否形成了目标字符串T的后缀,若形成则移除该后缀继续检查。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

传送门

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.
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值