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.

(1≤length of w,p≤5⋅1061≤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


题意:给出两个字符串,在第二个里面去找寻有没有第一个,有的话删除,然后再合并字符串,问最后的字符串是怎样的
如样例一 aaabcbc -> aabc -> a 思路:我们想想,这么大的数据范围,光是我们查找子串是否存在就必须要用kmp,那我们再想想如何优化呢
怎么解决那个删除之后再合并呢,我开始想的是用数组模拟链表,那样删除操作的话,只要改变一个指向就可以了
列出下面我的错误代码
#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
using namespace std;
struct sss
{
int next;
char c;
}s1[];
int len1,len2;
int head;
int next1[];
char s2[];
char str[];
int num[];
void getnext(char s[])
{
int i=,j=-;
next1[]=-;
while(i<len2)
{
if(j==-||s[i]==s[j])
{
next1[++i]=++j;
}
else j=next1[j];
}
}
int kmp(struct sss s1[],char s2[])
{
int i=head,j=;
int cnt=;
num[cnt++]=i;
while(i<len1)
{
if(j==-||s1[i].c==s2[j])
{
i=s1[i].next;
num[cnt++]=i;
j++;
if(j==len2)
{
if(cnt-len2-<) head=i;
else s1[num[cnt-len2-]].next=i;
return ;
}
}
else j=next1[j];
}
return ;
}
int main()
{
while(scanf("%s",s2)!=EOF)
{
scanf("%s",str);
len1=strlen(str);
len2=strlen(s2);
if(len2>len1)
{
printf("%s\n",str);
continue;
}
for(int i=;i<len1;i++)
{
s1[i].next=i+;
s1[i].c=str[i];
}
getnext(s2);
head=;
while(kmp(s1,s2));
int i=head;
while(i<len1)
{
printf("%c",s1[i].c);
i=s1[i].next;
}
printf("\n");
}
}

但是我的想法是一旦删除之后,就改变指向,然后再次退出去,然后再从头开始找,这样无疑还是超时了

我们再想想怎么利用kmp的性质去优化,我在每个位置存下模式串当前匹配到哪个位置,

然后我用一个数组存下来,当`我找到一个之后,那个匹配之前的位置的模式位置值又赋值给当前即可

如    样例一

aaabcbc

01112323

abc

删除一遍后j的值并不是从0开始而是赋值abc之前的那个位置值1,代表以后匹配了a,无需再去比

字符串中我们需要灵活的利用kmp的思想来优化时间复杂度

具体看代码

#include<cstdio>
#include<cstring>
#include<string>
#include<algorithm>
#include<queue>
using namespace std;
typedef long long LL ;
const int MX=;
char s[MX],t[MX],ans[MX];
int next1[MX],pos[MX],len1,len2; void init() {
memset(ans,,sizeof(ans));
memset(pos,,sizeof(pos));
memset(next1,,sizeof(next1));
} struct Node {
char ch;
int j;
Node() {};
Node(char c,int n):ch(c),j(n) {};
}; void GetNext() {
int i=,j=-;
next1[]=-;
while(i<len2) {
if(j==-||t[i]==t[j]) {
i++;
j++;
if(t[i]==t[j]) {
next1[i]=next1[j];
} else next1[i]=j;
} else j=next1[j];
}
}
void KMP() {
int i=,j=;
int cnt=;
while(i<len1) {
ans[cnt]=s[i++];
while(!(j==-||ans[cnt]==t[j])) {
j=next1[j];
}
j++;
cnt++;
pos[cnt]=j;//存当前模式串匹配到哪个位置
if(j==len2) {
cnt-=len2;//直接赋值之前的位置给他
j=pos[cnt];
}
}
for(int i=; i<cnt; i++) {
putchar(ans[i]);
}
puts("");
}
int main() {
while(~scanf("%s %s",t,s)) {
init();
len1=strlen(s);
len2=strlen(t);
GetNext();
KMP();
}
return ;
}

四川省赛 SCU - 4438的更多相关文章

  1. ACM: SCU 4438 Censor - KMP

     SCU 4438 Censor Time Limit:0MS     Memory Limit:0KB     64bit IO Format:%lld & %llu  Practice D ...

  2. SCU 4438 Censor(哈希+模拟栈)

    Censor frog is now a editor to censor so-called sensitive words (敏感词). She has a long text \(p\). He ...

  3. SCU 4440 Rectangle 2015年四川省赛题

    题目链接:http://acm.scu.edu.cn/soj/problem/4440/ 题目大意:给一个n*m的方格,求周长小于等于k的矩形有多少个. 解题思路:我之前直接暴力,显然超时,所以后来发 ...

  4. SCU 4436 Easy Math 2015年四川省赛题

    题目链接:http://acm.scu.edu.cn/soj/problem/4436/ 题意:给你n个整数,求这n个数的平方根和是否是一个整数: 解题思路:如果这题每个数给他算出来,必然费时间,可能 ...

  5. 第十五届四川省省赛 SCU - 4443 Range Query

    先给你1~N的N个数 再给你每种最多50个的条件(ai,bi,ci) 或者[ai,bi,ci] (ai,bi,ci)表示下标ai到bi的最小值必为ci [ai,bi,ci]表示下标ai到bi的最大值必 ...

  6. 第十五届四川省省赛 SCU - 4439 Vertex Cover

    给你一个一般图 保证每条边的一端下标不大于30 问最小覆盖集的大小为多少 爆搜:枚举前30个点是否在覆盖集内 剪枝1:如果不在的话 那么他所连的下标大于30的点都必须选 剪纸2:最优解剪枝 #incl ...

  7. 第十五届四川省省赛 SCU - 4444 Travel

    给你一个一共由两种边的完全图 要求你求1到N的最短路 q队列为前沿队列(已探索过且最外围的点)  p队列为未探索队列(未探索过的点) depth这个数组的用法并不是代表实际上这个点在第几层 而是防止死 ...

  8. HDU 4727 The Number Off of FFF 2013年四川省赛题

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4727 题目大意:队列里所有人进行报数,要找出报错的那个人 思路:,只要找出序列中与钱一个人的数字差不是 ...

  9. HDU 4722 Good Numbers 2013年四川省赛题

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4722 题目大意:给定一个区间,求区间中有多少个满足每位上的数的和是10的倍数. 解题思路:先打表暴力求 ...

随机推荐

  1. 练习 配置WCF服务

    http://blog.csdn.net/suntanyong88/article/details/8203572   目录(?)[+] 1OrderTrackWindowsKZT   控制台应用 程 ...

  2. python3-知识扩展扫盲易忘-generator的用法

    前部分转自: https://www.cnblogs.com/Tsukasa/p/6613934.html 通过列表list生成器,我们可以直接创建一个列表 ? 1 2 3 >>> ...

  3. 部署--云服务器(RubyChina上的转帖); 附加用cap部署sidekiq

    https://ruby-china.org/topics/36899 附加https://ruby-china.org/topics/36899 Capistrano + Rails5.2部署 使用 ...

  4. 关于导入高德地图 java.lang.UnsatisfiedLinkError: Couldn't load XXXfrom loader dalvik.system.PathClassLoader[DexPathLis

    然后后面就是找不到高德地图提供的地图so 就是上面几个 然后不要忘了在buildGradle文件里添加这么一句话 sourceSets { main { jniLibs.srcDirs = ['lib ...

  5. 手机中SN、MEID、IMEI的意思

    SN SN码是Serial Number的缩写,有时也叫SerialNo,也就是产品序列号,产品序列是为了验证“产品的合法身份”而引入的一个概念,它是用来保障用户的正版权益,享受合法服务的:一套正版的 ...

  6. vux, vue如何控制微信自带的返回按钮,让其返回其他页面?

    <script> import { mapState } from 'vuex' export default{ name: 'clockFx', data () { return { } ...

  7. 以CapsNet为例谈深度学习源码阅读

    本文的参考的github工程链接:https://github.com/laubonghaudoi/CapsNet_guide_PyTorch 之前是看过一些深度学习的代码,但是没有养成良好的阅读规范 ...

  8. leetcode-algorithms-32 Longest Valid Parentheses

    leetcode-algorithms-32 Longest Valid Parentheses Given a string containing just the characters '(' a ...

  9. commonJS,常用js工具方法

    说明:平时项目用到的一些常见过滤方法,有些是vue过滤器,稍微修改下吧,我就不改了. js四舍五入不准确的解决(重写方法): Number.prototype.toFixed = function(l ...

  10. python中生成器

    1.简介 通过列表生成式,我们可以直接创建一个列表,但是受到内存的限制,列表容量肯定是有限的. 如果列表元素可以按照某种算法推算出来,那我们是否可以在循环的过程中不断推算出后续的元素呢? 在Pytho ...