poj 1204 Word Puzzles 静态trie数解决多模式串匹配问题

题意:

给一个二维字符数组和w个模式串,求这w个模式串在二维字符数组的位置。

分析:

静态trie树。

代码:

//poj 1204
//sep9
#include <iostream>
using namespace std;
const int maxN=1024;
const int maxM=270*maxN;
char str[maxN][maxN];
char s[maxN];
int vis[27],query_id[maxM],ans[maxN][3];
int num,x,y,q,n,m;
int d[8][2]={-1,0,-1,1,0,1,1,1,1,0,1,-1,0,-1,-1,-1};
int f;
struct Trie
{
	int next[27];
}trie[maxM];

void insert(int k)
{
	int u=0,i=0,l=strlen(s);
	for(i=0;i<l;++i){
		int c=s[i]-'A';
		if(!trie[u].next[c]) trie[u].next[c]=++num;
		u=trie[u].next[c];
	}
	query_id[u]=k;
}

void search(int u,int i,int j)
{
	if(!u) return ;
	if(query_id[u]){
		ans[query_id[u]][0]=x;
		ans[query_id[u]][1]=y;
		ans[query_id[u]][2]=q;
		query_id[u]=0;
	}
	int nx=i+d[q][0];
	int ny=j+d[q][1];
	if(nx<0||nx>=n||ny<0||ny>=m)
		return ;
	int l=str[nx][ny]-'A';
	search(trie[u].next[l],nx,ny);
}
int main()
{
	int p,i,j,k;
	scanf("%d%d%d",&n,&m,&p);
	memset(vis,0,sizeof(vis));
	num=0;
	for(i=0;i<n;++i)
		scanf("%s",str[i]);
	for(i=1;i<=p;++i){
		scanf("%s",s);
		insert(i);
		vis[s[0]-'A']=1;
	}
	for(i=0;i<n;++i)
		for(j=0;j<m;++j)
			if(vis[str[i][j]-'A']){
				x=i,y=j;
				for(k=0;k<8;++k){
					q=k;
					search(trie[0].next[str[i][j]-'A'],i,j);
				}
			}
	for(i=1;i<=p;++i)
		printf("%d %d %c\n",ans[i][0],ans[i][1],ans[i][2]+'A');
	return 0;
} 
时间: 2024-08-09 06:38:23

poj 1204 Word Puzzles 静态trie数解决多模式串匹配问题的相关文章

[POJ 1204]Word Puzzles(Trie树暴搜)

Description Word puzzles are usually simple and very entertaining for all ages. They are so entertaining that Pizza-Hut company started using table covers with word puzzles printed on them, possibly with the intent to minimise their client's percepti

POJ 1204 Word Puzzles AC自动机题解

AC自动机的灵活运用,本题关键是灵活二字. 因为数据不是很大,时间要求也不高的缘故,所以本题有人使用暴力法也过了,有人使用Trie也过了. 当然有人使用AC自动机没AC的,在讨论区里喊AC自动机超时的,那是因为不会灵活运用,或者是硬套模板的,AC了速度也不会快. 给出本人的算法思路: 1 把需要查找的关键字建立Trie, 然后构造AC自动机 2 查找的时候分八个方向查找,比如棋盘是board[N][M],那么就可以循环i(0->N-1),然后每次把board[i]当做一个文本,做过HDU的key

[POJ 1204]Word Puzzles(Trie树暴搜&amp;amp;AC自己主动机)

Description Word puzzles are usually simple and very entertaining for all ages. They are so entertaining that Pizza-Hut company started using table covers with word puzzles printed on them, possibly with the intent to minimise their client's percepti

【 POJ - 1204 Word Puzzles】(Trie+爆搜)

Word Puzzles Time Limit: 5000MS Memory Limit: 65536K Total Submissions: 10782 Accepted: 4076 Special Judge Description Word puzzles are usually simple and very entertaining for all ages. They are so entertaining that Pizza-Hut company started using t

POJ 1204 Word Puzzles (AC自动机)

Word Puzzles Time Limit: 5000MS   Memory Limit: 65536K Total Submissions: 9926   Accepted: 3711   Special Judge Description Word puzzles are usually simple and very entertaining for all ages. They are so entertaining that Pizza-Hut company started us

poj 1204 Word Puzzles(字典树)

题目链接:http://poj.org/problem?id=1204 思路分析:由于题目数据较弱,使用暴力搜索:对于所有查找的单词建立一棵字典树,在图中的每个坐标,往8个方向搜索查找即可: 需要注意的是查找时不能匹配了一个单词就不在继续往该方向查找,因为在某个坐标的某个方向上可能会匹配多个单词,所以需要一直 查找直到查找到该方向上最后一个坐标: 代码如下: #include <cstdio> #include <cstring> #include <iostream>

POJ 1204 Word Puzzles AC自己主动机题解

AC自己主动机的灵活运用,本题关键是灵活二字. 由于数据不是非常大.时间要求也不高的缘故.所以本题有人使用暴力法也过了.有人使用Trie.然后枚举八个方向,也过了.只是速度非常慢. 当然有人使用AC自己主动机没AC的,在讨论区里喊AC自己主动机超时的,那是由于不会灵活运用.或者是硬套模板的,AC了速度也不会快. 给出本人的算法思路: 1 把须要查找的keyword建立Trie, 然后构造AC自己主动机 2 查找的时候分八个方向查找,比方棋盘是board[N][M].那么就能够循环i(0->N-1

POJ 1204 Word Puzzles(字典树+搜索)

题意:在一个字符矩阵中找每个给定字符串的匹配起始位置和匹配方向(A到H表示八个方向): 思路:将给定字符串插入字典树中,遍历字符矩阵,在每个字符处向八个方向用字典树找. #include<cstdio> #include<cstring> #include<algorithm> using namespace std; typedef struct node { int num; node *next[26]; }node; node *head; char str[1

POJ 1204 Word Puzzles | AC 自动鸡

题目: 给一个字母矩阵和几个模式串,矩阵中的字符串可以有8个方向 输出每个模式串开头在矩阵中出现的坐标和这个串的方向 题解: 我们可以把模式串搞成AC自动机,然后枚举矩阵最外围一层的每个字母,向八个方向进行匹配 代码中danger标记为判断这个节点是不是一个模式串的结尾, 这道题可以直接字符串反向构建AC自动机,匹配到的就是开头(代码是正向) #include<cstdio> #include<algorithm> #include<cstring> #include&