hdu1251统计难题(trie树)

http://acm.hdu.edu.cn/showproblem.php?pid=1251

将每次走过得字母统计数量  查找的时候输出字母数量的最小值

View Code
 1 #include<stdio.h>

 2 #include<iostream.h>

 3 #include<string.h>

 4 struct node

 5 {

 6     int count;

 7     node *next[27];

 8     node()

 9     {

10         count = 0;

11         memset(next,NULL,sizeof(next));

12     }

13 };

14 void trie(char *c,node *root)

15 {

16     int k = strlen(c),i,d;

17     struct node *p = root; 

18     for(i = 0 ; i < k ; i++)

19     {

20         d = c[i]-'a';

21         if(p->next[d]==NULL)

22         {            

23             p->next[d] = new node;

24         }        

25         p = p->next[d];

26         p->count++;

27     }

28 }

29 void ftire(char *c,node *root)

30 {

31     struct node *p = root;

32     int i,k = strlen(c),d,f = 0,w = 0;

33     int min = 100000;

34     for(i = 0 ;i <k  ;i++)

35     {

36         d = c[i]-'a';

37         if(p->next[d]==NULL)

38         {

39             w = 1;

40             break;

41         }

42         if(p->count<min)

43             min = p->count;

44         p = p->next[d];

45     }

46     if(!w)

47         f = p->count;

48     printf("%d\n",f);

49 }

50 int main()

51 {

52     int i;

53     char str[11];

54     struct node *root = new node;

55     while(gets(str)!=NULL)

56     {        

57         if(str[0]!='\0')

58         {

59             trie(str,root);            

60         }

61         else

62             break;

63     }

64     while(gets(str)!=NULL)

65     {

66         ftire(str,root);

67     }

68     return 0;

69 }

 

你可能感兴趣的:(trie)