标签:des style blog color java os strong io
3 Alice Bob Smith John Alice Smith 5 a c c d d e b e a d 0
Yes No
最近做字典树的题挺频繁的,自然而然又想到了字典树,把人名分成编号 TIME:15ms
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <algorithm>
using namespace std;
struct node{
int bianhao;
node *next[52];
};
node *root;
struct no{
int u,v,next;
}g[2010];
int head[2010];
int rudu[1010];
int t,l;
struct node *make()
{
node *p = new node;
for(int i = 0;i<52;i++)
{
p->next[i] = NULL;
}
p->bianhao = 0;
return p;
}
void make_()
{
root = (struct node *)malloc(sizeof(struct node));
for(int i = 0;i<52;i++)
{
root->next[i] = NULL;
}
root->bianhao = 0;
}
int INsert(char *s)
{
int len = strlen(s);
int num;
node *p =root;
for(int i = 0;i<len;i++)
{
if('A'<=s[i] && s[i]<='Z')
num = s[i] - 'A';
else
num = s[i] - 'a';
if(p->next[num]==NULL)
{
p->next[num] = make();
}
p = p->next[num];
}
if(p->bianhao==0)
{
t++;
p->bianhao = t;
}
return p->bianhao;
}
void add(int a, int b)
{
g[l].u = b;
g[l].v = b;
g[l].next = head[a];
head[a] = l++;
}
int aa[2010],ll;
void TOPSort()
{
int i,j,k;
for(i = 1;i<=t;i++)
{
if(rudu[i]==0)
{
ll++;
}
if(ll>1)
break;
// printf("ll = %d\n",ll);
/* for(j = 1;j<=t;j++)
{
if(rudu[j]==0)
{
ll++;
rudu[j]--;
for(k = head[j];k!=0;k = g[k].next)
{
rudu[g[k].v]--;
}
break;
}
}*/
}
}
int main()
{
int n;
char a[20],b[20];
while(scanf("%d",&n),n)
{
t = 0,l = 1,ll=0;
make_();
memset(rudu,0,sizeof(rudu));
memset(head,-1,sizeof(head));
for(int i = 0;i<n;i++)
{
scanf("%s%s",a,b);
int u = INsert(a);
int v = INsert(b);
// printf("%s %d\n",a,u);
// printf("%s %d\n",b,v);
add(u,v);
rudu[v]++;
}
TOPSort();
if(ll==1)
puts("Yes");
else
puts("No");
}
return 0;
}HDU 2094 产生冠军 (字典树+拓扑),布布扣,bubuko.com
标签:des style blog color java os strong io
原文地址:http://blog.csdn.net/wjw0130/article/details/38338283