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

Time Limit:3000MS Memory Limit:Unknown 64bit IO Format:%lld & %llu

[]  [Go Back]  [Status]  

Description

Download as PDF

The archaeologists are going to decipher a very mysterious ``language". Now, they know many language patterns; each pattern can be treated as a string on English letters (only lower case). As a sub string, these patterns may appear more than one times in a large text string (also only lower case English letters).

What matters most is that which patterns are the dominating patterns. Dominating pattern is the pattern whose appearing times is not less than other patterns.

It is your job to find the dominating pattern(s) and their appearing times.

Input

The entire input contains multi cases. The first line of each case is an integer, which is the number of patternsN, 1$ \le$N$ \le$150. Each of the following N lines contains one pattern, whose length is in range [1, 70]. The rest of the case is one line contains a large string as the text to lookup, whose length is up to106.

At the end of the input file, number `0' indicates the end of input file.

Output

For each of the input cases, output the appearing times of the dominating pattern(s). If there are more than one dominating pattern, output them in separate lines; and keep their input order to the output.

Sample Input

2 
aba 
bab 
ababababac 
6 
beta 
alpha 
haha 
delta 
dede 
tata 
dedeltalphahahahototatalpha 
0

Sample Output

4 
aba 
2 
alpha 
haha

题意:
有n个小写字母组成的字符床和一个文本串   你的任务是找出哪些字符串在文本中出现的次数最多   例如  aba 在ababa中出现2次  但是bab只出现了一次
输入n  之后n个字符串   长度为1-70   n小于等于150  之后一个文本串  长度最长为10的6次方    
输出出现最多的次数 以及出现最多的字符串为什么  如果存在多个  按输入顺序排列

分析:
AC自动机模板题
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<queue>
using namespace std;
#define rep(i,s,t) for(int i=(s);i<(t);i++)
const int INF = 1e9 + 9;
const int N = 150 * 70 + 9;
const int M = 26;
char str[155][77];
struct Trie {

    int nex[N][M], fail[N], val[N];
    int rt, L;
    int newnode() {
        memset (nex[L], -1, sizeof (nex[L]) );
        val[L++] = -1;
        return L - 1;
    }
    void init() {
        L = 0;
        rt = newnode();
    }
    int idx (char c) {
        return c - 'a';
    }
    void insert (char s[], int id) {
        int len = strlen (s);
        int now = rt;
        for (int i = 0; i < len; i++) {
            if (nex[now][idx (s[i])] == -1)
                nex[now][idx (s[i])] = newnode();
            now = nex[now][idx (s[i])];
        }
        val[now] = id;
    }
    void getFail() {
        queue<int>q;
        fail[rt] = rt;
        for (int i = 0; i < M; i++)
            if (nex[rt][i] == -1) nex[rt][i] = rt;
            else {
                fail[nex[rt][i]] = rt;
                q.push (nex[rt][i]);
            }
        while (!q.empty() ) {
            int now = q.front();
            q.pop();
            for (int i = 0; i < M; i++)
                if (nex[now][i] == -1)
                    nex[now][i] = nex[fail[now]][i];
                else {
                    fail[nex[now][i]] = nex[fail[now]][i];
                    q.push (nex[now][i]);
                }
        }
    }
    int num[155];
    void quert (char s[], int n) {
        memset (num, 0, sizeof (num) );
        int len = strlen (s);
        int now = rt;
        for (int i = 0; i < len; i++) {
            now = nex[now][idx(s[i])];
            int tmp = now;
            while (tmp != rt) {
                if (val[tmp] != -1)
                    num[val[tmp]]++;
                tmp = fail[tmp];
            }
        }
        int maxn=0;
        for(int i=0;i<n;i++)maxn=max(num[i],maxn);
        printf("%d\n",maxn);
        for (int i = 0; i < n; i++)
            if (num[i]==maxn) printf ("%s\n", str[i]);
    }
};
char buf[1000009];
Trie ac;
int main() {
   // freopen("f.txt","r",stdin);
    int n;
    while (~scanf ("%d", &n)&&n ) {
        ac.init();
        for (int i = 0; i < n; i++) {
            scanf ("%s", str[i]);
            ac.insert (str[i], i);
        }
        ac.getFail();
        scanf ("%s", buf);
        ac.quert (buf, n);
    }
    return 0;
}

 

转载于:https://www.cnblogs.com/01world/p/5816224.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值