您的位置:首页 > 其它

hdu 1305 Immediate Decodability

2015-02-20 19:00 369 查看
题意:给你一些0 1串,判断这些0 1串之间没有一个串是另一个串的前缀。

做法:Trie树,用count函数判断以它为头字符串的字符串的个数是否大于1就行了

#include<iostream>
#include<string>
#include<vector>
#include<cstring>
#include<cstdio>
using namespace std;
#define maxn 500
struct node
{    char* s;
int count1;
bool isword;
node* next[2];
node()
{    s=NULL;
count1=0;
isword=false;
memset(next,0,sizeof(next));
}
}*root;

void insert(node* root,char* s)
{    node* p=root;
for(int i=0;s[i];i++)
{    int x=s[i]-'0';
p->s=s+i;
if(p->next[x]==NULL)
p->next[x]=new node;
p=p->next[x];
p->count1++;
}
p->isword=true;
}

bool Search(node* root,const char* s)
{    node* p=root;
for(int i=0;s[i];i++)
{    int x=s[i]-'0';
if(p->next[x]==NULL)
return false;
p=p->next[x];
}
return p->isword;
}

int count(node* root,const char* s)
{
node* p=root;
for(int i=0;s[i];i++)
{
int x=s[i]-'0';
if(p->next[x]==NULL)
return 0;
p=p->next[x];
}
return p->count1 ;
}
int main()
{
char s[maxn];
bool flag;
int cas = 1;
while(scanf("%s",s) != EOF)
{     flag = true;
root = new node;
vector<string> v;
insert(root,s);
string str(s);
v.push_back(str);
while(scanf("%s",s))
{
if(s[0]=='9') break;
insert(root,s);
string str1(s);
v.push_back(str1);
}
for(int i = 0;i < (int)v.size(); i++)
{
if(count(root,v[i].c_str()) > 1)
{
flag = false;
break;
}
}
if(flag)
printf("Set %d is immediately decodable\n",cas++);
else
printf("Set %d is not immediately decodable\n",cas++);
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  acm Trie树