poj 1204 Word Puzzles(字典树)
题目链接:http://poj.org/problem?id=1204
思路分析:由于题目数据较弱,使用暴力搜索;对于所有查找的单词建立一棵字典树,在图中的每个坐标,往8个方向搜索查找即可;
需要注意的是查找时不能匹配了一个单词就不在继续往该方向查找,因为在某个坐标的某个方向上可能会匹配多个单词,所以需要一直
查找直到查找到该方向上最后一个坐标;
代码如下:
#include <cstdio>
#include <cstring>
#include <iostream>
using namespace std; const int KIND = ;
const int MAX_N = + ;
int row, column, num_word;
char map[MAX_N][MAX_N], words[MAX_N];
int dir[][] = {-, , -, , , , , , , , , -, , -, -, -};
int pos[MAX_N][]; struct Node {
Node *next[KIND];
bool end;
int order;
Node()
{
memset(next, , sizeof(next));
end = false;
order = ;
}
}; void CreateTrie(Node *root, char *str, int j)
{
int i = ;
Node *p = root; while (str[i]) {
int value = str[i] - 'A';
if (p->next[value] == NULL)
p->next[value] = new Node();
p = p->next[value];
++i;
}
p->end = true;
p->order = j;
} void Search(Node *root, int i, int j, int k)
{
Node *p = root;
int pos_i = i, pos_j = j, value = ; while (p && <= pos_i && pos_i < row && <= pos_j && pos_j < column) {
value = map[pos_i][pos_j] - 'A';
if (p->next[value]) {
if (p->next[value]->end) {
pos[p->next[value]->order][] = i;
pos[p->next[value]->order][] = j;
pos[p->next[value]->order][] = k;
}
pos_i += dir[k][];
pos_j += dir[k][];
p = p->next[value];
} else
break;
}
} int main()
{
while (scanf("%d %d %d\n", &row, &column, &num_word) != EOF) {
Node *root = new Node; for (int i = ; i < row; ++i) {
for (int j = ; j < column; ++j)
scanf("%c", &map[i][j]);
scanf("\n");
}
for (int i = ; i < num_word; ++i) {
scanf("%s", words);
CreateTrie(root, words, i);
}
for (int i = ; i < row; ++i)
for (int j = ; j < column; ++j)
for (int k = ; k < ; ++k)
Search(root, i, j, k);
for (int i = ; i < num_word; ++i)
printf("%d %d %c\n", pos[i][], pos[i][], pos[i][] + 'A');
}
return ;
}
poj 1204 Word Puzzles(字典树)的更多相关文章
- [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】(Trie+爆搜|AC自动机)
Word Puzzles Time Limit: 5000MS Memory Limit: 65536K Total Submissions: 10782 Accepted: 4076 Special ...
- [poj] 1204 Word Puzzles || AC自动机
原题 给个X*Y的字符串矩阵,W个询问,对每个询问输出这个串出现的位置及方向,共有8个方向,顺时针开始分别用A~H表示 AC自动机的板子题. 对于待匹配串建一个自动机,然后从矩阵的四周分别沿八个方向跑 ...
- POJ 1204 Word Puzzles | AC 自动鸡
题目: 给一个字母矩阵和几个模式串,矩阵中的字符串可以有8个方向 输出每个模式串开头在矩阵中出现的坐标和这个串的方向 题解: 我们可以把模式串搞成AC自动机,然后枚举矩阵最外围一层的每个字母,向八个方 ...
- POJ 1204 Word Puzzles(AC自动机)
这题的数据卡在,如下: 5 5 3 ABCDE FGHIJ KLMNO PQRST UVWXY PQR RS RST puzzle中间的行中可以包含要查询的多个单词.这个问题很好解决,SearchDf ...
- POJ 2001 Shortest Prefixes(字典树)
题目地址:POJ 2001 考察的字典树,利用的是建树时将每个点仅仅要走过就累加.最后从根节点開始遍历,当遍历到仅仅有1次走过的时候,就说明这个地方是最短的独立前缀.然后记录下长度,输出就可以. 代码 ...
- POJ 题目1204 Word Puzzles(AC自己主动机,多个方向查询)
Word Puzzles Time Limit: 5000MS Memory Limit: 65536K Total Submissions: 10244 Accepted: 3864 S ...
- POJ 2408 - Anagram Groups - [字典树]
题目链接:http://poj.org/problem?id=2408 World-renowned Prof. A. N. Agram's current research deals with l ...
- POJ 1816 - Wild Words - [字典树+DFS]
题目链接: http://poj.org/problem?id=1816 http://bailian.openjudge.cn/practice/1816?lang=en_US Time Limit ...
随机推荐
- ##DAY15——UICollectionView
DAY15——UICollectionView 创建UICollectionView //创建一个布局对象,采用系统布局类UICollectionViewFlowLayout UICollection ...
- 善于 调用Windows API
前一段时间看见别人做的一个自动填写信息并且点击登录的程序,觉得很有意思. 其实就是在程序中调用Windows的API,那么如何调用,下面就做个简单的介绍. 写的简单粗暴, 不喜轻喷. 0.首先引入名称 ...
- socket中的option
/// Set an option on the socket. /** * This function is used to set an option on the socket. * * @pa ...
- leetcode Roman to Integer python
class Solution(object): def romanToInt(self, s): """ :type s: str :rtype: int "& ...
- 小测试 php代理,nginx代理,直接访问对比
#php proxy total sent request num: 507 total handle read times: 506 506 fetches, 2 max parallel, 2.7 ...
- java——多线程——单例模式的static方法和非static方法是否是线程安全的?
单例模式的static方法和非static方法是否是线程安全的? 答案是:单例模式的static方法和非static方法是否是线程安全的,与单例模式无关.也就说,如果static方法或者非static ...
- Netty4.0学习教程
http://blog.csdn.net/u013252773/article/details/21046697 一些属性和方法介绍 http://blog.csdn.net/zxhoo/articl ...
- 老男孩python第六期
01 python s6 day7 上节回顾02 python s6 day7 SNMP使用03 python s6 day7 大型监控架构讲解04 python s6 day7 Redis使用05 ...
- js中if的简写方法
http://transitions1020.com/# 太帅! <script type="text/javascript"> 如果你想写 if (!false) { ...
- C#中窗体的一些简单运用(Sixteenth Day)
从今天开始,我们进入到学window form的知识,今天简单的学习了一些控件和事件的运用.没有什么很全面的理论,所以今天就总结下所写的程序.一个简单的注册页面程序 注册页面程序 要求: 1:修改所有 ...