poj 2503 Babelfish(字典树或STL水题) 解题报告(百炼2804)

Babelfish
Time Limit: 3000MS   Memory Limit: 65536K
Total Submissions: 36589   Accepted: 15616

Description

You have just moved from Waterloo to a big city. The people here speak an incomprehensible dialect of a foreign language. Fortunately, you have a dictionary to help you understand them.

Input

Input consists of up to 100,000 dictionary entries, followed by a blank line, followed by a message of up to 100,000 words. Each dictionary entry is a line containing an English word, followed by a space and a foreign language word. No foreign word appears more than once in the dictionary. The message is a sequence of words in the foreign language, one word on each line. Each word in the input is a sequence of at most 10 lowercase letters.

Output

Output is the message translated to English, one word per line. Foreign words not in the dictionary should be translated as "eh".

Sample Input

dog ogday
cat atcay
pig igpay
froot ootfray
loops oopslay

atcay
ittenkay
oopslay

Sample Output

cat
eh
loops

用STL的map,就是输入有些麻烦,其他就很简单了

STL代码:

#include<iostream>
#include<string>
#include<map>
using namespace std;

int main()
{
	char a[11],b[11];
	map<string,bool>have;
	map<string,string>get;
	while(true)
	{
		char t;
		if((t=getchar())=='\n')
			break;
		else
		{
			a[0]=t;
			int i=1;
			while(true)
			{
				t=getchar();
				if(t==' ')
				{
					a[i]='\0';
					break;
				}
				else
					a[i++]=t;
			}
		}
		scanf("%s",b);
		getchar();
		have[b]=true;
		get[b]=a;
	}
	char word[11];
	while(scanf("%s",word)!=EOF)
	{
		if(have[word])
			cout<<get[word]<<endl;
		else
			printf("eh\n");
	}

	return 0;
}

字典树代码:

#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cstdlib>
#include <iostream>
using namespace std;

typedef struct zc
{
    int count;
    char ans[12];
    struct zc *next[26];
}node;

char arr1[12],arr2[12],str[26];
node *T;

void build()
{
    node *q,*p=T;
    int len=strlen(arr2),k;
    for(int i=0;i<len-1;i++)
    {
        k=arr2[i]-'a';
        if(p->next[k]==NULL)
        {
            q=(node*)malloc(sizeof(node));
            p->next[k]=q;
            q->ans[0]=0;
            for(int j=0;j<26;j++)
                q->next[j]=NULL;
            p=q;
        }
        else p=p->next[k];
    }
    k=arr2[len-1]-'a';//最后一字符
    if(p->next[k]==NULL){
        q=(node*)malloc(sizeof(node));
        p->next[k]=q;strcpy(q->ans,arr1);
        for(int i=0;i<26;i++)
            q->next[i]=NULL;
    }
    else {
         p=p->next[k];
         strcpy(p->ans,arr1);
    }

}

void search()
{
    node *p=T;
    int len=strlen(str),k,i;
    for(i=0;i<len-1;i++)
    {
        k=str[i]-'a';
        if(p->next[k]==NULL)
        {
            printf("eh\n");
            break;
        }else{
            p=p->next[k];
        }
    }
    if(i==len-1)
    {
        k=str[i]-'a';
        if(p->next[k]==NULL)
            printf("eh\n");
        else
        {
            p=p->next[k];
            if(p->ans[0]!=0)
                puts(p->ans);
            else printf("eh\n");
        }
    }
}


int main()
{
    T=(node*)malloc(sizeof(node));
    for(int i=0;i<26;i++)
        T->next[i]=NULL;
    while(1)
    {
        gets(str);
        if(strcmp(str,"")==0)break;
        sscanf(str,"%s %s",arr1,arr2);
        build();
    }
    while(scanf("%s",str)!=EOF)
    {
        search();
    }
	return 0;
}


你可能感兴趣的:(ACM,STL,poj,字典树)