zoukankan      html  css  js  c++  java
  • [Usaco2015 Feb]Censoring

    Farmer John has purchased a subscription to Good Hooveskeeping magazine for his cows, so they have plenty of material to read while waiting around in the barn during milking sessions. Unfortunately, the latest issue contains a rather inappropriate article on how to cook the perfect steak, which FJ would rather his cows not see (clearly, the magazine is in need of better editorial oversight).

    FJ has taken all of the text from the magazine to create the string S of length at most 10^6 characters. From this, he would like to remove occurrences of a substring T to censor the inappropriate content. To do this, Farmer John finds the _first_ occurrence of T in S and deletes it. He then repeats the process again, deleting the first occurrence of T again, continuing until there are no more occurrences of T in S. Note that the deletion of one occurrence might create a new occurrence of T that didn't exist before.

    Please help FJ determine the final contents of S after censoring is complete

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

    Input
    The first line will contain S. The second line will contain T. The length of T will be at most that of S, and all characters of S and T will be lower-case alphabet characters (in the range a..z).
    Output
    The string S after all deletions are complete. It is guaranteed that S will not become empty during the deletion process.
    Sample Input
    whatthemomooofun

    moo
    Sample Output
    whatthefun
    每次s中字母压入栈中,处理出t的next数组,从头进行匹配,记录匹配到的位置,如果匹配的长度为t,就暴力把整个字符串出栈,接着j应该变为栈顶元素所匹配的位置。

    #include<cstdio>
    #include<cstring>
    using namespace std;
    const int maxn=1000050;
    int top,n,m;
    char s1[maxn],s2[maxn];
    int sta[maxn],num[maxn],nxt[maxn];
    void getnxt()
    {
      for(int i=2,j=0;i<=m;i++)
      {
        while(j&&s2[j+1]!=s2[i])
              j=nxt[j];
        if(s2[j+1]==s2[i])
           j++;
        nxt[i]=j;
      }
    }
    int main(){
      scanf("%s%s",s1+1,s2+1);
      n=strlen(s1+1);//母串长度 
      m=strlen(s2+1);//子串长度 
      getnxt();//求出子串的next数组 
      for(int i=1,j=0;i<=n;i++)
      {
          sta[++top]=i;
        while(j&&s2[j+1]!=s1[i]) 
        //当母串的第i个与子串的第j+1个不匹配时 
             j=nxt[j];
        if(s2[j+1]==s1[i])
            j++;
        num[i]=j;//母串的第i个可匹配到子串的第j个位置 
        
        if(j==m)
        {
            top-=m;
            j=num[sta[top]];
        }
      }
      for(int i=1;i<=top;i++)
          printf("%c",s1[sta[i]]);
     // puts("");
      return 0;
    }
  • 相关阅读:
    SpringBoot学习:整合shiro(身份认证和权限认证),使用EhCache缓存
    帝国备份王出错
    spring boot整合mybatis+mybatis-plus
    Druid连接池简介和配置
    thinkphp生成的验证码不显示问题解决
    分布式文件系统-FastDFS
    Spring Security OAuth2 Demo
    spring cloud-给Eureka Server加上安全的用户认证
    spring cloud 报错Error creating bean with name 'hystrixCommandAspect' ,解决方案
    分布式唯一ID极简教程
  • 原文地址:https://www.cnblogs.com/cutemush/p/12358035.html
Copyright © 2011-2022 走看看