Censor
frog is now a editor to censor so-called sensitive words (敏感词).

She has a long text p. Her job is relatively simple – just to find the first occurence of sensitive word w 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 1 string w. The second line contains 1 string p.

(1≤length of w,p≤5*106, w,p consists of only lowercase letter)

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

对于这道题不知道说什么,我对KMP算法还是有些懵,花了一个上午,看几篇大佬的博客,也算是入了些门,这个题当时知道是用KMP算法的,但当时只是知道有这么个算法,没理解,看了别人的讲解让我不得不感叹kmp的强大,题目的思路我是看了一个大佬的博客,我只能说我还是太菜了。
思路来源,KMP,Hash两种解法

#include<iostream>
#include<cstdio>
#include<algorithm>
#include<stack>
#include<cstring>
using namespace std;
int n,m;
char a[5000005],b[5000005],ans[5000005];
int nextt[5000005];
void getnext(){int j=0,k=-1;nextt[0]=-1;while (j<m-1){if (k==-1||b[j]==b[k]){j++;k++;nextt[j]=k;}elsek=nextt[k];}
}
void kmp(){stack<int> st;stack<int> mt;int i = 0, j = 0;getnext();while (i < n){if(j == -1 || a[i] == b[j]){j++;i++;st.push(a[i-1]);mt.push(j);if (j == m){   //这一句不知为何放到下面会WAfor(int i=0;i<m;i++){st.pop();mt.pop();}if(mt.empty()) j=0;else j=mt.top();}}else{j = nextt[j];}     }int cnt=st.size()-1;ans[cnt+1]='\0';while(!st.empty()){ans[cnt--]=st.top();st.pop();}printf("%s\n",ans);
}
int main()
{while(scanf("%s",&b)!=EOF){scanf("%s",&a);m=strlen(b);n=strlen(a);if(m>n){printf("%s\n",a);continue;}kmp();}return 0;
}

C - Censor KMP算法相关推荐

  1. Censor KMP算法

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

  2. SCU4438 Censor(审查员) (KMP算法与模拟栈的应用 || HASH表与模拟栈的结合)

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

  3. ACM: SCU 4438 Censor - KMP

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

  4. BF算法优化-------KMP算法

    百度百科:KMP算法是一种改进的字符串匹配算法,由D.E.Knuth,J.H.Morris和V.R.Pratt提出的,因此人们称它为克努特-莫里斯-普拉特操作(简称KMP算法).KMP算法的核心是利用 ...

  5. 数据结构与算法(5)字符串(BF算法、KMP算法及KMP算法优化)

    目录 一.BF算法(暴力算法) 二.KMP算法 三.KMP算法优化 一.BF算法(暴力算法) 一个一个往后匹配,匹配失败继续从母串下一个和头(子串的头)往后继续匹配. 虽然简单,但是需要较多的时间复杂 ...

  6. hiho 1015 KMP算法 CF 625 B. War of the Corporations

    #1015 : KMP算法 时间限制:1000ms 单点时限:1000ms 内存限制:256MB 描述 小Hi和小Ho是一对好朋友,出生在信息化社会的他们对编程产生了莫大的兴趣,他们约定好互相帮助,在 ...

  7. HDU 1711 Number Sequence(KMP算法)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1711 Number Sequence Time Limit: 10000/5000 MS (Java/ ...

  8. 算法(2)KMP算法

    1.0 问题描述 实现KMP算法查找字符串. 2.0 问题分析 "KMP算法"是对字符串查找"简单算法"的优化. 字符串查找"简单算法"是源 ...

  9. KMP算法求回溯数组的步骤

    KMP算法到底是什么原理就不说了,各种资料上讲的明明白白,下面我就如何用代码来实现做一下说明和记录. KMP的核心思想就是,主串不回溯,只模式串回溯.而模式串匹配到第几位时失配,要回溯多少,由模式串本 ...

最新文章

  1. 应用打包Ubuntu下部署ScriptCase应用一例
  2. C++改变基类成员在派生类中的访问属性
  3. 关于“习惯”的精彩分析
  4. php匿名类的应用场景,【modernPHP专题(9)】匿名类
  5. 3d怎么把两个面拼接在一起_vr全景看房用什么软件做的?3D看房是怎么拍摄的?...
  6. JDK1.8中String类的intern()方法学习
  7. 浅谈语音识别技术的发展趋势与应用前景
  8. cvpr2020 人脸检测与识别_Python人脸检测识别实例教程
  9. 数学建模学习1.18——模糊综合评价模型
  10. 2021年「博客之星」参赛博主:南浔Pyer
  11. @程序员:今日七夕,宜找对象
  12. D2大会资源分享(解决了GitHub下载限速)
  13. 安装colmap时报错“METIS_INCLUDE_DIRS-NOTFOUND“
  14. 给PowerPoint按钮加上提示文字
  15. nbextensions安装完不显示,404 GET /static/components/marked/lib/marked.js?v=20230331152041
  16. 中望CAD的引线标注格式怎么改_没想到啊,原来CAD命令还可以这样学习
  17. 通俗理解 三次握手四次挥手(老友依恋式)
  18. java alter session_java程序里面可以执行alter session之类的语句?
  19. 发现细分赛道:费控SaaS跨越资本寒冬
  20. c语言malloc,calloc,realloc函数介绍

热门文章

  1. 借“窄喉”之力,边缘计算期待“井喷”
  2. 洗头变洗脑,文峰的“金字招牌”
  3. 产品经理入门教程02——产品岗会涉及的互联网黑话
  4. 基本的excel表格开发
  5. 使用LODOP打印插件套打
  6. java服务器登录系统,JavaWeb入门登陆注册系统
  7. 计算机win10+上锁,手把手教您win10系统给电脑屏幕上锁的办法
  8. ffmpeg之demux
  9. 微信小程序开发大坑盘点
  10. Python爬虫实战(1) | 爬取豆瓣网排名前250的电影(下)