kmp+ Differential Article filter (filter)

Source: Internet
Author: User

Description

Given some short strings, you are asked to change these short strings into \ (*\) in a long string.

Input

The first line includes an integer \ (n\)that represents the number of short strings.

The next \ (n\) line is a short string of \ (n\) .

The last line, for the long string you need to change.

Output

A row that represents the long string after the change.

PS: Long string will have a variety of characters, short string contains only English characters

Sample input

3

Int

Ing

Kitty

Int is interesting! ~owo~

Sample output

\ (***\) is \ (***\)erest\ (***\)ing! ~owo~

Some of the inputs and outputs above are full-width, which is actually half-width.

This problem has a lot of ah, can be hashed, can be AC automata, can be KMP.

Let's talk about the KMP algorithm here.

The procedure is \ (kmp+\) differential.

You first need to convert all the letters in the long string to lowercase (uppercase).

\ (isalpha\) roar.

We find its \ (next\) array for each short string. Enumerate these strings and long string matches again.

But some locations can be problematic.

Like what

Given two short strings for \ (gre\) and \ (eat\), long strings for \ (great\)

Then our long string to become \ (*****\)

Therefore, the differential is introduced.

If the match is on, we are directly to its left and right endpoint \ (+1\) differential.

In the final enumeration, the direct judgment has no tag, if any, then change the current position to \ (*\)

Then direct output can be.

代码

#include <cstdio> #include <cctype> #include <cstring> #define R registerusing namespace Std;char sta[    150008],stb[150008];int pos[150008],k,n,len;struct cod{int nex[55],len; Char s[55];}    Str[55];char st[55],c;inline Char ch (char s) {if (!isalpha (s)) return s;    if (s<= ' Z ' and s>= ' a ') s=s-' a ' + ' a '; return s;}    int main () {scanf ("%d", &n);        for (R int i=1;i<=n;i++) {scanf ("%s", st+1);        Str[i].len=strlen (st+1);        for (R int j=1;j<=str[i].len;j++) str[i].s[j]=ch (St[j]);        str[i].nex[1]=0;k=0;            for (R int j=2;j<=str[i].len;j++) {while (Str[i].s[k+1]!=str[i].s[j] and k) k=str[i].nex[k];            if (Str[i].s[k+1]==str[i].s[j]) k++;        Str[i].nex[j]=k;    }} getchar ();        while (c!= ' \ n ') {C=getchar ();        if (c== ' \ n ') break;        Sta[++len]=c;    Stb[len]=ch (c);        } for (R int i=1;i<=n;i++) {k=0;    for (R int j=1;j<=len;j++) {        while (K and Str[i].s[k+1]!=stb[j]) k=str[i].nex[k];            if (Str[i].s[k+1]==stb[j]) k++;                if (K==str[i].len) {pos[j-str[i].len+1]++;            pos[j+1]--;        }}} for (R int i=1;i<=len;i++) {pos[i]+=pos[i-1];        if (Pos[i]) sta[i]= ' * ';    printf ("%c", Sta[i]); }}/*2gre eatgreat britain***** britain*/

Kmp+ Differential Article filter (filter)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.