Hdoj-1247-Hat's Words-dictionary tree

Source: Internet
Author: User

Hdoj-1247-Hat's Words-dictionary tree

 

 

# Include
 
  
# Include
  
   
# Include
   
    
# Define deusing namespace std; const int N = 30; const int MAX = 50005; char word [MAX] [30]; struct node {bool temp; node * next [N]; node () {temp = false; memset (next, 0, sizeof (next); // next indicates the number of types in each layer. If it is only a lowercase letter, then 26. // if it is changed to a case-sensitive letter, it is 52. If it is added with a number, it is 62 }}; void insert (node * rt, char * s) {int I, t; I = 0; node * p = rt; while (s [I]) {t = s [I ++]-'A '; if (p-> next [t] = NULL) p-> next [t] = new node (); p = p-> next [t];} p-> temp = true; // point to the end of the word} bool search (node * rt, char s []) {int I, top; I = top = 0; int stack [MAX]; node * p = rt; while (s [I]) {int t = s [I ++]-'A '; if (p-> next [t] = NULL) return false; p = p-> next [t]; if (p-> temp & s [I]) // locate the split point containing the subword and the character has not ended. stack [top ++] = I; // import the split point to the stack, then cyclically determine whether the remaining part contains a subword} while (top) // start from these Split points and find {bool OK = true; // mark whether I = stack [-- top] is found; // The first split point p = rt; while (s [I]) {int t = s [I ++]-'A'; if (p-> next [t] = NULL) {OK = false; // No break found ;} p = p-> next [t];} if (OK & p-> temp) return true;} return false;} int main () {int I = 0; node * rt = new node (); while (gets (word [I]) {insert (rt, word [I]); I ++ ;} for (int j = 0; j
    
     

 

Related Article

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.