您的位置:首页 > 其它

poj 1204:Word Puzzles(AC自动机)

2011-09-02 12:51 423 查看
Word Puzzles

Time Limit: 5000MSMemory Limit: 65536K
Total Submissions: 5727Accepted: 2173Special 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


题意:有W个单词,一个字符矩阵(L*C),从8个方向查找矩阵,找出每个 单词在矩阵出现的起始位置,与及其方向。

源代码:(1594MS)

#include<iostream>
#include<string.h>
using namespace std;

const int MAX=1005;
const int NODEMAX=26;
char word[MAX][MAX],s[MAX][MAX];
int L,C,W,dir[8][2]={{-1,0},{-1,1},{0,1},{1,1},{1,0},{1,-1},{0,-1},{-1,-1}};

struct Node
{
int c,r;
char dir;
}rec[MAX];

struct TrieNode
{
int num,count;
bool vis;
struct TrieNode *fail;      //失败指针
struct TrieNode *next[NODEMAX];//26个字母指针域
TrieNode()
{
num=0;
count=0;
fail=NULL;
memset(next,NULL,sizeof(next));
}
};

TrieNode *q[500005];//队列
TrieNode *pRoot;

void InsertTrie(TrieNode *pRoot,char s[],int num)//插入单词
{
TrieNode *p=pRoot;
if(p==NULL)
p=pRoot=new TrieNode();
int i=0;
while(s[i])
{
int k=s[i]-'A';
if(p->next[k]==NULL)
p->next[k]=new TrieNode();
i++;
p=p->next[k];
}
p->num=num;
p->count++;
}

void build_ac_automation(TrieNode *pRoot)
{
int head=0,tail=0;
TrieNode *p=pRoot;
p->fail=NULL;
q[tail++]=pRoot;
while(head!=tail)
{
TrieNode *tmp=q[head++];
for(int i=0;i<NODEMAX;i++)//设置tmp的孩子的fail指针
if(tmp->next[i]!=NULL)
{
if(tmp==pRoot)//根的孩子的fail指针指向根
tmp->next[i]->fail=pRoot;
else
{
p=tmp->fail;//递增的
while(p!=NULL)
{
if(p->next[i]!=NULL)
{
tmp->next[i]->fail=p->next[i];
break;
}
p=p->fail;
}
if(p==NULL) tmp->next[i]->fail=pRoot;//找到,设置为根
}
q[tail++]=tmp->next[i]; //入队
}
}
}

int Search(TrieNode *pRoot,char ch,int r,int c,int di)
{
int cnt=0,k,i;
TrieNode *p,*tmp;
p=pRoot;
i=0;
while(s[r][c])
{
k=s[r][c]-'A';
while(p->next[k]==NULL && p!=pRoot)//往p的失败指针回找,直至找到或返回根
p=p->fail;
p=p->next[k];  //一种是不通过根找到,另外或者可以通过根找到,或者为空
if(p==NULL) p=pRoot; //空,根本找不到
//可以往下找
tmp=p;
while(tmp!=pRoot && tmp->count!=-1)//以s[i]结尾的单词,通过tmp指针全部找出
{
cnt += tmp->count;
if(tmp->count>0)
{
int temp=tmp->num;
int len=strlen(word[temp]);
rec[temp].dir=ch;
rec[temp].r=r-dir[di][0]*(len-1);
rec[temp].c=c-dir[di][1]*(len-1);
}
tmp->count=-1;
tmp=tmp->fail;
}
r += dir[di][0];
c += dir[di][1];
}
return cnt;
}

void Solve()
{
int i,j;
//....A..
for(j=1;j<=C;j++)
Search(pRoot,'A',L,j,0);
//....B...
for(i=1;i<=L;i++)
Search(pRoot,'B',i,1,1);
for(j=2;j<=C;j++)
Search(pRoot,'B',L,j,1);
//.....C......
for(i=1;i<=L;i++)
Search(pRoot,'C',i,1,2);
//.....D.....
for(i=1;i<=L;i++)
Search(pRoot,'D',i,1,3);
for(j=2;j<=C;j++)
Search(pRoot,'D',1,j,3);
//....E...
for(j=1;j<=C;j++)
Search(pRoot,'E',1,j,4);
//.....F....
for(j=1;j<=C;j++)
Search(pRoot,'F',1,j,5);
for(i=1;i<=L;i++)
Search(pRoot,'F',i,C,5);
//.....G......
for(i=1;i<=L;i++)
Search(pRoot,'G',i,C,6);
//.....H.....
for(i=1;i<=L;i++)
Search(pRoot,'H',i,C,7);
for(j=1;j<=C;j++)
Search(pRoot,'H',L,j,7);
for(i=1;i<=W;i++)
cout<<rec[i].r-1<<" "<<rec[i].c-1<<" "<<rec[i].dir<<endl;
}

int main()
{
int i,j;
while(scanf("%d%d%d",&L,&C,&W)!=EOF)
{
pRoot=new TrieNode();
memset(s,0,sizeof(s));
for(i=1;i<=L;i++)
{
getchar();
for(j=1;j<=C;j++)
s[i][j]=getchar();
}
getchar();
for(i=1;i<=W;i++)
{
gets(word[i]);
InsertTrie(pRoot,word[i],i);
}
build_ac_automation(pRoot);
Solve();
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: