Censor(KMP)

Censor

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.

Input

The input consists of multiple tests. For each test:

The first line contains 11 string ww. The second line contains 11 string pp.

(1length of w,p51061≤length of w,p≤5⋅106, w,pw,p consists of only lowercase letter)

Output

For each test, write 11 string which denotes the censored text.

Sample Input

abc

aaabcbc

b bbb

abc ab

Sample Output

a

ab

 

// 题意:第一行一个敏感词,然后一行文本,操作如下,删除敏感词,然后如果又新组成敏感词, 重复删掉,直到没有敏感词

 

//如果用KMP和模拟链表的话,就是如果匹配成功了,用链表删除后,再回退lent长度继续匹配,有点麻烦,貌似还可以hash做

 1 #include <iostream>
 2 #include <cstdio>
 3 #include <cmath>
 4 #include <string.h>
 5 using namespace std;
 6 const int MX=5000010;
 7 
 8 int lens,lent;
 9 char T[MX];
10 char S[MX];
11 int l[MX];
12 int r[MX];
13 int fail[MX];
14 
15 void getfail()
16 {
17     int i,j;
18     i=1,j=0;
19     fail[1]=0;
20     while(i<lent)
21     {
22         if (j==0||T[i]==T[j])
23         {
24             i++,j++;
25             fail[i]=j;
26         }
27         else j = fail[j];
28     }
29 }
30 
31 void kmp()
32 {
33     int i=1;
34     int j=1;
35     while(i<=lens&&j<=lent)
36     {
37         if (j==0||S[i]==T[j])
38         {
39             i=r[i];
40             j++;
41         }
42         else j = fail[j];
43 
44         if (j>lent)
45         {
46             int num=0;
47             int pos=i;
48             while (num<lent)
49             {
50                 pos = l[pos];
51                 num++;
52             }
53             r [ l[ pos ] ] = i;
54             l [ i ] = l[ pos ];
55             
56             num=0;
57             pos=i;
58             while (num<lent)
59             {
60                 if (l[pos]==0) break;
61                 pos = l[pos];
62                 num++;
63             }
64             j=1;
65             i = pos;
66         }
67     }
68 }
69 
70 int main()
71 {
72     while(scanf("%s",T+1)!=EOF)
73     {
74         scanf("%s",S+1);
75         lens = strlen(&S[1]);
76         lent = strlen(&T[1]);
77 
78         getfail();
79 
80         for (int i=0;i<=lens+1;i++)
81         {
82             l[i] = i-1;
83             r[i] = i+1;
84         }
85 
86         kmp();
87         for (int i = r[0];S[i];i = r[i])
88         {
89             printf("%c",S[i]);
90         }
91         printf("\n");
92     }
93     return 0;
94 }
View Code

 

转载于:https://www.cnblogs.com/haoabcd2010/p/7384506.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值