zoukankan      html  css  js  c++  java
  • UVALive-4670

    不知道为啥这个题总是WA

    找了好长时间也没找出来

    求大佬帮忙看看

    #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
    ",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])
            {
                ans=max(ans,++cntword[j]);
                //ans+=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
    ",ans);
            for(int i=1;i<=t;i++)
            {
                if(ans==cntword[mp[p[i]]])cout<<p[i]<<endl;
            }
    
        }
        return 0;
    
    }
    View Code
  • 相关阅读:
    linux 自动重启崩溃的进程
    oracle配置出现的问题
    Castle Nvelocity 循环,条件
    asp.net水晶报表初体验
    关于codesmith中文乱码的解决方法
    ref,out
    DIV覆盖DropDownList解决方案(转)
    xp下设置文件的权限(转)
    关于Godaddy空间存放DotNet网站出现500 Internal server error 错误的总结
    为什么设计师应该学习编写代码
  • 原文地址:https://www.cnblogs.com/kayiko/p/12305928.html
Copyright © 2011-2022 走看看