HDOJ 1075

字典树

98909742013-12-25 15:31:06Accepted1075468MS59832K1342 BG++泽泽

 

 1 #include<stdio.h>
 2 #include<cstring>
 3 #include<cstdlib>
 4 struct node
 5 {
 6     node *next[26];
 7     int key;
 8     char ans_s[10];
 9 }root;
10 void insert(char *str,char *s)
11 {
12     int len=strlen(str);
13     node *p=&root,*q;
14     for(int i=0;i<len;i++)
15     {
16         int id=str[i]-'a';
17         if(p->next[id]==NULL)
18         {
19             q=(node *)malloc(sizeof(root));
20             q->key=1;
21             for(int j=0;j<26;j++)
22                 q->next[j]=NULL;
23             p->next[id]=q;
24             p=p->next[id];
25         }
26         else
27             p=p->next[id];
28     }
29     if(p->key!=-1)
30     {
31         strcpy(p->ans_s,s);
32         p->key=-1;
33     }
34 }
35 void find(char *str)
36 {
37     int len=strlen(str);
38     node *p=&root;
39     for(int i=0;i<len;i++)
40     {
41         int id=str[i]-'a';
42         if(p->next[id]==NULL)
43         {
44             printf("%s",str);
45             return ;
46         }
47         else
48         {
49             p=p->next[id];
50         }
51     }
52     if(p->key==-1)
53         printf("%s",p->ans_s);
54     else
55         printf("%s",str);
56     return;
57     
58 }
59 int main()
60 {
61     int i;
62     char s1[11],s2[11],s[3001];
63     for(i=0;i<26;i++)
64         root.next[i]=NULL;
65     scanf("%s",s1);
66     while(scanf("%s",s1)!=EOF&&strcmp(s1,"END")!=0)
67     {
68         scanf("%s",s2);
69         insert(s2,s1);
70     }
71     scanf("%s",s1);
72     getchar();
73     while(gets(s)&&strcmp(s,"END")!=0)
74     {
75         int k=0,len=strlen(s); 
76         for(i=0;i<len;i++)
77         {
78             while(s[i]>='a'&&s[i]<='z')//满足是字母,wa了一次
79             {
80                 s2[k++]=s[i++];
81             }
82             s2[k]='\0';
83             k=0;
84             find(s2);
85             printf("%c",s[i]);
86         }
87         printf("\n");
88     }
89     
90     return 0;
91 }
View Code

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值