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[1005][1005],ch[1005][1005];
int n,m,w;
int x,y,direction;
int result[1005][1005];
int dir[8][2]={-1,0,-1,1,0,1,1,1,1,0,1,-1,0,-1,-1,-1};
void init(node *h)
{
    for(int i=0;i<26;i++)
    {
        h->next[i]=NULL;
        h->num=0;
    }
}
void h_insert(char s[],int id)//建树
{
    node *t,*s1=head;
    int n=strlen(s);
    for(int i=0;i<n;i++)
    {
        int k=s[i]-‘A‘;
        if(s1->next[k]==NULL)
        {
            t=new node;
            init(t);
            s1->next[k]=t;
        }
        s1=s1->next[k];
    }
    s1->num=id;
}
void dfs(node *p,int a,int b,int d)//搜索
{
    if(p==NULL) return;

    if(p->num>0)
    {
        result[p->num][0]=x;
        result[p->num][1]=y;
        result[p->num][2]=d;
    }
    if(a<0||b<0||a>=n||b>=m) return;//放在后面判断
    dfs(p->next[str[a][b]-‘A‘],a+dir[d][0],b+dir[d][1],d);
}
int main()
{
    int i,j,k;
    while(scanf("%d%d%d",&n,&m,&w)!=EOF)
    {
        head=new node;
        init(head);
        memset(str,0,sizeof(str));
        memset(ch,0,sizeof(ch));
        memset(result,0,sizeof(result));
        for(i=0;i<n;i++)
        {
            scanf("%s",str[i]);
        }
        for(i=1;i<=w;i++)
        {
            scanf("%s",ch[i]);
            h_insert(ch[i],i);
        }
        for(i=0;i<n;i++)
        {
            for(j=0;j<m;j++)
            {
                for(k=0;k<8;k++)
                {
                    x=i;y=j;direction=k;
                    node *p=head;
                    dfs(p,x,y,direction);
                }
            }
        }
        for(i=1;i<=w;i++)
        {
            printf("%d %d %c\n",result[i][0],result[i][1],result[i][2]+‘A‘);
        }
    }
    return 0;
}
时间: 2024-08-09 06:38:21

POJ 1204 Word Puzzles(字典树+搜索)的相关文章

poj 1204 Word Puzzles(字典树)

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

[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 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 静态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】(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自己主动机题解

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

POJ 1204 Word Puzzles | AC 自动鸡

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