hdu 1075 What Are You Talking About(给你字典,让你翻译句子,字典中查不到的单词不用翻译)

1.这是一道字典树的题,但用map也可以做

2.代码:

#include<cstdio>
#include<cstring>
#include<iostream>
#include<map>
using namespace std;

map<string,string> mp;
char s[30100],ss[15];

int main()
{
    scanf("%s",s);
    while(1)
    {
        scanf("%s",s);
        if(strcmp(s,"END")==0)
            break;
        scanf("%s",ss);
        string s1,s2;
        int len1=strlen(s);
        int len2=strlen(ss);
        for(int i=0; i<len1; i++)
        {
            s1+=s[i];
        }
        for(int i=0; i<len2; i++)
        {
            s2+=ss[i];
        }
        mp.insert(make_pair(s2,s1));
    }
    map<string,string>::iterator it;
    /*for(it=mp.begin();it!=mp.end();it++)
    {
        cout<<(it->first)<<" "<<(it->second)<<endl;
    }*/
    scanf("%s",s);
    getchar();
    while(1)
    {
        gets(s);
        //getchar();
        if(strcmp(s,"END")==0)
        {
            break;
        }
        int len=strlen(s);
        //s[len]=' ';
        string sss;
        for(int i=0; i<=len; i++)
        {
            if(s[i]>=97&&s[i]<=122)
            {
                sss+=s[i];
            }
            else
            {
                if(sss.size()==0)
                {
                    if(i!=len)
                        cout<<s[i];
                    continue;
                }
                else
                {

                    it=mp.find(sss);
                    if(it!=mp.end())
                    {
                        cout<<it->second;
                    }
                    else
                    {
                        cout<<sss;
                    }
                    if(i!=len)
                        cout<<s[i];
                    sss.clear();

                }
            }
        }
        cout<<endl;
    }
    return 0;
}

版权声明:本文为博主原创文章,未经博主允许不得转载。

时间: 2024-10-19 22:03:43

hdu 1075 What Are You Talking About(给你字典,让你翻译句子,字典中查不到的单词不用翻译)的相关文章

HDU 1075 What Are You Talking About Trie题解

翻译火星语,不过火星语也是使用英文单词的,就是把一个单词对应到另外一个单词. 可以使用map, 使用二分,方法很多. 不过最快的应该都是Trie解法了. 把火星语挂在Trie树中,然后在叶子节点增加一个string容器,装英语单词. 查找的时候,找到了出现在Trie中的火星语,就返回string就可以了. #include <stdio.h> #include <string> #include <string.h> using namespace std; const

HDU 1075 map or 字典树

What Are You Talking About Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 102400/204800 K (Java/Others)Total Submission(s): 12773    Accepted Submission(s): 4069 Problem Description Ignatius is so lucky that he met a Martian yesterday. But

HDU 1075 What Are You Talking About(Trie的应用)

What Are You Talking About Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 102400/204800 K (Java/Others) Total Submission(s): 20680    Accepted Submission(s): 6852 Problem Description Ignatius is so lucky that he met a Martian yesterday. But

hdu 1075 字典树

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

HDU 1075 What Are You Talking About (map解法+Trie解法)

HDU 1075 What Are You Talking About (map解法+Trie解法) ACM 题目地址: HDU 1075 What Are You Talking About 题意: 给出一个"翻译-原文"的对应表,然后给出句子,要把句子中的原文都翻译出来. 分析: 可以用map赤裸裸地做,但是比较花费时间,虽然这题时间给了5s,map解法是能过的. 不过Trie解法500+ms,果然Trie字典树才是正解啊. Trie入门题. 另外发现ios_base::sync_

HDU 1075 What Are You Talking About (Trie树)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1075 map可以过...我上的字典树,小bug有点尴尬,题目没有明确给出数据范围也是无奈. 贡献了几次RE 一次WA.尴尬.discuss里面有个说注意前缀的到是给了点tip.总体来说不错 代码: 1 #define _CRT_SECURE_NO_WARNINGS 2 #include <functional> 3 #include <algorithm> 4 #include <

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

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1075 题意:根据词典翻译语句. 思路:裸的字典树.每个节点存以该节点为结尾的对应的单词. 代码: #include <iostream> #include <stdio.h> #include <string.h> #include <math.h> #include <algorithm> #include <string> #incl

HDU 1075 What Are You Talking About(map运用)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1075 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 leav

字典树 HDU 1075 What Are You Talking About

http://acm.hdu.edu.cn/showproblem.php?pid=1075 #include<iostream>#include<algorithm>#include<cstring>#include<ctype.h>#include<stdio.h>#include<stdlib.h>using namespace std; struct node{    int flag;    char str[15];