Problem D: STL——字典
Problem D: STL——字典
Time Limit: 2 Sec Memory Limit: 128 MBSubmit: 7647 Solved: 1560
[Submit][Status][Web Board]
Description
输入n个字符串对(str1,str2),再输入k个查询字符串str,从字符串对中查找查询字符串,即如果str=str2,则输出str1,如果查询不到则输出"eh"(不包含引号)。输入保证所有字符串对的str2不相同,字符串只含有字母和数字,长度小于20!
Input
输入包含多组数据,直到文件结尾。
每组数据第一行包含一个整数n(0≤n≤10^5)。接下来n行,每行描述一个字符串对。
接下来包含一个整数m(0≤m≤10^5)。接下来m行,每行描述一个查询字符串。
见样例
Output
输出每个查询的结果。
Sample Input
5 dog ogday cat atcay pig igpay froot ootfray loops oopslay 3 atcay ittenkay oopslay
Sample Output
cat eh loops
HINT
用STL的map容易实现
很多人会说cin的速度比scanf慢很多, 其实不然. cin慢的原因主要在于默认cin与stdin总是保持同步, 这一步是消耗时间大户.
只需要加上std::ios::sync_with_stdio(false)来关闭同步就好了, 速度甚至要优于scanf.
Append Code
法1:
#include <iostream>
#include <map>
using namespace std;
int main()
{
ios::sync_with_stdio(false)//关闭同步流,提高输入速度
int n, m;
while(cin>>n)
{
map<string ,string> animal;
for(int i=0; i<n; i++)
{
string x, y;
cin>>x>>y;
animal.insert(make_pair(x,y));
}
cin>>m;
for(int i=0; i<m ;i++)
{
string temp;
cin>>temp;
map<string ,string> ::iterator p;
int t=0;
for(p=animal.begin(); p!=animal.end(); p++)
{
if(p->second==temp)
{
cout<<p->first<<endl;
t++;
}
}
if(t==0)
cout<<"eh"<<endl;
}
}
}
法2:
#include <iostream>
#include <map>
using namespace std;
int main()
{
ios::sync_with_stdio(false);//关闭cin同步流提高速度
int n;
while(cin>>n)
{
string arr, brr, crr;
map<string , string> s;
while(n--)
{
cin>>arr>>brr;
s[brr]=arr;
}
cin>>n;
while(n--)
{
cin>>crr;
if(s.count(crr)!=0)
cout<<s[crr]<<endl;
else
cout<<"eh"<<endl;
}
}
}
作者:7oDo
仅供参考,请勿抄袭。
Hang Hang Hang !!!

浙公网安备 33010602011771号