hdu 1075 What Are You Talking About

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

  1 #include<stdio.h>
  2 #include<math.h>
  3 #include<stdlib.h>
  4 #include<string.h>
  5 #include<iostream>
  6 using namespace std;
  7 struct tree{
  8     char c[20];
  9     struct tree *next[26];
 10 };
 11 struct tree *root;
 12 char num[20];
 13 
 14 void insert(char *q,char *p)
 15 {
 16     struct tree *now,*cur=root;
 17     int len=strlen(p);
 18     for(int i=0;i<len;i++)
 19     {
 20         if(cur->next[p[i]-'a']==NULL)//判断是否存在字母节点
 21         {
 22             now=(struct tree*)malloc(sizeof(struct tree));//分配空间
 23             for(int j=0;j<26;j++)
 24             {
 25                 now->next[j]=NULL;//初始化
 26             }
 27             strcpy(now->c,"0");
 28             cur->next[p[i]-'a']=now;//赋给空间
 29             cur=cur->next[p[i]-'a'];
 30         }
 31         else
 32         {
 33             cur=cur->next[p[i]-'a'];
 34         }
 35     }
 36     strcpy(cur->c,q);
 37 }
 38 
 39 int find(char *p)
 40 {
 41     struct tree *cur=root;
 42     int len=strlen(p);
 43     for(int i=0;i<len;i++)
 44     {
 45         if(cur->next[p[i]-'a']==NULL)//无法找到,证明无对应单词直接返回0
 46         return 0;
 47         else
 48         cur=cur->next[p[i]-'a'];
 49     }
 50     if(strcmp(cur->c,"0")==0)
 51     return 0;
 52     strcpy(num,cur->c);//把找到的对应单词赋给num
 53     return 1;
 54 }
 55 
 56 int main()
 57 {
 58     //freopen("in.txt","r",stdin);
 59     root=(struct tree*)malloc(sizeof(struct tree));//初始化
 60     for(int i=0;i<26;i++)
 61     {
 62         root->next[i]=NULL;//初始化
 63     }
 64     strcpy(root->c,"0");//初始化
 65     char p[3005],q[20];
 66     while(~scanf("%s",q))
 67     {
 68         if(strcmp(q,"START")==0)
 69         continue;
 70         if(strcmp(q,"END")==0)
 71         break;
 72         scanf("%s",p);
 73         insert(q,p);//建立树
 74     }
 75     strcpy(q,"0");
 76     strcpy(p,"0");
 77     getchar();//必须加
 78     while(gets(p))
 79     {
 80         if(strcmp(p,"START")==0)
 81         continue;
 82         if(strcmp(p,"END")==0)
 83         break;
 84         for(int j=0,i=0;p[i]!='\0';i++)//判断
 85         {
 86             if(p[i]<'a' || p[i]>'z')//判断是否为字母
 87             {
 88                 q[j]='\0';//用\0顶替这个不是字母的符号
 89                 j=0;
 90                 int n=find(q);//查找
 91                 if(n==0)
 92                 printf("%s",q);
 93                 else
 94                 printf("%s",num);
 95                 printf("%c",p[i]);//将这个不是字母的符号输出
 96             }
 97             else
 98             {
 99                 q[j]=p[i];//赋给q
100                 j++;
101             }
102         }
103         printf("\n");
104     }
105 
106 
107     return 0;
108 }

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值