zoukankan      html  css  js  c++  java
  • 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
    ",maxn);
            for (int i = 0; i < n; i++)
                if (num[i]==maxn) printf ("%s
    ", 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;
    }
  • 相关阅读:
    编程语言本身不产生任何价值
    探索几种常见的广告平台
    Talk about my most-recent job application, Got acknowledgement of Native American programmers of two rounds of Video interviews for over 2 months' time, Chinese f2f interview is a deep question.
    UI 控件和工具库, 编程语言更高一层的Must have, before fully prepared.
    Python趣味入门6:能计数的循环语句for
    Python趣味入门5:循环语句while
    交个朋友
    2020年开始,中国程序员前景一片灰暗,是这样吗?
    2030年,程序员工资还能达到现在的水平吗?
    Java虚拟机调优(七)-典型配置举例1
  • 原文地址:https://www.cnblogs.com/01world/p/5816224.html
Copyright © 2011-2022 走看看