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 \leq \textrm{length of}\ w, p \leq 5 \cdot 10^6\), \(w, p\) consists of only lowercase letter)

Output

For each test, write \(1\) string which denotes the censored text.

Sample Input

    abc
aaabcbc
b
bbb
abc
ab

Sample Output

    a

    ab

题目链接:SCU 4438

用哈希来做的话比KMP简单很多,每一次加入一个字符,看倒数Len个字符的哈希值是否和模式串相同,若相同则弹出Len个元素,否则再加入元素并更新哈希值

代码:

#include <stdio.h>
#include <bits/stdc++.h>
using namespace std;
#define INF 0x3f3f3f3f
#define LC(x) (x<<1)
#define RC(x) ((x<<1)+1)
#define MID(x,y) ((x+y)>>1)
#define CLR(arr,val) memset(arr,val,sizeof(arr))
#define FAST_IO ios::sync_with_stdio(false);cin.tie(0);
typedef pair<int, int> pii;
typedef unsigned long long ULL;
const double PI = acos(-1.0);
const int N = 5e6 + 7;
const ULL seed = 1e9 + 7;
char w[N], p[N], ans[N];
int lw, lp;
ULL prefix[N] = {1ull}, st[N]; ULL gethash(char s[], int len)
{
ULL hashval = 0ull;
for (int i = 0; i < len; ++i)
hashval = hashval * seed + s[i];
return hashval;
} int main(void)
{
int i;
for (i = 1; i < N; ++i)
prefix[i] = prefix[i - 1] * seed;
while (~scanf("%s%s", w, p))
{
lw = strlen(w);
lp = strlen(p);
ULL tar = gethash(w, lw);
int top = 0;
st[top] = 0;
for (i = 0; i < lp; ++i)
{
ans[top++] = p[i];
st[top] = st[top - 1] * seed + p[i];
if (top >= lw && st[top] - st[top - lw]*prefix[lw] == tar)
top -= lw;
}
ans[top] = '\0';
printf("%s\n", ans);
}
return 0;
}

SCU 4438 Censor(哈希+模拟栈)的更多相关文章

  1. ACM: SCU 4438 Censor - KMP

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

  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 . Her j ...

  4. SCU 4438 Censor(Hash)题解

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

  5. SCU 4438 Censor|KMP变形题

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

  6. ACM/ICPC 之 用双向链表 or 模拟栈 解“栈混洗”问题-火车调度(TSH OJ - Train)

    本篇用双向链表和模拟栈混洗过程两种解答方式具体解答“栈混洗”的应用问题 有关栈混洗的定义和解释在此篇:手记-栈与队列相关 列车调度(Train) 描述 某列车调度站的铁道联接结构如Figure 1所示 ...

  7. java 16 - 5 LinkedList模拟栈数据结构的集合

    请用LinkedList模拟栈数据结构的集合,并测试 题目的意思是: 你自己的定义一个集合类,在这个集合类内部可以使用LinkedList模拟. package cn_LinkedList; impo ...

  8. hdu 4699 Editor 模拟栈

    思路:刚开始用STL中的栈,一直RE……,之后改为手动模拟栈操作,在注意点细节就可以了!!! 代码如下: #include<cstdio> #include<cstring> ...

  9. 【DataStructure In Python】Python模拟栈和队列

    用Python模拟栈和队列主要是利用List,当然也可以使用collection的deque.以下内容为栈: #! /usr/bin/env python # DataStructure Stack ...

随机推荐

  1. python_9_for

    #1 for i in range(10):#默认从0开始,步长为1 print("loop",i) #2 for i in range(0,10,1):#步长为1 print(& ...

  2. 小w的糖果

    题目连接 : https://ac.nowcoder.com/acm/contest/923/C 算是一道找规律的题了,因为后一个人会比前一个人多,可以理解成后一个人要继承前一个人,sum为当前糖果数 ...

  3. Andrew NG 自动化所演讲(20140707):DeepLearning Overview and Trends

    出处 以下内容转载于 网友 Fiona Duan,感谢作者分享 (原作的图片显示有问题,所以我从别处找了一些附上,小伙伴们可以看看).最近越来越觉得人工智能,深度学习是一个很好的发展方向,应该也是未来 ...

  4. JS中进行浮点数计算式,遇到的问题

    今天在做项目时,需要在页面进行计算,但是当两个数都是小数时,计算的结果却不是想象中的: 比如1371.3-0.9算出来却是1370.39999999,后来上网搜一下,原来js是弱类型语言,没有那么高的 ...

  5. detection in video and image

    video中的detection,背景更加复杂,目标更加不聚焦,同时由于图片分辨率低于图像,因此更加难做. image中的Detection,背景相对简单些,目标更加聚焦,同时图片分辨率高,因此更加容 ...

  6. java基础面试题:运行时异常与一般异常有何异同?error和exception有什么区别? 请写出你最常见到的5个runtimeexception?

    Throwable是Java错误处理的父类,有两个子类:Error和Exception. Error:无法预期的严重错误,导致JVM虚拟机无法继续执行,几乎无法恢复捕捉的 Exception:可恢复捕 ...

  7. SpringMVC URL模板模式映射

    使用@RequestMaping和@PathVariable 组合使用 通过 @PathVariable  可以将 URL 中占位符参数绑定到控制器处理方法的入参中:URL 中的 {xxx} 占位符可 ...

  8. jQuery实现复选框的全选、反选功能

    <ul id="list"> <li><label><input type="checkbox" value=&quo ...

  9. 【CSS】非常简单的css实现div悬浮页面底部

    <div id="demo_div"></div> <style> #demo_div{ left:; position: fixed; bot ...

  10. 【PHP】根据两地经纬度计算距离

    最近做一个H5活动的项目,有个要求是必须现场玩家才能参与,所以就需要计算玩家位置和活动地点的距离来判断是否在活动现场. 以下是写的一个根据经纬度计算两地距离的方法 1 function getDist ...