HDU 1057 What Are You Talking About trie树 简单

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

题意 : 给一个单词表然后给一些单词,要求翻译单词表中有的单词,没有则直接输出原单词.
翻译文段部分getchar()输入即可,需要仔细一点,但是很简单不恶心
代码
 1 #include<cstdio>
 2 #include<cstring>
 3 #include<iostream>
 4 #include<algorithm>
 5 #include<cmath>
 6 #include<queue>
 7 using namespace std;
 8 const int maxn=100010;
 9 const double eps=1e-8;
10 const long long modn=1000;
11 struct tri{
12     bool exist;
13     int next[26];
14     char ch[20];
15 }e[maxn*10];
16 int tot=0;
17 char a[20]={},b[20]={},cc;
18 char st[20]={'S','T','A','R','T'};
19 char ed[20]={'E','N','D'};
20 void doit(int x,int k,int j){
21     if(k<j){
22         e[x].exist=1;
23         strcpy(e[x].ch,b);
24         return;
25     }
26     int z=a[j]-'a';
27     if(e[x].next[z]){
28         doit(e[x].next[z],k,j+1);
29     }else{
30         e[x].next[z]=++tot;
31         doit(tot,k,j+1);
32     }
33 }
34 bool getit(int x,int k,int j){
35     if(k<j){
36         if(e[x].exist){
37             printf("%s",&e[x].ch);
38             return 1;
39         }
40         return 0;
41     }
42     int z=a[j]-'a';
43     if(e[x].next[z]){
44         return getit(e[x].next[z],k,j+1);
45     }else{
46         return 0;
47     }
48 }
49 int main(){
50     while(scanf("%s",&b)){
51         if(strcmp(b,st)==0){
52             continue;
53         }if(strcmp(b,ed)==0){
54             break;
55         }
56         scanf("%s",&a);
57         doit(0,strlen(a)-1,0);
58     }
59     scanf("%s",&b);
60     a[0]=getchar();
61     a[0]=getchar();
62     int i=0;
63     while(a[0]!='E'){
64         while(a[i]<='z'&&a[i]>='a'){
65             i++;
66             a[i]=getchar();
67         }
68         cc=a[i];
69         a[i]=0;
70         if(i>0){
71             if(!getit(0,i-1,0)){
72                 for(int j=0;j<i;j++){
73                     printf("%c",a[j]);
74                 }
75             }
76         }
77         printf("%c",cc);
78         i=0; 
79         a[0]=getchar();
80     }
81     return 0;
82 }
View Code

 

转载于:https://www.cnblogs.com/137shoebills/p/7786491.html

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值