题目大意:

就是现在给出两个字符串A, B, 长度都不超过500万, 要求每次将B中第一次A的出现删除, 然后将B剩下的两段前后合并, 然后重复这个过程直到没有可以删除的A位置, 问最后剩下的串是什么

大致思路:

首先看这个字符串的长度不难想到用KMP处理出A的next数组

然后考虑在B上进行匹配, 这里我用的是两个数组来模拟链表, 表示剩下的串, 每次发生删除时, 将当前位置的指针前移即可, 由于删除的串不会在之后进行匹配, 整体的时间复杂度是O(|A| + |B|)

代码如下:

Result  :  Accepted     Memory  :  51820 KB     Time  :  848 ms

/** Author: Gatevin* Created Time:  2015/10/1 9:22:45* File Name: A.cpp*/
#include<iostream>
#include<sstream>
#include<fstream>
#include<vector>
#include<list>
#include<deque>
#include<queue>
#include<stack>
#include<map>
#include<set>
#include<bitset>
#include<algorithm>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<cctype>
#include<cmath>
#include<ctime>
#include<iomanip>
using namespace std;
const double eps(1e-8);
typedef long long lint;
char a[5000010];
char b[5000010];
int next[5000010];
int len1, len2;
void getNext()
{for(int i = 0; i <= len1; i++)next[i] = 0;for(int i = 1; i < len1; i++){int j = i;while(j > 0){j = next[j];if(a[j] == a[i]){next[i + 1] = j + 1;break;}}}return;
}
int nex[5000010];
int bef[5000010];
int main()
{while(gets(a)){
<span style="white-space:pre"> </span>gets(b);len1 = strlen(a), len2 = strlen(b);if(len1 > len2){printf("%s\n", b);continue;}getNext();for(int i = 0; i <= len2; i++){nex[i] = i + 1;bef[i] = i - 1;}for(int i = 0, j = 0; i < len2;){if(j < len1 && b[i] == a[j]) j++;else{while(j > 0){j = next[j];if(b[i] == a[j]){j++;break;}}}if(j == len1){int ti = i;for(int k = 0; k < len1; k++){i = bef[i];}if(i == -1){bef[nex[ti]] = -1;i = nex[ti];j = 0;continue;}else{nex[i] = nex[ti];bef[nex[ti]] = i;for(int k = 2; k < len1; k++){if(bef[i] != -1)i = bef[i];}j = 0;}}else i = nex[i];}int cnt = 0;int now = len2;while(bef[now] != -1){//putchar(b[bef[now]]);a[cnt++] = b[bef[now]];now = bef[now];}for(int i = cnt - 1; i >= 0; i--)putchar(a[i]);putchar('\n');}return 0;
}

SCU 4438 Censor (KMP)相关推荐

  1. ACM: SCU 4438 Censor - KMP

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

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

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

  3. SCU - 4438 Censor (KMP)

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

  4. SCU - 4438 Censor

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

  5. SCU - 4438 Censor

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

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

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

  7. SCU 4438:Censor

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

  8. SCU 4438 Censor

    $KMP$,链表. 将$p$弄成链表,每次匹配到,删掉中间的,继续匹配. #include<bits/stdc++.h> using namespace std;const int INF ...

  9. SCU 4438 Censor(Hash)题解

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

最新文章

  1. linux文本编辑nano
  2. ElementUI中el-table在表格最下方添加一列汇总小计行
  3. Tomcat源码学习(7)-How Tomcat works(转)
  4. 【Tuxedo】Tuxedo入门
  5. 数据结构与算法-链表
  6. difference between SAP UI5 RTL false and true - set breakpoint to change behavior
  7. 通俗讲解:图像傅里叶变换
  8. 【流媒體】live555—VS2010 下live555编译、使用及测试
  9. 设计模式之十一:创建模式(Builder Pattern)
  10. tpcc-mysql安装测试与使用生成对比图
  11. 在英文Win7操作系统上部署C#开发的Web系统出现乱码的解决方法
  12. 2019 中科院 信工所二室 夏令营 笔试面试经验
  13. ROS2的launch文件如何编写?快来学习一下使用Python编写launch文件~
  14. P1_C1-3:系统分析与设计概要
  15. 详解Linux运维工程师必备技能
  16. linux恢复rm删除文件ext4,EXT4中恢复使用rm命令误删除的文件
  17. 送别贴:《李兴》 秋风为落叶所破歌!
  18. wireshark插件 - 添加UI子树, 向子树上添加分析后的文本
  19. 实现多线程的方法有哪几种?
  20. 【Git学习】如何删除Git中的大文件

热门文章

  1. input 正则校验
  2. BPM、CRM、ERP、MES、DRP这类都是什么系统?
  3. java计算机毕业设计健身房管理系统设计MyBatis+系统+LW文档+源码+调试部署
  4. 邮箱怎么注册好呢?简单的邮箱注册方法
  5. TPS7A3301RGWR稳压器 AK4493EQ音频DAC,电路原理图
  6. 英语购物计算机,关于互联网购物英语作文
  7. TIPTOP 调用其他系统的webservice案例
  8. OOD - blackjack design
  9. 简易理发预约系统(Spring+SpringMVC+Mybatis)
  10. 学生资助管理系统毕业设计