POJ 1204 Word Puzzles (AC automatic mechanism), pojpuzzles

Source: Internet
Author: User

POJ 1204 Word Puzzles (AC automatic mechanism), pojpuzzles


Word Puzzles
Time Limit:5000 MS   Memory Limit:65536 K
Total Submissions:9926   Accepted:3711   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 minimize 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 coshould devise a program to speedup (hopefully !) Solution finding in such puzzles.

The following figure extends strates 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 ctions 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 shocould 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 10QWSPILAATIRAGRAMYKEIAGTRCLQAXLPOIJLFVBUQTQTKAZXVMRWALEMAPKCWLIEACNKAZXKPOTPIZCEOFGKLSTCBTROPICALBLBCJEWHJEEWSMLPOEKORORALUPQWRNJOAAGJKMUSJAEKRQEIOLOAOQPRTVILCBZQOPUCAJSPPOUTMTSLPSFLPOUYTRFGMMLKIUISXSWWAHCPOIYTGAKLMNAHBVAEIAKHPLBGSMCLOGNGJMLLDTIKENVCSWQAZUAOEALHOPLPGEJKMNUTIIORMNCLOIUFTGSQACAXMOPBEIOQOASDHOPEPNBUYUYOBXBIONIAELOJHSWASMOUTRKHPOIYTJPLNAQWDRIBITGLPOINUYMRTEMPTMLMNBOPAFCOPLHAVAIANALBPFSMARGARITAALEMABARBECUETROPICALSUPREMALOUISIANACHEESEHAMEUROPAHAVAIANACAMPONESA

Sample Output

0 15 G2 11 C7 18 A4 8 C16 13 B4 15 E10 3 D5 1 E19 7 C11 11 H

Source

Southwestern Europe 2002


Question link: http://poj.org/problem? Id = 1204

Give A character matrix, and some words, and find the position of the first letter of the word and the direction of the word, A total of eight directions, the North is A, clockwise BCD...

Question Analysis: Create a trie tree for words, construct an ac automatic machine, and start with the characters on the four sides and look for them 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]! = '\ 0'; 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 <26; 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-> 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) {// find the word, then record 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", & 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); // enumerate all directions of each boundary for (int I = 0; I <c; I ++) {for (int dir = 0; dir <8; dir ++) {Query (root, 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 % c \ n", ans [I] [0], ans [I] [1], ans [I] [2] + 'A ');}




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.