NYOJ 数单词

思路:道题做从学AC自动机,到做了这个题,用了几天时间用map 映射 每个字符串的标号在字典数的每个单词结尾处,标记上该单词的标号并把相同的单词放到一个队列里</p剩下的就是AC 自动机了


#include<map>
#include<queue>
#include<stdint.h>
#include<stdio.h>
#include<string.h>
#include<bits/stdc++.h>
using namespace std;
#include<iostream>
#pragma comment(linker,"/STACK:1024000000,1024000000")
const int maxn = 1e6+10;
map<string ,int> m;
char  str[maxn];
char a[200][200];
struct  node
{
    int next[maxn][26];
    int  end[maxn];
    int fail[maxn];
    int ans [1000];
    int root;
    int L ;
    void init()
    {
        L = 0 ;
        for(int i = 1; i<10000; i++)
        {
            ans[i]= 0;
        }
        root = newnode();
    }
    int newnode()
    {
        for(int i = 0; i<26; i++)
        {
            next[L][i] = -1;
        }
        end[L++]=0 ;
        return L -1;
    }
    void insert()
    {
        int now = root;
       int len = strlen(str);
        for(int i = 0; i<len; i++)
        {
            if(next[now][str[i]-'a']==-1)
            {
                next[now][str[i]-'a'] = newnode();


            }
            now = next[now][str[i]-'a'];
        }
        end[now] = m[str];
    }
    void getfali()
    {
        queue<int>q;
        fail[root] = root;
        for(int i = 0 ; i<26; i++)
        {
            if(next[root][i]==-1)
            {


                next[root][i] =root;
            }
            else
            {
                fail[next[root][i]] = root ;
                q.push(next[root][i]);
            }
        }
        while(!q.empty())
        {
            int now = q.front();
            q.pop();
            for(int i = 0 ; i<26; i++)
            {
                if( next[now][i]==-1)
                {
                    next[now][i] =next[fail[now]][i];
                }
                else
                {
                    fail[next[now][i]]= next[fail[now]][i];
                    q.push(next[now][i]);
                }
            }
        }
    }
    void   search()
    {
          int len = strlen(str);
        int now = root ;
        for(int i = 0; i<len; i++)
        {
            now = next[now][str[i]-'a'];
            int temp = now ;
            while(temp!=root)
            {
                ans[end[temp]]++;
                temp  = fail[temp];
            }
        }
    }
};
node ac;
int main()
{
    int T;
    scanf("%d",&T);
    while(T--)
    {
        int ggg[1000];
        memset(ggg,0,sizeof(ggg));
        ac.init();
        int n;
        scanf("%d",&n);
        int cot  = 1 ;
        queue<int>q[200];
        for(int i = 1; i<= n; i++)
        {
            scanf("%s",str);
            strcpy(a[i],str);
            if(!m[str])
            {
                m[str] = cot++;
                q[m[str]].push(i);
                ac.insert();
            }
            else
            {
                q[m[str]].push(i);
            }
        }
        ac.getfali();
         scanf("%s",str);
        ac.search();
        int maxv= -1;
        for(int i = 1; i<cot; i++)
        {
            if(ac.ans[i]>maxv)
            {
                maxv = ac.ans[i];
            }
        }
        printf("%d\n",maxv);
        int flag[1000];
        memset(flag,0,sizeof(flag));
        int  gg= 0 ;
        for(int i = 1  ; i<cot; i++)
        {
            if(ac.ans[i]==maxv)
            {
                flag[gg++] = i;
            }
        }
        int ax[1000];
          memset(ax,0,sizeof(ax));
        int wocao = 0;
        for(int i = 0 ;i<gg;i++)
        {
              while(!q[flag[i]].empty())
              {
                    ax[wocao++] = q[flag[i]].front();
                    q[flag[i]].pop();
              }
        }
        sort(ax+1,ax+wocao);
          for(int i = 0 ;i<wocao;i++)
          {
            puts(a[ax[i]]);
          }
        m.clear();
    }
    return 0 ;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值