首页 > 代码库 > ZOJ 1109 Language of FatMouse
ZOJ 1109 Language of FatMouse
最近学的线段树和扫描线实在是有点难度,所以我干脆拿出点时间写篇自己之前做的题目报告吧!
Language of FatMouse
Description
We all know that FatMouse doesn‘t speak English. But now he has to be prepared since our nation will join WTO soon. Thanks to Turing we have computers to help him.
Input Specification
Input consists of up to 100,005 dictionary entries, followed by a blank line, followed by a message of up to 100,005 words. Each dictionary entry is a line containing an English word, followed by a space and a FatMouse word. No FatMouse word appears more than once in the dictionary. The message is a sequence of words in the language of FatMouse, one word on each line. Each word in the input is a sequence of at most 10 lowercase letters.
Output Specification
Output is the message translated to English, one word per line. FatMouse words not in the dictionary should be translated as "eh".
Sample Input
dog ogdaycat atcaypig igpayfroot ootfrayloops oopslayatcayittenkayoopslay
Output for Sample Input
catehloops
算法分析:
学过算法的话,能看出来这道题应该用字典树做,我个人觉得除非功底非常强,否则单枪匹马
敲出针对题目的字典树来其实还真不容易。由于最近学习了STL,所以我选择用STL来做,这样代
码比较简洁,也容易看懂。我个人觉得,你只要把题意弄懂,清楚针对题目的算法,用STL来描述
算法还是比较容易、方便、快捷的!
本题的题意是:每行输入两个单词a, b; a是正常单词,b是胖老鼠语言(兽语吧)!
然后有多组询问:每组给你兽语,让你输出对应这个兽语的正常单词。
我选择用STL的map来做,开始时我以为map可以这样定义:map<string, string>,我第一
次的代码是这么写的但编译不过去。于是赶紧查资料发现好多模板里的map定义都包含int,没有我
刚才的那种定义。我这是突然想起来,记得在描述map时,说它是:键——值的类型。所以那个值好像
就是int类型的。
所以这道题,我定义map<string, int>类型,再额外开辟一个 char s[][]; 用来存储正常
语言 。就像这样:
读入s1,s2;
e=1;
map[s2]=e++; // map的值是存到二维数组里的下标
strcpy(s[e-1], s1 );
询问时,读入s3,if(map[s3>0]), 直接输出map[s3]
否则,,,,,,,,
Accepted的代码:
#include <stdio.h>#include <string.h>#include <string>#include <map>#include <algorithm>using namespace std;char s1[20], s2[20], s3[20];char s[100007][15];int main(){ map<string, int>ma; map<string, int>::iterator it; int e=1; while(scanf("%c", &s1[0]) && s1[0]!=‘\n‘ ) { scanf("%s %s%*c", s1+1, s2 ); ma[s2]=e++ ; strcpy(s[e-1], s1); } int flag; while(scanf("%s", s3)!=EOF) { if(ma[s3]>=1) { printf("%s\n", s[ma[s3]] ); } if(ma[s3]==0) { printf("eh\n"); } } return 0;}