您的位置:首页 > 其它

【POJ1204】Word Puzzles——AC自动机

2016-03-19 09:29 483 查看
Time Limit: 5000MS Memory Limit: 65536K

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

题意:给定一个L x C(C <= 1000, L <= 1000)的字母矩阵,再给定W(W <= 1000)个字符串,保证这些字符串都会在字母矩阵中出现(8种方向),求它们的出现位置和方向。

题解:先缓存所有数据,然后对W个字符串建立字典树和失败指针,再扫描字母矩阵所有8个方向的字符串进行匹配。

#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <cmath>
#include <set>
#include <map>
#include <string>
#include <queue>
#include <stack>
#include <iostream>
#include <algorithm>

using namespace std;

const int Max = 1100;

int Dir[][2] = {{-1,0},{-1,1},{0,1},{1,1},{1,0},{1,-1},{0,-1},{-1,-1}};//建立字典树

int Hash[]={4,5,6,7,0,1,2,3};

typedef struct node
{
int Id;

node * fail;

node *next[26];

node()
{
Id = 0;

fail = NULL;

for(int i = 0; i < 26; i++)
next[i] = NULL;
}
}Tree;

typedef struct Node
{
int x,y;

int op;

}Point;

Point P[Max];

char str[Max][Max];

char s[Max];

int n,m,k;

void BuildTree(Tree * Root,int num)//建立字典树
{
int len = strlen(s);

for(int i = len-1; i >=0 ;i--)//逆序建树,便于记录位置
{
int ans = s[i] - 'A';

if(Root->next[ans] == NULL) Root->next[ans] = new node();

Root = Root -> next[ans];
}
Root -> Id = num;
}

void BFS(Tree * Root)//构建fail指针
{
queue<node *>Q ;

Q.push(Root);

Tree *u,*v;

while(!Q.empty())
{
u = Q.front(); Q.pop();

v = NULL;

for(int i = 0; i < 26 ; i++)
{
if(u->next[i] == NULL) continue;

if(u == Root) u->next[i]->fail = Root;
else
{
v = u->fail ;

while(v != NULL)
{
if(v -> next[i]==NULL)   v = v -> fail;
else
{
u->next[i]->fail = v->next[i];
break;
}
}
if(v==NULL) u->next[i]->fail = Root;
}
Q.push(u->next[i]);
}
}
}

bool Judge(int x,int y)
{
if(x>=0&&x<n&&y>=0&&y<m) return true;

return false;
}

void Query(int x,int y,int fx,int fy,int op,Tree * Root)//字符串匹配
{
Tree *p = Root;

for(;Judge(x,y);x+=fx,y+=fy)
{
int ans = str[x][y]-'A';

while(p->next[ans] == NULL && p != Root) p = p->fail;

p=p->next[ans];

if(p==NULL) p = Root;

Tree * temp = p;

while(temp!=Root)
{
if(temp->Id&&P[temp->Id].op == -1)
{
P[temp->Id].x = x;

P[temp->Id].y = y;

P[temp->Id].op = op;
}
temp = temp->fail;
}
}
}

int main()
{
while(~scanf("%d %d %d",&n,&m,&k))
{

Tree * Root = new node();

for(int i = 0; i < n; i++)
scanf("%s",str[i]);

for(int i = 1; i <= k; i++)
{
scanf("%s",s);

BuildTree(Root,i);

P[i].op = -1;
}

BFS(Root);

for(int i = 0;i<m;i++)//四个边界枚举
for(int j = 0;j<8;j++)
Query(0,i,Dir[j][0],Dir[j][1],j,Root);

for(int i = 0;i<m;i++)
for(int j = 0;j<8;j++)
Query(n-1,i,Dir[j][0],Dir[j][1],j,Root);

for(int i = 0;i<n;i++)
for(int j = 0;j<8;j++)
Query(i,0,Dir[j][0],Dir[j][1],j,Root);

for(int i = 0;i<n;i++)
for(int j = 0;j<8;j++)
Query(i,m-1,Dir[j][0],Dir[j][1],j,Root);

for(int i = 1;i<=k;i++)
printf("%d %d %c\n",P[i].x,P[i].y,Hash[P[i].op]+'A');

}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: