1204 word puzzles

Source: Internet
Author: User

When you are a beginner, you can use the code of someone else to modify the template.

When I used query () to query a suitable solution, I returned the result. After wa n times, I made such a low-level error, so I should not.

I have benefited a lot from reading other people's code this time, so I can record it

# Include <iostream> <br/> # include <stdio. h> <br/> # include <string. h> <br/> using namespace STD; <br/> # define txtl 1005 </P> <p> // when a subtree node is inserted, <br/> // The queue used in the search for the mismatched pointer is simulated using an array. <br/> const int kind = 26; // Number of subtree <br/> struct node <br/> {<br/> node * fail; <br/> node * Next [kind]; <br/> int count; // record the number of complete words with the current prefix <br/> node () <br/>{< br/> fail = NULL; <br/> COUNT =-1; <br/> memset (next, 0, sizeof (next); <br/>}< br/>} * Q [1000001]; // The queue used to find the mismatched pointer <br/> struct cword {<br/> char data [1001]; <br/> int wordlen; <br/> int row, Col; <br/> char Ori; <br/>}; <br/> const int dir [] [2] = {-1, 0}, {-1, 1}, {0, 1}, {1, 1}, {1, 0}, {1,-1}, {0, -1 },{-1,-1 }}; <br/> char WP [txtl] [txtl]; <br/> cword words [txtl]; <br/> int row, Col, N; <br/> void insert (node * root, char * STR, int ID) <br/> {<br/> node * P = root; <br/> int I = 0, index; <br/> while (STR [I]) <br/>{< br/> Index = STR [I]-'A'; <br/> If (p-> next [Index] = NULL) <br/> P-> next [Index] = new node (); <br/> P = p-> next [Index]; <br/> I ++; <br/>}< br/> P-> COUNT = ID; <br/>}< br/> // search for failure pointers <br/> void build_ac_automation (node * root) <br/>{< br/> int I; <br/> int head, tail; <br/> root-> fail = NULL; <br/> head = 0; <br/> tail = 0; <br/> q [tail ++] = root; <br/> while (Head! = Tail) <br/>{< br/> node * temp = Q [head ++]; // gets the first element of the queue <br/> node * P = NULL; <br/> for (I = 0; I <kind; I ++) <br/>{< br/> If (temp-> next [I]! = NULL) // find the failure pointer of the current subtree <br/>{< br/> P = temp-> fail; <br/> while (P! = NULL) <br/>{< br/> If (p-> next [I]! = NULL) // find the failure pointer <br/>{< br/> temp-> next [I]-> fail = p-> next [I]; <br/> break; <br/>}< br/> P = p-> fail; <br/>}< br/> If (P = NULL) // cannot be obtained. The current subtree failure pointer is the root <br/> temp-> next [I]-> fail = root; <br/> q [tail ++] = temp-> next [I]; // enter the current subtree <br/>}< br/> // query that STR contains N keywords number of <br/> void query (node * root, char * STR, Int & wordid, int IR, int IC, int di) <br/>{< br/> int I = 0, CNT = 0, index, Len; <br/> Len = strlen (St R); <br/> node * P = root; <br/> while (STR [I]) <br/>{< br/> Index = STR [I]-'A'; <br/> while (p-> next [Index] = NULL & P! = Root) // mismatch <br/> P = p-> fail; <br/> P = p-> next [Index]; <br/> If (P = NULL) // The mismatched pointer is the root <br/> P = root; <br/> node * temp = P; <br/> while (temp! = Root) // mark the accessed point in this process <br/>{< br/> If (temp-> count! =-1) {<br/> wordid = temp-> count; <br/> // return I; a suitable solution is returned after a query is found, wa has made such a low-level mistake n times, so it is not necessary <br/> words [wordid]. row = IR + (I-words [wordid]. wordlen + 1) * dir [di] [0]; <br/> words [wordid]. col = IC + (I-words [wordid]. wordlen + 1) * dir [di] [1]; <br/> words [wordid]. ori = di + 'a'; <br/>}< br/> temp = temp-> fail; <br/>}< br/> I ++; <br/>}< br/> inline int inbound (int r, int C) {<br/> return 0 <= R & R <row & 0 <= C & C <Col; <br/>}< br/> void findsub (node * root, int IR, int IC, int di) {<br/> int JS, wordid, RS; <br/> char ss [txtl]; <br/> for (JS = 0; inbound (IR + js * dir [di] [0], IC + js * dir [di] [1]); ++ JS) {<br/> SS [JS] = WP [IR + js * dir [di] [0] [IC + js * dir [di] [1]; <br/>}< br/> SS [JS] = '/0'; <br/> query (root, SS, wordid, IR, IC, DI ); <br/>}< br/> int main () <br/> {<br/> scanf ("% d", & Row, & Col, & N); <br/> node * root = new node (); <br/> for (INT I = 0; I <row; ++ I) {<br/> scanf ("% s", WP [I]); <br/>}< br/> for (INT I = 0; I <N; ++ I) <br/>{< br/> scanf ("% s", words [I]. data); <br/> words [I]. wordlen = strlen (words [I]. data); <br/> insert (root, words [I]. data, I); <br/>}< br/> // find the failure pointer <br/> build_ac_automation (Root); <br/> for (INT I = 0; I <n; ++ I) {<br/> words [I]. col = words [I]. row = words [I]. ori =-100; <br/>}< br/> for (Int J = col-1; j> = 0; -- j) {<br/> findsub (root, 0, J, 3); <br/> findsub (root, 0, J, 4); <br/> findsub (root, 0, J, 5 ); <br/> findsub (root, row-1, J, 7); <br/> findsub (root, row-1, J, 0 ); <br/> findsub (root, row-1, J, 1); <br/>}< br/> for (INT I = row-1; I >= 0; -- I) {<br/> findsub (root, I, 0, 1); <br/> findsub (root, I, 0, 2 ); <br/> findsub (root, I, 0, 3); <br/> findsub (root, I, col-1, 5); <br/> findsub (root, i, col-1, 6); <br/> findsub (root, I, col-1, 7 ); <br/>}< br/> for (INT I = 0; I <n; ++ I) {<br/> printf ("% d % C/N", words [I]. row, words [I]. col, words [I]. ori); </P> <p >}< br/> return 0; <br/>}< br/> 

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.