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. tnt_esri.dat Arcgis8.1安装license

    arcgis8.1授权文件内容,复制个txt重命名为tnt_esri.dat即可.注意替换yourcomputername为你的计算机名   SERVER yourcomputername ESRI_ ...

  2. Bash 小问题【待更新】

    bash 问题: 编写一个函数,用来返回某个目录下的目录个数.对于主目录下的所有目录,显示其属性信息,并把属性信息重定位到file_n(n=1.2.3)文件(第一个目录信息重定位到file_1, 第二 ...

  3. 如何合并两个Docker 镜像

    http://www.open-open.com/lib/view/open1437746544709.html 在你的机器上使用docker pull来从Docker Hub下载镜像. docker ...

  4. jquery-easyui 树的使用笔记

    通常还是使用jquery-ui, 它是完全免费的, jquery-easyui可以使用 freeware edition. 但easyui还不是完全免费的: 它是基于jquery, 但是第三方开发的, ...

  5. [Recommendation System] 推荐系统之协同过滤(CF)算法详解和实现

    1 集体智慧和协同过滤 1.1 什么是集体智慧(社会计算)? 集体智慧 (Collective Intelligence) 并不是 Web2.0 时代特有的,只是在 Web2.0 时代,大家在 Web ...

  6. Problem to be sovled

    Given an array A of N integers, we draw N discs in a 2D plane such that the I-th disc is centered on ...

  7. 什么叫哈希表(Hash Table)

    散列表(也叫哈希表),是根据关键码值直接进行访问的数据结构,也就是说,它通过把关键码值映射到表中一个位置来访问记录,以加快查找的速度.这个映射函数叫做散列函数,存放记录的数组叫做散列表. - 数据结构 ...

  8. 在Linux上挂载Windows共享文件夹,如何开机自动挂载(mount)?

    按照一般的思路,我们先将文件夹挂载上去,命令如下: mkdir /mnt/share_software mount //192.9.206.43/share_software /mnt/share_s ...

  9. 2016年11月24日--面向对象、C#小复习

    面对对象就是:把数据及对数据的操作方法放在一起,作为一个相互依存的整体——对象.对同类对象抽象出其共性,形成类.类中的大多数数据,只能用本类的方法进行处理.类通过一个简单的外部接口与外界发生关系,对象 ...

  10. python的os模块

    1.得到当前工作目录,即当前Python脚本工作的目录路径: os.getcwd() 2.返回指定目录下的所有文件和目录名:os.listdir() 3.函数用来删除一个文件:os.remove() ...