HDU 1075 What Are You Talking About(用map进行翻译)

题目地址:点击打开链接

题意:给出日语和英语的对应词典,输入一段日语,把日语翻译成英语,不是小写字母字符的不进行翻译,例如逗号,空格,没有对应英语的日语则直接输出日语

思路:字典树,二分查找,map映射,都可以做,效率逐渐降低,代码逐渐缩短,本题用map映射没超时

AC代码:

#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <algorithm>
#include <queue>
#include <stack>
#include <map>
#include <cstring>
#include <climits>
#include <cmath>
#include <cctype>//islower的头文件

using namespace std;

char a[20],english[15],martian[15];
char b[3010];

int main()
{
    int i;
    map<string,string> map1;
    scanf("%s",a);
    while(scanf("%s",english) && strcmp(english,"END") != 0)
    {
        scanf("%s",martian);
        map1[martian] = english;
    }
    scanf("%s",a);
    string language = "";
    getchar();//把残留在流中的空格吃掉,不然会被gets吃掉
    while(gets(b) && strcmp(b,"END") != 0)
    {
        int n = strlen(b);
        for(i=0; i<n; i++)
        {
            if(islower(b[i]))
            {
                language += b[i];
            }
            else
            {
                if(map1.find(language) != map1.end())
                    cout<<map1[language];
                else
                    cout<<language;
                language = "";
                printf("%c",b[i]);
            }
        }
        printf("\n");
    }
    return 0;
}


你可能感兴趣的:(HDU 1075 What Are You Talking About(用map进行翻译))