您的位置:首页 > 其它

POJ -- 1056 IMMEDIATE DECODABILITY (Trie 树)

2015-06-20 16:22 453 查看
题目大意:



判断在一组字符串中,是否有一个字符串是另一个字符串的前缀;

代码实现:

#include<cstdio>
#include<cstring>
#include<iostream>
#include<cstdlib>
using namespace std;
char code[15];
bool flag;

struct tree{
int flag;
tree *next[2];
}*root;

tree *Create(){
tree *p;
p=(tree *)malloc(sizeof(tree));
p->flag=0;
for(int i=0;i<2;++i) p->next[i]=NULL;
return p;
}

void Insert(char *code){
tree *p=root;
int i=0,x,l=strlen(code);
while(i<l){
x=code[i++]-'0';
if((p->next[x])==NULL) p->next[x]=Create();
else if(i==l) flag=1;
p=p->next[x];
if(p->flag==1) flag=1;
}
p->flag=1;
}

int main(){
int cnt=0;
root=Create();
while(~scanf("%s",code)){
if(code[0]=='9'){
if(!flag) printf("Set %d is immediately decodable\n",++cnt);
else printf("Set %d is not immediately decodable\n",++cnt);
root=Create();
flag=0;
}else Insert(code);
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: