https://cn.vjudge.net/problem/POJ-1204
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
题目大意:给一个L*C字符矩阵和W个字符串,问那些字符串出现在矩阵的位置,横竖斜八个向。
枚举字符矩阵八个方向的所有字符串构成主串,然后在W个模式串构造的AC自动机上跑。
//#include<bits/stdc++.h>
#include<cstdio>
#include<cstring>
#include<queue>
using namespace std;
typedef long long ll;
const int maxn=2e6+10;
const int maxc=26;
char mp[1005][1005];
char s[1005];
int ans[1005][3];
int Len[1005];
bool vis[1005];
int dx[8]={0,1,0,-1,1,1,-1,-1};
int dy[8]={1,0,-1,0,-1,1,1,-1};
char dir[8]={'C','E','G','A','F','D','B','H'};
int l,c,w;
int root=1;
int tot=0;
struct TRIE{
int next[maxn][maxc],fail[maxn],num[maxn];
char base;
void init(){
for(int i=1;i<=tot;i++){
memset(next[i],0,sizeof(next[i]));
fail[i]=0;
num[i]=0;
}
tot=1;
}
void add(char s[],int id){
int len=strlen(s);
int now=root;
for(int i=0;i<len;i++){
int c=s[i]-base;
if(!next[now][c]) next[now][c]=++tot;
now=next[now][c];
}
num[now]=id;
}
void getfail(){
queue<int> q;
fail[root]=root;
for(int i=0;i<26;i++){
if(!next[root][i]) next[root][i]=root;
else{
fail[next[root][i]]=root;
q.push(next[root][i]);
}
}
while(!q.empty()){
int now=q.front();
q.pop();
for(int i=0;i<26;i++){
if(!next[now][i])
next[now][i]=next[fail[now]][i];
else{
fail[next[now][i]]=next[fail[now]][i];
q.push(next[now][i]);
}
}
}
}
void check(int x,int y,int d){
int now=root;
while(x>=0&&x<l&&y>=0&&y<c){
int c=mp[x][y]-base;
now=next[now][c];
int p=now;
while(p!=root){
if(num[p]&&!vis[num[p]]){
vis[num[p]]=true;
ans[num[p]][0]=x-dx[d]*(Len[num[p]]-1);
ans[num[p]][1]=y-dy[d]*(Len[num[p]]-1);
ans[num[p]][2]=d;
}
p=fail[p];
}
x+=dx[d];
y+=dy[d];
}
}
/*
void find(char s[]){//求每种模式串在主串中的数目
int len=strlen(s);
int now=root;
for(int i=0;i<len;i++){
int c=s[i]-base;
now=next[now][c];
int p=now;
while(p!=root){
if(num[p]) ans[num[p]]++;
p=fail[p];
}
}
}
*/
/*
void find(char s[]){//求主串中有哪几种模式串
cnt=0;
int now=root;
int len=strlen(s);
for(int i=0;i<len;i++){
int c=s[i]-base;
now=next[now][c];
int p=now;
while(p!=root){
if(num[p]) ans[cnt++]=num[p];
p=fail[p];
}
}
}
*/
}tr;
int main(){
scanf("%d%d%d",&l,&c,&w);
for(int i=0;i<l;i++) scanf("%s",mp[i]);
tr.init();
tr.base='A';
for(int i=1;i<=w;i++){
scanf("%s",s);
Len[i]=strlen(s);
tr.add(s,i);
}
tr.getfail();
for(int i=0;i<l;i++){
for(int j=0;j<8;j++){
tr.check(i,0,j);
}
}
for(int i=0;i<l;i++){
for(int j=0;j<8;j++){
tr.check(i,c-1,j);
}
}
for(int i=0;i<c;i++){
for(int j=0;j<8;j++){
tr.check(0,i,j);
}
}
for(int i=0;i<c;i++){
for(int j=0;j<8;j++){
tr.check(l-1,i,j);
}
}
for(int i=1;i<=w;i++)
printf("%d %d %c\n",ans[i][0],ans[i][1],dir[ans[i][2]]);
return 0;
}