分析

  • 题意
  1. 给我们一个字符串 t,又给我们一个字符串s,让我们删除 s中第一个的t,如果在删除之后的s字符串还存在t那么我们一直删除,直到删除之后不在 出现t为止,输出s剩余的内容

样例

abc
aaabcbc
  • 思路
  1. 这题的巧妙之处是使用了栈的特性(括号匹配题型),而Hash的作用是通过将子串转化为 hash值,同判断hash值相同,我们就将相同的这个子串从 栈 中弹出,然后继续往里面添加字符串,继续进行 相同字符串的匹配(这个匹配类似于 括号匹配题型)
  2. 过程模拟,我们假设有一个 栈st,当我把 s中的前四个字符 a、a、a、b 依次压入st中的时候,我们判读 栈顶的前3个字符仍然不等于abc,
  3. 当我们压入 s中的 第5个字符串的时候,此时 st 顶部的前三个单词为 a、b、c,整好和 t 字符串相同,此时我们把 st的前三个字符 弹出,
  4. 之后 我们再次把s中的第 6、7个字符压入st中,此时 st 顶部的前三个单词为 a、b、c,整好和 t 字符串相同,此时我们把 st的前三个字符 弹出,
  5. 这样栈中剩下的字符就是,就是ans,
  6. 注意我们 在判断字符串相同的时候 用的是 hash

代码

#include <iostream>
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <algorithm>
#include <string>
#include <queue>
#include <map>
#include <set>
#include <bitset>
#include <vector>
#include <stack>
#include <unordered_map>
#include <sstream>
void fre() { system("clear"), freopen("A.txt", "r", stdin); freopen("Ans.txt","w",stdout); }
void Fre() { system("clear"), freopen("A.txt", "r", stdin);}
#define ios ios::sync_with_stdio(false)
#define Pi acos(-1)
#define pb push_back
#define fi first
#define se second
#define ll long long
#define ull unsigned long long
#define db double
#define Pir pair<int, int>
#define m_p make_pair
#define INF 0x3f3f3f3f
#define esp 1e-7
#define mod (ll)(1e9 + 7)
#define for_(i, s, e) for(int i = (ll)(s); i <= (ll)(e); i ++)
#define rep_(i, e, s) for(int i = (ll)(e); i >= (ll)(s); i --)
#define sc scanf
#define pr printf
#define sd(a) scanf("%d", &a)
#define ss(a) scanf("%s", a)
using namespace std;const int mxn = 5e6 + 10;
ull bsc = 131;
ull bse[mxn];
ull has[mxn];void init()
{bse[0] = 1;for_(i, 1, mxn - 1) bse[i] = bse[i - 1] * bsc;
}ull Hash(char s[])
{int n = strlen(s + 1);ull hs = 0;for_(i, 1, n) hs = hs * bsc + s[i];return hs;
}ull get_has(int l, int r)
{return has[r] - has[l] * bse[r - l];
}char s[mxn], t[mxn], ans[mxn];int main()
{/* fre(); */init();while(~ sc("%s %s", s + 1, t + 1)){int n = strlen(s + 1); int m = strlen(t + 1);ull hs = Hash(s);int tp = 0;for_(i, 1, m){ans[++ tp] = t[i];has[tp] = has[tp - 1] * bsc + t[i];if(tp >= n && get_has(tp - n, tp) == hs)tp -= n;}ans[tp + 1] = '\0';pr("%s\n", ans + 1);}return 0;
}

F - Censor SCU - 4438(栈 + hash)相关推荐

  1. Censor SCU - 4438 (hash 哈希做法)

    Censor SCU - 4438 题意:给你两串字符串w和p.在p中不停删除w,直到p中没有w.输出此时剩余的字符串. 解题思路: 字符串问题考虑hash做法.先求出字符串w的哈希值.然后遍历字符串 ...

  2. C - Censor SCU - 4438

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

  3. Censor SCU - 4438 (KMP)

    题目来源: https://vjudge.net/contest/297070#problem/C http://acm.scu.edu.cn/soj/problem.action?id=4438 C ...

  4. 【哈希-字符串匹配+模拟栈】SCU - 4438: Censor(哈希详解哈哈哈)

    写在前:由于哈希没有好好听讲,也没有下来看.这是排位我开的第一道题,直接string暴力T了.昨天其实就看了哈希,太浮躁,于是本来很简单的哈希愣是没看懂.(于是放弃去看爱5了23333333)今天补上 ...

  5. ACM: SCU 4438 Censor - KMP

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

  6. SCU - 4438 (KMP)

    SCU - 4438 (KMP) Censor frog is now a editor to censor so-called sensitive words (敏感词). She has a lo ...

  7. SCU 4438 Censor(Hash)题解

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

  8. SCU 4438 Censor 字符串hash

    http://fastvj.rainng.com/problem/SCU-4438 题意:一个文本串和模式串,如果文本串中出现了模式串直接删除,然后拼接成一个新串,继续删除.求最后处理过的字符串. 做 ...

  9. SCU - 4438——Censor(哈希)

    题目链接:http://acm.scu.edu.cn/soj/problem.action?id=4438 题意:给出一个串a和串b,串b中如果有a串则删除,删除后b剩下的串再连到一起,再找串a,反复 ...

最新文章

  1. 一个n位的数,去掉其中的k位,问怎样去使得留下来的(n-k)位数按原来的前后顺序组成的数最小...
  2. C语言怎么读取串口的数据为,如何通过串口来读写数据,请教达人
  3. 简单认识Hexo的目录结构
  4. Python基础教程(第3版) 笔记(一)
  5. linux系统文件查找实验报告,Linux 文件查找与打包
  6. phpcms v9 配置sphinx全文索引教程
  7. 利用文件摘要简化游戏资源的引用管理
  8. 【超分辨率实验】基于高斯模糊的训练数据集构建方法改进(matlab)
  9. 用etcd实现服务注册和发现
  10. MySQL数据库的卸载
  11. 如何下载mysql-java驱动jar包
  12. Cent OS 7 配置静态ip
  13. 红米K30 4G手机图纸 主板元件位号图
  14. 史上最全的中高级JAVA工程师-面试题汇总
  15. 通过迅雷下载谷歌浏览器下载的内容
  16. 【可达性分析中的增量更新和原始快照】
  17. 计算机上如何保存ico格式,PS怎么保存ico格式
  18. leetcode 刷题ing
  19. 傻-amp;gt;天使
  20. 全球排名第一的免费开源ERP Odoo替代料管理应用解决方案

热门文章

  1. 什么是鉴权?这些postman鉴权方式你又知道多少?
  2. linux 下强大的 JSON 解析命令 jq
  3. Rosdep安装(最简单的方法)
  4. 学术论文发表/期刊分类
  5. 服务器提示文档锁定,服务器锁定设置方法
  6. 低压铸造,模具温度计算有多重要
  7. 用Wordpress搭建独立网站
  8. 这些牛人,你能看的他们的脸
  9. 细述!!DHCP中继服务配置!!
  10. win7 android 编译环境搭建,在Win7下配置Android开发环境