frog is now a editor to censor so-called sensitive words (敏感词).
She has a long text pp. Her job is relatively simple -- just to find the first occurence of sensitive word ww and remove it.
frog repeats over and over again. Help her do the tedious work.
弗雷格现在是一个编辑员,专门审查所谓的河蟹词。
他现在有一个长文本p,他的工作相对简单点——就是去找到敏感词的首次出现的位置并且删掉它。
弗雷格一遍又一遍的重复,帮助他去完成这个无聊的任务。
The input consists of multiple tests. For each test:
输入包含多组数据,对于每组数据:
The first line contains 11 string ww. The second line contains 11 string pp.
第一行包含一个字符串W,第二行包含一个字符串P
(1≤length of w,p≤5⋅1061≤length of w,p≤5⋅106, w,pw,p consists of only lowercase letter)
For each test, write 11 string which denotes the censored text.
对于每个测试样例,输出一个字符串,代表已经河蟹后的文本。
abc
aaabcbc
b
bbb
abc
ab
a
ab
当我刚看到这个题的时候,因为想到删除文本后后边的文本要跟着前移至删除文本的位置,类似于Python的动态内存管理。我一开始的想法是kmp+vector,不过在SCUOJ上得到了1986ms的高评价,只好作罢。
由于在程序实现中我们需要中断文本,由此达成删去关键词的目的,栈这种数据结构刚好能满足我们的要求。我们在用kmp算法匹配相应字符串的同时把原先的文本字符串逐渐压入栈中,如果匹配到了,那么就把匹配到的字符串整个从栈中拽出,实现了“删除”的效果,而匹配是在被匹配成功字符串的最后一个字符后继续进行的,所以这个过程也就满足了“自动连接”的效果。
详情见代码
#include
#include
#include
#include
#include
#include
#include
#include
#define DETERMINATION main
#define lldin(a) scanf("%lld",&a)
#define din(a) scanf("%d",&a)
#define printlnlld(a) printf("%lld\n",a)
#define printlnd(a) printf("%d\n",a)
#define printlld(a) printf("%lld",a)
#define printd(a) printf("%d",a)
#define reset(a,b) memset(a,b,sizeof(a))
const int INF=0x3f3f3f3f;
using namespace std;
const double PI=acos(-1);
typedef long long ll;
typedef long double ld;
const int mod=1000000007;
///Schlacht von Stalingrad
/**Although there will be many obstructs ahead,
the desire for victory still fills you with determination..**/
char text[7650000],key[7650000];
int next2[7650000],tmp[7650000];
ll indice;//仿真栈的指针
char impersonated[7650000];//仿真栈
void generator()//next数组生成函数,这个数组是用来决定某次匹配失败后指向
匹配文本(相对于被匹配文本而言)的指针回到前面的哪个位置。
{
ll ph=-1,pt=0;
next2[0]=-1;
ll klen=strlen(key);
while(pt0&&key[pk]!=text[pt])
pk=next2[pk];//指针跳回去
if(key[pk]==text[pt])
pk++;//前移
tmp[indice]=pk;//因为栈中逐渐形成了一个字符串,所以需要把next数组移植过来.
// cout<
我们都知道hash表是一种KEY-VALUE类数据结构,只要掌握一个键值,我们就可以访问这个键值指向的内容,相应的,如果键值错误,我们自然无法访问到正确的内容。
思路大致如下:
代码如下:
#include
#include
#include
#include