四川省赛 SCU - 4438
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 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 11 string ww. The second line contains 11 string pp.
(1≤length of w,p≤5⋅1061≤length of w,p≤5⋅106, w,pw,p consists of only lowercase letter)
Output
For each test, write 11 string which denotes the censored text.
Sample Input
abc
aaabcbc
b
bbb
abc
ab
Sample Output
a ab
题意:给出两个字符串,在第二个里面去找寻有没有第一个,有的话删除,然后再合并字符串,问最后的字符串是怎样的
如样例一 aaabcbc -> aabc -> a 思路:我们想想,这么大的数据范围,光是我们查找子串是否存在就必须要用kmp,那我们再想想如何优化呢
怎么解决那个删除之后再合并呢,我开始想的是用数组模拟链表,那样删除操作的话,只要改变一个指向就可以了
列出下面我的错误代码
#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
using namespace std;
struct sss
{
int next;
char c;
}s1[];
int len1,len2;
int head;
int next1[];
char s2[];
char str[];
int num[];
void getnext(char s[])
{
int i=,j=-;
next1[]=-;
while(i<len2)
{
if(j==-||s[i]==s[j])
{
next1[++i]=++j;
}
else j=next1[j];
}
}
int kmp(struct sss s1[],char s2[])
{
int i=head,j=;
int cnt=;
num[cnt++]=i;
while(i<len1)
{
if(j==-||s1[i].c==s2[j])
{
i=s1[i].next;
num[cnt++]=i;
j++;
if(j==len2)
{
if(cnt-len2-<) head=i;
else s1[num[cnt-len2-]].next=i;
return ;
}
}
else j=next1[j];
}
return ;
}
int main()
{
while(scanf("%s",s2)!=EOF)
{
scanf("%s",str);
len1=strlen(str);
len2=strlen(s2);
if(len2>len1)
{
printf("%s\n",str);
continue;
}
for(int i=;i<len1;i++)
{
s1[i].next=i+;
s1[i].c=str[i];
}
getnext(s2);
head=;
while(kmp(s1,s2));
int i=head;
while(i<len1)
{
printf("%c",s1[i].c);
i=s1[i].next;
}
printf("\n");
}
}
但是我的想法是一旦删除之后,就改变指向,然后再次退出去,然后再从头开始找,这样无疑还是超时了
我们再想想怎么利用kmp的性质去优化,我在每个位置存下模式串当前匹配到哪个位置,
然后我用一个数组存下来,当`我找到一个之后,那个匹配之前的位置的模式位置值又赋值给当前即可
如 样例一
aaabcbc
01112323
abc
删除一遍后j的值并不是从0开始而是赋值abc之前的那个位置值1,代表以后匹配了a,无需再去比
字符串中我们需要灵活的利用kmp的思想来优化时间复杂度
具体看代码
#include<cstdio>
#include<cstring>
#include<string>
#include<algorithm>
#include<queue>
using namespace std;
typedef long long LL ;
const int MX=;
char s[MX],t[MX],ans[MX];
int next1[MX],pos[MX],len1,len2; void init() {
memset(ans,,sizeof(ans));
memset(pos,,sizeof(pos));
memset(next1,,sizeof(next1));
} struct Node {
char ch;
int j;
Node() {};
Node(char c,int n):ch(c),j(n) {};
}; void GetNext() {
int i=,j=-;
next1[]=-;
while(i<len2) {
if(j==-||t[i]==t[j]) {
i++;
j++;
if(t[i]==t[j]) {
next1[i]=next1[j];
} else next1[i]=j;
} else j=next1[j];
}
}
void KMP() {
int i=,j=;
int cnt=;
while(i<len1) {
ans[cnt]=s[i++];
while(!(j==-||ans[cnt]==t[j])) {
j=next1[j];
}
j++;
cnt++;
pos[cnt]=j;//存当前模式串匹配到哪个位置
if(j==len2) {
cnt-=len2;//直接赋值之前的位置给他
j=pos[cnt];
}
}
for(int i=; i<cnt; i++) {
putchar(ans[i]);
}
puts("");
}
int main() {
while(~scanf("%s %s",t,s)) {
init();
len1=strlen(s);
len2=strlen(t);
GetNext();
KMP();
}
return ;
}
四川省赛 SCU - 4438的更多相关文章
- ACM: SCU 4438 Censor - KMP
SCU 4438 Censor Time Limit:0MS Memory Limit:0KB 64bit IO Format:%lld & %llu Practice D ...
- SCU 4438 Censor(哈希+模拟栈)
Censor frog is now a editor to censor so-called sensitive words (敏感词). She has a long text \(p\). He ...
- SCU 4440 Rectangle 2015年四川省赛题
题目链接:http://acm.scu.edu.cn/soj/problem/4440/ 题目大意:给一个n*m的方格,求周长小于等于k的矩形有多少个. 解题思路:我之前直接暴力,显然超时,所以后来发 ...
- SCU 4436 Easy Math 2015年四川省赛题
题目链接:http://acm.scu.edu.cn/soj/problem/4436/ 题意:给你n个整数,求这n个数的平方根和是否是一个整数: 解题思路:如果这题每个数给他算出来,必然费时间,可能 ...
- 第十五届四川省省赛 SCU - 4443 Range Query
先给你1~N的N个数 再给你每种最多50个的条件(ai,bi,ci) 或者[ai,bi,ci] (ai,bi,ci)表示下标ai到bi的最小值必为ci [ai,bi,ci]表示下标ai到bi的最大值必 ...
- 第十五届四川省省赛 SCU - 4439 Vertex Cover
给你一个一般图 保证每条边的一端下标不大于30 问最小覆盖集的大小为多少 爆搜:枚举前30个点是否在覆盖集内 剪枝1:如果不在的话 那么他所连的下标大于30的点都必须选 剪纸2:最优解剪枝 #incl ...
- 第十五届四川省省赛 SCU - 4444 Travel
给你一个一共由两种边的完全图 要求你求1到N的最短路 q队列为前沿队列(已探索过且最外围的点) p队列为未探索队列(未探索过的点) depth这个数组的用法并不是代表实际上这个点在第几层 而是防止死 ...
- HDU 4727 The Number Off of FFF 2013年四川省赛题
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4727 题目大意:队列里所有人进行报数,要找出报错的那个人 思路:,只要找出序列中与钱一个人的数字差不是 ...
- HDU 4722 Good Numbers 2013年四川省赛题
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4722 题目大意:给定一个区间,求区间中有多少个满足每位上的数的和是10的倍数. 解题思路:先打表暴力求 ...
随机推荐
- LeetCode--459--重复的字符串
问题描述: 给定一个非空的字符串,判断它是否可以由它的一个子串重复多次构成.给定的字符串只含有小写英文字母,并且长度不超过10000. 示例 1: 输入: "abab" 输出: T ...
- mysql 时间戳转换 、cnd、dns 通俗理解
- SWUST OJ(954)
单链表的链接 #include <stdio.h> #include <stdlib.h> typedef struct LinkNode //单链表节点结构的定义 { cha ...
- 【Java】【3】BeanUtils.copyProperties();将一个实体类的值复制到另外一个实体类
正文: a,b为对象 BeanUtils.copyProperties(a, b); 1,BeanUtils是org.springframework.beans.BeanUtils, a拷贝到b 2 ...
- Leetcode 868. 二进制间距
868. 二进制间距 显示英文描述 我的提交返回竞赛 用户通过次数201 用户尝试次数220 通过次数207 提交次数396 题目难度Easy 给定一个正整数 N,找到并返回 N 的二进制表示中 ...
- 51Nod 1001 数组中和等于K的数对
http://www.51nod.com/onlineJudge/questionCode.html#!problemId=1001一开始的想法是排序后二分搜索,发现会进行非常多不必要的遍历,十分耗时 ...
- Echarts 简单报表系列三:饼状图
代码如下: <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF ...
- [CodeForces - 197A] A - Plate Game
A - Plate Game You've got a rectangular table with length a and width b and the infinite number of p ...
- 2015-09-28认识js1
Javascript 一.特点 1. 区分大小写 2. 弱类型变量,只能用关键字“var" 3.注释 /*….*/ 二. 变量 1.变量通过关键字var声明. 2.var ...
- 【转】Entity Framework教程(第二版)
源起 很多年前刚毕业那阵写过一篇关于Entity Framework的文章,没发首页却得到100+的推荐.可能是当时Entity Framework刚刚发布介绍EF的文章比较少.一晃这么多年过去了,E ...