您的位置:首页 > 其它

HDU 1075 What Are You Talking About(Tire树)

2012-08-13 20:36 323 查看
题目链接

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

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
char str[500001][101];
char word[500001];
char ch[5001];
struct node
{
int flag;
struct node *next[26];
};
int num = 1;
struct node *build()
{
int i;
struct node *p;
p = (struct node *)malloc(sizeof(struct node));
for(i = 0;i <= 25;i ++)
p -> next[i] = NULL;
return p;
}
void insert(struct node *head,char *str1)
{
int i;
struct node *p;
p = head;
for(i = 0;str1[i];i ++)
{
if(p -> next[str1[i]-'a'] == NULL)
{
p -> next[str1[i]-'a'] = build();
}
p = p -> next[str1[i] - 'a'];
}
p -> flag = num;
}
int find(struct node *head,char *s)
{
int i,j,len;
struct node *p;
p = head;
len = strlen(s);
for(i = 0;i <= len;i ++)
{
if(s[i] >= 'a'&&s[i] <= 'z')
{
if(p -> next[s[i]-'a'] == NULL)
{
for(j = 0;j <= i;j ++)
{
printf("%c",s[j]);
}
return i;
}
else
p = p -> next[s[i]-'a'];
}
else
{
if(p -> flag > 0&&p ->flag <= num-1)
{
printf("%s",str[p->flag]);
return i-1;
}
else
{
for(j = 0;j <= i-1;j ++)
{
printf("%c",s[j]);
}
return i-1;
}
}
}
return i-1;
}
void search(struct node *head,char *str1)
{
int i,len;
len = strlen(str1);
for(i = 0;i <= len-1;i ++)
{
if(str1[i] <= 'z'&&str1[i] >= 'a')
{
i += find(head,str1+i);
}
else
printf("%c",str1[i]);
}
printf("\n");
}
int main()
{
struct node *head;
head = build();
for(;;)
{
scanf("%s%*c",ch);
if(strcmp("END",ch) == 0)
break;
if(strcmp("START",ch)!= 0)
{
scanf("%s%*c",word);
strcpy(str[num],ch);
insert(head,word);
num ++;
}
}
for(;;)
{
gets(word);
if(strcmp("END",word) == 0)
break;
if(strcmp("START",word)!= 0)
{
search(head,word);
}
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: