题解分享
题解分享简介
map的查询 - 题解
include
include
include
using namespace std;
int main()
{
map
mp;
string name, number;
int n, m;
cin >> n >> m;
for (int i = 1; i
> name >> number;
mp[name] = number;
}
for (int i = 1; i
> c >> x;
if (c == 1)
{
auto it = mp.find(x);
if (it != mp.end())
{
cout
second
second
second << endl;
}
else
cout << "NO" << endl;
}
}
return 0;
}
查看全文
0
0
1
3
map的查询 - 题解
```
#include
#include<map>
#include<utility>
using namespace std;
map<string,string>ma;
pair<string,string>p;
int main()
{
int m, n, a;
string s,t,str;
cin>>n>>m;
while(n--)
{
cin>>s>>t;
p.first = s;
p.second = t;
ma.insert(p);
}
while(m--)
{
cin>>a>>str;
if(a==1)
{
if(ma.find(str)!=ma.end())
{
cout<<ma[str];
}
else{
cout<<"NO";
}
}
if(a==2)
{
if(str<=(ma.begin()->first))
{
cout<<"NO";
}
else
{
auto it = ma.lower_bound(str);
it--;
cout<<it->second;
}
}
if(a==3)
{
if(str>=((--ma.end())->first))
{
cout<<"NO";
}
else
{
auto it = ma.upper_bound(str);
cout<<it->second;
}
}
cout<<endl;
}
}
```
查看全文
0
0
2
3
map的查询 - 题解
```cpp
#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
6
map的查询 - 题解
```
import java.util.*;
import java.io.*;
public class Main{
public static void main(String[] args)throws IOException{
BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st = new StringTokenizer(in.readLine());
int n = Integer.parseInt(st.nextToken());
int m = Integer.parseInt(st.nextToken());
TreeMap<String,String> map = new TreeMap<>();
for(int i=0;i<n;i++){
StringTokenizer st2 = new StringTokenizer(in.readLine());
String name = st2.nextToken();
String number = st2.nextToken();
map.put(name,number);
}
for(int i=0;i<m;i++){
StringTokenizer st2 = new StringTokenizer(in.readLine());
int j = Integer.parseInt(st2.nextToken());
String name = st2.nextToken();
if(j==1){
System.out.println(map.getOrDefault(name,"NO"));
}
else if(j==2){
String lower = map.lowerKey(name);
if(lower == null) System.out.println("NO");
else System.out.println(map.getOrDefault(lower,"NO"));
}
else if(j==3){
String higher = map.higherKey(name);
if(higher == null) System.out.println("NO");
else System.out.println(map.getOrDefault(higher,"NO"));
}
}
}
}
```
查看全文
0
0
0
3
map的查询 - 题解
```
#include <iostream>
#include <map>
#include <string>
using namespace std;
int main() {
int n, m;
cin >> n >> m;
map<string, string> users;
for(int i = 0; i < n; i++) {
string name, num;
cin >> name >> num;
users[name] = num;
}
for(int i = 0; i < m; i++) {
int c;
string name;
cin >> c >> name;
if(c == 1) {
auto it = users.find(name);
if(it != users.end()) {
cout << it->second << endl;
} else {
cout << "NO" << endl;
}
} else if(c == 2) {
auto it = users.lower_bound(name);
if(it != users.begin()) {
--it;
cout << it->second << endl;
} else {
cout << "NO" << endl;
}
} else if(c == 3) {
auto it = users.upper_bound(name);
if(it != users.end()) {
cout << it->second << endl;
} else {
cout << "NO" << endl;
}
}
}
return 0;
}
```
查看全文
0
0
0
2
map的查询 - 题解
用文档中给的代码总有一个测试点TLE,故自己写了一个,成功AC。
```cpp
#include <iostream>
#include <algorithm>
using namespace std;
int main()
{
int n, t[1000100];
cin >> n;
for (int i = 0; i < n; i++)
scanf("%d", &t[i]);
sort(t, t + n);
printf("%d", t[0]);
for (int i = 1; i < n; i++)
if (t[i] != t[i - 1])
printf(" %d", t[i]);
puts("");
}
```
查看全文
0
0
0
4



