hdu1671 Phone List

简单题~

注意del();

#include <iostream>
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>
#include <algorithm>
#include <vector>
#include <limits.h>
#include <queue>
#include <stack>
using namespace std;
struct trie
{
    int is;
    trie* next[11];
};

char ch[11];
int ans;
void inst(trie *root,char* s)
{
    trie *p = root;
    int i = 0,len = strlen(s),f = 1,ff=1;
    //cout<<s<<endl;
    while(i < len)
    {
        //cout<<i<<endl;
        int k = s[i]-'0';
        if(p->next[k] == NULL)
        {
            //cout<<i<<endl;
            trie* tmp = new trie;
            //cout<<i<<endl;
            for(int j= 0;j < 11;j ++) tmp->next[j] = NULL;
            tmp->is = 0;
            p->next[k] = tmp;
        }
        else
        {
            if(p->next[k]->is == 1) f = 0;
        }

        p = p->next[k];
        i ++;
    }
    p->is = 1;
    for(i = 0;i < 11;i ++) if(p->next[i]!=NULL) ff = 0;
    if(f&&ff) ans ++;
}
void del(trie *root)                    //释放空间
{
	for(int i=0;i<11;i++)
	{
		if(root->next[i]!=NULL)
		{
			del(root->next[i]);
		}
	}
	delete root;
}
int main()
{
    int t,n;
    //cin>>t;
    scanf("%d",&t);

    while(t --)
    {
        trie *root = new trie;
        scanf("%d",&n);
        ans = 0;
        for(int i = 0;i < 11;i ++) root->next[i] = NULL;
        for(int i = 0;i < n;i ++)
        {
            scanf("%s",ch);
            inst(root,ch);
        }

        //sort(s,s+n,cmp);
        //cout<<ans<<endl;
        if(ans == n) printf("YES\n");
        else printf("NO\n");
        del(root);
    }

}


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值