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

1、Trie树暴搜,能够说是Trie模板题,将单词插入Trie树后,以地图上每一个点作为起点。8个方向搜索单词就可以,WA了6发,总算AC了,须要注意的就是当眼下的单词已经搜到尽头(终止节点),不要停止搜索!

#include <stdio.h>
#include <iostream>
#include <string>
#include <string.h> #define WORD 26
#define MAXN 1050 using namespace std; struct trie
{
trie *child[WORD]; //儿子节点指针
int id; //若该节点为某单词的终止节点,id=该单词编号
trie(){
memset(child,0,sizeof(child)); //儿子节点初始化为空
id=-1;
}
}; //root=根节点 trie *root=new trie(); int dx[]={-1,-1,0,1,1,1,0,-1};
int dy[]={0,1,1,1,0,-1,-1,-1}; //搜索方向,暴力枚举每一个点八个方向
int ans[MAXN][3],visit[MAXN],L,C,W; //第i个单词的位置=(ans[i][1],ans[i][2]),方向为ans[i][0] string c[MAXN],word; //保存全部字符串的矩阵 void build(string s,int num) //将编号为num的字符串s插入trie树中
{
int i;
trie *p=root; //初始时p指向根节点
for(i=0;i<s.size();i++)
{
if(p->child[s[i]-'A']==NULL) //无现成的字符串相应位置儿子节点
p->child[s[i]-'A']=new trie();
p=p->child[s[i]-'A']; //将指针移向当前节点以下的相应儿子节点
}
p->id=num; //记下终止节点的相应单词编号
} void search(int sx,int sy,int dir) //搜索trie树,单词起点(sx,sy),延伸方向为dir
{
int xx=sx,yy=sy; //当前单词字母的坐标(xx,yy)
trie *point=root; //point指向当前trie树的节点
while(xx>=0&&xx<L&&yy>=0&&yy<C) //坐标未越界
{
if(!point->child[c[xx][yy]-'A']) //到达了终止节点,但单词还没结束
break;
else
point=point->child[c[xx][yy]-'A']; //指针向该节点下方移动
if(point->id!=-1) //该节点为终止节点
{
if(visit[point->id]==0)
{
visit[point->id]=1;
ans[point->id][0]=dir; //记录下该单词的開始坐标、方向
ans[point->id][1]=sx;
ans[point->id][2]=sy;
}
}
xx+=dx[dir]; //移动坐标
yy+=dy[dir];
}
} int main()
{
int i,j,k;
scanf("%d%d%d",&L,&C,&W);
for(i=0;i<L;i++)
{
cin>>c[i];
}
for(i=0;i<W;i++)
{
cin>>word;
build(word,i); //将单词插入trie树
}
for(i=0;i<L;i++) //枚举起点(i,j)
for(j=0;j<C;j++)
for(k=0;k<8;k++) //暴力枚举单词存在的方向
search(i,j,k);
for(i=0;i<W;i++)
printf("%d %d %c\n",ans[i][1],ans[i][2],ans[i][0]+'A');
return 0;
}

2、AC自己主动机。Trie树暴力的方法由于是从地图上每一个点開始搜。并且Trie树失配后就必须回到根节点又一次来过。所以复杂度太高,能够直接用AC自己主动机。利用AC自己主动机能够将字符串的后缀和模式串匹配的特点。从地图的四个边上的点作为起点,向八个方向匹配就可以,加之匹配过程中失配后能够沿着失败指针向上走,不必直接到根节点重头来,因此复杂度比第一种方法低不少,可是代码也太长太复杂(140行)

#include <stdio.h>
#include <iostream>
#include <queue>
#include <string>
#include <string.h> #define WORD 26
#define MAXN 1050 using namespace std; struct trie
{
trie *child[WORD]; //儿子节点指针
trie *fail; //失败指针(前缀指针)
int id; //若该节点为某单词的终止节点,id=该单词编号
trie()
{
memset(child,0,sizeof(child)); //儿子节点初始化为空
fail=NULL;
id=-1;
}
}tree[MAXN*100]; //root=根节点 int dx[]={-1,-1,0,1,1,1,0,-1};
int dy[]={0,1,1,1,0,-1,-1,-1}; //搜索方向,暴力枚举每一个点八个方向
int ans[MAXN][3],len[MAXN],L,C,W,nNodesCount=0; //第i个单词的位置=(ans[i][1],ans[i][2]),方向为ans[i][0],nNodeCounts=节点个数 string c[MAXN],word; //保存全部字符串的矩阵 void build(trie *pRoot,string s,int num) //将编号为num的字符串s插入树根为pRoot的trie树中
{
int i;
len[num]=s.size();
for(i=0;s[i];i++)
{
if(pRoot->child[s[i]-'A']==NULL) //无现成的字符串相应位置儿子节点
{
pRoot->child[s[i]-'A']=tree+nNodesCount;
nNodesCount++; //节点个数+1
}
pRoot=pRoot->child[s[i]-'A']; //将指针移向当前节点以下的相应儿子节点
}
pRoot->id=num; //记下终止节点的相应单词编号
} void acAutomation() //搭建前缀指针,构造AC自己主动机
{
int i;
for(i=0;i<WORD;i++)
tree[0].child[i]=tree+1; //让第0个节点的全部儿子节点都指向第一个节点,即不论什么单词都能从根节点匹配下去
tree[0].fail=NULL; //根节点的失败指针指向空
tree[1].fail=tree; //第一个节点的失败指针指向根节点
trie *pRoot,*point;
queue<trie*>q;
q.push(tree+1); //将第一个节点入队
while(!q.empty()) //队列不为空
{
pRoot=q.front();
q.pop(); //队首出队
for(i=0;i<WORD;i++) //遍历该节点的儿子
{
point=pRoot->child[i]; //point=单词当前字母相应的儿子
if(point)
{
trie *pPrev=pRoot->fail; //失败指针指向的节点
while(pPrev) //当指向的节点不为空,不断向上爬
{
if(pPrev->child[i]) //指向的节点能匹配
{
point->fail=pPrev->child[i]; //将该节点的失败指针指向失败指针指向的节点的能够匹配的儿子节点
break;
}
else
pPrev=pPrev->fail; //无法匹配,向该失败指针指向的节点的失败指针往上爬
}
q.push(point); //该节点入队
}
}
}
} void ACsearch(int sx,int sy,int dir) //搜索,单词起点(sx,sy),延伸方向为dir
{
int i,xx=sx,yy=sy;
trie *point=tree+1;
while(xx>=0&&xx<L&&yy>=0&&yy<C)
{
while(1)
{
if(point->child[c[xx][yy]-'A']) //该节点的儿子节点能够匹配
{
point=point->child[c[xx][yy]-'A']; //指针向儿子移动
if(point->id!=-1) //指针相应节点为危急节点,则匹配成功,记录答案
{
ans[point->id][0]=dir; //记录下该单词的開始坐标、方向
ans[point->id][1]=xx-(len[point->id]-1)*dx[dir];
ans[point->id][2]=yy-(len[point->id]-1)*dy[dir];
}
break;
}
else
point=point->fail; //否则。无法匹配,向失败指针移动
}
xx+=dx[dir];
yy+=dy[dir];
}
} int main()
{
int i,j,k;
nNodesCount=2;
scanf("%d%d%d",&L,&C,&W);
for(i=0;i<L;i++) cin>>c[i];
for(i=0;i<W;i++)
{
cin>>word;
build(tree+1,word,i); //将单词插入trie树
}
acAutomation(); //插入失败指针,构造AC自己主动机
for(i=0;i<L;i++)
for(j=0;j<8;j++)
ACsearch(i,0,j);
for(i=0;i<L;i++)
for(j=0;j<8;j++)
ACsearch(i,C-1,j);
for(i=0;i<C;i++)
for(j=0;j<8;j++)
ACsearch(0,i,j);
for(i=0;i<C;i++)
for(j=0;j<8;j++)
ACsearch(L-1,i,j);
for(i=0;i<W;i++)
printf("%d %d %c\n",ans[i][1],ans[i][2],ans[i][0]+'A');
return 0;
}

[POJ 1204]Word Puzzles(Trie树暴搜&amp;AC自己主动机)的更多相关文章

  1. poj 1204 Word Puzzles(字典树)

    题目链接:http://poj.org/problem?id=1204 思路分析:由于题目数据较弱,使用暴力搜索:对于所有查找的单词建立一棵字典树,在图中的每个坐标,往8个方向搜索查找即可: 需要注意 ...

  2. 【 POJ - 1204 Word Puzzles】(Trie+爆搜|AC自动机)

    Word Puzzles Time Limit: 5000MS Memory Limit: 65536K Total Submissions: 10782 Accepted: 4076 Special ...

  3. POJ 1204 Word Puzzles(AC自动机)

    这题的数据卡在,如下: 5 5 3 ABCDE FGHIJ KLMNO PQRST UVWXY PQR RS RST puzzle中间的行中可以包含要查询的多个单词.这个问题很好解决,SearchDf ...

  4. [poj] 1204 Word Puzzles || AC自动机

    原题 给个X*Y的字符串矩阵,W个询问,对每个询问输出这个串出现的位置及方向,共有8个方向,顺时针开始分别用A~H表示 AC自动机的板子题. 对于待匹配串建一个自动机,然后从矩阵的四周分别沿八个方向跑 ...

  5. POJ 1204 Word Puzzles | AC 自动鸡

    题目: 给一个字母矩阵和几个模式串,矩阵中的字符串可以有8个方向 输出每个模式串开头在矩阵中出现的坐标和这个串的方向 题解: 我们可以把模式串搞成AC自动机,然后枚举矩阵最外围一层的每个字母,向八个方 ...

  6. PKU 1204 Word Puzzles(AC自动机)

    题目大意:原题链接 给定一个字符串矩阵和待查找的单词,可以朝8个不同的方向查找,输出待查找单词第一个字母在矩阵中出现的位置和该单词被查到的方向. A~H代表8个不同的方向,A代表正北方向,其他依次以4 ...

  7. poj 2778 AC自己主动机 + 矩阵高速幂

    // poj 2778 AC自己主动机 + 矩阵高速幂 // // 题目链接: // // http://poj.org/problem?id=2778 // // 解题思路: // // 建立AC自 ...

  8. BZOJ 1212 HNOI2004 L语言 AC自己主动机(Trie树)+动态规划

    标题效果:给定词的列表,并m串 每个字符串q个最长前缀,这个前缀可满足拆分成一些字符串 这些字符串中存在的词汇太 再也不怕错误的数据范围--有一个很明显Trie树能解决的问题竟然被我写的AC自己主动机 ...

  9. BZOJ 3172 [Tjoi2013]单词 AC自己主动机(fail树)

    题意:链接 方法:AC自己主动机与fail树性质 解析:复习AC自己主动机的第一道题?(真正的第一题明明是又一次写了遍hdu2222! ) 这题说实话第一眼看上去就是个sb题,仅仅要建出来自己主动机. ...

随机推荐

  1. php接口编程

    1:自定义接口编程 对于自定义接口最关键就是写接口文档,在接口文档中规定具体的请求地址以及方式,还有具体的参数信息 2:接口文档编写 请求地址 http://jxshop.com/Api/login ...

  2. Mysql基础2-数据定义语言DDL

    主要: 数据库操作语句 数据表操作语句 视图定义语句 数据库表设计原则 DDL: Data Definition Language 数据定义语言 数据库操作语句 创建库 创建数据库: create d ...

  3. Hadoop(4)-Hadoop集群环境搭建

    准备工作 开启全部三台虚拟机,确保hadoop100的机器已经配置完成 分发脚本 操作hadoop100 新建一个xsync的脚本文件,将下面的脚本复制进去 vim xsync #这个脚本使用的是rs ...

  4. python基础,导入模块,if语句,while语句

    python基础 python代码 变为字节码 变为机器码 最后执行执行‘文件名.py’文件时出现的‘文件名.pyc’文件为字节码 缓存机制 使用pycharm的时候在文件最开始添加下面这两行代码,中 ...

  5. ThinkPHP中的pathinfo模式和URL重写

    语文一直不太好,要我怎么解释这个pathinfo模式还真不知道怎么说,那就先来一段代码说下pathinfo模式吧 http://serverName/appName/module/action/id/ ...

  6. Java:Random函数及其种子的作用

    伪随机(preundorandom):通过算法产生的随机数都是伪随机!! 只有通过真实的随机事件产生的随机数才是真随机!!比如,通过机器的硬件噪声产生随机数.通过大气噪声产生随机数 Random生成的 ...

  7. 安装和配置Sentry(收录)

    安装和配置Sentry 本文主要记录安装和配置Sentry的过程,关于Sentry的介绍,请参考 Apache Sentry架构介绍 . 1. 环境说明 系统环境: 操作系统:CentOs 6.6 H ...

  8. Ubuntu中搭建Hadoop集群(简记)

    stp1:在Vmware虚拟机上创建Ubantu.2环境 步骤:文件—>新建虚拟机—>典型(下一步)—>下一步——>位置(不建议放c盘,文件地址一定要全英文)—>下一步— ...

  9. Github上的1000多本免费电子书重磅来袭!

    Github上的1000多本免费电子书重磅来袭!   以前 StackOverFlow 也给出了一个免费电子书列表,现在在Github上可以看到时刻保持更新的列表了. 瞥一眼下面的书籍分类目录,你就能 ...

  10. spring boot 过滤器实现接收 压缩数据 并解压

    1.新加类GzipRequestWrapper 继承HttpServletRequestWrapper类 public class GzipRequestWrapper extends HttpSer ...