HDU 1075 What Are You Talking About(Tire树)

题目链接

写的太渣了,依旧RE了几次。。。

  1 #include <stdio.h>

  2 #include <string.h>

  3 #include <stdlib.h>

  4 char str[500001][101];

  5 char word[500001];

  6 char ch[5001];

  7 struct node

  8 {

  9     int flag;

 10     struct node *next[26];

 11 };

 12 int num = 1;

 13 struct node *build()

 14 {

 15     int i;

 16     struct node *p;

 17     p = (struct node *)malloc(sizeof(struct node));

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

 19     p -> next[i] = NULL;

 20     return p;

 21 }

 22 void insert(struct node *head,char *str1)

 23 {

 24     int i;

 25     struct node *p;

 26     p = head;

 27     for(i = 0;str1[i];i ++)

 28     {

 29         if(p -> next[str1[i]-'a'] == NULL)

 30         {

 31             p -> next[str1[i]-'a'] = build();

 32         }

 33         p = p -> next[str1[i] - 'a'];

 34     }

 35     p -> flag = num;

 36 }

 37 int find(struct node *head,char *s)

 38 {

 39     int i,j,len;

 40     struct node *p;

 41     p = head;

 42     len = strlen(s);

 43     for(i = 0;i <= len;i ++)

 44     {

 45         if(s[i] >= 'a'&&s[i] <= 'z')

 46         {

 47             if(p -> next[s[i]-'a'] == NULL)

 48             {

 49                 for(j = 0;j <= i;j ++)

 50                 {

 51                     printf("%c",s[j]);

 52                 }

 53                 return i;

 54             }

 55             else

 56             p = p -> next[s[i]-'a'];

 57         }

 58         else

 59         {

 60             if(p -> flag > 0&&p ->flag <= num-1)

 61             {

 62                 printf("%s",str[p->flag]);

 63                 return i-1;

 64             }

 65             else

 66             {

 67                 for(j = 0;j <= i-1;j ++)

 68                 {

 69                     printf("%c",s[j]);

 70                 }

 71                 return i-1;

 72             }

 73         }

 74     }

 75     return i-1;

 76 }

 77 void search(struct node *head,char *str1)

 78 {

 79     int i,len;

 80     len = strlen(str1);

 81     for(i = 0;i <= len-1;i ++)

 82     {

 83         if(str1[i] <= 'z'&&str1[i] >= 'a')

 84         {

 85             i += find(head,str1+i);

 86         }

 87         else

 88         printf("%c",str1[i]);

 89     }

 90     printf("\n");

 91 }

 92 int main()

 93 {

 94     struct node *head;

 95     head = build();

 96     for(;;)

 97     {

 98         scanf("%s%*c",ch);

 99         if(strcmp("END",ch) == 0)

100         break;

101         if(strcmp("START",ch)!= 0)

102         {

103             scanf("%s%*c",word);

104             strcpy(str[num],ch);

105             insert(head,word);

106             num ++;

107         }

108     }

109     for(;;)

110     {

111         gets(word);

112         if(strcmp("END",word) == 0)

113         break;

114         if(strcmp("START",word)!= 0)

115         {

116             search(head,word);

117         }

118     }

119     return 0;

120 }

你可能感兴趣的:(HDU)