zoukankan      html  css  js  c++  java
  • Censor(KMP)

    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.

    (1length of w,p51061≤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

    // 题意:第一行一个敏感词,然后一行文本,操作如下,删除敏感词,然后如果又新组成敏感词, 重复删掉,直到没有敏感词

    //如果用KMP和模拟链表的话,就是如果匹配成功了,用链表删除后,再回退lent长度继续匹配,有点麻烦,貌似还可以hash做

     1 #include <iostream>
     2 #include <cstdio>
     3 #include <cmath>
     4 #include <string.h>
     5 using namespace std;
     6 const int MX=5000010;
     7 
     8 int lens,lent;
     9 char T[MX];
    10 char S[MX];
    11 int l[MX];
    12 int r[MX];
    13 int fail[MX];
    14 
    15 void getfail()
    16 {
    17     int i,j;
    18     i=1,j=0;
    19     fail[1]=0;
    20     while(i<lent)
    21     {
    22         if (j==0||T[i]==T[j])
    23         {
    24             i++,j++;
    25             fail[i]=j;
    26         }
    27         else j = fail[j];
    28     }
    29 }
    30 
    31 void kmp()
    32 {
    33     int i=1;
    34     int j=1;
    35     while(i<=lens&&j<=lent)
    36     {
    37         if (j==0||S[i]==T[j])
    38         {
    39             i=r[i];
    40             j++;
    41         }
    42         else j = fail[j];
    43 
    44         if (j>lent)
    45         {
    46             int num=0;
    47             int pos=i;
    48             while (num<lent)
    49             {
    50                 pos = l[pos];
    51                 num++;
    52             }
    53             r [ l[ pos ] ] = i;
    54             l [ i ] = l[ pos ];
    55             
    56             num=0;
    57             pos=i;
    58             while (num<lent)
    59             {
    60                 if (l[pos]==0) break;
    61                 pos = l[pos];
    62                 num++;
    63             }
    64             j=1;
    65             i = pos;
    66         }
    67     }
    68 }
    69 
    70 int main()
    71 {
    72     while(scanf("%s",T+1)!=EOF)
    73     {
    74         scanf("%s",S+1);
    75         lens = strlen(&S[1]);
    76         lent = strlen(&T[1]);
    77 
    78         getfail();
    79 
    80         for (int i=0;i<=lens+1;i++)
    81         {
    82             l[i] = i-1;
    83             r[i] = i+1;
    84         }
    85 
    86         kmp();
    87         for (int i = r[0];S[i];i = r[i])
    88         {
    89             printf("%c",S[i]);
    90         }
    91         printf("
    ");
    92     }
    93     return 0;
    94 }
    View Code
  • 相关阅读:
    第一章 新手入门
    Excle生成T层加工ODS层存储过程
    Excel生成建表角本
    数据仓库建模技巧
    算法第四章作业
    删数问题
    算法第三章作业
    第三章上机实践报告
    第二章作业
    Thinkphp6框架学习:($this->error()undefined)Call to undefined method appindexcontrollerAdmin::error()
  • 原文地址:https://www.cnblogs.com/haoabcd2010/p/7384506.html
Copyright © 2011-2022 走看看