欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

POJ1204- Word Puzzles(字典树)

程序员文章站 2022-06-07 21:44:22
...

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.
POJ1204- Word Puzzles(字典树)

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

题意:
给出一个n*m的字符矩阵和w个字符串,有八个方向可以选择,问字符串在矩阵中第一次出现的位置是哪里,方向又是什么。

解法:
首先用字典树保存这w个字符串,在每个字符串的结尾打一个数字标记pos,然后对矩阵的每一个位置在字典树上进行查询,如果查询到的位置的pos正好存在(也就是说是一个字符串的结尾),并且是第一次遇到(因为问的是第一次出现的位置),那就更新这个字符串的答案。

代码:

#include<iostream>
#include<cstdio>
#include<algorithm>
#include<stdlib.h>
#include<cstring>
using namespace std;

int n,m,w;
int num=0;
char x,ss[1000+5];
char s[1000+5][1000+5];
int dx[8]={-1,-1,0,1,1,1,0,-1};
int dy[8]={0,1,1,1,0,-1,-1,-1};
char dz[8]={'A','B','C','D','E','F','G','H'};

struct tree{
    int son[26+5],pos;
}trie[1000000+5];

struct node{
    int x,y;
    char c;
}ans[1000+5];

void iinsert(char s[],int pos){
    int len=strlen(s),u=0;
    for(int i=0;i<len;i++){
        int v=s[i]-'a';
        if(!trie[u].son[v])trie[u].son[v]=++num;
        u=trie[u].son[v];
    }
    trie[u].pos=pos;
}

void ffind(int x,int y,int k){
    int xx=x,yy=y,u=0;
    while(xx>=0&&xx<n&&yy>=0&&yy<m){
        int v=s[xx][yy]-'a';
        if(!trie[u].son[v])return ;
        u=trie[u].son[v];
        if(trie[u].pos&&ans[trie[u].pos].x==-1){
            int p=trie[u].pos;
            ans[p].x=x;
            ans[p].y=y;
            ans[p].c=dz[k];
        }
        xx+=dx[k];yy+=dy[k];
    }
}

int main()
{
    scanf("%d%d%d",&n,&m,&w);
    scanf("%c",&x);
    for(int i=0;i<n;i++){
        for(int j=0;j<m;j++){
            scanf("%c",&s[i][j]);
        }
        scanf("%c",&x);
    }
    for(int i=1;i<=w;i++){
        scanf("%s",ss);
        ans[i].x=-1;ans[i].y=-1;
        iinsert(ss,i);
    }
    for(int i=0;i<n;i++){
        for(int j=0;j<m;j++){
            for(int k=0;k<8;k++){
                ffind(i,j,k);
            }
        }
   }
   for(int i=1;i<=w;i++)printf("%d %d %c\n",ans[i].x,ans[i].y,ans[i].c);
   return 0;
}
相关标签: ICPC