HDU 1671 loose dictionary tree

Source: Internet
Author: User

The difficulty of this question is similar to that of the previous bare template.
Check whether the string is the prefix of another string,
Check whether the count of the last node is 1 in the dictionary tree.
If the value is 1, the string is unique.
If it is not unique, it indicates that the string is completely contained by other strings. Isn't it a prefix!
Water over...

#include<iostream>#include<string>#include<cstdio>#define MAX 10using namespace std;char s[11111][11];int allocp;struct TireNode{       int nCount;       TireNode *next[MAX];};TireNode Memeroy[1111111];void InitTire( TireNode **root ){     *root=NULL;}TireNode *CreateTire(){         int i;         TireNode *p=&Memeroy[allocp++];         p->nCount=1;         for( int i=0;i<MAX;i++ )              p->next[i]=NULL;         return p;}void InsertTire( TireNode **root,char *s ){     int i=0,k;     TireNode *p;     if( !(p=*root) )         p=*root=CreateTire();          while( s[i] )     {            k=s[i++]-'0';            if( p->next[k] )                p->next[k]->nCount++;            else                p->next[k]=CreateTire();            p=p->next[k];      }}bool SearchTire( TireNode **root,char *s ){     int i=0,k;     TireNode *p=*root;     int cnt=0;      while( s[i] )     {            k=s[i++]-'0';            cnt=p->next[k]->nCount;             p=p->next[k];         }     if( cnt==1 )         return false;     else         return true; }int main(){    int T;    scanf( "%d",&T );    while( T-- )    {           allocp=0;           TireNode *root;           root=NULL;           int len=0;           scanf( "%d",&len );            for( int i=0;i<len;i++ )           {                scanf( "%s",&s[i] );                InsertTire(&root,s[i]);           }           bool found=true;           for( int i=0;i<len;i++ )           {                if( SearchTire(&root,s[i]) )                {    found=false;                    break;}           }           if( found==false )               printf( "NO\n" );           else               printf( "YES\n" );     }    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.