当前位置: 代码迷 >> 综合 >> HDU1075“What Are You Talking About”
  详细解决方案

HDU1075“What Are You Talking About”

热度:96   发布时间:2023-10-31 09:09:27.0

原题链接:http://acm.hdu.edu.cn/showproblem.php?pid=1075

大意:将火星文翻译成对应的单词,除了字符和字典里没有的单词可以不变,

这个题如果用常规的两个数组存入和依次去比较会超时。从其他博主哪里学到了一个新东西,map容器,容器很好用,但是有时候总是会忘记使用它。

Map是STL的一个关联容器,它提供一对一(其中第一个可以称为关键字,每个关键字只能在map中出现一次,第二个可能称为该关键字的值)的数据 处理能力。

数据的查找(包括判定这个关键字是否在map中出现)
在这里我们将体会,map在数据插入时保证有序的好处。
要判定一个数据(关键字)是否在map中出现的方法比较多,这里标题虽然是数据的查找,在这里将穿插着大量的map基本用法。
这里给出三种数据查找方法
第一种:用count函数来判定关键字是否出现,其缺点是无法定位数据出现位置,由于map的特性,一对一的映射关系,就决定了count函数的返回值只有两个,要么是0,要么是1,出现的情况,当然是返回1了
第二种:用find函数来定位数据出现位置,它返回的一个迭代器,当数据出现时,它返回数据所在位置的迭代器,如果map中没有要查找的数据,它返回的迭代器等于end函数返回的迭代器,程序说明
#include <map>
#include <string>
#include <iostream>
using namespace std;
int main()
{map<int, string> mapStudent;mapStudent.insert(pair<int, string>(1, "student_one"));mapStudent.insert(pair<int, string>(2, "student_two"));mapStudent.insert(pair<int, string>(3, "student_three"));map<int, string>::iterator iter;iter = mapStudent.find(1);if(iter != mapStudent.end()){cout<<"Find, the value is "<<iter->second<<endl;}Else{cout<<"Do not Find"<<endl;}
#include<iostream>       
#include<cstdlib>      
#include<cstdio> 
#include<cstring>      
#include<cmath>           
#include<string>      
#include<cstdlib>      
#include<iomanip>      
#include<vector>      
#include<list>      
#include<map>      
#include<queue>    
#include<algorithm>    
using namespace std;
int main()
{char buf[12], s1[12], s2[12], ch;map<string, string> mp;int id = 0;gets(buf); while(scanf("%s%s", s1, s2), strcmp(s1, "END")){mp[s2] = s1;}getchar();while(scanf("%c", &ch)){		if(isalpha(ch)) buf[id++] = ch; //读取每个字符,是字符就储存到bufelse  //否则就翻译已经输入的火星文{	buf[id] = '\0';id = 0;if(strcmp(buf, "END") == 0) break;if(mp.find(buf) != mp.end()) {cout << mp[buf]; //找到对应的就输出 }else printf("%s", buf); //否则输出原字符串 putchar(ch);}}return 0;
}