POJ 题目1204 Word Puzzles(AC自动机,多个方向查询)

Word Puzzles

Time Limit: 5000MS   Memory Limit: 65536K
Total Submissions: 10244   Accepted: 3864   Special Judge

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
perception of any possible delay in bringing them their order.

Even though word puzzles may be entertaining to solve by hand, they may become boring when they get very large. Computers do not yet get bored in solving tasks, therefore we thought you could devise a program to speedup (hopefully!) solution finding in such
puzzles.

The following figure illustrates the PizzaHut puzzle. The names of the pizzas to be found in the puzzle are: MARGARITA, ALEMA, BARBECUE, TROPICAL, SUPREMA, LOUISIANA, CHEESEHAM, EUROPA, HAVAIANA, CAMPONESA.

Your task is to produce a program that given the word puzzle and words to be found in the puzzle, determines, for each word, the position of the first letter and its orientation in the puzzle.

You can assume that the left upper corner of the puzzle is the origin, (0,0). Furthemore, the orientation of the word is marked clockwise starting with letter A for north (note: there are 8 possible directions in total).

Input

The first line of input consists of three positive numbers, the number of lines, 0 < L <= 1000, the number of columns, 0 < C <= 1000, and the number of words to be found, 0 < W <= 1000. The following L input lines, each one of
size C characters, contain the word puzzle. Then at last the W words are input one per line.

Output

Your program should output, for each word (using the same order as the words were input) a triplet defining the coordinates, line and column, where the first letter of the word appears, followed by a letter indicating the orientation
of the word according to the rules define above. Each value in the triplet must be separated by one space only.

Sample Input

20 20 10
QWSPILAATIRAGRAMYKEI
AGTRCLQAXLPOIJLFVBUQ
TQTKAZXVMRWALEMAPKCW
LIEACNKAZXKPOTPIZCEO
FGKLSTCBTROPICALBLBC
JEWHJEEWSMLPOEKORORA
LUPQWRNJOAAGJKMUSJAE
KRQEIOLOAOQPRTVILCBZ
QOPUCAJSPPOUTMTSLPSF
LPOUYTRFGMMLKIUISXSW
WAHCPOIYTGAKLMNAHBVA
EIAKHPLBGSMCLOGNGJML
LDTIKENVCSWQAZUAOEAL
HOPLPGEJKMNUTIIORMNC
LOIUFTGSQACAXMOPBEIO
QOASDHOPEPNBUYUYOBXB
IONIAELOJHSWASMOUTRK
HPOIYTJPLNAQWDRIBITG
LPOINUYMRTEMPTMLMNBO
PAFCOPLHAVAIANALBPFS
MARGARITA
ALEMA
BARBECUE
TROPICAL
SUPREMA
LOUISIANA
CHEESEHAM
EUROPA
HAVAIANA
CAMPONESA

Sample Output

0 15 G
2 11 C
7 18 A
4 8 C
16 13 B
4 15 E
10 3 D
5 1 E
19 7 C
11 11 H

Source

Southwestern Europe 2002

题目大意:给你一个矩阵,然后问每个串在矩阵中的起点和方向,方向从上(A)顺时针8个方向

就是也8个方向的查询就好。。

ac代码

#include<stdio.h>
#include<string.h>
char map[1010][1010],key[1010];
int dx[8]={-1,-1,0,1,1,1,0,-1};
int dy[8]={0,1,1,1,0,-1,-1,-1};
int ansx[1010],ansy[1010],ansd[1010],len[1010];
int head,tail;
int n,m,w;
struct node
{
    node *fail;
    node *next[26];
    int id;
    node()
    {
        fail=NULL;
        id=0;
        for(int i=0;i<26;i++)
            next[i]=NULL;
    }
}*q[50005000];
node *root;
void insert(char *s,int id)
{
    int temp,len,i;
    node *p=root;
    len=strlen(s);
    for(i=0;i<len;i++)
    {
        temp=s[i]-'A';
        if(p->next[temp]==NULL)
            p->next[temp]=new node();
        p=p->next[temp];
    }
    p->id=id;
}
void build_ac()
{
	head=tail=0;
    q[tail++]=root;
    while(head!=tail)
    {
        node *p=q[head++];
        node *temp=NULL;
        for(int i=0;i<26;i++)
        {
            if(p->next[i]!=NULL)
            {
                if(p==root)
                    p->next[i]->fail=root;
                else
                {
                    temp=p->fail;
                    while(temp!=NULL)
                    {
                        if(temp->next[i]!=NULL)
                        {
                            p->next[i]->fail=temp->next[i];
                            break;
                        }
                        temp=temp->fail;
                    }
                    if(temp==NULL)
                    {
                        p->next[i]->fail=root;
                    }
                }
                q[tail++]=p->next[i];
            }
        }
    }
}
void query(int x,int y,int dir)
{
	node *p=root,*temp;
	int i,j;
	for(i=x,j=y;i>=0&&i<n&&j>=0&&j<m;i+=dx[dir],j+=dy[dir])
	{
		int x=map[i][j]-'A';
        while(p->next[x]==NULL&&p!=root)
            p=p->fail;
        p=p->next[x];
        if(p==NULL)
        {
            p=root;
        }
        temp=p;
        while(temp!=root&&temp->id!=-1)
        {
            int id=temp->id;
			ansx[id]=i-(len[id]-1)*dx[dir];
			ansy[id]=j-(len[id]-1)*dy[dir];
			ansd[id]=dir;
			temp->id=-1;
            temp=temp->fail;
        }
	}
}
int main()
{
//	int n,m,w;
	while(scanf("%d%d%d",&n,&m,&w)!=EOF)
	{
		int i,j;
		root=new node();
		for(i=0;i<n;i++)
		{
			scanf("%s",map[i]);
		}
		for(i=1;i<=w;i++)
		{
			scanf("%s",key);
			len[i]=strlen(key);
			insert(key,i);
		}
		build_ac();
		for(i=0;i<m;i++)
		{
			for(j=0;j<8;j++)
			{
				query(0,i,j);
				query(n-1,i,j);
			}
		}
		for(i=0;i<n;i++)
		{
			for(j=0;j<8;j++)
			{
				query(i,0,j);
				query(i,m-1,j);
			}
		}
		for(i=1;i<=w;i++)
		{
			printf("%d %d %c\n",ansx[i],ansy[i],ansd[i]+'A');
		}
	}
}

版权声明:本文为博主原创文章,未经博主允许不得转载。

时间: 2024-08-12 03:43:04

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

这道题目各种wa.首先是错了一个坐标,居然没测出来.然后是剪枝错误.搜索pen时就返回,可能还存在串pen*. 1 #include <cstdio> 2 #include <cstring> 3 #include <cstdlib> 4 5 #define MAXN 1005 6 7 typedef struct Trie { 8 int v; 9 Trie *next[26]; 10 Trie() { 11 v = 0; 12 for (int i=0; i<

POJ 1204 Word Puzzles | AC 自动鸡

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

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

字典树PKU 1204 Word Puzzles

PKU 1204 Word Puzzles 题目的大意为: 要求编程实现查找字谜拼图中的单词,输出查找到的单词的起始位置和方向(ABCDEFGH分别代表北.东北.东.东南.南.西南.西.西北八个方向). 输入: 输入的第一行包含三个正数:行数,0<L<=1000:列数,0<C<= 1000:和字谜单词个数0<W<=1000.接下来的L行,每行输入C个字符.最后W行每行输入一个字谜单词. 输出: 你的程序应该每行输出每个单词(跟输入的顺序相同)第一个字母出现的行和列的下标

POJ 3691 DNA repair 基于AC自动机的DP

dp[i][j] 表示长度为 i 的前缀到达第 j 个节点的最小更改数目. 很显然有dp[0][0] = 0; dp[ i ][ j ] = min(dp[ i ][ j ],dp[i-1][k] + (j == k ? 0 : 1)),当且仅当j,k满足下列条件时. j 不为某条模式串的末节点 且 j 到 root 的由失败指针组成的路径上无末节点. j 是k的儿子节点 或者 j 的父节点可由 k 沿着失败指针找到. #include <algorithm> #include <ios

[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