HDOJ/HDU 1075 What Are You Talking About(字符串查找翻译~Map)

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!

Hint
Huge input, scanf is recommended.

题意:
给出一个“翻译-原文”的对应表,然后给出句子,要把句子中的原文都翻译出来。
如果不是字母,就原样输出~

开始因为空格问题、PE了几次~~

用Map秒过~~

考虑的时候一行一行的考虑~
一行一行的输出~

注意读取第一个END后面的回车~
用nextLine();

import java.util.HashMap;
import java.util.Map;
import java.util.Scanner;

/**
 * @author 陈浩翔
 *
 *         2016-5-26
 */
public class Main{

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        while (sc.hasNext()) {
            Map<String, String> map = new HashMap<String, String>();
            String str1 = "";
            String str2 = "";
            while (true) {
                str1 = sc.next();
                if ("START".equals(str1)) {
                    continue;
                }
                if ("END".equals(str1)) {
                    break;
                }
                str2 = sc.next();
                map.put(str2, str1);
            }

            sc.nextLine();
            String s = "";
            while (true) {
                s = sc.nextLine();
                if ("START".equals(s)) {
                    continue;
                }
                if ("END".equals(s)) {
                    break;
                }
                String str = "";
                for (int i = 0; i < s.length(); i++) {
                    if (s.charAt(i) >= 'a' && s.charAt(i) <= 'z') {
                        str += s.charAt(i);

                        if(i==s.length()-1){
                            String v = map.get(str);
                            if (v != null) {
                                System.out.print(v);
                            } else {
                                System.out.print(str);
                            }
                            str="";
                        }
                    } else {
                        String v = map.get(str);
                        if (v != null) {
                            System.out.print(v);
                        } else {
                            System.out.print(str);
                        }
                        System.out.print(s.charAt(i));
                        str="";
                    }
                }
                System.out.println();
            }

        }
    }
}
时间: 2024-10-11 15:30:02

HDOJ/HDU 1075 What Are You Talking About(字符串查找翻译~Map)的相关文章

HDOJ/HDU 1200 To and Fro(加密解密字符串)

Problem Description Mo and Larry have devised a way of encrypting messages. They first decide secretly on the number of columns and write the message (letters only) down the columns, padding with extra random letters so as to make a rectangular array

算法题:HDU 1075 What Are You Talking About(字典树学习题)

链表: http://acm.hdu.edu.cn/showproblem.php?pid=1075 题目: What Are You Talking About Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 102400/204800 K (Java/Others) Total Submission(s): 7939    Accepted Submission(s): 2470 Problem Description Ign

HDOJ/HDU 1161 Eddy&amp;#39;s mistakes(大写字母转换成小写字母)

Problem Description Eddy usually writes articles ,but he likes mixing the English letter uses, for example "computer science" is written frequently "coMpUtEr scIeNce" by him, this mistakes lets Eddy's English teacher be extremely disco

HDOJ/HDU 1087 Super Jumping! Jumping! Jumping!(经典DP~)

Problem Description Nowadays, a kind of chess game called "Super Jumping! Jumping! Jumping!" is very popular in HDU. Maybe you are a good boy, and know little about this game, so I introduce it to you now. The game can be played by two or more t

HDOJ(HDU) 2061 Treasure the new start, freshmen!(水题、)

Problem Description background: A new semester comes , and the HDU also meets its 50th birthday. No matter what's your major, the only thing I want to tell you is:"Treasure the college life and seize the time." Most people thought that the colle

HDOJ(HDU) 2109 Fighting for HDU(简单排序比较)

Problem Description 在上一回,我们让你猜测海东集团用地的形状,你猜对了吗?不管结果如何,都没关系,下面我继续向大家讲解海东集团的发展情况: 在最初的两年里,HDU发展非常迅速,综合各种ACM算法生成的老鼠药效果奇好,据说该药专对老鼠有效,如果被人误食了,没有任何副作用,甚至有传闻说还有健胃的效果,不过这倒没有得到临床验证.所以,公司的销量逐年递增,利润也是节节攀升,作为股东之一的公主负责财务,最近半年,她实在辛苦,多次因为点钞票造成双手抽筋而住院,现在在她面前你根本不要提到"

HDOJ(HDU) 2107 Founding of HDU(找最大值)

Problem Description 经过慎重的考虑,XHD,8600, LL,Linle以及RPG等ACM队员集体退役,甚至正在酝酿退学. 为什么?要考研?那也不用退学呀- 当然不是!真正的原因是他们想提前创业,想合伙成立一家公司,据说公司的名称都想好了,为了感谢多年的ACM集训队队长XHD,公司就叫海东集团(HaiDong Union),简称HDU.(对于这个公司名称,几个人私下里开玩笑说,外面的人看到HDU,可别以为是"胡捣集团",呵呵) 公司成立了,谁来做老大呢?这对于合伙的

HDOJ(HDU) 2500 做一个正气的杭电人(水~)

Problem Description 做人要有一身正气,杭电学子都应该如此.比如我们今天的考试就应该做到"诚信"为上. 每次考试的第一个题目总是很简单,今天也不例外,本题是要求输出指定大小的"HDU"字符串,特别地,为了体现"正气"二字,我们要求输出的字符串也是正方形的(行数和列数相等). Input 输入的第一行包含一个正整数N(N<=20),表示一共有N组数据,接着是N行数据,每行包含一个正整数M(M<=50),表示一行内有M个

HDOJ(HDU) 1491 Octorber 21st

Problem Description HDU's 50th birthday, on Octorber 21st, is coming. What an exciting day!! As a student of HDU, I always want to know how many days are there between today and Octorber 21st.So, write a problem and tell me the answer.Of course, the