POJ 1204 Word Puzzles | AC 自动鸡

题目:

给一个字母矩阵和几个模式串,矩阵中的字符串可以有8个方向

输出每个模式串开头在矩阵中出现的坐标和这个串的方向



题解:

我们可以把模式串搞成AC自动机,然后枚举矩阵最外围一层的每个字母,向八个方向进行匹配

代码中danger标记为判断这个节点是不是一个模式串的结尾,

这道题可以直接字符串反向构建AC自动机,匹配到的就是开头(代码是正向)

#include<cstdio>
#include<algorithm>
#include<cstring>
#include<queue>
#define Z 27
#define N 1010
using namespace std;
struct node
{
    int trans[Z],fail,danger;
    void init()
	{
	    memset(trans,0,sizeof(trans));
	    danger=fail=0;
	}
}tr[1000010];
char puzzle[N][N],s[N],dir[10]={‘A‘,‘B‘,‘C‘,‘D‘,‘E‘,‘F‘,‘G‘,‘H‘};
int l,c,w,dx[10]={-1,-1,0,1,1,1,0,-1},dy[10]={0,1,1,1,0,-1,-1,-1};
int fa[N],ansx[N],ansy[N],tot=1,len[N],d[N];

int find(int x)
{
    return fa[x]=fa[x]==x?x:find(fa[x]);
}

void insert(char s[],int id)
{
    int len=strlen(s+1),now=1;
    for (int i=1;i<=len;i++)
    {
	if (tr[now].trans[s[i]-‘A‘]==0)
	    tr[tr[now].trans[s[i]-‘A‘]=++tot].init();
	now=tr[now].trans[s[i]-‘A‘];
    }
    if (tr[now].danger)
	fa[find(id)]=find(tr[now].danger);
    else
	tr[now].danger=id;
}

void BuildFail()
{
    queue <int> q;
    tr[1].fail=1;
    for (int i=0;i<26;i++)
    {
	if (tr[1].trans[i]==0)
	    tr[1].trans[i]=1;
	else
	{
	    tr[tr[1].trans[i]].fail=1;
	    q.push(tr[1].trans[i]);
	}
    }
    while (!q.empty())
    {
	int u=q.front(),v,w;
	for (int i=0;i<26;i++)
	{
	    v=tr[u].fail;
	    v=tr[v].trans[i],w=tr[u].trans[i];
	    if (w) tr[w].fail=v,q.push(w);
	    else tr[u].trans[i]=v;
	}
	q.pop();
	if (tr[u].danger==0 && tr[tr[u].fail].danger)
	    tr[u].danger=tr[tr[u].fail].danger;
    }
}

void query(int x,int y,int t)
{
    int p=1;
    while (x>0 && y>0 && x<=l && y<=c)
    {
	p=tr[p].trans[puzzle[x][y]-‘A‘];
	int ap=p;
	while (tr[ap].danger)
	{
	    int k=tr[ap].danger;
	    ansx[k]=x-dx[t]*(len[k]-1);
	    ansy[k]=y-dy[t]*(len[k]-1);
	    d[k]=t;
	    ap=tr[ap].fail;
	}
	x+=dx[t];
	y+=dy[t];
    }
}

int main()
{
    scanf("%d%d%d",&l,&c,&w);
    for (int i=1;i<=l;i++)
	scanf("%s",puzzle[i]+1);
    for (int i=1;i<=w;i++)
    {
	scanf("%s",s+1);
	fa[i]=i;
	len[i]=strlen(s+1);
	insert(s,i);
    }
    BuildFail();
    for (int i=1;i<=l;i++)
	for (int j=0;j<8;j++)
	    query(i,1,j),query(i,c,j);
    for (int i=1;i<=c;i++)
	for (int j=0;j<8;j++)
	    query(1,i,j),query(l,i,j);
    for (int i=1;i<=w;i++)
	printf("%d %d %c\n",ansx[i]-1,ansy[i]-1,dir[d[i]]);
    return 0;
}
时间: 2024-08-27 23:19:36

POJ 1204 Word Puzzles | AC 自动鸡的相关文章

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 (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 AC自己主动机题解

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

[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(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 静态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

poj 1204 Word Puzzles(字典树)

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

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