[poj] 1204 Word Puzzles || AC自动机
原题
给个X*Y的字符串矩阵,W个询问,对每个询问输出这个串出现的位置及方向,共有8个方向,顺时针开始分别用A~H表示
AC自动机的板子题。
对于待匹配串建一个自动机,然后从矩阵的四周分别沿八个方向跑,找到一个串的时候就记录答案,但不能退出。
另:方向要找好啊!!
#include<cstdio>
#include<cstring>
#define N 1010
using namespace std;
int x,y,n,ansx[N],ansy[N],d[N],l[N];
//int dx[10]={0,0,1,1,1,0,-1,-1,-1},dy[10]={0,1,1,0,-1,-1,-1,0,1};
int dx[10]={0,-1,-1,0,1,1,1,0,-1},dy[10]={0,0,1,1,1,0,-1,-1,-1};
char t[N][N],s[N],r[10]=" ABCDEFGH";
struct hhh
{
int data;
hhh *child[26],*fail;
hhh()
{
fail=NULL;
data=0;
for (int i=0;i<26;i++) child[i]=NULL;
}
};
hhh *root=new hhh;
hhh *now,*tmp;
hhh* q[100*1000+10];
void add(int x)
{
now=root;
for (int i=1;i<=l[x];i++)
{
if (now->child[s[i]-'A']==NULL)
now->child[s[i]-'A']=new hhh;
now=now->child[s[i]-'A'];
}
now->data=x;
}
void bfs()
{
int l=0,r=0;
root->fail=root;
for (int i=0;i<26;i++)
{
if (root->child[i]==NULL)
root->child[i]=root;
else
root->child[i]->fail=root,q[r++]=(root->child[i]);
}
while (r!=l)
{
hhh* nw=q[l++];
for (int i=0;i<26;i++)
{
if (nw->child[i])
{
now=nw->child[i];
if (now!=root) q[r++]=now;
if (now->fail==NULL) now->fail=nw->fail->child[i];
}
else nw->child[i]=nw->fail->child[i];
if (nw->data==0 && nw->fail->data)
nw->data=nw->fail->data;
}
}
}
void find(int a,int b,int i)
{
now=root;
while (1)
{
if (a<=0 || a>x) break;
if (b<=0 || b>y) break;
now=now->child[t[a][b]-'A'];
tmp=now;
while (tmp->data)
{
int tp=tmp->data;
ansx[tp]=a-dx[i]*(l[tp]-1);
ansy[tp]=b-dy[i]*(l[tp]-1);
d[tp]=i;
tmp=tmp->fail;
}
a+=dx[i];
b+=dy[i];
}
}
int main()
{
scanf("%d%d%d",&x,&y,&n);
for (int i=1;i<=x;i++)
scanf("%s",t[i]+1);
for (int i=1;i<=n;i++)
{
scanf("%s",s+1);
l[i]=strlen(s+1);
add(i);
}
bfs();
for (int i=1;i<=8;i++)
{
for (int j=1;j<=x;j++)
{
find(j,1,i);
find(j,y,i);
}
for (int j=1;j<=y;j++)
{
find(1,j,i);
find(x,j,i);
}
}
for (int i=1;i<=n;i++)
printf("%d %d %c\n",ansx[i]-1,ansy[i]-1,r[d[i]]);
return 0;
}
[poj] 1204 Word Puzzles || AC自动机的更多相关文章
- PKU 1204 Word Puzzles(AC自动机)
题目大意:原题链接 给定一个字符串矩阵和待查找的单词,可以朝8个不同的方向查找,输出待查找单词第一个字母在矩阵中出现的位置和该单词被查到的方向. A~H代表8个不同的方向,A代表正北方向,其他依次以4 ...
- POJ 1204 Word Puzzles | AC 自动鸡
题目: 给一个字母矩阵和几个模式串,矩阵中的字符串可以有8个方向 输出每个模式串开头在矩阵中出现的坐标和这个串的方向 题解: 我们可以把模式串搞成AC自动机,然后枚举矩阵最外围一层的每个字母,向八个方 ...
- 【 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 ...
- pku1204 Word Puzzles AC自动机 二维字符串矩阵8个方向找模式串的起点坐标以及方向 挺好的!
/** 题目:pku1204 Word Puzzles 链接:http://poj.org/problem?id=1204 题意:给定一个L C(C <= 1000, L <= 1000) ...
- POJ 1204 Word Puzzles(AC自动机)
这题的数据卡在,如下: 5 5 3 ABCDE FGHIJ KLMNO PQRST UVWXY PQR RS RST puzzle中间的行中可以包含要查询的多个单词.这个问题很好解决,SearchDf ...
- poj 1204 Word Puzzles(字典树)
题目链接:http://poj.org/problem?id=1204 思路分析:由于题目数据较弱,使用暴力搜索:对于所有查找的单词建立一棵字典树,在图中的每个坐标,往8个方向搜索查找即可: 需要注意 ...
- POJ 题目1204 Word Puzzles(AC自己主动机,多个方向查询)
Word Puzzles Time Limit: 5000MS Memory Limit: 65536K Total Submissions: 10244 Accepted: 3864 S ...
- poj 2778 DNA Sequence ac自动机+矩阵快速幂
链接:http://poj.org/problem?id=2778 题意:给定不超过10串,每串长度不超过10的灾难基因:问在之后给定的长度不超过2e9的基因长度中不包含灾难基因的基因有多少中? DN ...
随机推荐
- JavaScript中并非一切皆对象
对象是js中的基础以及核心,在js中有六种主要类型:string number boolean null undefined object 除了oject类型以为其他五种本身并非对象,null本身 ...
- npm run build根据不同参数打包不同环境url
config文件夹下 dev.env.js中修改代码 'use strict' const merge = require('webpack-merge') const prodEnv = requi ...
- MySQL5.6基于mysql-proxy实现读写分离
已经搭建好MySQL主从架构 10.205.22.185 #mysql-proxy 10.205.22.186 #master 10.205.22.187 #slave 1.安装mysql-proxy ...
- Linux帮助都有哪几种,如何使用?
帮助文件有2类,内置命令和外部命令. 其中内置命令就是shell内核自带的,因为shell当中自己要进行管理,那么就需要一些命令进行管理,不同的shell肯定有不同的shell命令,我们用type命令 ...
- 如何解决mysql中读取含表情符号的内容无法识别的问题
当内容中包含有表情符号的时候,写入mysql时一般会设置字段或者表的charset为utf8mb4的形式: ) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicod ...
- 课时16.HTML-XHTML-HTML5区别(了解)
简而言之 HTML语法非常宽松容错性强: XHTML更为严格,它要求标签必须小写,必须严格闭合,标签中的属性必须使用引号引起等等. HTML5是HTML的下一个版本所以除了非常宽松容错性强以外,还增加 ...
- laravel通过make auth实现手机号登录
首先按照Laravel的教程,安装认证系统. php artisan make:auth php artisan migrate laravel已经安装完成认证系统,默认注册和登录都是用邮箱. 如果想 ...
- POJ:3185-The Water Bowls(枚举反转)
The Water Bowls Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 7402 Accepted: 2927 Descr ...
- Leetcode 538. 把二叉搜索树转换为累加树
题目链接 https://leetcode.com/problems/convert-bst-to-greater-tree/description/ 题目描述 大于它的节点值之和. 例如: 输入: ...
- 零基础学css第二天
内边距与外边距: <!DOCTYPE html> <html> <head> <title></title> <style type= ...