SCU 4438 Censor

Time Limit:0MS     Memory Limit:0KB     64bit IO Format:%lld & %llu

Description

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

/*/
题意:
给出两个字符串T和S,把所有在S中出现的T删掉,并且合并S,如果合并后还有T继续删掉。 KMP算法,练习 关键在于next表的建立,还有怎么利用next去查询字符串是否相同。 这里推荐一个大佬的博客,我也是从上面学习到的:
next->door( http://blog.csdn.net/sjf0115/article/details/8579484 ) 然后KMP主要有两种用法,一种是用数组+模拟指针去覆盖掉匹配了的字符串,一种是用栈去弹掉匹配成功的串串,理论基本相同。 AC代码:
/*/
#include"algorithm"
#include"iostream"
#include"cstring"
#include"cstdlib"
#include"cstdio"
#include"string"
#include"vector"
#include"stack"
#include"queue"
#include"cmath"
#include"map"
using namespace std;
typedef long long LL ;
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define FK(x) cout<<"["<<x<<"]\n"
#define memset(x,y) memset(x,y,sizeof(x))
#define memcpy(x,y) memcpy(x,y,sizeof(x))
#define bigfor(T) for(int qq=1;qq<= T ;qq++) const int MX=5555555;
/***************************************************/
char s[MX],t[MX],ans[MX];
int next[MX],pos[MX],len1,len2; void init() {
memset(ans,0);
memset(pos,0);
memset(next,0);
} struct Node {
char ch;
int j;
Node() {};
Node(char c,int n):ch(c),j(n) {};
}; void GetNext() {
int i=0,j=-1;
next[0]=-1;
while(i<len2) {
if(j==-1||t[i]==t[j]) {
i++;
j++;
if(t[i]==t[j]) {
next[i]=next[j];
} else next[i]=j;
} else j=next[j];
}
} void KMPStack() {
int i = 0, j = 0;
stack<Node> st;
while(i < len1) {
if(j == -1 || s[i] == t[j]) {//如果前面找不到相匹配的字符或者两个字符相同,加入栈。
j ++;
st.push(Node(s[i], j));
i ++;
} else j = next[j];
if(j == len2) { //匹配成功把栈内的匹配到的T串弹出。
int len = len2;
while(len --) st.pop();
if(st.empty()) j = 0;//如果栈已经空了j返回到0;
else j = st.top().j; //如果不是空的,j变为最后一个字符的next值。
}
}
int cnt = 0;
while(!st.empty()) {
ans[cnt ++] = st.top().ch;
st.pop();
}
for(int i=cnt-1; i>=0; i--) {
putchar(ans[i]);
}
puts("");
} /************************************************/
void KMP() {
int i=0,j=0;
int cnt=0;
while(i<len1) {
ans[cnt]=s[i++]; //字符串一个个的往暗示里面读入
while(!(j==-1||ans[cnt]==t[j])) {
j=next[j];
}
j++;
cnt++; //模拟指针
pos[cnt]=j;
if(j==len2) { //如果找到匹配字符串长度的,指针指回该被匹配到的字符串最初位置
cnt-=len2;
j=pos[cnt];
}
}
// puts(ans);
for(int i=0; i<cnt; i++) {
putchar(ans[i]);
}
puts("");
}
/************************************************/ int main() {
while(~scanf("%s %s",t,s)) {
init();
len1=strlen(s);
len2=strlen(t);
GetNext();
// KMP(); //数组
KMPStack();//栈
}
return 0;
}

  

 
 

ACM: SCU 4438 Censor - KMP的更多相关文章

  1. SCU 4438 Censor|KMP变形题

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

  2. SCU 4438 Censor KMP/Hash

    题意:给定一个模式串和文本,要求删除所有模式串.可能删除后会形成新的模式串,必须全部删除. 思路1:kmp算法求得失配数组,用一个match数组记录文本串中第i字符和未删除的字符能匹配模式串的长度.这 ...

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

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

  4. SCU 4438:Censor

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

  5. SCU 4438 Censor(Hash)题解

    题意:找出字符串p中的w串删除,反复操作,直到找不到w,输出这个串 思路:哈希处理前缀和,如果值相同就删掉. 代码: #include<iostream> #include<algo ...

  6. Censor SCU - 4438

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

  7. 四川省赛 SCU - 4438

    Censor frog is now a editor to censor so-called sensitive words (敏感词). She has a long text pp. Her j ...

  8. Censor(KMP)

    Censor frog is now a editor to censor so-called sensitive words (敏感词). She has a long text pp. Her j ...

  9. ACM:SCU 4437 Carries - 水题

    SCU 4437  Carries Time Limit:0MS     Memory Limit:0KB     64bit IO Format:%lld & %llu  Practice  ...

随机推荐

  1. Java排序算法——基数排序

  2. 浅谈ARP协议以及应用

    0. 前言 本章主要简单的介绍ARP的协议格式,主机如何发送和处理ARP报文,以及免费ARP. 1. ARP协议原理 ARP,全称Address Resolution Protocol,地址解析协议, ...

  3. Linux命令--删除软连接

    1,建立软链接 ln -s 源文件 目标文件 例如:ln -s /usr/hb/ /home/hb_link 2,删除软链接 正确的是:rm -rf hb_link 错误的是:rm -rf hb_li ...

  4. 【转载】使用Pandas进行数据提取

    使用Pandas进行数据提取 本文转载自:蓝鲸的网站分析笔记 原文链接:使用python进行数据提取 目录 set_index() ix 按行提取信息 按列提取信息 按行与列提取信息 提取特定日期的信 ...

  5. [Network] HTML、XML和JSON学习汇总

    写在前面:楼主也是刚刚接触这方面的知识,之前完全是零基础,后来经朋友推荐了几个不错的博文,看完以后豁然开朗.但是此博文更加偏重于基础知识介绍(其实更深的楼主也还不了解,这方面的大神请绕道),只是分享个 ...

  6. Unix/Linux进程间通信(二):匿名管道、有名管道 pipe()、mkfifo()

    1. 管道概述及相关API应用 1.1 管道相关的关键概念 管道是Linux支持的最初Unix IPC形式之一,具有以下特点: 管道是半双工的,数据只能向一个方向流动:需要双方通信时,需要建立起两个管 ...

  7. 计算 TP90TP99TP...

    what-do-we-mean-by-top-percentile-or-tp-based-latency tp90 is a minimum time under which 90% of requ ...

  8. HDU 5686 斐波那契数列、Java求大数

    原题:http://acm.hdu.edu.cn/showproblem.php?pid=5686 当我们要求f[n]时,可以考虑为前n-1个1的情况有加了一个1. 此时有两种情况:当不适用第n个1进 ...

  9. 巧用dimens适配多个分辨率

      让应用自动适配多个分辨率的屏幕,是每个android程序员的基本功,就好像前端工程师熟练编写CSS Hack一样.适配工作中一个重要的工作就是对页面的调整. 对于页面的适配,有很多的方法和技巧.比 ...

  10. 内网安全工具之cain劫持工具

    满足arp的条件为:目标IP为动态IP(arp -a查看) 下载地址:cain4.9.zip 官网:http://www.oxid.it/cain.html 08专版:cain08安装版 把cain下 ...