poj3630 Trie字典树

原题:http://poj.org/problem?id=3630

题解:将所有数字建成字典树,当插入时无新节点生成,经过别的有标记的节点时,就是前缀。

#include<cstdio>
#include<cstring>
#include<algorithm>
#include<iostream>
using namespace std;
const int N=1e5+10,Z=10;
int ch[N][Z],cas,n,tot;
bool mark[N],flag;
char s[Z<<1];
inline void clear(){
	tot=1;flag=0;
	memset(ch,0,sizeof ch);
	memset(mark,0,sizeof mark);
}
bool ins(char *s){	
	int len=strlen(s+1);
	bool f=0;int p=1;int last=tot;
	for(int i=1;i<=len;i++){
		int c=s[i]-'0';
		if(!ch[p][c])ch[p][c]= ++tot;
		p=ch[p][c];
		if(mark[p]) f=1;
	}
	mark[p]=1;
	if(last==tot) f=1;
	return f; 
}
int main(){
	//freopen("poj3630.in","r",stdin);
	scanf("%d",&cas);
	while(cas--){
		clear();
		scanf("%d",&n);
		for(int i=1;i<=n;i++){
			scanf("%s",s+1);
			if(ins(s)) flag=1;
		}
		if(flag) puts("NO");
		else puts("YES");		
	}
	return 0;
}

 

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值