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(‘\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!

题意:一对字符串相互对应,

现在给你一串字符,让你翻译,可以选择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]!=‘\0‘){
        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]!=‘\0‘&&p->next[str[i]-‘a‘]){
        p=p->next[str[i]-‘a‘];
        i++;
    }
    if(str[i]==‘\0‘&&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] = ‘\0‘;
                if(t[0]!=‘\0‘){ //不是空的
                    find(t);    //找到对应的翻译并输出,没找到则输出原来的字符串
                    t[0]=‘\0‘,j=0;  //初始化t
                }
                printf("%c",str[i]);
            }
        }
        printf("\n");
    }

    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]=‘\0‘;
                if(a[0]!=‘\0‘){
                    find(a);
                    k=0;
                    a[0]=‘\0‘;
                }
                printf("%c",s[i]);
            }

        }
        cout<<endl;
    }
}*/

Hint

Huge input, scanf is recommended.

时间: 2024-08-07 12:07:11

hdu 1075(字典树)的相关文章

hdu 1075 字典树

// hdu 1075 字典树 // // 题目大意: // // 给你一个字典,即有两个字符串,一个是英文,一个是火星文,然后 // 输入一段火星文,要你翻译成英文. // // 解题思路: // // 字典树,查字典嘛,有就输出查到的,没有原样输出.将火星文插入到 // 字典树中,然后在字典输中查找.找到了,输出对应的英文,否则,原样输 // 出. // // 感悟: // // 题目确实很简单,但是,没告诉数据范围啊,导致我一直RE,原来单词 // 可能对应很长的英文啊,找人家ac的开数组

HDU 1800 字典树

Flying to the Mars Time Limit: 5000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 10065    Accepted Submission(s): 3270 Problem Description In the year 8888, the Earth is ruled by the PPF Empire . As the popul

HDU 1251 字典树入门

统计难题 Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 131070/65535 K (Java/Others)Total Submission(s): 17177    Accepted Submission(s): 7410 Problem Description Ignatius最近遇到一个难题,老师交给他很多单词(只有小写字母组成,不会有重复的单词出现),现在老师要他统计出以某个字符串为前缀的单词数量(单词本身也是自己的前

HDU 5384 字典树、AC自动机

题目:http://acm.hdu.edu.cn/showproblem.php?pid=5384 用字典树.AC自动机两种做法都可以做 1 #include<stdio.h> 2 #include<string.h> 3 #include<string> 4 #include<iostream> 5 using namespace std; 6 struct node{ 7 int cnt; 8 node *next[26]; 9 node(){ 10 c

HDU 5687 字典树插入查找删除

题目:http://acm.hdu.edu.cn/showproblem.php?pid=5687 2016百度之星资格赛C题,直接套用字典树,顺便巩固了一下自己对字典树的理解 1 #include<stdio.h> 2 #include<string.h> 3 struct node{ 4 int next[27]; 5 int cnt; 6 void init(){ 7 cnt = 0;//计数 8 memset(next,-1,sizeof(next)); 9 } 10 };

hdu 5269 字典树

题目链接:hdu 5269 ZYB loves Xor I Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Total Submission(s): 184    Accepted Submission(s): 101 Problem Description Memphis loves xor very musch.Now he gets an array A.The lengt

hdu 2112(字典树+最短路)

HDU Today Time Limit: 15000/5000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 23388    Accepted Submission(s): 5614 Problem Description 经过锦囊相助,海东集团终于度过了危机,从此,HDU的发展就一直顺风顺水,到了2050年,集团已经相当规模了,据说进入了钱江肉丝经济开发区500强.这时候,

Flying to the Mars HDU - 1800(字典树)

Flying to the Mars HDU - 1800 题目链接:https://vjudge.net/problem/HDU-1800 题目:在8888年,地球由PPF帝国统治.随着人口的增长,PPF需要为新生儿寻找更多的土地.最后,PPF决定攻击统治火星的Kscinow.问题来了!士兵怎么能到达火星? PPF召集他的士兵并询问他们的建议. “匆匆......”一名士兵回答. “闭嘴 !我是否必须提醒你,从这里到火星没有任何道路!“PPF回复道. “飞!”另一个答案. PPF笑道:“聪明的

Chip Factory HDU - 5536 字典树(删除节点|增加节点)

题意: t组样例,对于每一组样例第一行输入一个n,下面在输入n个数 你需要从这n个数里面找出来三个数(设为x,y,z),找出来(x+y)^z(同样也可以(y+z)^1)的最大值 (“^”代表异或操作,即“相同为0,不同为1”) 题解: 这一道题和Xor Sum HDU - 4825很相似 因为异或运算的特性,我们最后要求最大值,那我们就对这n个数的二进制形式建一颗字典树.然后就暴力枚举是哪两个数相加,然后在字典树中把这两个数删掉.然后在处理完的字典树中查找那个能使结果尽可能大的第三个数(至于怎么