POJ 题目1204 Word Puzzles(AC自己主动机,多个方向查询)
Time Limit: 5000MS | Memory Limit: 65536K | |||
Total Submissions: 10244 | Accepted: 3864 | Special Judge |
Description
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
size C characters, contain the word puzzle. Then at last the W words are input one per line.
Output
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
题目大意:给你一个矩阵,然后问每一个串在矩阵中的起点和方向,方向从上(A)顺时针8个方向
就是也8个方向的查询就好。。
ac代码
#include<stdio.h>
#include<string.h>
char map[1010][1010],key[1010];
int dx[8]={-1,-1,0,1,1,1,0,-1};
int dy[8]={0,1,1,1,0,-1,-1,-1};
int ansx[1010],ansy[1010],ansd[1010],len[1010];
int head,tail;
int n,m,w;
struct node
{
node *fail;
node *next[26];
int id;
node()
{
fail=NULL;
id=0;
for(int i=0;i<26;i++)
next[i]=NULL;
}
}*q[50005000];
node *root;
void insert(char *s,int id)
{
int temp,len,i;
node *p=root;
len=strlen(s);
for(i=0;i<len;i++)
{
temp=s[i]-'A';
if(p->next[temp]==NULL)
p->next[temp]=new node();
p=p->next[temp];
}
p->id=id;
}
void build_ac()
{
head=tail=0;
q[tail++]=root;
while(head!=tail)
{
node *p=q[head++];
node *temp=NULL;
for(int i=0;i<26;i++)
{
if(p->next[i]!=NULL)
{
if(p==root)
p->next[i]->fail=root;
else
{
temp=p->fail;
while(temp!=NULL)
{
if(temp->next[i]!=NULL)
{
p->next[i]->fail=temp->next[i];
break;
}
temp=temp->fail;
}
if(temp==NULL)
{
p->next[i]->fail=root;
}
}
q[tail++]=p->next[i];
}
}
}
}
void query(int x,int y,int dir)
{
node *p=root,*temp;
int i,j;
for(i=x,j=y;i>=0&&i<n&&j>=0&&j<m;i+=dx[dir],j+=dy[dir])
{
int x=map[i][j]-'A';
while(p->next[x]==NULL&&p!=root)
p=p->fail;
p=p->next[x];
if(p==NULL)
{
p=root;
}
temp=p;
while(temp!=root&&temp->id!=-1)
{
int id=temp->id;
ansx[id]=i-(len[id]-1)*dx[dir];
ansy[id]=j-(len[id]-1)*dy[dir];
ansd[id]=dir;
temp->id=-1;
temp=temp->fail;
}
}
}
int main()
{
// int n,m,w;
while(scanf("%d%d%d",&n,&m,&w)!=EOF)
{
int i,j;
root=new node();
for(i=0;i<n;i++)
{
scanf("%s",map[i]);
}
for(i=1;i<=w;i++)
{
scanf("%s",key);
len[i]=strlen(key);
insert(key,i);
}
build_ac();
for(i=0;i<m;i++)
{
for(j=0;j<8;j++)
{
query(0,i,j);
query(n-1,i,j);
}
}
for(i=0;i<n;i++)
{
for(j=0;j<8;j++)
{
query(i,0,j);
query(i,m-1,j);
}
}
for(i=1;i<=w;i++)
{
printf("%d %d %c\n",ansx[i],ansy[i],ansd[i]+'A');
}
}
}
POJ 题目1204 Word Puzzles(AC自己主动机,多个方向查询)的更多相关文章
- poj 3691 DNA repair(AC自己主动机+dp)
DNA repair Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 5877 Accepted: 2760 Descri ...
- POJ 1204 Word Puzzles | AC 自动鸡
题目: 给一个字母矩阵和几个模式串,矩阵中的字符串可以有8个方向 输出每个模式串开头在矩阵中出现的坐标和这个串的方向 题解: 我们可以把模式串搞成AC自动机,然后枚举矩阵最外围一层的每个字母,向八个方 ...
- [poj] 1204 Word Puzzles || AC自动机
原题 给个X*Y的字符串矩阵,W个询问,对每个询问输出这个串出现的位置及方向,共有8个方向,顺时针开始分别用A~H表示 AC自动机的板子题. 对于待匹配串建一个自动机,然后从矩阵的四周分别沿八个方向跑 ...
- PKU 1204 Word Puzzles(AC自动机)
题目大意:原题链接 给定一个字符串矩阵和待查找的单词,可以朝8个不同的方向查找,输出待查找单词第一个字母在矩阵中出现的位置和该单词被查到的方向. A~H代表8个不同的方向,A代表正北方向,其他依次以4 ...
- 【POJ】1204 Word Puzzles
这道题目各种wa.首先是错了一个坐标,居然没测出来.然后是剪枝错误.搜索pen时就返回,可能还存在串pen*. #include <cstdio> #include <cstring ...
- POJ 3691 DNA repair 基于AC自己主动机DP
dp[i][j] 它表示的长度 i 下游前缀 j 更改节点的最小数量. 很清楚dp[0][0] = 0; dp[ i ][ j ] = min(dp[ i ][ j ],dp[i-1][k] + (j ...
- ZOJ 3494 BCD Code (AC自己主动机 + 数位DP)
题目链接:BCD Code 解析:n个病毒串.问给定区间上有多少个转换成BCD码后不包括病毒串的数. 很奇妙的题目. . 经典的 AC自己主动机 + 数位DP 的题目. 首先使用AC自己主动机,得到b ...
- [POJ 1204]Word Puzzles(Trie树暴搜&AC自己主动机)
Description Word puzzles are usually simple and very entertaining for all ages. They are so entertai ...
- poj 1699 Best Sequence(AC自己主动机+如压力DP)
id=1699" target="_blank" style="">题目链接:poj 1699 Best Sequence 题目大意:给定N个D ...
随机推荐
- 8. EVENTS
8. EVENTS EVENTS表提供有关事件管理器事件的信息,这将在"使用事件调度程序"中讨论. EVENTS表有以下列: - EVENT_CATALOG:事件所属目录的名称.这 ...
- 标量子查询SQL改写
一网友说下面sql跑的好慢,让我看看 sql代码: select er, cid, pid, tbl, zs, sy, (select count(sr.mobile_tele_no) from tb ...
- Go:channel
一.channel 在 Go 语言里,不仅可以使用原子函数和互斥锁来保证对共享资源的安全访问以及消除竞争状态,还可以使用 channel,通过发送和接收需要共享的资源,在 goroutine 之间做同 ...
- python3.6以上 asyncio模块的异步编程模型 async await语法
这是python3.6以上版本的用法,本例是python3.7.2编写使用asyncio模块的异步编程模型,生产这消费者,异步生产,用sleep来代替IO等待使用async和await语法来进行描述a ...
- JS模块之AMD, CMD, CommonJS、UMD和ES6模块
CommonJS 传送门 同步加载,适合服务器开发,node实现了commonJS.module.exports和require 判断commonJS环境的方式是(参考jquery源码): if ( ...
- 大数据学习——flume日志分类采集汇总
1. 案例场景 A.B两台日志服务机器实时生产日志主要类型为access.log.nginx.log.web.log 现在要求: 把A.B 机器中的access.log.nginx.log.web.l ...
- POJ3037 Skiing
Skiing 题目大意: 给定一个M*N的网格,已知在每个网格中的点可以向上下左右四个方向移动一个单位,每个点都有一个高度值. 从每个点开始移动时存在一个速度值,从A点移动到B点,则此时B点的速度为& ...
- hihocoder 1425What a Beautiful Lake(实验专用)
Problem D. What a Beautiful Lake Description Weiming Lake, also named "Un-named Lake", is ...
- idea web项目启动失败的情况---webapp文件夹路径不对,应如图位置
- SQLSERVER金额转换成英文大写的函数
CREATE FUNCTION [dbo].[f_num_eng] (@num numeric(15,2)) RETURNS varchar(400) WITH ENCRYPTION AS BEGIN ...