poj 1204
http://poj.org/problem?id=1204
大意:给出一个棋盘puzzle,,和w个模式串,,在棋盘中寻找模式串。。棋盘中任意一格有8个方向可以走。。
解题思路: AC自动机 + 枚举8个方向即可
get_trie(),get_ac().直接用模版。。。。
query 有一点小技巧。。需要枚举8个方向,,对于一行,我们对竖直方向上的枚举可以省略,因为我们在竖直方向上枚举时,会补充上。。
另外 我们无需枚举一行中的每一个点,,只需第一个即可,因为,,我们在一个方向上枚举时,,会包含到后面的。
#include <iostream>
#include<cstring>
using namespace std;
struct point{
point *fail;
point *next[];
bool istail;//是否为单词结尾
int id;//单词的输入顺序
point(){ //初始化
istail =false;
fail = NULL;
memset(next,NULL,sizeof(next));
}
}*q[];//bfs 获得fail 用 struct pos{
int x,y,dis;
};
pos ans[];
point *root = NULL;
int l,c,w;
char map[][];//puzzle
char str[];//模版串
int len[];//记录每个摸版的长度
int tail,head;
int dix[][] = {-,,-,,,,,,,,,-,,-,-,-};//顺时针,,上下左右。。。八个方向
char res[]={"ABCDEFGH"};
void build_trie(char *str,int id){
point *p = root;
int i=,index;
while(str[i]){
index = str[i]-'A';
if(p->next[index]==NULL) p->next[index] = new point();
p = p->next[index];
i++;
}
p->istail = true;
p->id = id;
}
void get_ac(){
int i;
root->fail = NULL;
q[head++] = root;
while(head!=tail){
point *temp = q[tail++];
point *p =NULL;
for(i=;i<;i++){
if(temp->next[i]!=NULL){
if(temp==root) temp->next[i]->fail=root;
else{
p = temp->fail;
while(p!=NULL){
if(p->next[i]!=NULL){
temp->next[i]->fail = p->next[i];
break;
}
p = p->fail;
}
if(p==NULL) temp->next[i]->fail = root;
}
q[head++] = temp->next[i];
}
}
}
} void query(int x,int y,int i){
int index;
point *p,*qq;
p = root;
for(;map[x][y];x+=dix[i][],y+=dix[i][]){
index = map[x][y]-'A';
while(p->next[index]==NULL&&p!=root) p = p->fail;
p = p->next[index];
if(p==NULL)
p = root;
qq= p;
while(qq!=root){
if(qq->istail){
qq->istail = false;
ans[qq->id].x = x-dix[i][]*(len[qq->id]-)-;//从1开始记得数
ans[qq->id].y = y-dix[i][]*(len[qq->id]-);
ans[qq->id].dis = i;
}
qq = qq->fail;
}
}
}
int main()
{
root = new point();
cin>>l>>c>>w;
for(int i=;i<=l;i++)
cin>>map[i];
for(int i=;i<w;i++){
cin>>str;
len[i] = strlen(str);
build_trie(str,i);
}
head = tail =;
get_ac();
for(int i=;i<=l;i++){//枚举水平
query(i,,),query(i,,),query(i,,);//一行只需枚举开头和结尾即可,一列同样
query(i,c-,),query(i,c-,),query(i,c-,);
}
for(int i=;i<c;i++){//枚举竖直
query(,i,),query(,i,),query(,i,);
query(l,i,),query(l,i,),query(l,i,);
} for(int i=;i<w;i++){
cout<<ans[i].x<<" "<<ans[i].y<<" "<<res[ans[i].dis]<<endl;
}
return ;
}
poj 1204的更多相关文章
- poj 1204 Word Puzzles(字典树)
题目链接:http://poj.org/problem?id=1204 思路分析:由于题目数据较弱,使用暴力搜索:对于所有查找的单词建立一棵字典树,在图中的每个坐标,往8个方向搜索查找即可: 需要注意 ...
- 【 POJ - 1204 Word Puzzles】(Trie+爆搜|AC自动机)
Word Puzzles Time Limit: 5000MS Memory Limit: 65536K Total Submissions: 10782 Accepted: 4076 Special ...
- [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自动机
原题 给个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题目分类(按初级\中级\高级等分类,有助于大家根据个人情况学习)
本文来自:http://www.cppblog.com/snowshine09/archive/2011/08/02/152272.spx 多版本的POJ分类 流传最广的一种分类: 初期: 一.基本算 ...
- [转] POJ字符串分类
POJ 1002 - 487-3279(基础)http://acm.pku.edu.cn/JudgeOnline/problem?id=1002题意:略解法:二叉查找数,map,快排... POJ 1 ...
- ACM 字符串 题目整理
AC自动机 UVa 11468 Substring AC自动机+概率DP. 注意要补全不存在的边. 为什么要补全不存在的边呢?补全以后可以直接找到状态的转移,即从所有子节点就可以实现所有状态转移. ...
随机推荐
- elk 日志处理的一点思路
zjtest7-frontend:/usr/local/logstash-2.3.4/bin# ./logstash -f ../config/logstash_agent.conf zjtest7- ...
- perl 爬取同花顺数据
use LWP::UserAgent; use utf8; use DBI; $user="root"; $passwd='xxx'; $dbh=""; $db ...
- 数据库 BUG:Illegal mix of collations (utf8_unicode_ci,IMPLICIT) and (utf8_general_ci,IMPLICIT) for operation '=
在mysql5中遇到的问题: Illegal mix of collations (utf8_general_ci,IMPLICIT) and (utf8_unicode_ci,IMPLICIT) f ...
- document.domain - JavaScript的同源策略问题:错误信息:Permission denied to access property 'document'_eecc00_百度空间
document.domain - JavaScript的同源策略问题:错误信息:Permission denied to access property 'document'_eecc00_百度空间 ...
- UVA - 1103Ancient Messages(dfs)
UVA - 1103Ancient Messages In order to understand early civilizations, archaeologists often study te ...
- 模式匹配-KMP算法
/***字符串匹配算法***/ #include<cstring> #include<iostream> using namespace std; #define OK 1 # ...
- unix more命令
[语法]: more [-cdflrsuw] [- 行数] [+ 行数] [+ / 模式 ] [ 文件 ... ] [说明]: 将文件显示在终端上.每次一屏,在左下部显示 --more--.若是 ...
- Debug目录下没有.exe文件
记一下小笔记: VC6.0设置.exe文件的输出路径: Project->Settings->Link Category选择"General" 在Output file ...
- Linux/UNIX进程控制(1)
进程控制(1) 进程标识符 每一个进程都有肺腑的整形表示唯一的进程ID.按一个进程终止后,其进程ID就能够再次使用了.例如以下是几个典型进程的ID及其类型和功能. ID 进程名 ...
- subversion和客户端的应用
1.安装svn的服务器端subversion.以及windows客户端TortoiseSVN: 2 cmd 建立库,名字为svnpro ----- svnadmin create F:\svnpro, ...