poj1204 Word Puzzles
Time Limit: 5000MS | Memory Limit: 65536K | |||
Total Submissions: 12090 | Accepted: 4547 | Special Judge |
Description
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
Output
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
#include <cstdio>
#include <queue>
#include <cstring>
#include <iostream>
#include <algorithm> using namespace std; int l, c, tot = , w,ans[][], len[];
char s[][], s2[],dir[]; int dx[] = { -, -, , , , , , - };
int dy[] = { , , , , , -, -, - }; struct node
{
int tr[], id, fail;
}e[ * ]; void insert(char *ss,int x)
{
int len = strlen(ss);
int u = ;
for (int i = ; i < len; i++)
{
int t = ss[i] - 'A';
if (!e[u].tr[t])
e[u].tr[t] = ++tot;
u = e[u].tr[t];
}
e[u].id = x;
} void build()
{
for (int i = ; i < ; i++)
e[].tr[i] = ;
queue <int> q;
q.push();
while (!q.empty())
{
int u = q.front();
q.pop();
int fail = e[u].fail;
for (int i = ; i < ; i++)
{
int y = e[u].tr[i];
if (y)
{
e[y].fail = e[fail].tr[i];
q.push(y);
}
else
e[u].tr[i] = e[fail].tr[i];
}
}
} void solve(int sx, int sy, int dirr)
{
int x = sx, y = sy, u = ;
while (x >= && x <= l && y >= && y <= c)
{
while (u && !e[u].tr[s[x][y] - 'A'])
u = e[u].fail;
u = e[u].tr[s[x][y] - 'A'];
if (e[u].id)
{
ans[e[u].id][] = x - (len[e[u].id] - ) * dx[dirr];
ans[e[u].id][] = y - (len[e[u].id] - ) * dy[dirr];
dir[e[u].id] = dirr;
}
x += dx[dirr];
y += dy[dirr];
}
} int main()
{
scanf("%d%d%d", &l, &c, &w);
for (int i = ; i <= l; i++)
scanf("%s", s[i] + );
for (int i = ; i <= w; i++)
{
scanf("%s", s2);
len[i] = strlen(s2);
insert(s2,i);
}
build();
for (int i = ; i <= l; i++)
for (int j = ; j < ; j++)
solve(i, , j), solve(i, c, j);
for (int i = ; i <= c; i++)
for (int j = ; j < ; j++)
solve(, i, j), solve(l, i, j);
for (int i = ; i <= w; i++)
printf("%d %d %c\n", ans[i][] - , ans[i][] - , dir[i] + 'A'); return ;
}
poj1204 Word Puzzles的更多相关文章
- POJ1204 Word Puzzles(AC自动机)
给一个L*C字符矩阵和W个字符串,问那些字符串出现在矩阵的位置,横竖斜八个向. 就是个多模式匹配的问题,直接AC自动机搞了,枚举字符矩阵八个方向的所有字符串构成主串,然后在W个模式串构造的AC自动机上 ...
- 【 POJ - 1204 Word Puzzles】(Trie+爆搜|AC自动机)
Word Puzzles Time Limit: 5000MS Memory Limit: 65536K Total Submissions: 10782 Accepted: 4076 Special ...
- pku1204 Word Puzzles AC自动机 二维字符串矩阵8个方向找模式串的起点坐标以及方向 挺好的!
/** 题目:pku1204 Word Puzzles 链接:http://poj.org/problem?id=1204 题意:给定一个L C(C <= 1000, L <= 1000) ...
- [POJ 1204]Word Puzzles(Trie树暴搜&AC自己主动机)
Description Word puzzles are usually simple and very entertaining for all ages. They are so entertai ...
- POJ 题目1204 Word Puzzles(AC自己主动机,多个方向查询)
Word Puzzles Time Limit: 5000MS Memory Limit: 65536K Total Submissions: 10244 Accepted: 3864 S ...
- POJ1204:Word Puzzles——题解
http://poj.org/problem?id=1204 题目大意:给一个字母表,求一些字符串的开端第一次出现的位置和字符串的方向(字符串可以按照八个方向放在字母表中可匹配的位置) ——————— ...
- Word Puzzles
poj1204:http://poj.org/problem?id=1204 题意:给你n*m的字符串矩阵,然后p个查询,每个查询会给出一个字符串,然后问你在矩阵中能否通过8个方向搜索到这个字符串,输 ...
- PKU 1204 Word Puzzles(AC自动机)
题目大意:原题链接 给定一个字符串矩阵和待查找的单词,可以朝8个不同的方向查找,输出待查找单词第一个字母在矩阵中出现的位置和该单词被查到的方向. A~H代表8个不同的方向,A代表正北方向,其他依次以4 ...
- 【POJ】1204 Word Puzzles
这道题目各种wa.首先是错了一个坐标,居然没测出来.然后是剪枝错误.搜索pen时就返回,可能还存在串pen*. #include <cstdio> #include <cstring ...
随机推荐
- 建立 Python 虚拟环境
1.1 安装依赖包 $ yum -y install wget gcc epel-release git 1.2 安装 Python3.6和pip $ yum -y install python36 ...
- 袋鼠云旗下新公司云掣科技启航,深耕云MSP业务助推企业数字化转型
1983年3月15日,国际消费者联盟组织将3月15日确立为国际消费者权益日. 2019年3月15日,袋鼠云举办三周年年会. 一生二,二生三,三生万物.植树节后,万物生长. 年会现场,袋鼠云宣布成立新公 ...
- Python基础灬序列(字符串、列表、元组)
序列 序列是指它的成员都是有序排列,并且可以通过下标偏移量访问到它的一个或几个成员.序列包含字符串.列表.元组. 字符串 chinese_zodiac = '鼠牛虎兔龙蛇马羊猴鸡狗猪' print(c ...
- Literature Books
Lean In (Sheryl Sandberg) Option B (Sheryl Sandberg) Ready Player One
- 【Alpha】阶段第二次Scrum Meeting
[Alpha]阶段第二次Scrum Meeting 工作情况 团队成员 今日已完成任务 明日待完成任务 刘峻辰 发表评论接口 更新评论接口 赵智源 部署实际项目 编写脚本实现持续集成 肖萌威 编写注册 ...
- 每日scrum--No.1
Yesterday:无 Today: 查找学校的官方地图和亲自测试其准确性 Problem :不能使地图适应我们的软件;未解决地图上存在的问题: 明天继续加油.
- 在windows和unbuntu上安装octave
windows安装octave 安装wiki Octave ftp库 从上述的库中可以找到对应的版本的octave的exe安装程序,或者是zip等的压缩包,建议直接下载对应系统的exe执行文件.安装. ...
- lintcode-201-线段树的构造
201-线段树的构造 线段树是一棵二叉树,他的每个节点包含了两个额外的属性start和end用于表示该节点所代表的区间.start和end都是整数,并按照如下的方式赋值: 根节点的 start 和 e ...
- J2EE面试常见试题
一.基础问答 1.下面哪些类可以被继承? java.lang.Thread (T) java.lang.Number (T) java.lang.Double (F) java.lang.Math ...
- JDK源码分析 – HashMap
HashMap类的申明 HashMap的定义如下: public class HashMap<K,V> extends AbstractMap<K,V> implements ...