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. 【转】iOS开发4:关闭键盘

    在 iOS 程序中当想要在文本框中输入数据,轻触文本框会打开键盘.对于 iPad 程序,其键盘有一个按钮可以用来关闭键盘,但是 iPhone 程序中的键盘却没有这样的按钮,不过我们可以采取一些方法关闭 ...

  2. 问题 B: Curriculum Vitae

    问题 B: Curriculum Vitae 时间限制: 1 Sec  内存限制: 128 MB提交: 109  解决: 25[提交][状态][讨论版][命题人:acm4302] 题目描述 Hideo ...

  3. python基础一 day16 内置函数

    # 数据类型 :int bool ...# 数据结构 : dict list tuple set str reversed()l = [1,2,3,4,5]l.reverse()print(l)l = ...

  4. red hat的防火墙怎么关闭

    查看是否开启: service iptables status 关闭方法: service iptables stop 永远关闭: Ntsysv 把iptables前的*号去掉. 查看SELinux状 ...

  5. 10个HTML5 实战教程 提升你的综合开发能力

    HTML5 作为下一代网站开发技术,无论你是一个 Web 开发人员或者想探索新的平台的游戏开发者,都值得去研究.借助尖端功能,技术和 API,HTML5 允许你创建响应性.创新性.互动性以及令人惊叹的 ...

  6. Jquery动态添加多行,返回数据至每一行中

    <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="sys_channel_ed ...

  7. testC-I

    总时间限制:  20000ms 单个测试点时间限制:  1000ms 内存限制:  128000kB 描述 给你一组数,a1,a2,a3,⋯,an. 令:G=gcd(a1,a2,a3,⋯,an) 现在 ...

  8. 牛客NOIP提高组R1 C保护(主席树)

    题意 题目链接 Sol Orz lyq 我们可以把一支军队(u, v)拆分为两个(u, lca)和(v, lca) 考虑一个点x,什么时候军队对它有贡献,肯定是u或v在他的子树内,且lca在他的子树外 ...

  9. 对于新能源Can数据、电池BMS等字节和比特位的解析

    1.对于1个字节(8个bit)以上的数据需要先进行倒序(因为高位在前 低位在后). CanID CanData 排序后的 字节数据 十进制 分辨率(0.005) 偏移量(40) 0x18FEC117 ...

  10. FTP、SFTP与FTPS

    先简单介绍下FTP的基础知识 FTP的传输有两种方式:ASCII.二进制. FTP支持两种模式:Standard (PORT方式,主动方式),Passive (PASV,被动方式). 主动模式 FTP ...