hdu 1075 What Are You Talking About(字典树)

What Are You Talking About

Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 102400/204800 K (Java/Others)
Total Submission(s): 11207    Accepted Submission(s): 3576


Problem Description

 

Ignatius is so lucky that he met a Martian yesterday. But he didn't know the language the Martians use. The Martian gives him a history book of Mars and a dictionary when it leaves. Now Ignatius want to translate the history book into English. Can you help him?
 


 

Input

 

The problem has only one test case, the test case consists of two parts, the dictionary part and the book part. The dictionary part starts with a single line contains a string "START", this string should be ignored, then some lines follow, each line contains two strings, the first one is a word in English, the second one is the corresponding word in Martian's language. A line with a single string "END" indicates the end of the directory part, and this string should be ignored. The book part starts with a single line contains a string "START", this string should be ignored, then an article written in Martian's language. You should translate the article into English with the dictionary. If you find the word in the dictionary you should translate it and write the new word into your translation, if you can't find the word in the dictionary you do not have to translate it, and just copy the old word to your translation. Space(' '), tab('\t'), enter('\n') and all the punctuation should not be translated. A line with a single string "END" indicates the end of the book part, and that's also the end of the input. All the words are in the lowercase, and each word will contain at most 10 characters, and each line will contain at most 3000 characters.
 


 

Output

 

In this problem, you have to output the translation of the history book.
 


 

Sample Input

 

 
 
START from fiwo hello difh mars riwosf earth fnnvk like fiiwj END START difh, i'm fiwo riwosf. i fiiwj fnnvk! END
 


 

Sample Output

 

 
 
hello, i'm from mars. i like earth!
Hint
Huge input, scanf is recommended.

 

#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#define N 100010
struct node
{
    int next[26],w;    //记录当前字母编号的儿子字母编号
}tree[N*5];                //记录每个单词的编号(按照输入的顺序)
char str[N*5][11],s1[N];
int index;
void Insert(int k,int len)
{
    int i,m,s=0;
    for(i=0;i<len;i++)
    {
        m=s1[i]-'a';
        if(tree[s].next[m]==0)
        {
            if(i==len-1)
                tree[index].w=k;  //当读取到一个单词的最后一个字母是把该单词编号为k
            tree[s].next[m]=index++;    //index记录的是每个单词中每个字母的编号
        }
        else
        {
            if(i==len-1)
                tree[tree[s].next[m]].w=k;   //该种情况是读取了相同的单词
        }
        s=tree[s].next[m];          //把该单词的编号延续到下一个
    }
}
int Query(char s0[],int len)
{
    int i,m,s=0;
    for(i=0;i<len;i++)
    {
        m=s0[i]-'a';
        if(tree[s].next[m])
        {
            s=tree[s].next[m];
            if(i==len-1&&tree[s].w)
            {
                printf("%s",str[tree[s].w]);
                return 1;
            }
        }
        else
            return 0;
    }
    return 0;
}
int  main()
{
    int i,n=0,j;
    char s2[20];
    index=0;
    scanf("%s",s1);
    while(scanf("%s",str[n]),str[n][0]!='E')
    {
        scanf("%s",s1);
        Insert(n,strlen(s1));
        n++;
    }
    scanf("%s",s1);
    getchar();
    while(gets(s1),s1[0]!='E')
    {
        for(i=0,j=0;s1[i]!='\0';i++)
        {
            if(s1[i]>='a'&&s1[i]<='z')
                s2[j++]=s1[i];
            else
            {
                s2[j]='\0';
                if(Query(s2,j)==0)
                    printf("%s",s2);                      
                printf("%c",s1[i]);      //          s2[0]='\0';   不用要,因为每次j从0开始
                j=0;
            }
        }
        printf("\n");
    }
    return 0;
}

指针写法:

#include"stdio.h"
#include"malloc.h"
#include"string.h"
#define N 26
struct node
{
	int v;
	node * next[N];
	char s1[15];
}root;
void creat(char *s1,char *str)
{
	int i,j,len=strlen(str);
	node *p=&root,*q;
	for(i=0;i<len;i++)
	{
		int id=str[i]-'a';
		if(p->next[id]==NULL)       //建立新的树
		{
			q=(node*)malloc(sizeof(root));     
			for(j=0;j<N;j++)   
				q->next[j]=NULL;
			q->v=0;         
			p->next[id]=q;
			p=p->next[id];
		}
		else
			p=p->next[id];
	}
	p->v=1;         //以该字母结尾有单词
	strcpy(p->s1,s1);
}
int find(char *str)
{
	int i,len=strlen(str);
	node*p=&root;
	for(i=0;i<len;i++)
	{
		int id=str[i]-'a';
		if(p->next[id]==NULL)
			return 0;
		p=p->next[id];
	}
	if(p->v)
	{
		printf("%s",p->s1);
		return 1;
	}
	return 0;
}
int main()
{
	int i,k;
	char s1[15],s2[15],str[3005];
	for(i=0;i<N;i++)
		root.next[i]=NULL;
	while(scanf("%s",s1))
	{
		if(strcmp(s1,"START")==0)
			continue;
		if(strcmp(s1,"END")==0)
			break;
		scanf("%s",s2);
		creat(s1,s2);
	}
	getchar();               //吸收换行
	while(gets(str))
	{
		if(strcmp(str,"START")==0)
			continue;
		if(strcmp(str,"END")==0)
			break;
		for(i=0,k=0;str[i]!='\0';i++)
		{
			if(str[i]>='a'&&str[i]<='z')
			{
				s1[k++]=str[i];
			}
			else
			{
				if(k)
				{
					s1[k]='\0';
					if(find(s1)==0)
						printf("%s",s1);
				}
				printf("%c",str[i]);
				k=0;
			}
		}
		if(k)        //以字母结尾,最后翻译
		{
			s1[k]='\0';       //貌似不考虑也能过
			if(find(s1)==0)
				printf("%s",s1);
		}
		printf("\n");
	}
	return 0;
}


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值