hdu 1075 What Are You Talking About

翻译 2015年07月08日 20:59:41

题意:用字典把火星历史书翻译成英文,注意边界。


import java.util.Scanner;

public class P1075a {
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		// 输入字典的部分
		String s1 = "";// 接输入的字符
		String a = "";
		s1 = sc.nextLine();
		s1 = sc.nextLine();
		while (!s1.equals("END")) {
			a += s1;
			a += "\n";
			s1 = sc.nextLine();
		}
		// 输入书的部分
		String s2 = "";// 接输入的字符
		String b = "";
		s2 = sc.nextLine();
		s2 = sc.nextLine();
		while (!s2.equals("END")) {
			b += s2;
			b += "\n";
			s2 = sc.nextLine();
		}
		// 处理数据,***核心***
		for (int i = 0; i < b.length();) {
			// 1)先取出b中的一个火星单词
			int m = 0;
			for (int j = i; j < b.length(); j++) {
				char ch = b.charAt(j);
				if (!(ch <= 'z' && ch >= 'a' || ch == '\'')) {
					m = j;
					break;// 这个要写啊!
				}
			}
			String bWord = b.substring(i, m);// b中火星单词
			// 2)取出a中的一个火星单词的起始位置
			int middle = a.indexOf(bWord);// 没有找到就返回-1
			boolean boo = false;
			if (middle > 0) {// 找到了
				// 3)确定是不是a中火星单词的子串
				char aCh1 = a.charAt(middle - 1);
				char aCh2 = a.charAt(middle + bWord.length());
				if (aCh1 == ' ') {
					if (aCh2 == '\n') {
						boo = true;
					}
				}
			}
			if (boo) {// 字典中真的找到了火星语
				// 4)找到a中的英语单词
				int n = 0;
				for (int j = middle - 2; j >= 0; j--) {
					if (a.charAt(j) == '\n') {
						n = j + 1;// 不要换行符
						break;// 这个要写啊!会死的!!!
					}
				}
				// 希望是火星语,,不然程序必wa
				String word = a.substring(n, middle - 1);// 英语单词,范围[n,middle-1)
				// 5)替换b中的bWord
				b = b.replace(bWord, word);// 返回一个新的字符串
				// 6)修改i的位置
				i = i + word.length();// 正好最后一个,走出单词的最后一个,一定不是单词了
			} else {
				i = i + bWord.length();
			}
			// 7)确定i的位置,可能出现两个非字母字符
			if (i != (b.length() - 1)) {
				char ch = b.charAt(i + 1);
				if (ch <= 'z' && ch >= 'a') {
					i += 1;
				} else {
					i += 2;
				}
			}
		}
		System.out.println(b);
	}

}



What Are You Talking About

你说的是什么?
Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 102400/204800 K (Java/Others)
Total Submission(s): 15929    Accepted Submission(s): 5163


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, 
字典部分的起始行包含一个字符串 "START"(开始),这个字符串应该被忽略,接下来的每一行,都包含两个字符串。
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. 
如果有一行是"END",那么表示字典部分结束了,这一行应该被忽略。
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. 
书的那部分,第一行是以"START"字符串开始的,这一行应该被忽略,那后是一篇用火星语写的文章。
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.
"END"表示书的部分结束,也代表整个输入结束。
 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.
 所有单词都是用小写,每一个单词最长也只有十个字符,每一行最多有3000个字符。

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.
 

Author
Ignatius.L

相关文章推荐

hdu1075 What Are You Talking About(map的get读入)

这题因为用getline读一直WA,直接导致我将近一天不想做题。。。。。。。。 话说为毛不能用getline啊,可能是我还有别的问题,但我真的是检查了好几遍了,虽然时间快接近2000ms了,...

HDU1075 What Are You Talking About 【STL】

What Are You Talking About Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 102400/204800...

hdu1075What Are You Talking About (字典树)

Problem Description Ignatius is so lucky that he met a Martian yesterday. But he didn't know the la...

hdu-1075-What Are You Talking About

What Are You Talking About Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 102400/204800 K ...

hdu1075What Are You Talking About(map容器)

题目地址 http://acm.hdu.edu.cn/showproblem.php?pid=1075 What Are You Talking About Time Limit: ...

hdu 1075 What Are You Talking About 字典树(静态版)

HDU1075 -------千万别让不开心的事影响了自己正常的生活,今天遇到了尴尬的事情,郁闷了一晚上没看书,就只是快要熄灯了昨晚我的每日一水题          《What Are You T...

【字典树】 hdu1075 What Are You Talking About

What Are You Talking About http://acm.hdu.edu.cn/showproblem.php?pid=1075 Problem Descript...
  • ACM_Ted
  • ACM_Ted
  • 2012年08月04日 12:00
  • 1018

HDU 1075 what are you talking about?

题目: 输入:这个问题只有一个测试案例,包括两部分,字典部分和书籍部分。字典部分有”START“字符串所在的一行开头,跟随一些行,每一行有两个字符串,一个是英语单词,另一个 是对应的火星文。一个写着...

【HDU】 1075 What Are You Talking About

What Are You Talking About Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 102400/204800 K...
  • S_Black
  • S_Black
  • 2016年01月15日 09:24
  • 223

hdu 1075 What Are You Talking About 字典树 trie

比较裸的最小生成树题目。开始打完RE了,检查了一个晚上字典树代码。后来发现是存映射字符串的arr开小了。出题人说的3000个单词指的是后面的文章中的句子。我理解成查找的映射只有3000个,后来开到50...
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:hdu 1075 What Are You Talking About
举报原因:
原因补充:

(最多只允许输入30个字)