UVALive-4670

I do not know why this question is always WA

Looking for a long time did not find out

Gangster seeking help us to see

#include<iostream>
#include<iomanip>
#include<cstring>
#include<sstream>
#include<algorithm>
#include<cstdio>
#include<cmath>
#include<stdlib.h>
#include<string>
#include<queue>
#include<vector>
#include<map>
#include<stack>
//#include<bits/stdc++.h>
#define _for(i,a,b) for(int i=a;i<=b;i++)
using namespace std;
typedef long long ll;
const int mod =1e6+7;
double esp=1e-6;
int INF =0x3f3f3f3f;
const int inf = 1<<28;
const int MAXN=3e5+5;
int fail[MAXN];
int ch[MAXN][27];
int cntword[MAXN];
int tot;
bool f[MAXN];
map<string,int> mp;
void insertword(string s)
{
    int u=0;
    for(int i=0;i<s.size();i++)
    {
        int v=s[i]-'a';
        if(!ch[u][v])
        {
            ch [u] [v] = ++ tot;
        }
        u=ch[u][v];
    }
    Mp [S] = U;
    f[u]=1;
}
void getfail()
{
    queue<int> q;
    for(int i=0;i<26;i++)
    {
        if(ch[0][i])
        {
            fail[ch[0][i]]=0;
            q.push(ch[0][i]);
        }
    }
    while(!q.empty())
    {
        int u=q.front();
        q.pop();
        for(int i=0;i<26;i++)
        {
            if(ch[u][i])
            {
                fail[ch[u][i]]=ch[fail[u]][i];
                q.push(ch[u][i]);
            }
            else
                ch[u][i]=ch[fail[u]][i];
        }
    }
    //for(int i=1;i<=tot;i++)
    //printf("%d %d\n",i,fail[i]);
}
int query(string s)
{
    int u=0,ans=-1;
    for(int i=0;i<s.size();i++)
    {
        u=ch[u][s[i]-'a'];
        for(int j=u;j&&f[j];j=fail[j])
        {
            years = max (years ++ cntword [j]);
            // years cntword + = [j];
            // cntword [j] = - 1; 
        }
    }
    return ans;
}
string s;
string p[MAXN];
int main()
{
    int t;
    while(~scanf("%d",&t)&&t)
    {
        mp.clear();
        memset(ch,0,sizeof(ch));
        memset(fail,0,sizeof(fail));
        memset(cntword,0,sizeof(cntword));
        memset(f,0,sizeof(f));
        tot=0;
        for(int i=1;i<=t;i++)
        {
            cin>>p[i];
            insertword(p[i]);
        }
        getfail();
        cin>>s;
        int ans=query(s);
        printf("%d\n",ans);
        for(int i=1;i<=t;i++)
        {
            if(ans==cntword[mp[p[i]]])cout<<p[i]<<endl;
        }

    }
    return 0;

}
View Code

 

Guess you like

Origin www.cnblogs.com/kayiko/p/12305928.html