标签:
题目链接:
http://acm.hdu.edu.cn/showproblem.php?pid=2896
题目大意:
给你N个模式串(编号为1~N)。接下来给你M个主串。问:主串中出现的模式串的编号。
思路:
和HDU2222一样。都是求文本串(主串)中出现的模式串。不同的是这道题要求输出的是模式
串的编号。用Val[]来保存模式串的编号id。同时注意字符为ASCII码可见字符,即32~127共
95个。其实定义95就可以了,我这里定义了128,不过没什么影响。
AC代码:
#include<iostream> #include<algorithm> #include<cstdio> #include<cstring> #include<queue> using namespace std; const int MAXN = 210*500; const int SIZE = 128; struct Trie { //Next[i][j]表示字典中i节点的字符为j的儿子节点编号 int Next[MAXN][SIZE]; int Fail[MAXN]; //失配数组 int Val[MAXN]; //出现模式串的id int root,L; int NewNode() { for(int i = 0; i < SIZE; ++i) Next[L][i] = -1; Val[L++] = -1; return L-1; } void Init() { L = 0; root = NewNode(); } void Insert(char *s,int id) { int len = strlen(s); int now = root; for(int i = 0; i < len; ++i) { if(Next[now][s[i]] == -1) Next[now][s[i]] = NewNode(); now = Next[now][s[i]]; } Val[now] = id; } void Build() { queue<int> Q; Fail[root] = root; for(int i = 0; i < SIZE; ++i) if(Next[root][i] == -1) Next[root][i] = root; else { Fail[Next[root][i]] = root; Q.push(Next[root][i]); } while(!Q.empty()) { int now = Q.front(); Q.pop(); for(int i = 0; i < SIZE; ++i) if(Next[now][i] == -1) Next[now][i] = Next[Fail[now]][i]; else { Fail[Next[now][i]] = Next[Fail[now]][i]; Q.push(Next[now][i]); } } } bool vis[510]; bool Query(char *buf,int n,int id) { int len = strlen(buf); int now = root; memset(vis,false,sizeof(vis)); bool flag = false; for(int i = 0; i < len; ++i) { now = Next[now][buf[i]]; int temp = now; while(temp != root) { if(Val[temp] != -1) { vis[Val[temp]] = true; flag = true; } temp = Fail[temp]; } } if(!flag) return false; printf("web %d:",id); for(int i = 1; i <= n; ++i) if(vis[i]) printf(" %d",i); printf("\n"); return true; } }; char Buf[10010]; Trie AC; int main() { int N,M; while(~scanf("%d",&N)) { AC.Init(); for(int i = 1; i <= N; ++i) { scanf("%s",Buf); AC.Insert(Buf,i); } AC.Build(); int ans = 0; scanf("%d",&M); for(int i = 1; i <= M; ++i) { scanf("%s",Buf); if(AC.Query(Buf,N,i)) ans++; } printf("total: %d\n",ans); } return 0; }
标签:
原文地址:http://blog.csdn.net/lianai911/article/details/45922845