HDU 1671 轻轻松松字典树

这题的难度和前一篇的裸模版差不多。
题目大意就是看字符串是否是其他字符串的前缀,
其实就是在字典树中查看最后一个节点的count数是否为1.
若为1,则代表该字符串唯一。
若不唯一,则代表该字符串被其他字符串完全包含,不就是前缀么!
水过。。。

#include<iostream>
#include<string>
#include<cstdio>
#define MAX 10
using 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;
}


 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值