1 条题解

  • 0
    @ 2025-10-8 17:09:29
    #include<bits/stdc++.h>
    using namespace std;
    int pre[1110000], p[1110000];
    string s1, s2;
    int main()
    {
        cin>>s1>>s2;
        int len1=s1.length();
        int len2=s2.length();
        s1=' '+s1;
        s2=' '+s2;
        memset(pre,0,sizeof(pre));
        for(int i=1,j=pre[i];i<len2;i++,j=pre[i])
    	{
            while(j>0&&s2[i+1]!=s2[j+1])j=pre[j];
            if(s2[i+1]==s2[j+1])pre[i+1]=j+1;
        }
    
        for(int i=0,j=0;i<len1;i++,j=p[i])
    	{
            while(j>0&&s1[i+1]!=s2[j+1]) j=pre[j];
            if(s1[i+1]==s2[j+1])j=j+1;
            p[i+1]=j;
            if(j==len2) 
    		{
    			s1.erase(i+1-len2+1,len2);
    			len1-=len2;
    			i=i+1-len2+1-1-1;
    		}
        }
        s1.erase(0,1);
        cout<<s1<<endl;
        return 0;
    }
    
    • 1

    *【KMP】删掉屏蔽词[USACO15FEB] Censoring S

    信息

    ID
    5607
    时间
    1000ms
    内存
    128MiB
    难度
    6
    标签
    递交数
    71
    已通过
    23
    上传者