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

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

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

#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] = {⑴, ⑴, 0, 1, 1, 1, 0, ⑴};
int dy[8] = {0, 1, 1, 1, 0, ⑴, ⑴, ⑴};

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

void Insert(node *p, char *s, int id)
{
for(int i = 0; s[i] != ''; 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 != ⑴)
{
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 = ⑴;
}
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
", ans[i][0], ans[i][1], ans[i][2] + 'A');
}

波比源码 – 精品源码模版分享 | www.bobi11.com
1. 本站所有资源来源于用户上传和网络,如有侵权请邮件联系站长!
2. 分享目的仅供大家学习和交流,您必须在下载后24小时内删除!
3. 不得使用于非法商业用途,不得违反国家法律。否则后果自负!
4. 本站提供的源码、模板、插件等等其他资源,都不包含技术服务请大家谅解!
5. 如有链接无法下载、失效或广告,请联系管理员处理!
6. 本站资源售价只是赞助,收取费用仅维持本站的日常运营所需!
7. 本站源码并不保证全部能正常使用,仅供有技术基础的人学习研究,请谨慎下载
8. 如遇到加密压缩包,请使用WINRAR解压,如遇到无法解压的请联系管理员!

波比源码 » POJ 1204 Word Puzzles (AC自动机)

发表评论

Hi, 如果你对这款模板有疑问,可以跟我联系哦!

联系站长
赞助VIP 享更多特权,建议使用 QQ 登录
喜欢我嘛?喜欢就按“ctrl+D”收藏我吧!♡