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 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

题目链接:http://poj.org/problem?id=1204

题目大意:给一个字符矩阵,和一些单词,求这些单词的首字母的位置和单词的方向,一共八个方向,正北为A,顺时针BCD...

题目分析:对单词建立trie树,构造ac自动机,以四个边上的字符为起点向八个方向枚举找

#include <cstdio>
#include <cstring>
#include <queue>
#include <algorithm>
using namespace std;
int const MAX = 1005;
char map[MAX][MAX], s[MAX];
int r, c, n;
int len[MAX], ans[MAX][3];
int dx[8] = {-1, -1, 0, 1, 1, 1, 0, -1};
int dy[8] = {0, 1, 1, 1, 0, -1, -1, -1};

struct node
{
    int id;
    node *next[26];
    node *fail;
    node()
    {
        id = -1;
        memset(next, NULL, sizeof(next));
        fail = NULL;
    }
};

void Insert(node *p, char *s, int id)
{
    for(int i = 0; s[i] != '\0'; i++)
    {
        int idx = s[i] - 'A';
        if(p -> next[idx] == NULL)
            p -> next[idx] = new node();
        p = p -> next[idx];
    }
    p -> id = id;
}

void AC_Automation(node *root)
{
    queue <node*> q;
    q.push(root);
    while(!q.empty())
    {
        node *p = q.front();
        q.pop();
        for(int i = 0; i < 26; i++)
        {
            if(p -> next[i])
            {
                if(p == root)
                    p -> next[i] -> fail = root;
                else
                    p -> next[i] -> fail = p -> fail -> next[i];
                q.push(p -> next[i]);
            }
            else
            {
                if(p == root)
                    p -> next[i] = root;
                else
                    p -> next[i] = p -> fail -> next[i];
            }
        }
    }
}

bool judge(int i, int j)
{
    if(i >= 0 && i < r && j >= 0 && j < c)
        return true;
    return false;
}

void Query(node *root, int x, int y, int k)
{
    node *p = root;
    for(int i = x, j = y; judge(i, j); i += dx[k], j += dy[k])
    {
        int idx = map[i][j] - 'A';
        while(!p -> next[idx] && p != root)
            p = p -> fail;
        p = p -> next[idx];
        if(!p)
        {
            p = root;
            continue;
        }
        node *tmp = p;
        while(tmp != root)
        {
            //找到单词,则记录下来相关信息
            if(tmp -> id != -1)
            {
                int id = tmp -> id;
                ans[id][0] = i - len[id] * dx[k];
                ans[id][1] = j - len[id] * dy[k];
                ans[id][2] = k;
                tmp -> id = -1;
            }
            else
                break;
            tmp = tmp -> fail;
        }
    }
}

int main()
{
    scanf("%d %d %d", &r, &c, &n);
    node *root = new node();
    for(int i = 0; i < r; i++)
        scanf("%s", map[i]);
    for(int i = 0; i < n; i++)
    {
        scanf("%s", s);
        len[i] = strlen(s) - 1;
        Insert(root, s, i);
    }
    AC_Automation(root);
    //枚举各个边界的各个方向
    for(int i = 0; i < c; i++)
    {
        for(int dir = 0; dir < 8; dir++)
        {
            Query(root, 0, i, dir);
            Query(root, r - 1, i, dir);
        }
    }
    for(int i = 0; i < r; i++)
    {
        for(int dir = 0; dir < 8; dir++)
        {
            Query(root, i, 0, dir);
            Query(root, i, c - 1, dir);
        }
    }
    for(int i = 0; i < n; i++)
        printf("%d %d %c\n", ans[i][0], ans[i][1], ans[i][2] + 'A');
}
时间: 2024-10-07 05:41:17

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 自动鸡

题目: 给一个字母矩阵和几个模式串,矩阵中的字符串可以有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

[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