您的位置:首页 > 其它

UVA-1449 - Dominating Patterns(AC自动机)

2013-03-19 19:46 363 查看
刘汝佳新书---训练之南

题意:求模板串中与文本串匹配最多次数的模板

分析:使用AC自动机,因为模板串有相同的,故用map<string,int>来判重

// File Name: 1449.cpp
// Author: zlbing
// Created Time: 2013/3/19 11:00:17

#include<iostream>
#include<string>
#include<algorithm>
#include<cstdlib>
#include<cstdio>
#include<set>
#include<map>
#include<vector>
#include<cstring>
#include<stack>
#include<cmath>
#include<queue>
using namespace std;
#define CL(x,v); memset(x,v,sizeof(x));
#define INF 0x3f3f3f3f
#define LL long long
#define REP(i,r,n) for(int i=r;i<=n;i++)
#define RREP(i,n,r) for(int i=n;i>=r;i--)

const int SIGMA_SIZE = 26;
const int MAXNODE = 11000;
const int MAXS = 150 + 10;

map<string,int> ms;
//ms是为了满足特殊要求,比如模板串相同时
struct ACautomata {
int ch[MAXNODE][SIGMA_SIZE];
int f[MAXNODE];    // fail函数
int val[MAXNODE];  // 每个字符串的结尾结点都有一个非0的val
int last[MAXNODE]; // 输出链表的下一个结点
int cnt[MAXS];
int sz;

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

// 字符c的编号
int idx(char c) {
return c-'a';
}

// 插入字符串。v必须非0
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;
ms[string(s)] = v;
}

// 递归打印匹配文本串str[i]结尾的后缀,以结点j结尾的所有字符串
void print(int i,int j) {
if(j) {
cnt[val[j]]++;
print(i,last[j]);
}
}

// 在T中找模板
int find(char* T) {
int n = strlen(T);
int j = 0; // 当前结点编号,初始为根结点
for(int i = 0; i < n; i++) { // 文本串当前指针
int c = idx(T[i]);
j = ch[j][c];
if(val[j]) print(i,j);
else if(last[j]) print(i,last[j]); // 找到了!
}
}

// 计算fail函数
void getFail() {
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; }
}
// 按BFS顺序计算fail
while(!q.empty()) {
int r = q.front(); q.pop();
for(int c = 0; c < SIGMA_SIZE; c++) {
int u = ch[r][c];
if(!u) {
ch[r][c]=ch[f[r]][c];
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]];
}
}
}

};
const int MAXN=1e6+10;
char str[MAXN];
char s[200][100];
ACautomata solver;
int main(){
int n;
while(~scanf("%d",&n))
{
if(!n)break;
solver.init();
REP(i,1,n){
scanf("%s",s[i]);
solver.insert(s[i],i);
}
scanf("%s",str);
solver.getFail();
solver.find(str);
int maxn=-1;
REP(i,1,n){
maxn=max(maxn,solver.cnt[i]);
}
printf("%d\n",maxn);
REP(i,1,n){
if(solver.cnt[ms[string(s[i])]]==maxn)
printf("%s\n",s[i]);
}
}
return 0;
}


上述代码将失配函数中的语句"if(!u) continue;"改成了"if(!u) {ch[r][c]=ch[f[r]][c];continue;}"这样就完全不需要失配函数,而是对所有的转移一视同仁.也就是说,find函数中的语句"while(j && !ch[j][c]) j = f[j]; "可以直接完全删除

// File Name: 1449.cpp
// Author: zlbing
// Created Time: 2013/3/19 11:00:17

#include<iostream>
#include<string>
#include<algorithm>
#include<cstdlib>
#include<cstdio>
#include<set>
#include<map>
#include<vector>
#include<cstring>
#include<stack>
#include<cmath>
#include<queue>
using namespace std;
#define CL(x,v); memset(x,v,sizeof(x));
#define INF 0x3f3f3f3f
#define LL long long
#define REP(i,r,n) for(int i=r;i<=n;i++)
#define RREP(i,n,r) for(int i=n;i>=r;i--)

const int SIGMA_SIZE = 26;
const int MAXNODE = 11000;
const int MAXS = 150 + 10;

map<string,int> ms;

struct ACautomata {
int ch[MAXNODE][SIGMA_SIZE];
int f[MAXNODE];    // fail函数
int val[MAXNODE];  // 每个字符串的结尾结点都有一个非0的val
int last[MAXNODE]; // 输出链表的下一个结点
int cnt[MAXS];
int sz;

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

// 字符c的编号
int idx(char c) {
return c-'a';
}

// 插入字符串。v必须非0
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;
ms[string(s)] = v;
}

// 递归打印以结点j结尾的所有字符串
void print(int j) {
if(j) {
cnt[val[j]]++;
print(last[j]);
}
}

// 在T中找模板
int 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]); // 找到了!
}
}

// 计算fail函数
void getFail() {
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; }
}
// 按BFS顺序计算fail
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]];
}
}
}

};
const int MAXN=1e6+10;
char str[MAXN];
char s[200][100];
ACautomata solver;
int main(){
int n;
while(~scanf("%d",&n))
{
if(!n)break;
solver.init();
REP(i,1,n){
scanf("%s",s[i]);
solver.insert(s[i],i);
}
scanf("%s",str);
solver.getFail();
solver.find(str);
int maxn=-1;
REP(i,1,n){
maxn=max(maxn,solver.cnt[i]);
}
printf("%d\n",maxn);
REP(i,1,n){
if(solver.cnt[ms[string(s[i])]]==maxn)
printf("%s\n",s[i]);
}
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: