SCU oj 4438:Censor

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≤length of w,p≤5?106

, 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

题意: 给你一个主串,递归删除模式串。比如: T: abc  S: aaabcbc    aaabcbc->aabc->a

非常巧妙的KMP,我们用一个栈记录当前的字符以及其在模式串匹配的位置,当位置等于模式串长度之后,将模式串长度的串出栈,从栈顶元素开始继续匹配主串.时间复杂度 O(n).

#include <stdio.h>
#include <iostream>
#include <string.h>
#include <stack>
#include <algorithm>
using namespace std;
const int N = 5000005;
struct Node{
    char c;
    int k;
};
char w[N],t[N],ans[N];
int Next[N];
void get_next(char *p){
    int len = strlen(p);

    int i=0,k=-1;
    Next[0] = -1;
    while(i<len){
        if(k==-1||p[i]==p[k]){
            i++,k++;
            Next[i] = k;
        }
        else k = Next[k];
    }
}

void Kmp(char *s,char *p){
    int len1 = strlen(s),len2 = strlen(p);
    int i=0,j=0,len;
    stack <Node> stk;
    while(!stk.empty()) stk.pop();
    while(i<len1){
        if(j==-1||s[i]==p[j]){
            i++,j++;
            stk.push(Node{s[i-1],j});
        }else {
            j=Next[j];
        }
        if(j==len2){
            len = len2;
            while(!stk.empty()&&len--) stk.pop();
            if(stk.empty()) j = 0;
            else j = stk.top().k;
        }
    }
    int k = 0;
    while(!stk.empty()){
        ans[k++] = stk.top().c;
        stk.pop();
    }
    for(int i=k-1;i>=0;i--) printf("%c",ans[i]);
    printf("\n");
}
int main(){
    while(scanf("%s%s",w,t)!=EOF){
        get_next(w);
        Kmp(t,w);
    }
    return 0;
}
 
时间: 2024-10-05 03:17:33

SCU oj 4438:Censor的相关文章

SCU 4438 Censor

$KMP$,链表. 将$p$弄成链表,每次匹配到,删掉中间的,继续匹配. #include<bits/stdc++.h> using namespace std; const int INF = 0x7FFFFFFF; const int mod = 1e9 + 7; const int N = 5e6 + 10; const int M = 1e4 + 1; const double eps = 1e-10; int T,n,m; char w[N],p[N]; int L[N],R[N],

scu oj 4442 Party(2015年四川省acm程序设计竞赛)

Party n frogs are invited to a tea party. Frogs are conveniently numbered by 1,2,-,n. The tea party has black and green tea in service. Each frog has its own preference. He or she may drink only black/green tea or accept both. There are m pairs of fr

scu oj 4445 Right turn 2015年四川省赛J题(模拟题)

一般的模拟题.对于出现过的每一个x建立一个set 集合,对于y也如此.然后查找要走到哪个点即可,主要要对状态记录,判断是否无线循环,否者出现无线循环的情况,就tle了. #include<stdio.h> #include<string.h> #include<iostream> #include<string> #include<queue> #include<cmath> #include<map> #include&

scu oj 4437: Carries (2015年四川省程序ACM设计竞赛B题目 )

其实这题只要想到这个结论就简单了.如果2个数a,b的第k位相加要进位,那么必须满足(a%10^k+b%10^k)>=10^k  .有了这个结论就很简单了,枚举没一位就好了. #include<stdio.h> #include<string.h> #include<iostream> #include<string> #include<queue> #include<cmath> #include<map> #inc

scu oj 4439 : Vertex Cover(2015年四川省程序ACM设计竞赛D题 )

一般图的最小点覆盖问题是是一个npc问题,目前哈没有比较好的多项式的算法.但是这题有一点特殊的地方,每条边必定包含前面30个点的的一个,所以这题可以枚举钱30个点的选和不选的状态,后面的点对应的状态就唯一了.    所以这题就是  dfs+可行性减枝和答案最优减枝. #include<stdio.h> #include<string.h> #include<iostream> #include<string> #include<queue> #i

Censor SCU - 4438

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 w

SCU Censor

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 tedio

四川省赛 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 tedi

SCU 4438 字符串哈希

题意:一个敏感词w和一个文本p,在文本中不断地删除敏感词w,求最后的剩下的文本p. 题解:求出敏感词的hash值,定p的每一个字符都是以第一个字符开始的一个句子,求出它们的hash值入栈,当某一段的hash值等于敏感词的hash值时,将这段字符出栈. #include <iostream> #include <cstring> #include <cstdio> using namespace std; #define maxn 5000006 long long ha