比赛成绩查询问题II
时间限制(普通/Java) :
1000 MS/ 3000 MS 运行内存限制 : 81920 KByte
总提交 : 316 测试通过 : 33
总提交 : 316 测试通过 : 33
题目描述
2014“华为杯”南京邮电大学大学生团体歌唱大赛参赛团队的队名由“2014nupthw”和顺序号组合而成,例如2014nupthw001、2014nupthw002、2014nupthw028、2014nupthw089等。大赛结束后,任何人都可以通过队名查询任何一支队伍的排名,同时也可以通过排名查询队名,这里队伍排名从1开始且无并列情况。主办方提供所有参赛团队队名和排名的对应列表以及需要咨询的参赛团队队名或排名,请你完成此次大赛的成绩查询工作。
输入
输入包括多个行:第1行给出参赛团体总数N、需要查询成绩的参赛团队数;接下来有N行,每一行先后给出参赛团体的队名和最终排名;接下来有M行,每一行给出需要查询排名的参赛团队的队名或者需要查询参赛团体队名的排名。这里1≤N<10000,1≤M≤1000000。
输出
对应输入中最后M行要查询排名的参赛团队的队名或者需要查询参赛团体队名的排名,输出M行,每一行给出要查询的相应参赛团队队名、排名,以一个空格分隔上述两项内容。
样例输入
4 3
2014nupthw089 2
2014nupthw028 1
2014nupthw001 4
2014nupthw002 3
2014nupthw002
2
2014nupthw028
样例输出
2014nupthw002 3
2014nupthw089 2
2014nupthw028 1
代码(超时):
#include<iostream>
#include<string>
#include<map>
using namespace std;
int main()
{
int i,n,m;
string str;
int id;
map<string,int> word;
cin >> n >> m;
for(i = 0; i < n; ++i)
{
cin >> str >> id;
word.insert(make_pair(str,id));
}
for(i = 0; i < m;)
{
string str1;
cin>>str1;
if (str1.length()>10) {
map<string,int>::iterator it = word.find(str1);
cout << str1 << ' ' << it->second << endl;
}
else {
map<string,int>::iterator it = word.begin();
char *p=const_cast<char *>(str1.c_str());
while(it != word.end()){
if(it->second==atoi(p))
{
cout<<it->first<<' '<<str1<<endl;
break;
}
it++;
}
}
i++;
}
return 0;
}