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],h;
int lenp,lenw;
int t[N];
int nt[N];

void init()
{
    nt[0] = -1;
    for (int i = 0; w[i]; i++)
    {
        int k = nt[i];
        while (k >= 0 && w[i] != w[k]) k = nt[k];
        nt[i + 1] = k + 1;
    }
}

int main()
{
    while(~scanf("%s%s",w,p+1))
    {
        lenp=strlen(p+1); lenw = strlen(w);
        for(int i=1;i<=lenp;i++) L[i]=i-1,R[i]=i+1;

        init(); R[0]=1; L[lenp+1] = lenp; t[0]=nt[1];

        for (int i = R[0], j = nt[1]; p[i];)
        {
            t[i]=j;

            if (j < 0 || p[i] == w[j])
            {
                i=R[i], j++;
                if (!w[j])
                {
                    int sum=0;
                    for(int u=L[i];;u=L[u])
                    {
                        if(sum==lenw)
                        {
                            j=t[u];
                            i=u;
                            if(i==0) i=R[0];
                            break;
                        }
                        int x=L[u];
                        int y=R[u];
                        R[x]=y;
                        L[y]=x;
                        sum++;
                    }
                }
            }
            else j = nt[j];
        }

        for(int i=R[0];i!=lenp+1;i=R[i])
        {
            printf("%c",p[i]);
        }
        printf("\n");

    }
    return 0;
}
时间: 2024-10-14 20:00:53

SCU 4438 Censor的相关文章

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 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 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

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 - 3254 - Rain and Fgj(最小点权割)

题意:N个点.M条边(2 <= N <= 1000 , 0 <= M <= 10^5),每一个点有个权值W(0 <= W <= 10^5),现要去除一些点(不能去掉点0),使得结点 0 与结点 N - 1 不连通,求去掉的点的最小权值和. 题目链接:http://cstest.scu.edu.cn/soj/problem.action?id=3254 -->>这是很明显的最小点权割.. 建图方案: 1)将全部点 i 拆成 i 和 i + N.i ->

SCU 2941 I NEED A OFFER!(01背包变形)

I NEED A OFFER!     64bit IO Format: %lld & %llu Submit Status Description Description Speakless很早就想出国,现在他已经考完了所有需要的考试,准备了所有要准备的材料, 于是,便需要去申请学校了.要申请国外的任何大学,你都要交纳一定的申请费用, 这可是很惊人的.Speakless没有多少钱,总共只攒了n万美元.他将在m个学校中选择若干的 (当然要在他的经济承受范围内).每个学校都有不同的申请费用a(万美

SCU 4436 Easy Math 2015年四川省赛题

题目链接:http://acm.scu.edu.cn/soj/problem/4436/ 题意:给你n个整数,求这n个数的平方根和是否是一个整数: 解题思路:如果这题每个数给他算出来,必然费时间,可能还会超精度,所以巧妙的方法就是判断这些整数是否全是完全平方数,如果有一个不是,则他们的平方根和肯定不是一个整数. AC代码: #include <cstdio> #include <cmath> #include <cstring> #include <algorit

SCU 4424(求子集排列数)

A - A Time Limit:0MS     Memory Limit:0KB     64bit IO Format:%lld & %llu Submit Status Practice SCU 4424 Description Time Limit: 1000ms Description Given N distinct elements, how many permutations we can get from all the possible subset of the eleme