SOJ 4438 Censor(KMP匹配)
2016-08-17 18:22
239 查看
C - CensorTime Limit:0MS Memory Limit:0KB 64bit IO Format:%lld& %lluSubmit StatusDescription
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 andremove 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≤lengthof w,p≤5⋅1061≤lengthof w,p≤5⋅106, w,pw,p consistsof only lowercase letter)Output
For each test, write 11 stringwhich denotes the censored text.Sample Input
abc aaabcbc b bbb abc ab
Sample Output
a ab
/*头文件模板*/#include <map>#include <set>#include <cmath>#include <ctime>#include <stack>#include <queue>#include <vector>#include <cctype>#include <cstdio>#include <string>#include <cstring>#include <sstream>#include <cstdlib>#include <iomanip>#include <typeinfo>#include <iostream>#include <algorithm>#include <functional>using namespace std;#define pb push_back#define mp make_pair#define mem(a, x) memset(a, x, sizeof(a))#define copy(a, b) memcpy(a, b, sizeof(a))#define lson rt << 1, l, mid#define rson rt << 1|1, mid + 1, r#define FIN freopen("input.txt", "r", stdin)#define FOUT freopen("output.txt", "w", stdout)typedef long long LL;typedef pair<int, int > PII;typedef pair<int, string> PIS;typedef pair<LL, LL>PLL;typedef unsigned long long uLL;template<typename T>void print (T* p, T* q, string Gap = " ", bool flag = false) {int d = p < q ? 1 : -1;while (p != q) {if (flag) cout << Gap[0] << *p << Gap[1];else cout << *p;p += d;if (p != q && !flag) cout << Gap;}cout << endl;}template<typename T>void print (const T &a, string bes = "") {int len = bes.length();if (len >= 2) cout << bes[0] << a << bes[1] << endl;else cout << a << endl;}template<typename T>void debug (T* p, T* q, string Gap = " ", bool flag = false) {int d = p < q ? 1 : -1;cout << "Debug out : ";while (p != q) {if (flag) cout << Gap[0] << *p << Gap[1];else cout << *p;p += d;if (p != q && !flag) cout << Gap;}cout << endl;}template<typename T>void debug (const T &a, string bes = "") {int len = bes.length();cout << "Debug out : ";if (len >= 2) cout << bes[0] << a << bes[1] << endl;else cout << a << endl;}void IO_Init() {ios::sync_with_stdio (false);}LL LLabs (const LL a) {return a >= 0 ? a : -a;}const double PI = 3.1415926535898;const double eps = 1e-10;const int MAXM = 1e5 + 5;const int MAXN = 5e6 + 5;const int INF = 0x3f3f3f3f;/*头文件模板*/char a[MAXN], b[MAXN], L[MAXN];int nexts[MAXN];struct o {char c;int l;o() {}o (char c, int l) : c (c), l (l) {}};void getNextval (char *p) {int plen = strlen (p);int k = nexts[0] = -1;int j = 0;while (j < plen - 1) {if (k == -1 || p[j] == p[k]) {j++;k++;if (p[j] != p[k]) {nexts[j] = k;} else {nexts[j] = nexts[k];}} else {k = nexts[k];}}}int kmp (char *s, char *p) {int i = 0, j = 0;int slen = strlen (s);int plen = strlen (p);stack<o>O;while (i < slen) {if (j == -1 || s[i] == p[j]) {i++;j++;O.push (o (s[i - 1], j) );} else {j = nexts[j];}if (j == plen) {int l = plen;while (l --) O.pop();if (O.empty()) {j = 0;} else {j = O.top().l;}}}int cnt = 0;while(!O.empty()){L[cnt ++] = O.top().c;O.pop();}for(int i = cnt - 1;i >= 0;i --){printf("%c", L[i]);}printf("\n");}int main() {#ifndef ONLINE_JUDGE//FIN;//FOUT;#endifIO_Init();while (~scanf ("%s%s", a, b) ) {getNextval (a);kmp (b, a);}return 0;}
相关文章推荐
- [编程题]单词接龙
- Bluetooth在andriod下的开发基本操作
- window常用运行代码
- C++中const成员函数和const对象
- Java实现简单的FTP服务器
- 深度学习笔记3:实现一个卷积神经网络
- Android MVP 利用rxjava 避免向Model传入监听方法
- hdoj 1179 Ollivanders: Makers of Fine Wands since 382 BC. (二分图--最大匹配)
- 见过的智力题及我的解题思路
- iOS开发系列--音频播放、录音、视频播放、拍照、视频录制
- 转:国内外著名开源b2c电子商务系统比较包括asp.net和php
- Guava------------Cache使用方法
- 栈和队列的实现
- iOS时间那些事
- Sorted insert for circular linked list
- mongify--数据迁移mysql至mongo
- android开发中联系人列表显示字母索引
- 长连接短连接socket及windows连接数限制配置
- 覆盖equals时总要覆盖hashCode
- 闭包