http://poj.org/problem?id=1204

题目大意:给一个字母表,求一些字符串的开端第一次出现的位置和字符串的方向(字符串可以按照八个方向放在字母表中可匹配的位置)

————————————————————————————————

一定是AC自动机,而且我们不可能对二位字母表AC一下,所以我们要把待匹配串AC一下,然后枚举字母表的起点(不要枚举多了),ACcheck一下就好了,蛮裸的。

为了保证最小序,需要对枚举顺序改一下,具体循环方法我piao了这位大佬的博客

#include<cstdio>
#include<cmath>
#include<algorithm>
#include<cstring>
#include<iostream>
#include<queue>
using namespace std;
const int L=;
const int C=;
const int M=;
const int W=;
int dx[]={,,,-,,,-,-};
int dy[]={,,-,,-,,,-};
char dir[]={'C','E','G','A','F','D','B','H'};
struct trie{
int ed;
int a[];
int fail;
int l;
}tree[C*L]={};
char mp[L][C],s[M];
int ans[W][];
bool vis[W];
int cnt=,l,c,w;
inline void insert(int k){
int now=;
int len=strlen(s);
for(int i=;i<len;i++){
int t=s[i]-'A';
if(!tree[now].a[t]){
cnt++;
tree[now].a[t]=cnt;
}
now=tree[now].a[t];
}
tree[now].ed=k;
tree[now].l=len;
return;
}
int q[C*L];
void getfail(){
int r=;
//以下是对第一层的特殊处理
for(int i=;i<;i++){
if(tree[].a[i]!=){
tree[tree[].a[i]].fail=;
r++;q[r]=tree[].a[i];
}
}
for(int l=;l<=r;l++){
int u=q[l];
for(int i=;i<;i++){
if(tree[u].a[i]!=){
tree[tree[u].a[i]].fail=tree[tree[u].fail].a[i];
r++;q[r]=tree[u].a[i];
}else{
tree[u].a[i]=tree[tree[u].fail].a[i];
}
}
}
return;
}
void check(int x,int y,int d){
int now=;
while(x>=&&x<l&&y>=&&y<c){
int t=mp[x][y]-'A';
now=tree[now].a[t];
for(int j=now;j;j=tree[j].fail){
int k=tree[j].ed;
int len=tree[j].l-;
if(k&&!vis[k]){
vis[k]=;
ans[k][]=x-dx[d]*len;
ans[k][]=y-dy[d]*len;
ans[k][]=d;
}
}
x+=dx[d];
y+=dy[d];
}
return;
}
int main(){
cin>>l>>c>>w;
for(int i=;i<l;i++){
cin>>mp[i];
}
for(int i=;i<=w;i++){
cin>>s;
insert(i);
}
getfail();
for(int i=;i<l;i++)
for(int j=;j<;j++)
check(i,,j);
for(int i=;i<l;i++)
for(int j=;j<;j++)
check(i,c-,j);
for(int i=;i<c;i++)
for(int j=;j<;j++)
check(,i,j);
for(int i=;i<c;i++)
for(int j=;j<;j++)
check(l-,i,j);
for(int i=;i<=w;i++)
printf("%d %d %c\n",ans[i][],ans[i][],dir[ans[i][]]);
return ;
}

POJ1204:Word Puzzles——题解的更多相关文章

  1. poj1204 Word Puzzles

    Word Puzzles Time Limit: 5000MS   Memory Limit: 65536K Total Submissions: 12090   Accepted: 4547   S ...

  2. POJ1204 Word Puzzles(AC自动机)

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

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

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

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

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

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

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

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

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

  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 | AC 自动鸡

    题目: 给一个字母矩阵和几个模式串,矩阵中的字符串可以有8个方向 输出每个模式串开头在矩阵中出现的坐标和这个串的方向 题解: 我们可以把模式串搞成AC自动机,然后枚举矩阵最外围一层的每个字母,向八个方 ...

随机推荐

  1. What is the "internal" interface and port for on Openvswitch?

    转:https://ask.openstack.org/en/question/4276/what-is-the-internal-interface-and-port-for-on-openvswi ...

  2. Android事件分发机制浅析(1)

    本文来自网易云社区 作者:孙有军 事件机制是Android中一个比较复杂且重要的知识点,比如你想自定义拦截事件,或者某系组件中嵌套了其他布局,往往会出现这样那样的事件冲突,坑爹啊!!事件主要涵盖onT ...

  3. EF SQLite的Like语句,生成为CHARINDEX的解决办法

    在使用EF SQLite的时候发现Like语句不能完全查询出来,看了下生成的SQL语句类似于这种 (CHARINDEX(@p__linq__2, [Extent1].[LeagueName])) &g ...

  4. 2019年猪年海报PSD模板-第六部分

    14套精美猪年海报,免费猪年海报,下载地址:百度网盘,https://pan.baidu.com/s/1WdlIiIdj1VVWxI4je0ebKw            

  5. java 前后端 日期转换

    1.前传后 @DateTimeFormat(pattern="yyyy-MM-dd") private Date payTime; 2.后传前 @JsonFormat(patter ...

  6. Python搭配unittest

    unittest是Python的单元测试框架, 类似于Java里面的TestNG. Unittest.py: import unittest from selenium import webdrive ...

  7. Navicat 导入sql脚本文件

    Navicat 导入sql脚本文件 我在组建自己工作用的数据库时要导入.sql脚本文件,用cmd窗口导入太慢,navicat的导入向导里又无导入sql脚本的选项, 但不是navicat中没有导入sql ...

  8. 阿里云ECS下Ubuntu 16.04系统安装python3.6.5 环境并设置为默认

    一.添加python3.6安装包并安装: 二.修改系统默认python版本为3.6: 三.安装并升级pip版本: 一.添加python3.6安装包并安装: sudo apt-get install s ...

  9. 166. Nth to Last Node in List

    Description Find the nth to last element of a singly linked list. The minimum number of nodes in lis ...

  10. 数数字 (Digit Counting,ACM/ICPC Dannang 2007 ,UVa1225)

    题目描述:算法竞赛入门经典习题3-3 #include <stdio.h> #include <string.h> int main(int argc, char *argv[ ...