bzoj3940[usaco2015feb]censoring

aziint aziint     2022-10-20     298

关键词:

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 \cdots 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 \(\mathrmFJ\) determine the final contents of \(S\) after censoring is complete.

\(\mathrmFJ\) 把杂志上所有的文章摘抄了下来并把它变成了一个长度不超过 \(10^5\) 的字符串 \(S\) 。他有一个包含 \(n\) 个单词的列表,列表里的 \(n\) 个单词记为 \(t_1\cdots t_N\) 。他希望从 \(S\) 中删除这些单词。

\(\mathrmFJ\) 每次在S中找到最早出现的列表中的单词(最早出现指该单词的开始位置最小),然后从 \(S\) 中删除这个单词。他重复这个操作直到 \(S\) 中没有列表里的单词为止。注意删除一个单词后可能会导致 \(S\) 中出现另一个列表中的单词

\(\mathrmFJ\) 注意到列表中的单词不会出现一个单词是另一个单词子串的情况,这意味着每个列表中的单词在 \(S\) 中出现的开始位置是互不相同的

请帮助 \(\mathrmFJ\) 完成这些操作并输出最后的 \(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 \cdots t_N\) . Each string will contain lower-case alphabet characters (in the range \(\mathrma\cdots \mathrmz\) ), 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

Sample Input

begintheescapexecutionatthebreakofdawn
2
escape
execution

Sample Output

beginthatthebreakofdawn

Solution

\(\mathrmac\) 自动机 + 栈。匹配到了就弹,并且修改指针的位置。

#include<bits/stdc++.h>
using namespace std;

#define N 100011
#define rep(i, a, b) for (int i = a; i <= b; i++)

int q[N], ch[N][26], fail[N], endTag[N], tot, now, n, loc[N], m, len[N], stk[N], top;
char str[N], s[N];

int main() 
    scanf("%s%d", str, &m); n = strlen(str);
    rep(i, 1, m) 
        scanf("%s", s); len[i] = strlen(s); now = 0;
        rep(j, 0, len[i] - 1) 
            if (!ch[now][s[j] - 'a']) ch[now][s[j] - 'a'] = ++tot;
            now = ch[now][s[j] - 'a'];
        
        endTag[now] = i;
    
    int l = 0, r = -1;
    rep(i, 0, 25) if (ch[0][i]) q[++r] = ch[0][i];
    for (; l <= r; l++) rep(i, 0, 25)
        if (!ch[q[l]][i])  ch[q[l]][i] = ch[fail[q[l]]][i]; continue; 
        else fail[ch[q[l]][i]] = ch[fail[q[l]]][i], q[++r] = ch[q[l]][i];
    now = 0;
    rep(i, 0, n - 1) 
        stk[++top] = i;
        int t = ch[now][str[i] - 'a'];
        if (t && endTag[t])  top -= len[endTag[t]], now = loc[stk[top]]; continue; 
        loc[i] = now = t;
    
    rep(i, 1, top) putchar(str[stk[i]]);
    return 0;

bzoj3940[usaco2015feb]censoring*

bzoj3940[Usaco2015Feb]Censoring题意:有一个S串和一大堆T串,不断地在S串里找最早出现的T串,然后将其删除。S串≤100000,T串总长度≤100000。题解:对所有T串建AC自动机,然后同bzoj3942。注意,本题的AC自动机必须利用所有fail函数建成... 查看详情

[bzoj3940][usaco2015feb]censoring

最近做题成双成对?不是双倍经验就是两题同解。3940  3942给定字典,给定字符串,删去字符串中所有字典内单词。保证不会出现二者包含状况。$nleq1e5,sumlenleq1e5$AC自动机裸题。build出AC自动机后从左到右插入文本串,同时... 查看详情

bzoj3940:[usaco2015feb]censoring

给个长度<=1e5的串s,再给n个模板串总长不超1e5,每次把s中起始位置最早的一个模板串删掉,求最后剩的串。AC自动机,开个栈记一下每次走到哪里,匹配成功后直接在栈里找到这一串的初始位置对应自动机上的节点,从而回... 查看详情

bzoj3940[usaco2015feb]censoring

【题目描述】FJ把杂志上所有的文章摘抄了下来并把它变成了一个长度不超过10^5的字符串S。他有一个包含n个单词的列表,列表里的n个单词记为t_1...t_N。他希望从S中删除这些单词。 FJ每次在S中找到最早出现的列表中的单词(... 查看详情

bzoj3942:[usaco2015feb]censoring

3942:[Usaco2015Feb]CensoringTimeLimit: 10Sec  MemoryLimit: 128MBSubmit: 404  Solved: 221[Submit][Status][Discuss]DescriptionFarmerJohnhaspurchasedasubscriptiont 查看详情

3940.[usaco15feb]censoringac自动机+栈(代码片段)

DescriptionFarmerJohnhaspurchasedasubscriptiontoGoodHooveskeepingmagazineforhiscows,sotheyhaveplenty ofmaterialtoreadwhilewaitingaroundinthebarnduringmilkingsessions.Unfortunately,thelatest  查看详情

bzoj3942[usaco2015feb]censoring*

bzoj3942[Usaco2015Feb]Censoring题意:有一个S串和一个T串,不断地在S串里匹配T串,然后将其删除。S串、T串长度≤1000000。题解:用1、2两个栈,每次将S串的当前字符压入1栈,当前匹配到T串的位置压入2栈,如果匹配出一个T串,则让1... 查看详情

最大生成树bzoj3943[usaco2015feb]superbull

 TimeLimit: 10Sec  MemoryLimit: 128MBSubmit: 374  Solved: 217[Submit][Status][Discuss]DescriptionBessieandherfriendsareplayinghoofballintheannualSuperbullc 查看详情

bzoj_3942_[usaco2015feb]censoring_kmp

BZOJ_3942_[Usaco2015Feb]Censoring_KMPDescription有一个S串和一个T串,长度均小于1,000,000,设当前串为U串,然后从前往后枚举S串一个字符一个字符往U串里添加,若U串后缀为T,则去掉这个后缀继续流程。InputThefirstlinewillcontainS.Thesecondlinewill... 查看详情

bzoj2015:[usaco2010feb]chocolategivingspfa

因为是双向边,所以相当于两条到1的最短路和,先跑spfa然后直接处理询问即可#include<iostream>#include<cstdio>#include<queue>usingnamespacestd;constintN=50005,inf=1e9;intn,m,b,h[N],cnt,dis[N];boolv[N];structqweintne,to, 查看详情

bzoj3939[usaco2015feb]cowhopscotch动态开点线段树优化dp

题目描述JustlikehumansenjoyplayingthegameofHopscotch,FarmerJohn‘scowshaveinventedavariantofthegameforthemselvestoplay.Beingplayedbyclumsyanimalsweighingnearlyaton,CowHopscotchalmostalwaysendsindisaster,bu 查看详情

[bzoj3943][usaco2015feb]superbull_kruskal

SuperBullbzoj-3943Usaco-2015Feb题目大意:贝西和她的朋友们在参加一年一度的“犇”(足)球锦标赛。FJ的任务是让这场锦标赛尽可能地好看。一共有N支球队参加这场比赛,每支球队都有一个特有的取值在1-230-1之间的整数编号(即... 查看详情

bzoj3939[usaco2015feb]cowhopscotch

传送门:http://www.lydsy.com/JudgeOnline/problem.php?id=3939【题解】f[i][j]=s[i-1][j-1]-sum[a[i][j]]用cdq分治来处理横坐标,先处理上面的dp值,再讨论上面部分对下面的贡献,然后分治处理下面的dp值即可。如果加了快读好像排bzojrk1?#include<s... 查看详情

bzoj3942:[usaco2015feb]censoringkmp+栈

好久没写kmp都不会写了……开两个栈,s存当前串,c存匹配位置用t串在栈s上匹配,栈每次入栈一个原串字符,用t串匹配一下,如果栈s末尾匹配了t则弹栈#include<iostream>#include<cstdio>#include<cstring>usingnamespacestd;constintN=1... 查看详情

bzoj3943[usaco2015feb]superbull:最大生成树

题目链接:http://www.lydsy.com/JudgeOnline/problem.php?id=3943题意:  有n只队伍,每个队伍有一个编号a[i]。  每场比赛有两支队伍参加,然后选一支队伍淘汰。共进行n-1场比赛,然后比赛结束。  若某场比赛是队伍i和j参加,则该... 查看详情

[bzoj2591][usaco2012feb]nearbycows

2591:[Usaco2012Feb]NearbyCowsTimeLimit:4Sec  MemoryLimit:128MBSubmit:149  Solved:89[Submit][Status][Discuss]Description FarmerJohnhasnoticedthathiscowsoftenmovebetweennearbyfi 查看详情

bzoj1651:[usaco2006feb]stallreservations专用牛棚

二次联通门: BZOJ1651:[Usaco2006Feb]StallReservations专用牛棚   权限题放题面DescriptionOhthosepickyN(1<=N<=50,000)cows!TheyaresopickythateachonewillonlybemilkedoversomeprecisetimeintervalA.. 查看详情

[bzoj]1631:[usaco2007feb]cowparty

1631:[Usaco2007Feb]CowPartyTimeLimit:5Sec  MemoryLimit:64MBSubmit:866  Solved:624[Submit][Status][Discuss]Description    农场有N(1≤N≤1000)个牛棚,每个牛棚都有1只奶牛要参加在X 查看详情