zoukankan      html  css  js  c++  java
  • POJ1204 Word Puzzle(AC自动机)

    传送门

    Word Puzzles
    Time Limit: 5000MS   Memory Limit: 65536K
    Total Submissions:    Accepted:    Special Judge

    Description

    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

    Source

     
    AC自动机模板题。。写外围的扫描比较烦。。
     1 #include <set>
     2 #include <queue>
     3 #include <vector>
     4 #include <cstdio>
     5 #include <cstdlib>
     6 #include <cstring>
     7 #include <iostream>
     8 #include <algorithm>
     9 using namespace std;
    10 const int N = 1010;
    11 const int SONS = 26;
    12 const int dx[8]={-1,-1,0,1,1,1,0,-1},
    13           dy[8]={0,1,1,1,0,-1,-1,-1};
    14 #define For(i,n) for(int i=1;i<=n;i++)
    15 #define For0(i,n) for(int i=0;i<n;i++)
    16 #define Rep(i,l,r) for(int i=l;i<=r;i++)
    17 #define debug puts("TTTTTTTT________TTTTTTTT");
    18 
    19 pair< pair<int,int>,char> ans[N];
    20 int n,m,k,Len[N];
    21 char word[N][N],st[N];
    22 struct trie{
    23     int sz,ch[N*N][SONS],Loc[N*N],Q[N*N],next[N*N];
    24     trie(){sz=0;}
    25     void insert(char st[],int locs){
    26         int cur=0,len=strlen(st);
    27         For0(i,len){
    28             int id=st[i]-'A';
    29             if(!ch[cur][id]) ch[cur][id]=++sz;
    30             cur=ch[cur][id];
    31         }
    32         Loc[cur]=locs;
    33     }
    34     void BuildNext(){
    35         int l=0,r=0;
    36         For0(i,SONS)
    37           if(ch[0][i]) Q[++r]=ch[0][i];
    38         while(l<r){
    39             int cur=Q[++l];
    40             For0(i,SONS)
    41               if(ch[cur][i]){
    42                   Q[++r]=ch[cur][i];
    43                   next[ch[cur][i]]=ch[next[cur]][i];
    44               }
    45               else ch[cur][i]=ch[next[cur]][i];
    46         }
    47     }
    48     void match(int x,int y,int kind){
    49         int cur=0;
    50         while(x>=0&&y>=0&&x<n&&y<m){
    51             int id=word[x][y]-'A';
    52             cur=ch[cur][id];
    53             if(Loc[cur]){
    54                 int rx=x-dx[kind]*(Len[Loc[cur]]-1);
    55                 int ry=y-dy[kind]*(Len[Loc[cur]]-1);
    56                 ans[Loc[cur]]=make_pair(make_pair(rx,ry),kind+'A');
    57             }
    58             x+=dx[kind];y+=dy[kind];
    59         }
    60     }
    61 }ac;
    62 
    63 int main(){
    64     scanf("%d%d%d",&n,&m,&k);
    65     For0(i,n) scanf("%s",&word[i]);
    66     For(i,k){
    67         scanf("%s",&st);
    68         ac.insert(st,i);
    69         Len[i]=strlen(st);
    70     }
    71     ac.BuildNext();
    72     For0(i,m) ac.match(n-1,i,0);For0(i,n) ac.match(i,0,1);
    73     For0(i,m) ac.match(n-1,i,1);For0(i,n) ac.match(i,0,2);
    74     For0(i,n) ac.match(i,0,3);For0(i,m) ac.match(0,i,3);
    75     For0(i,m) ac.match(0,i,4);For0(i,m) ac.match(0,i,5);
    76     For0(i,n) ac.match(i,m-1,5);For0(i,n) ac.match(i,m-1,6);
    77     For0(i,n) ac.match(i,m-1,7);For0(i,m) ac.match(n-1,i,7);
    78     For(i,k) printf("%d %d %c
    ",ans[i].first.first,ans[i].first.second,ans[i].second);
    79     return 0;
    80 }
  • 相关阅读:
    迭代器、可迭代对象、生成器
    提问的智慧
    爬虫【自动登陆github和抽屉】
    爬虫【爬取汽车之家新闻】
    Python多线程-守护线程
    Python的多线程
    一个简单的Python多线程
    Python实现SSH传输文件(sftp)
    Python实现SSH连接远程服务器
    实验吧CTF题库-WEB题(部分)
  • 原文地址:https://www.cnblogs.com/zjdx1998/p/3973618.html
Copyright © 2011-2022 走看看