Word Puzzles
Time Limit: 5000MS   Memory Limit: 65536K
Total Submissions: 12090   Accepted: 4547   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

大致题意:给一个表格,再给w个单词,要求每个单词首字母在表格中出现的位置和延伸方向,有8种方向!
分析:因为给了很多串,那么把这些串都放到trie树里.每次暴力枚举所有位置和方向,dfs扩展,将经过的节点标记,非常暴力,但是可以过.
          考虑对这个算法优化,事实上不需要对每一个点都dfs扩展,只需要对第一列和最后一列以及第一行和最后一行上的每个点dfs一下就可以了,这样可以覆盖到每条对角线和行列.然后考虑到每次扩展实际上就是在trie上匹配字符串,如果失配了不走了,这样的话就需要搜索每个点.那么可以利用AC自动机来加速.利用fail指针,如果当前匹配的字符串下一位没有节点能与当前位置匹配,就跳到失配指针所指的位置上去.
#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的更多相关文章

  1. POJ1204 Word Puzzles(AC自动机)

    给一个L*C字符矩阵和W个字符串,问那些字符串出现在矩阵的位置,横竖斜八个向. 就是个多模式匹配的问题,直接AC自动机搞了,枚举字符矩阵八个方向的所有字符串构成主串,然后在W个模式串构造的AC自动机上 ...

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

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

  3. pku1204 Word Puzzles AC自动机 二维字符串矩阵8个方向找模式串的起点坐标以及方向 挺好的!

    /** 题目:pku1204 Word Puzzles 链接:http://poj.org/problem?id=1204 题意:给定一个L C(C <= 1000, L <= 1000) ...

  4. [POJ 1204]Word Puzzles(Trie树暴搜&amp;AC自己主动机)

    Description Word puzzles are usually simple and very entertaining for all ages. They are so entertai ...

  5. POJ 题目1204 Word Puzzles(AC自己主动机,多个方向查询)

    Word Puzzles Time Limit: 5000MS   Memory Limit: 65536K Total Submissions: 10244   Accepted: 3864   S ...

  6. POJ1204:Word Puzzles——题解

    http://poj.org/problem?id=1204 题目大意:给一个字母表,求一些字符串的开端第一次出现的位置和字符串的方向(字符串可以按照八个方向放在字母表中可匹配的位置) ——————— ...

  7. Word Puzzles

    poj1204:http://poj.org/problem?id=1204 题意:给你n*m的字符串矩阵,然后p个查询,每个查询会给出一个字符串,然后问你在矩阵中能否通过8个方向搜索到这个字符串,输 ...

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

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

  9. 【POJ】1204 Word Puzzles

    这道题目各种wa.首先是错了一个坐标,居然没测出来.然后是剪枝错误.搜索pen时就返回,可能还存在串pen*. #include <cstdio> #include <cstring ...

随机推荐

  1. Oracle 11g用exp无法导出空表的处理方法

    Oracle 11G在用EXPORT导出时,空表不能导出. 11G中有个新特性,当表无数据时,不分配segment,以节省空间 解决方法: 1.insert一行,再rollback就产生segment ...

  2. 冲刺ing-3

    第三次Scrum冲刺 队员完成的任务 队员 完成任务 吴伟华 分配任务,燃尽图 蔺皓雯 编写博客,美化主界面 蔡晨旸 美化主界面 曾茜 主页面设计 鲁婧楠 服务器建构 杨池宇 服务器建构 成员遇到的问 ...

  3. 02慕课网《vue.js2.5入门》——Vue中的组件,实现todolist

    TodoList功能开发 例子:输入字符,在列表中显示: 由于有v-for属性,<li>不会被渲染,它已经和数据绑定在一起,有数据来决定 input和button上都有事件监听器,inpu ...

  4. 一个整数N中的1的个数

    设计思想: 通过大量数据分解找规律 abcd 从d开始若d=0则count(1的个数)=左边的abc *d的位值(1.10.100..) 若等欲1则count=左边的abc*d的位值(1.10.100 ...

  5. ASLR/DEP绕过技术概览

    在经典的栈溢出模型中,通过覆盖函数的返回地址来达到控制程序执行流程(EIP寄存器),通常将返回地址覆盖为0x7FFA4512,这个地址是一条JMP ESP指令,在函数返回时就会跳转到这个地址去执行,也 ...

  6. 1014 我的C语言文法定义与C程序推导过程

    程序> -> <外部声明> | <程序> <外部声明> <外部声明> -> <函数定义> | <声明> < ...

  7. 简易四则运算生成程序——添加GUI支持

    项目成员:张金生     张政 工程地址: https://coding.net/u/jx8zjs/p/paperOne/git ssh://git@git.coding.net:jx8zjs/pap ...

  8. VSVC2010中常用的C++11特性

    static_assert 声明 static_assert 声明在编译时测试软件断言,这与在运行时进行测试的其他断言机制不同. 如果断言失败,则编译也将失败,且系统将发出指定的错误消息. const ...

  9. 这可能是目前最全的Redis高可用技术解决方案总结

    本文主要针对 Redis 常见的几种使用方式及其优缺点展开分析. 一.常见使用方式 Redis 的几种常见使用方式包括: Redis 单副本: Redis 多副本(主从): Redis Sentine ...

  10. php过滤字符串

    addslashes(); stripslashes(); //对数据库教程操作时,转义特殊字符 定义:addslashes() 函数在指定的预定义字符前添加反斜杠. 语法:addslashes(st ...