What Are You Talking About HDU - 1075(字典树)

What Are You Talking About HDU - 1075

题目链接:https://vjudge.net/problem/HDU-1075

题目:

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?

InputThe 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.
OutputIn 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.
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?

InputThe 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.
OutputIn 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.

        
 题意:火星文翻译,标点符号不变
  思路:这题也可以不用字典树来求,可以用map来求,练习一下字典树,所以就来写一下字典树的写法:
// 
// Created by HJYL on 2019/8/19.
//
#include 
#include 
#include 
#include 
#include 
#include 
#include <set>
#include 
#include 
#include 
#include 
#include 
using namespace std;
typedef long long ll;
const int maxn=1e6+10;
struct trie{
    trie* next[26];
    int sum=0;
    char str[20];
}root;
void init(trie *a)
{
    for(int i=0;i<26;i++)
        a->next[i]=NULL;
    a->sum=0;
}
trie *root1=new trie;
void insert(char* s,char* s2)
{
    int len=strlen(s);
    trie *p=root1;
    for(int i=0;s[i];i++)
    {
        if(p->next[s[i]-'a']==NULL)
        {
            p->next[s[i]-'a']=new trie;
           trie *n=new trie;
           init(n);
           p->next[s[i]-'a']=n;
           p=p->next[s[i]-'a'];
        }
        else
        p=p->next[s[i]-'a'];
    }
    p->sum=1;
    strcpy(p->str,s2);
}
char* find(char *s)
{
    int len=strlen(s);
    trie *p=root1;
    for(int i=0;i)
    {
        if(p->next[s[i]-'a']==NULL)
            return s;
        p=p->next[s[i]-'a'];
    }
    if(p->sum)
        return p->str;
    return s;
}
int main()
{
    //freopen("C:\\Users\\asus567767\\CLionProjects\\untitled\\text","r",stdin);
    char str[maxn],str1[maxn];
    char ch;
    init(root1);
    scanf("%s",str);
    while(scanf("%s",str1)&&strcmp(str1,"END"))
    {
        scanf("%s",str);
        insert(str,str1);
    }
    scanf("%s",str);
    getchar();
    int len2;
    while(gets(str)&&strcmp(str,"END"))
    {
        int len=strlen(str);
        len2=0;
        for(int i=0;i)
        {
            if(islower(str[i]))
                str1[len2++]=str[i];
            else
            {
                str1[len2]='\0';
                if(len2)
                    printf("%s",find(str1));
                len2=0;
                printf("%c",str[i]);
            }
        }
        printf("\n");
    }
    return 0;
}

 

转载于:https://www.cnblogs.com/Vampire6/p/11380470.html

你可能感兴趣的:(What Are You Talking About HDU - 1075(字典树))