frog is now a editor to censor so-called sensitive words (敏感词).

She has a long text 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 p.

(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


题意:给出两个字符串,在第二个里面去找寻有没有第一个,有的话删除,然后再合并字符串,问最后的字符串是怎样的
如样例一 aaabcbc -> aabc -> a

思路:我们想想,这么大的数据范围,光是我们查找子串是否存在就必须要用kmp,那我们再想想如何优化呢
怎么解决那个删除之后再合并呢,我开始想的是用数组模拟链表,那样删除操作的话,只要改变一个指向就可以了
列出下面我的错误代码
#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
using namespace std;
struct sss
{
    int next;
    char c;
}s1[5000001];
int len1,len2;
int head;
int next1[5000001];
char s2[5000001];
char str[5000001];
int num[5000001];
void getnext(char s[])
{
    int i=0,j=-1;
    next1[0]=-1;
    while(i<len2)
    {
        if(j==-1||s[i]==s[j])
        {
            next1[++i]=++j;
        }
        else j=next1[j];
    }
}
int kmp(struct sss s1[],char s2[])
{
    int i=head,j=0;
    int cnt=0;
    num[cnt++]=i;
    while(i<len1)
    {
        if(j==-1||s1[i].c==s2[j])
        {
            i=s1[i].next;
            num[cnt++]=i;
            j++;
            if(j==len2) 
            {
                if(cnt-len2-2<0) head=i; 
                else s1[num[cnt-len2-2]].next=i;
                return 1;
            }
        }
        else  j=next1[j];
    }
    return 0;
}
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=0;i<len1;i++)
        {
            s1[i].next=i+1;
            s1[i].c=str[i];
        }
        getnext(s2);
        head=0;
        while(kmp(s1,s2));
        int i=head;
        while(i<len1)
        {
            printf("%c",s1[i].c);
            i=s1[i].next;
        }
        printf("\n");
    }
}
View Code

相关文章:

  • 2022-02-08
  • 2021-07-25
  • 2021-09-14
  • 2021-11-28
  • 2022-03-02
  • 2022-12-23
  • 2021-12-03
  • 2021-08-12
猜你喜欢
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2021-04-11
  • 2021-11-06
  • 2021-11-20
相关资源
相似解决方案