UVALive 4670 - Dominating Patterns (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 patterns N1$ \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 to 106.

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个小写字母组成的字符串和一个文本串,你的任务是找出哪些字符串在文本串中出现的次数最多。


AC自动机模板题


#include <cstdio>
#include <iostream>
#include <vector>
#include <algorithm>
#include <cstring>
#include <string>
#include <map>
#include <cmath>
#include <queue>
#include <set>

using namespace std;

//#define WIN
#ifdef WIN
typedef __int64 LL;
#define iform "%I64d"
#define oform "%I64d\n"
#define oform1 "%I64d"
#else
typedef long long LL;
#define iform "%lld"
#define oform "%lld\n"
#define oform1 "%lld"
#endif

#define S64I(a) scanf(iform, &(a))
#define P64I(a) printf(oform, (a))
#define P64I1(a) printf(oform1, (a))
#define REP(i, n) for(int (i)=0; (i)<n; (i)++)
#define REP1(i, n) for(int (i)=1; (i)<=(n); (i)++)
#define FOR(i, s, t) for(int (i)=(s); (i)<=(t); (i)++)

const int INF = 0x3f3f3f3f;
const double eps = 10e-9;
const double PI = (4.0*atan(1.0));

const int MAX_NODE = 14000 + 20;
const int SIGMA_SIZE = 26;

struct ACAutomata {
    int ch[MAX_NODE][SIGMA_SIZE];
    int val[MAX_NODE];
    int f[MAX_NODE];
    int last[MAX_NODE];
    int cnt[160];
    map<string, int> ms;
    int sz;
    void init() {
        sz = 1;
        ms.clear();
        memset(ch[0], 0, sizeof(ch[0]));
        memset(cnt, 0, sizeof(cnt));
    }
    int idx(char c) { return c - 'a'; }
    void insert(char * s, int v) {
        int n = strlen(s);
        int u = 0;
        for(int i=0; i<n; i++) {
            int c = idx(s[i]);
            if(ch[u][c] == 0) {
                memset(ch[sz], 0, sizeof(ch[sz]));
                val[sz] = 0;
                ch[u][c] = sz++;
            }
            u = ch[u][c];
        }
        val[u] = v;
        ms[string(s)] = v;
    }
    void build() {
        queue<int> Q;
        f[0] = 0;
        for(int i=0; i<SIGMA_SIZE; i++) {
            int u = ch[0][i];
            if(u) {
                f[u] = last[u] = 0;
                Q.push(u);
            }
        }
        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]];
            }
        }
    }

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

    void query(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]);
        }
    }
};

ACAutomata ac;
char dic[160][100];
char s[1000000 + 20];

int main() {
    int n;

    while(scanf("%d", &n) != EOF && n) {
        ac.init();
        for(int i=1; i<=n; i++) {
            scanf("%s", dic[i]);
            ac.insert(dic[i], i);
        }
        ac.build();
        scanf("%s", s);
        ac.query(s);
        int maxt = 0;
        for(int i=1; i<=n; i++)
            maxt = max(maxt, ac.cnt[i]);
        printf("%d\n", maxt);
        for(int i=1; i<=n; i++) {
            if(ac.cnt[ac.ms[string(dic[i])]] == maxt) {
                puts(dic[i]);
            }
        }
    }

    return 0;
}


另外一种标记重复方法:


#include <cstdio>
#include <iostream>
#include <vector>
#include <algorithm>
#include <cstring>
#include <string>
#include <map>
#include <cmath>
#include <queue>
#include <set>

using namespace std;

//#define WIN
#ifdef WIN
typedef __int64 LL;
#define iform "%I64d"
#define oform "%I64d\n"
#define oform1 "%I64d"
#else
typedef long long LL;
#define iform "%lld"
#define oform "%lld\n"
#define oform1 "%lld"
#endif

#define S64I(a) scanf(iform, &(a))
#define P64I(a) printf(oform, (a))
#define P64I1(a) printf(oform1, (a))
#define REP(i, n) for(int (i)=0; (i)<n; (i)++)
#define REP1(i, n) for(int (i)=1; (i)<=(n); (i)++)
#define FOR(i, s, t) for(int (i)=(s); (i)<=(t); (i)++)

const int INF = 0x3f3f3f3f;
const double eps = 10e-9;
const double PI = (4.0*atan(1.0));

const int MAX_NODE = 14000 + 20;
const int SIGMA_SIZE = 26;

struct ACAutomata {
    int ch[MAX_NODE][SIGMA_SIZE];
    int val[MAX_NODE];
    int f[MAX_NODE];
    int last[MAX_NODE];
    int cnt[MAX_NODE];
    map<string, int> ms;
    int sz;
    void init() {
        sz = 1;
        ms.clear();
        memset(ch[0], 0, sizeof(ch[0]));
        memset(cnt, 0, sizeof(cnt));
    }
    int idx(char c) { return c - 'a'; }
    void insert(char * s, int v) {
        int n = strlen(s);
        int u = 0;
        for(int i=0; i<n; i++) {
            int c = idx(s[i]);
            if(ch[u][c] == 0) {
                memset(ch[sz], 0, sizeof(ch[sz]));
                val[sz] = 0;
                ch[u][c] = sz++;
            }
            u = ch[u][c];
        }
        val[u] = v;
        ms[string(s)] = u;
    }
    void build() {
        queue<int> Q;
        f[0] = 0;
        for(int i=0; i<SIGMA_SIZE; i++) {
            int u = ch[0][i];
            if(u) {
                f[u] = last[u] = 0;
                Q.push(u);
            }
        }
        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]];
            }
        }
    }

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

    void query(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]);
        }
    }
};

ACAutomata ac;
char dic[160][100];
char s[1000000 + 20];

int main() {
    int n;

    while(scanf("%d", &n) != EOF && n) {
        ac.init();
        for(int i=1; i<=n; i++) {
            scanf("%s", dic[i]);
            ac.insert(dic[i], i);
        }
        ac.build();
        scanf("%s", s);
        ac.query(s);
        int maxt = 0;
        for(int i=1; i<=n; i++)
            maxt = max(maxt, ac.cnt[ac.ms[string(dic[i])]]);
        printf("%d\n", maxt);
        for(int i=1; i<=n; i++) {
            if(ac.cnt[ac.ms[string(dic[i])]] == maxt) {
                puts(dic[i]);
            }
        }
    }

    return 0;
}





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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值