您的位置:首页 > 其它

UVALive 4670 - Dominating Patterns (AC自动机)

2014-08-04 16:19 295 查看
Dominating Patterns

Time Limit: 3000MSMemory Limit: Unknown64bit 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; 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 N, 1

N

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;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: