hdu 1075(字典树)

What Are You Talking About

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

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(' '), enter(' ') 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!
题意:一对字符串相互对应,
现在给你一串字符,让你翻译,可以选择map写 我用字典树写的  输入数据有点懵
#include<iostream>
#include<cstdio>
#include<cmath>
#include<cstring>
#include<algorithm>
#include<queue>
#include<map>
#include<set>
#include<vector>
#include<cstdlib>
#include<string>
#define eps 0.000000001
typedef long long ll;
typedef unsigned long long LL;
using namespace std;
const int N=100+10;
struct tire{
    char str[N];
    int flag;
    struct tire *next[30];
};
tire *root;
tire *init(){
    tire *p=(tire *)malloc(sizeof(tire));
    for(int i=0;i<30;i++){
        p->next[i]=NULL;
    }
   // p->terminal=false;
    p->flag=0;
    return p;
}
void insert(char *a,char *b){
    int i=0;
    tire *p=root;
    while(b[i]!=''){
        if(p->next[b[i]-'a']==0){
            p->next[b[i]-'a']=init();
        }
        p=p->next[b[i]-'a'];
        //p->num++;
        i++;
    }
    p->flag=1;
    strcpy(p->str,a);
}
void find(char *str){
    int i=0;
    tire *p=root;
    while(str[i]!=''&&p->next[str[i]-'a']){
        p=p->next[str[i]-'a'];
        i++;
    }
    if(str[i]==''&&p->flag){
        printf("%s",p->str);
    }
    else
        printf("%s",str);


}
int main()
{
    char word[11],trans[11];
    char s[20];
    scanf("%s",word);
    root=init();
    while(scanf("%s",word)!=EOF){   //输入字典
        if(strcmp(word,"END")==0)   //遇到结束标记
            break;
        scanf("%s",s);
        insert(word,s); //将单词word插入到字典树中,并在最后加入其翻译
    }
    scanf("%s",word);
    getchar();
    char str[3001];
    while(gets(str)){
        if(strcmp(str,"END")==0)
            break;
        int i,j=0;
        char t[3001]={0};
        for(i=0;str[i];i++){
            if('a'<=str[i] && str[i]<='z'){ //检测到的是小写字母
                t[j++] = str[i];
            }
            else{
                t[j] = '';
                if(t[0]!=''){ //不是空的
                    find(t);    //找到对应的翻译并输出,没找到则输出原来的字符串
                    t[0]='',j=0;  //初始化t
                }
                printf("%c",str[i]);
            }
        }
        printf("
");
    }

    return 0;
}
/*
int main(){
    char str1[N],str2[N],str3[N],str4[N],s[N];
    gets(str1);
    root=init();
    while(scanf("%s",str2)!=EOF){
        if(strcmp(str2,"END")==0)break;
        scanf("%s",str3);
        insert(str2,str3);
    }
    //getchar();
    gets(str4);
    getchar();
    char a[N];
    //getchar();
    while(gets(s)){
        //find(s);
        int k=0;
        if(strcmp(s,"END")==0)break;
        int len=strlen(s);
        int flag=0;
        for(int i=0;i<len;i++){
            if(s[i]>='a'&&s[i]<='z'){
                a[k++]=s[i];
            }
            else{
                a[k]='';
                if(a[0]!=''){
                    find(a);
                    k=0;
                    a[0]='';
                }
                printf("%c",s[i]);
            }

        }
        cout<<endl;
    }
}*/

Hint
Huge input, scanf is recommended.
原文地址:https://www.cnblogs.com/Aa1039510121/p/6411212.html