您的位置:首页 > 其它

hdu 1075 What Are You Talking About

2015-04-23 13:53 447 查看
题目:http://acm.hdu.edu.cn/showproblem.php?pid=1075

#include<stdio.h>
#include<math.h>
#include<stdlib.h>
#include<string.h>
#include<iostream>
using namespace std;
struct tree{
char c[20];
struct tree *next[26];
};
struct tree *root;
char num[20];

void insert(char *q,char *p)
{
struct tree *now,*cur=root;
int len=strlen(p);
for(int i=0;i<len;i++)
{
if(cur->next[p[i]-'a']==NULL)//判断是否存在字母节点
{
now=(struct tree*)malloc(sizeof(struct tree));//分配空间
for(int j=0;j<26;j++)
{
now->next[j]=NULL;//初始化
}
strcpy(now->c,"0");
cur->next[p[i]-'a']=now;//赋给空间
cur=cur->next[p[i]-'a'];
}
else
{
cur=cur->next[p[i]-'a'];
}
}
strcpy(cur->c,q);
}

int find(char *p)
{
struct tree *cur=root;
int len=strlen(p);
for(int i=0;i<len;i++)
{
if(cur->next[p[i]-'a']==NULL)//无法找到,证明无对应单词直接返回0
return 0;
else
cur=cur->next[p[i]-'a'];
}
if(strcmp(cur->c,"0")==0)
return 0;
strcpy(num,cur->c);//把找到的对应单词赋给num
return 1;
}

int main()
{
//freopen("in.txt","r",stdin);
root=(struct tree*)malloc(sizeof(struct tree));//初始化
for(int i=0;i<26;i++)
{
root->next[i]=NULL;//初始化
}
strcpy(root->c,"0");//初始化
char p[3005],q[20];
while(~scanf("%s",q))
{
if(strcmp(q,"START")==0)
continue;
if(strcmp(q,"END")==0)
break;
scanf("%s",p);
insert(q,p);//建立树
}
strcpy(q,"0");
strcpy(p,"0");
getchar();//必须加
while(gets(p))
{
if(strcmp(p,"START")==0)
continue;
if(strcmp(p,"END")==0)
break;
for(int j=0,i=0;p[i]!='\0';i++)//判断
{
if(p[i]<'a' || p[i]>'z')//判断是否为字母
{
q[j]='\0';//用\0顶替这个不是字母的符号
j=0;
int n=find(q);//查找
if(n==0)
printf("%s",q);
else
printf("%s",num);
printf("%c",p[i]);//将这个不是字母的符号输出
}
else
{
q[j]=p[i];//赋给q
j++;
}
}
printf("\n");
}

return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: