POJ 1204 Word Puzzles (AC automaton)

Source: Internet
Author: User


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

Description

Word puzzles is usually simple and very entertaining for all ages. They is so entertaining this Pizza-hut company started using table covers with Word puzzles printed on them, possibly wit H the intent to minimise their client's perception of any possible delay in bringing them their order.

Even though word puzzles may is entertaining to solve by hand, they if 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!) s Olution finding in such puzzles.

The following figure illustrates the Pizzahut puzzle. The names of the pizzas to being found in the puzzle Are:margarita, Alema, barbecue, Tropical, SUPREMA, Louisiana, Cheeseham , EUROPA, Havaiana, Camponesa.

Your task is to produce a program this given the word puzzle and words to being found in the puzzle, determines, for each WOR D, the position of the first letter and their orientation in the puzzle.

You can assume so the left upper corner of the origin, (0,0). Furthemore, the orientation of the word is marked clockwise starting with letter A for North (Note:there be 8 possible D Irections in total).

Input

The first line of input consists of three positive numbers, the number of lines, 0 < L <=, the number of column S, 0 < C <=, 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 is 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 coordinate s, line and column, where the first letter of the word appears, followed by a letter indicating the orientation of the WOR D according to the rules define above. Each value in the triplet must is separated by one space only.

Sample Input

20 20 10QWSPILAATIRAGRAMYKEIAGTRCLQAXLPOIJLFVBUQTQTKAZXVMRWALEMAPKCWLIEACNKAZXKPOTPIZCEOFGKLSTCBTROPICALBLBCJEWHJEEWSMLPOEKOROR Alupqwrnjoaagjkmusjaekrqeioloaoqprtvilcbzqopucajsppoutmtslpsflpouytrfgmmlkiuisxswwahcpoiytgaklmnahbvaeiakhplbgsmclogngjml Ldtikenvcswqazuaoealhoplpgejkmnutiiormncloiuftgsqacaxmopbeioqoasdhopepnbuyuyobxbioniaelojhswasmoutrkhpoiytjplnaqwdribitgl Poinuymrtemptmlmnbopafcoplhavaianalbpfsmargaritaalemabarbecuetropicalsupremalouisianacheesehameuropahavaianacamponesa

Sample Output

0 G2 C7 A4 8 C16 B4 3 E10 1 D5 7 E19 one H

Source

Southwestern Europe 2002


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

Title: To a character matrix, and some words, to find the first letter of the position of the word and the direction of the word, a total of eight directions, north of a, clockwise BCD ...

Topic Analysis: Establish trie tree for Word, construct AC automaton, enumerate the characters on four sides in eight directions

#include <cstdio> #include <cstring> #include <queue> #include <algorithm>using namespace std; int const MAX = 1005;char Map[max][max], S[max];int R, C, N;int Len[max], ans[max][3];int dx[8] = {-1,-1, 0, 1, 1, 1, 0,             -1};int Dy[8] = {0, 1, 1, 1, 0,-1,-1, -1};struct node {int id;       Node *next[26];             Node *fail;        Node () {id =-1;        Memset (Next, NULL, sizeof (next));    Fail = NULL;        }};void Insert (node *p, char *s, int id) {for (int i = 0; s[i]! = ' + '; i++) {int idx = s[i]-' A ';        if (P-next[idx] = = NULL) P--NEXT[IDX] = new node ();    p = P-NEXT[IDX]; } P-id = ID;}     void Ac_automation (node *root) {queue <node*> q;    Q.push (root);        while (!q.empty ()) {Node *p = Q.front ();        Q.pop ();             for (int i = 0; i < i++) {if (P, next[i]) {if (p = = root)       P--next[i]--fail = root;                else P-next[i], fail = p, fail, next[i];            Q.push (P-next[i]);                } else {if (p = = root) P--next[i] = root;            else P-next[i] = p, fail-and next[i];        }}}}bool judge (int i, int j) {if (i >= 0 && i < r && J >= 0 && J < c)    return true; return false;}    void Query (node *root, int x, int y, int k) {node *p = root;        for (int i = x, j = y; Judge (I, j); i + = Dx[k], j + = Dy[k]) {int idx = map[i][j]-' A ';        while (!p-next[idx] && p! = root) p = p, fail;        p = P-NEXT[IDX];            if (!p) {p = root;        Continue        } node *tmp = p; while (tmp! = root) {///finds the word, it logs the relevant information if (TMP ID! =-1) {int id = tmp-ID;                Ans[id][0] = i-len[id] * Dx[k];                ANS[ID][1] = j-len[id] * Dy[k];                Ans[id][2] = k;            TMP--id =-1;            } else break;        TMP = TMP, fail;     }}}int Main () {scanf ("%d%d%d", &r, &c, &n);    Node *root = new node ();    for (int i = 0; i < R; i++) scanf ("%s", Map[i]);        for (int i = 0; i < n; i++) {scanf ("%s", s);        Len[i] = strlen (s)-1;    Insert (root, S, i);    } ac_automation (root); Enumerates the various directions for each boundary for (int i = 0; i < C; i++) {for (int dir = 0; dir < 8; dir++) {Query (R            Oot, 0, I, dir);            Query (Root, R-1, I, dir);  }} for (int i = 0; i < R; i++) {for (int dir = 0; dir < 8; dir++) {Query (root, I,            0, dir);         Query (Root, I, c-1, dir);  }  } for (int i = 0; i < n; i++) printf ("%d%d%c\n", ans[i][0], ans[i][1], ans[i][2] + ' A ');} 




POJ 1204 Word Puzzles (AC automaton)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.