hdu 1075 What Are You Talking About

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


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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值