HDU 1075 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!

这是我第一次写博客,也是我第一次用map解题,如果有错误请多多包涵

注意事项:getchar的使用,字符数组可尽量开大些,strcmp在两个字符串相同时返回值为0

示例代码:

#include 
#include 
#include 
#include 
#include 

using namespace std;

char  str[100004];
int main()
{
	char a[12], b[12], s[8], buf[12];
	map<string, string>dic;   
	scanf("%s", s);                                  //输入START
	while (scanf("%s", a) && strcmp(a, "END"))       //输入火星文并将与之对应的英文单词相关联起来
	{
		scanf("%s", b);
		dic[b] = a;
	}
	scanf("%s", s);        //输入START
	getchar();                //缓冲最后输入的回车
	while (gets(str) && strcmp(str, "END"))       //输入火星文
	{
		int len = strlen(str);
		for (int i = 0; i < len; ++i) {
			memset(buf, '\0', sizeof(buf));          //初始化buf
			int j = 0;
			while ('a' <= str[i] && str[i] <= 'z'&&i < len)     //将一个单词输到buf中
				buf[j++] = str[i++];                      
			if (strcmp(buf, ""))             
			{
				if (dic[buf] != "")             //如果有对应的翻译
				{
					cout << dic[buf];
				}
				else                               //如果没有对应的翻译就直接输出
				{
					cout << buf;               
				}
			}
			printf("%c", str[i]);              //输出标点符号、回车、空格等
		}
		printf("\n");                           
	}
	return 0;
}

你可能感兴趣的:(HDU)