Luo Gu [P3796] (template) AC automaton (enhanced version)

Topic Link

(Template) AC automaton (enhanced version)

Title Description

There \ (N \) th lowercase letters and a pattern string text string \ (T \) . Each mode string may appear in multiple text string. You need to find out which strings in text mode string \ (T \) the maximum number that appears.

Input Format

Containing multiple sets of data input.
A first row of data each positive integer \ (N \) , represents a total of \ (N \) modes string, \ (. 1 \ Leq N \ Leq 150 \) .
Next \ (N \) rows, each row is equal to a length less than \ (70 \) pattern strings. The next line is equal to a length less than \ (10 ^ 6 \) of the text string \ (T \) .
Input end flag is \ (N = 0 \) .

Output Format

For each test, the number of the first row of the output pattern string appear at most, next a plurality of output lines each pattern string appears the most frequently, arranged in order of input.

Sample input

A 2 
aba 
bab 
ababababac 
6 And 
beta 
alpha 
haha 
delta 
dede 
tata 
dedeltalphahahahototatalpha 
of 0

Sample Output

4 
into 
2 
alpha 
haha

answer

This is an automatic AC board problem,
we use an ordinary AC automaton made directly just fine, but because of this question requires determined number of times each pattern string appears in the text string, so we have to trie each record of each node from the root node to this mode which is a string, where I was \ (Vector \) storage.
Note: Because multiple sets of data, so pay attention to empty the data, do not let the influence of a set of data to the next set of data
on the code:

#include<bits/stdc++.h>
#include<vector>
using namespace std;
int n;
char c[209][79],cc[1000009];
int ss[209];
struct aa{
    int s;
    int up;
    int to[30];
    vector<int>tt;
}p[100009];
int len;
int ans;
void add(int x){
    int l=strlen(c[x]);
    int u=0;
    for(int j=0;j<l;j++){
        if(p[u].to[c[x][j]-'a']) u=p[u].to[c[x][j]-'a'];
        else {p[u].to[c[x][j]-'a']=++len;u=len;p[len].s=p[len].up=0;memset(p[len].to,0,sizeof(p[len].to));p[len].tt.clear();}
    }
    p[u].s++;
    p[u].tt.push_back(x);
}
int q[1000009],l=1,r=0;
void bfs(){
    for(int j=0;j<='z'-'a';j++)
        if(p[0].to[j]) q[++r]=p[0].to[j];
    while(l<=r){
        int u=q[l++];
        for(int j=0;j<='z'-'a';j++){
            if(p[u].to[j]){
                p[p[u].to[j]].up=p[p[u].up].to[j];
                q[++r]=p[u].to[j];
            }else p[u].to[j]=p[p[u].up].to[j];
        }
    }
}
int main(){
    scanf("%d",&n);
    while(n!=0){
        memset(ss,0,sizeof(ss));
        len=0;
        p[0].s=p[0].up=0;memset(p[0].to,0,sizeof(p[0].to));p[0].tt.clear();
        for(int j=1;j<=n;j++){
            scanf("%s",c[j]);
            add(j);
        }
        bfs();
        scanf("%s",cc);
        int l=strlen(cc);
        int uu=0;
        for(int j=0;j<l;j++){
            uu=p[uu].to[cc[j]-'a'];
            int k=uu;
            while(k){
                for(int j=0;j<p[k].tt.size();j++)
                    ss[p[k].tt[j]]++;
                k=p[k].up;
            }
        }
        int mx=0;
        for(int j=1;j<=n;j++)
            mx=max(mx,ss[j]);
        printf("%d\n",mx);
        for(int j=1;j<=n;j++)
            if(ss[j]==mx) printf("%s\n",c[j]);
        scanf("%d",&n);
    }
    return 0;
}

Guess you like

Origin www.cnblogs.com/linjiale/p/12220585.html