NYOJ 1085 数单词 (AC自动机模板题)

数单词

时间限制:1000 ms  |  内存限制:65535 KB

难度:4

描述

为了能够顺利通过英语四六级考试,现在大家每天早上都会早起读英语。

LYH本来以为自己在6月份的考试中可以通过六级,可是没想到,成绩出来以后,居然没有通过。所以他不得不付出更多的时间来学习英语。

要想通过六级,最基本的要求就是词汇量。为了能够更快的记住一些陌生单词,LYH有时会找一些英语文章来读。

今天早上,LYH又找了一篇文章。读之前,他突然萌生出一个想法:文章中哪些单词出现的次数最多呢?

输入
第一行输入一个整数T,表示有T组测试数据(1≤T≤200)。

对于每组测试数据,第一行输入一个整数n(1≤n≤150),表示LYH要查询的单词数量(有些单词可能会重复出现)。

接下来n行,每行输入一个单词,长度不大于100。

最后一行包含一个由小写字母组成的英语文章(字符串),长度不大于10^6。

输出
对于每组数据,第一行输出一个整数,表示单词出现的次数。

然后按照输入顺序,每行输出一个出现次数最多的单词。如果有重复出现的单词,把它们全部输出。

样例输入
2
3
good
oo
one
goodafternooneveryone
1
to
welcometotopcoder
样例输出
2
oo
one
2
to

分析:这就是一个AC自动机模板题,要注意的是查询的单词中,一个单词可能会出现多次,这里要处理一下。

#include <cstring>
#include <cstdio>
#include <algorithm>
#include <map>
#include <string>
#include <queue>
using namespace std;

#define SIGMA_SIZE 26 //文本串字符内容
#define MAXNODE 20000 //节点数量
#define TEXT_SIZE 1000005 //文本串长度
#define P_SIZE 100 //模式串长度
#define P_NUM 200 //模式串数量

map <string, int> mp;

struct AhoCorasickAutomata
{
    int cnt[P_NUM];
    int sz;
    int ch[MAXNODE][SIGMA_SIZE];
    int f[MAXNODE];
    int val[MAXNODE];
    int last[MAXNODE];

    void Init() {
        sz = 1;
        memset(ch[0],0,sizeof(ch[0]));
        memset(cnt,0,sizeof(cnt));
        mp.clear();
    }

    int idx(char c) {
        return c - 'a';
    }

    void Insert(char *s,int v) {
        int u = 0, n = strlen(s);
        for(int i = 0; i < n; i++) {
            int c = idx(s[i]);
            if(!ch[u][c]) {
                memset(ch[sz], 0, sizeof(ch[sz]));
                val[sz] = 0;
                ch[u][c] = sz++;
            }
            u = ch[u][c];
        }
        val[u] = v;
        mp[string(s)] = v;
    }

    void print(int j) {
        if(j) {
            cnt[val[j]]++;
            print(last[j]);
        }
    }

    void Find(char *T) {
        int n = strlen(T);
        int j = 0;
        for(int i = 0; i < n; i++) {
            int c = idx(T[i]);
            while(j && !ch[j][c]) j = f[j];
            j = ch[j][c];
            if(val[j]) print(j);
            else if(last[j]) print(last[j]);
        }
    }

    void Get_Fail() {
        queue<int> q;
        f[0] = 0;
        for(int c = 0; c<SIGMA_SIZE; c++) {
            int u = ch[0][c];
            if(u) {
                f[u] = 0;
                q.push(u);
                last[u] = 0;
            }
        }
        while(!q.empty()) {
            int r = q.front();
            q.pop();
            for(int c = 0; c<SIGMA_SIZE; c++) {
                int u = ch[r][c];
                if(!u) continue;
                q.push(u);
                int v = f[r];
                while(v && !ch[v][c]) v = f[v];
                f[u] = ch[v][c];
                last[u] = val[f[u]] ? f[u] : last[f[u]];
            }
        }
    }
};

char text[TEXT_SIZE];
char P[P_NUM][P_SIZE];
AhoCorasickAutomata ac;
int n, T;

int main() {
    scanf("%d", &T);
    int cas = 0;
    while(T--) {
        scanf("%d", &n);
        ac.Init();
        for(int i = 1; i <= n; i++) {
            scanf("%s", P[i]);
            ac.Insert(P[i], i);
        }
        ac.Get_Fail();
        scanf("%s", text);
        ac.Find(text);
        int Max_cnt = -1;
        for(int i = 1; i <= n; i++)
            if(ac.cnt[i] > Max_cnt)
                Max_cnt = ac.cnt[i];
        printf("%d\n", Max_cnt);
        for(int i = 1; i <= n; i++)
            if(ac.cnt[mp[string(P[i])]] == Max_cnt)
                printf("%s\n", P[i]);
    }
    return 0;
}
时间: 2024-08-25 17:43:24

NYOJ 1085 数单词 (AC自动机模板题)的相关文章

【BZOJ3172】【Tjoi2013】单词 AC自动机模板题

转载请注明出处:http://blog.csdn.net/vmurder/article/details/42711351 其实我就是觉得原创的访问量比未授权盗版多有点不爽233... 题解:水爆了,直接AC自动机瞎写就行. 坑:--时隔一个半月的感动AC,竟然是因为这道题可以有重复单词233. 代码: #include <queue> #include <cstdio> #include <cstring> #include <iostream> #inc

HDU 2222 Keywords Search(AC自动机模板题)

原题大意:原题链接 先给定T个单词,然后给定一个字符串,查询该字符串中包含多少个给定的单词 解题思路:AC自动机模板题 参考链接:哔哩哔哩算法讲堂 WA版本 注意:因为输入的单词可能有重复,那么Insert()函数中p->id=id;语句中p->id会被覆盖,在Query()函数中会一次性全部被清零,导致不能查询重复单词,以至于结果res错误. #include<queue> #include<cstdio> #include<cstring> using

LA 4670 出现次数最多的子串 (AC自动机模板题)

Dominating Patterns Time Limit:3000MS   Memory Limit:Unknown   64bit IO Format:%lld & %llu [Submit]  [Go Back]  [Status] Description The archaeologists are going to decipher a very mysterious ``language". Now, they know many language patterns; ea

hdu5384 AC自动机模板题,统计模式串在给定串中出现的个数

http://acm.hdu.edu.cn/showproblem.php?pid=5384 Problem Description Danganronpa is a video game franchise created and developed by Spike Chunsoft, the series' name is compounded from the Japanese words for "bullet" (dangan) and "refutation&q

HDU 2222(AC自动机模板题)

题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=2222 题目大意:多个模式串.问匹配串中含有多少个模式串.注意模式串有重复,所以要累计重复结果. 解题思路: AC自动机模板题. 一开始使用LRJ的坑爹静态模板,不支持重复的模式串. 在做AC自动机+DP的时候,扒了zcwwzdjn大神的动态优化(失配指向root)写法,以及借鉴了网上的AC自动机模板, 搞出了这么一个支持重复串的模板. #include "cstdio" #include

HDU3695(AC自动机模板题)

题意:给你n个字符串,再给你一个大的字符串A,问你着n个字符串在正的A和反的A里出现多少个? 其实就是AC自动机模板题啊( ╯□╰ ) 正着query一次再反着query一次就好了 /* gyt Live up to every day */ #include<cstdio> #include<cmath> #include<iostream> #include<algorithm> #include<vector> #include<st

HDU 2222 AC自动机模板题

题目: http://acm.hdu.edu.cn/showproblem.php?pid=2222 AC自动机模板题 1 #include<stdio.h> 2 #include<string.h> 3 #include<queue> 4 using namespace std; 5 char key[55]; 6 char des[1111111]; 7 struct node{ 8 node *fail; 9 node *next[26]; 10 int cnt;

HDU 3065 (AC自动机模板题)

题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=3065 题目大意:多个模式串,范围是大写字母.匹配串的字符范围是(0~127).问匹配串中含有哪几种模式串,且每种模式串出现了多少次. 解题思路: AC自动机模板题.模式串的范围是大写字母,但是匹配串的范围却是(0~127). 如果Trie 开到 128 加上不回收内存,就会MLE. 实际上开到26就行了,find的时候对于c<0||c>26,强制令pos=root出现失配,并开始下一个字符就行了

HDU 2896 (AC自动机模板题)

题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=2896 题目大意:多个模式串.多个匹配串.其中串的字符范围是(0~127).问匹配串中含有哪几个模式串. 解题思路: AC自动机模板题.注意一下字符范围. cnt记录这个模式串的个数改为这个模式串的index. find的时候,把找到的index压入vector里面即可. 注意有多个匹配串,每次find之后会把last->cnt修改,原因是防止一个模式串出现了多次被压入vector,所以先备份一下,