ACM: SCU 4438 Censor - KMP
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
abcaaabcbcbbbbabcab
Sample Output
aab
/*/ 题意: 给出两个字符串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;
}
转载于:https://www.cnblogs.com/HDMaxfun/p/5784276.html
ACM: SCU 4438 Censor - KMP相关推荐
- SCU 4438 Censor (KMP)
题目大意: 就是现在给出两个字符串A, B, 长度都不超过500万, 要求每次将B中第一次A的出现删除, 然后将B剩下的两段前后合并, 然后重复这个过程直到没有可以删除的A位置, 问最后剩下的串是什么 ...
- 【哈希-字符串匹配+模拟栈】SCU - 4438: Censor(哈希详解哈哈哈)
写在前:由于哈希没有好好听讲,也没有下来看.这是排位我开的第一道题,直接string暴力T了.昨天其实就看了哈希,太浮躁,于是本来很简单的哈希愣是没看懂.(于是放弃去看爱5了23333333)今天补上 ...
- SCU - 4438——Censor(哈希)
题目链接:http://acm.scu.edu.cn/soj/problem.action?id=4438 题意:给出一个串a和串b,串b中如果有a串则删除,删除后b剩下的串再连到一起,再找串a,反复 ...
- SCU - 4438 Censor (KMP)
Censor frog is now a editor to censor so-called sensitive words (敏感词). She has a long text pp. Her j ...
- SCU - 4438 Censor
Censor frog is now a editor to censor so-called sensitive words (敏感词). She has a long text pp. Her j ...
- SCU - 4438 Censor
Censor frog is now a editor to censor so-called sensitive words (敏感词). She has a long text p . Her j ...
- SCU 4438:Censor
Censor frog is now a editor to censor so-called sensitive words (敏感词). She has a long text p . Her j ...
- SCU 4438 Censor
$KMP$,链表. 将$p$弄成链表,每次匹配到,删掉中间的,继续匹配. #include<bits/stdc++.h> using namespace std;const int INF ...
- SCU 4438 Censor(Hash)题解
题意:找出字符串p中的w串删除,反复操作,直到找不到w,输出这个串 思路:哈希处理前缀和,如果值相同就删掉. 代码: #include<iostream> #include<algo ...
最新文章
- JAVA上传服务器端代码
- c嵌入python类获取返回值
- 关于linux shell脚本开头的#!/bin/sh
- java数据类型划分_一张图搞定java数据类型的划分
- pyecharts第十节、地图(二、中国地图)
- 为iOS 项目引入其它开发包 的方法
- flask-前端-requests之response对应关系 img
- VS2012+OpenCV2.4.9+Qt5.3.1环境配置
- python双人贪吃蛇游戏
- python 爬虫 中乱码问题0xb5 和b'\x1f\x8b\x08
- 如何买到货真价实的OA产品?
- 怎样计算期货交易盈亏(期货交易盈利怎么算)
- 数据分析/运营——数据异常的排查方法
- 语义化你的HTML标签和属性
- Jetpack 架构组件:Room 数据库应用
- IDELAY输入延迟分析
- 关于VS2022 C++报错 const char* 类型的实参与char *类型的形参不兼容解决办法
- java 圆类 圆锥类_喉室位于_java程序设计答案_学小易找答案
- C语言例题理解(小写字母转换成大写字母,兔子生兔子问题,求100以内勾股数,整数逆序输出)
- 省市县结合身份证号6位码的三级联动