• bzoj3940: [Usaco2015 Feb]Censoring


    AC自动机。为什么洛谷水题赛会出现这种题然而并不会那么题意就不说啦 。终于会写AC自动机判断是否是子串啦。。。用到kmp的就可以用AC自动机水过去啦

    #include<cstdio>
    #include<cstring>
    #include<iostream>
    #include<algorithm>
    #include<queue>
    using namespace std;
    #define rep(i,s,t) for(int i=s;i<=t;i++)
    #define dwn(i,s,t) for(int i=s;i>=t;i--)
    #define clr(x,c) memset(x,c,sizeof(x))
    const int nmax=100005;
    const int maxn=27;
    const int inf=0x7f7f7f7f;
    int ch[nmax][maxn],fail[nmax],F[nmax],T[nmax],pt=0;
    char s[nmax],t[nmax],S[nmax];
    void insert(int x){
    	int len=strlen(s),t=0;
    	rep(i,0,len-1) {
    		if(!ch[t][s[i]-'a']) ch[t][s[i]-'a']=++pt;
    		t=ch[t][s[i]-'a'];
    	}
    	F[t]=len;
    }
    void getfail(){
    	queue<int>q;fail[0]=0;q.push(0);
    	while(!q.empty()){
    		int x=q.front();q.pop();
    		rep(i,0,25) {
    			if(ch[x][i]) q.push(ch[x][i]),fail[ch[x][i]]=x==0?0:ch[fail[x]][i];
    			else ch[x][i]=x==0?0:ch[fail[x]][i];
    		}
    	}
    }
    int main(){
    	scanf("%s",t+1);
    	int n;scanf("%d",&n);
    	rep(i,1,n) scanf("%s",s),insert(i);
    	getfail();
    	//rep(i,0,pt) printf("%d ",F[i]);printf("
    ");
    	//rep(i,0,pt) printf("%d ",fail[i]);printf("
    ");
    	int len=strlen(t+1),x=0,top=0;
    	rep(i,1,len){
    		S[++top]=t[i];
    		x=ch[x][t[i]-'a'];T[top]=x;
    		if(F[x]) top-=F[x],x=T[top];
    		//printf("%d",x);
    	}
    	rep(i,1,top) printf("%c",S[i]);printf("
    ");
    	return 0;
    }
    

      

    3940: [Usaco2015 Feb]Censoring

    Time Limit: 10 Sec  Memory Limit: 128 MB
    Submit: 253  Solved: 126
    [Submit][Status][Discuss]

    Description

    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^5 characters. 
    He has a list of censored words t_1 ... t_N that he wishes to delete from S. To do so Farmer John finds 
    the earliest occurrence of a censored word in S (having the earliest start index) and removes that instance 
    of the word from S. He then repeats the process again, deleting the earliest occurrence of a censored word 
    from S, repeating until there are no more occurrences of censored words in S. Note that the deletion of one 
    censored word might create a new occurrence of a censored word that didn't exist before.
    Farmer John notes that the censored words have the property that no censored word appears as a substring of 
    another censored word. In particular this means the censored word with earliest index in S is uniquely 
    defined.Please help FJ determine the final contents of S after censoring is complete.
    FJ把杂志上所有的文章摘抄了下来并把它变成了一个长度不超过10^5的字符串S。他有一个包含n个单词的列表,列表里的n个单词
    记为t_1...t_N。他希望从S中删除这些单词。 
    FJ每次在S中找到最早出现的列表中的单词(最早出现指该单词的开始位置最小),然后从S中删除这个单词。他重复这个操作直到S中
    没有列表里的单词为止。注意删除一个单词后可能会导致S中出现另一个列表中的单词 
    FJ注意到列表中的单词不会出现一个单词是另一个单词子串的情况,这意味着每个列表中的单词在S中出现的开始位置是互不相同的 
    请帮助FJ完成这些操作并输出最后的S
    
    
    

    Input

    The first line will contain S. The second line will contain N, the number of censored words. The next N lines contain the strings t_1 ... t_N. Each string will contain lower-case alphabet characters (in the range a..z), and the combined lengths of all these strings will be at most 10^5.
    第一行包含一个字符串S 
    第二行包含一个整数N 
    接下来的N行,每行包含一个字符串,第i行的字符串是t_i
    
    
    

    Output

    The string S after all deletions are complete. It is guaranteed that S will not become empty during the deletion process.
    一行,输出操作后的S
     
     

    Sample Input

    begintheescapexecutionatthebreakofdawn
    2
    escape
    execution

    Sample Output

    beginthatthebreakofdawn

    HINT

     

    Source

    [Submit][Status][Discuss]
  • 相关阅读:
    JWT实现用户权限认证
    给你的网页添加一个随机的BGM
    Git版本控制
    常见数据的解析
    PHP处理CSV表格文件的常用操作方法是怎么样呢
    php curl语句的用法
    PHP操作Memcache基本函数的方法
    PHP使用PHPExcel删除Excel单元格指定列的方法是怎样
    php单链表实现的代码
    PHP中Array关于数组的用法
  • 原文地址:https://www.cnblogs.com/fighting-to-the-end/p/5721800.html
Copyright © 2020-2023  润新知