Poj 1204 word puzzles AC automatic question Solution

Source: Internet
Author: User

The key to this question is the flexible use of AC automatic machines.

Because the data is not big and the time requirement is not high, some people have used the violence law and others have used the trie.

Of course, some people do not use the AC automatic machine. In the discussion area, they call the AC automatic machine time-out. This is because it will not be used flexibly, or it is hard to set the template, and the AC speed will not be fast.


Give your own algorithm ideas:

1. Create a trie for the keywords to be searched, and then construct an AC automatic machine.

2 search in eight directions, such as Board [N] [m], then you can cycle I (0-> N-1 ), then, every time we take the Board [I] as a text, people who have worked on the key word of HDU know how to search for keywords in a text, in this way, we will not convert this question to the question of searching for text keywords. However, in this question, we need to search all eight texts in eight directions. Eight is a small constant. You can think about how to construct a text in other directions.

3. record the results depends on the individual's programming capabilities. The trick here is to reverse the keyword to establish trie, so the end Of the search is the beginning of each keyword.


Although the AC automatic mechanism is advanced, it is still an algorithm tool, a set of templates, a large copy of the code is meaningless, or completely digested, and then flexible use is the key.

It is more than 5 stars to write efficient algorithms for this question.


The following provides an implementation algorithm based on the above idea, which is fast.

Source: http://blog.csdn.net/kenden23

# Include <stdio. h> # include <string. h ># include <queue> Using STD: queue; const int max_len = 1001; const int arr_size = 26; char Board [max_len] [max_len]; char word [max_len], gdir [max_len]; int gindices [max_len] [2]; int L, C, W; inline int getindex (char ch) {return ch-'A ';} struct node {int N, index; node * fail; node * arr [arr_size] ;}; void clearnode (node * P) {P-> n = 0; p-> Index = 0; P-> fail = NULL; For (INT I = 0; I <arr_size; I ++) {P-> arr [I] = NULL;} node gpool [max_len * max_len]; int gpoolid; node * trie; void inserttrie (char W [], int index) {node * pcrawl = trie; int Len = strlen (w); For (INT I = len-1; I> = 0; I --) // reverse insert a string to locate the Start Node {int id = getindex (W [I]); If (! Pcrawl-> arr [ID]) {pcrawl-> arr [ID] = & gpool [gpoolid ++]; clearnode (pcrawl-> arr [ID]);} pcrawl = pcrawl-> arr [ID];} pcrawl-> N ++; pcrawl-> Index = index;} void buildfail () {queue <node *> qu; qu. push (trie); While (! Qu. empty () {node * pcrawl = Qu. front (); qu. pop (); For (INT I = 0; I <arr_size; I ++) {If (! Pcrawl-> arr [I]) continue; pcrawl-> arr [I]-> fail = trie; node * fail = pcrawl-> fail; while (fail) {If (fail-> arr [I]) {pcrawl-> arr [I]-> fail = fail-> arr [I]; break ;} fail = fail-> fail;} Qu. push (pcrawl-> arr [I]) ;}} void searchdirect (INT R, int C, int rdir, int cdir, char DIR) {node * pcrawl = trie; while (0 <= R & 0 <= C & R <L & C <c) {int id = getindex (Board [r] [c]); while (! Pcrawl-> arr [ID] & pcrawl! = Trie) pcrawl = pcrawl-> fail; If (pcrawl-> arr [ID]) {pcrawl = pcrawl-> arr [ID]; node * TMP = pcrawl; while (TMP & TMP-> n! =-1) {If (TMP-> N) // record results {gindices [TMP-> Index] [0] = R; gindices [TMP-> Index] [1] = C; gdir [TMP-> Index] = dir;} TMP-> N =-1; TMP = TMP-> fail;} r + = rdir, C + = cdir; // watch out, if we use "continue "!}} Void searchall () {char dir = 'E', counterdir = 'a'; For (int c = 0; C <C; C ++) {searchdirect (L-1, C, -1, 0, DIR); searchdirect (0, C, 1, 0, counterdir);} dir = 'G', counterdir = 'C '; for (INT r = 0; r <L; r ++) {searchdirect (R, 0, 0, 1, DIR); searchdirect (R, C-1, 0,-1, counterdir);} dir = 'h', counterdir = 'D'; For (int c = 0; C <C; C ++) {searchdirect (0, C, 1, 1, DIR); searchdirect (L-1, C,-1,-1, counterdir );} For (INT r = 1; r <L; r ++) {searchdirect (R, 0, 1, 1, DIR); searchdirect (R, C-1, -1,-1, counterdir);} dir = 'F', counterdir = 'B'; For (INT C = 0; C <C; C ++) {searchdirect (L-1, C,-1, 1, DIR); searchdirect (0, C, 1,-1, counterdir) ;}for (INT r = 0; r <L; r ++) {searchdirect (R, 0,-1, 1, DIR); searchdirect (R, C-1, 1,-1, counterdir );}} int main () {trie = & gpool [0]; while (scanf ("% d", & L, & C, & W )! = EOF) {clearnode (trie); gpoolid = 1; getchar (); For (INT I = 0; I <L; I ++) {gets (Board [I]) ;}for (INT I = 0; I <W; I ++) {gets (Word); inserttrie (word, I );} buildfail (); searchall (); For (INT I = 0; I <W; I ++) {printf ("% d % C \ n ", gindices [I] [0], gindices [I] [1], gdir [I]) ;}} return 0 ;}



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.