返回题解分享
讨论 / 题解分享/ 帖子详情

map的查询 - 题解

#include <bits/stdc++.h>

using namespace std;

map<string,string> m;

int main()

{

	int n,k;

	string name,tel;

	cin>>n>>k;

	for(int i=0;i<n;i++)

	{

		cin>>name>>tel;

		m[name]=tel;

	}

	while(k--)

	{

		int c;

		string x;

		cin>>c>>x;

		if(c==1){

			auto it = m.find(x);

			if(it!=m.end()) cout<<it->second<<endl;

			else cout<<"NO"<<endl;

		}else if(c==2){

			auto it = m.lower_bound(x);

			if(it!=m.begin()){

				--it;

				cout<<it->second<<endl;

			}else cout<<"NO"<<endl;

		}else if(c==3){

			auto it = m.upper_bound(x);

			if(it!=m.end()){

				cout<<it->second<<endl;

			}else cout<<"NO"<<endl;

		}

	}

	return 0;

}
0 回复 0 转发 0 喜欢 5 阅读
回复 (0)
默认 最新
暂无回复,快来抢沙发!