P3796 [template] AC automata (Enhanced) (waiting to fill the pit)

Portal

 

#include<cstdio>
#include<iostream>
#include<cstdlib>
#include<iomanip>
#include<cmath>
#include<cstring>
#include<string>
#include<algorithm>
#include<time.h>
#include<queue>
using namespace std;
typedef long long ll;
typedef long double ld;
typedef pair<int,int> pr;
const double pi=acos(-1);
#define rep(i,a,n) for(int i=a;i<=n;i++)
#define per(i,n,a) for(int i=n;i>=a;i--)
#define Rep(i,u) for(int i=head[u];i;i=Next[i])
#define clr(a) memset(a,0,sizeof a)
#define pb push_back
#define mp make_pair
#define fi first
#define sc second
ld eps=1e-9;
ll pp=1000000007;
ll mo(ll a,ll pp){if(a>=0 && a<pp)return a;a%=pp;if(a<0)a+=pp;return a;}
ll powmod(ll a,ll b,ll pp){ll ans=1;for(;b;b>>=1,a=mo(a*a,pp))if(b&1)ans=mo(ans*a,pp);return ans;}
ll read(){
    ll ans=0;
    char last=' ',ch=getchar();
    while(ch<'0' || ch>'9')last=ch,ch=getchar();
    while(ch>='0' && ch<='9'10) years = years *+ch-'0',ch=getchar();
    if(last=='-')ans=-ans;
    return ans;
}
//head

const int N=1e6+5;

int n,t,cnt;
int ch[N][26],nxt[N],bo[N],ans[N];
char buf[1000001];
string s[500];

inline void build(string s,int id)
{
    int now=1,len=s.size (); 
    REP (I, 0 , len . 1 ) 
    { 
        int C = S [I] - ' A ' ;
         IF (! CH [now] [C]) 
        { 
            CH [now] [C] = + + cnt; 
        } // If you do not have this node, you create a new node 
        now = CH [now] [c]; // current node metastasis in the past 
    } 
    BO [now] = the above mentioned id; // give node now is terminating node the numerals marked 
} 


inline void get_nxt () // pretreatment NXT 
{ 
    Queue < int > Q; 
    REP (I,0 , 25 ) CH [ 0 ] [I] = 1 ;
     // all sides initialization transfer point node 0 1 
    NXT [ 1 ] = 0 ; // prefix pointer is initialized to 1 to point 0 
    q.push ( 1 ); / / let 1 enqueued 
    the while (! q.empty ()) 
    { 
        int now q.front = (); // remove the head of the queue elements 
        q.pop (); 
        REP (I, 0 , 25 ) 
        { 
            int U = CH [now] [i];
             IF (! U) // if the node is not now transfer to edge node i is 
               ch [now] [i] =CH [NXT [now]] [i];
     // will continue to look for the pointer now points to the prefix node to the edge node i is transferred forward 
            the else  // If the node has now transferred to the edge node i is 
            { 
                q.push (u); // first enqueue 
                int V = NXT [now]; 
                NXT [u] = CH [V] [I];
 // son node pointer prefix for u prefix pointer edge connector by transferring I 
            } 
        } 
    } 
} 

inline void Find ( String S) 
{ 
    int now = . 1 , len = s.size (); // access to the current node 
    REP (I, 0 , len . 1 )  
    {
        now=ch[now][s[i]-'a'];//更新节点 
        for(int j=now;j;j=nxt[j])ans[bo[j]]++;
    }
    return ;
}

int main()
{
    while(scanf("%d",&n)!=EOF&&n)
    {
        memset(ch,0,sizeof(ch));
        memset(nxt,0,sizeof(nxt));
        memset(bo,0,sizeof(bo));
        memset(ans,0,sizeof(ans));
        
        cnt=1;
        rep(i,0,25)
        {
            ch[0][i]=1,ch[1][i]=0;
        }
        rep(i,1,n)
        {
            scanf("%s",buf);
            s[i]=buf;
            build(s[i],i);
        }
        get_nxt();
        string t;
        scanf("%s",buf);
        t=buf;
        find(t);
        int temp=0;
        rep(i,1,n) 
        {
            if(ans[i]>temp)
            temp=ans[i];
        }
        printf("%d\n",temp);
        rep(i,1,n)
        {
            if(ans[i]==temp) {
                strcpy(buf,s[i].c_str());
                printf("%s\n",buf);
            }
        }
    }
    return 0;
    
}

 

Guess you like

Origin www.cnblogs.com/lcezych/p/11007387.html