STL <set>练习

找球号(一)

时间限制:3000 ms  |  内存限制:65535 KB
难度:3
描述
在某一国度里流行着一种游戏。游戏规则为:在一堆球中,每个球上都有一个整数编号i(0<=i<=100000000),编号可重复,现在说一个随机整数k(0<=k<=100000100),判断编号为k的球是否在这堆球中(存在为"YES",否则为"NO"),先答出者为胜。现在有一个人想玩玩这个游戏,但他又很懒。他希望你能帮助他取得胜利。
输入
第一行有两个整数m,n(0<=n<=100000,0<=m<=1000000);m表示这堆球里有m个球,n表示这个游戏进行n次。
接下来输入m+n个整数,前m个分别表示这m个球的编号i,后n个分别表示每次游戏中的随机整数k
输出
输出"YES"或"NO"
样例输入
6 4
23 34 46 768 343 343
2 4 23 343
样例输出
NO
NO
YES
YES

代码:

耗时:800ms set的二叉平衡查找树还蛮快!!!

#include <iostream>
#include <set>
#include <cstdio>
using namespace std;

int main()
{
   int x,n,m;
   set<int> s;
   cin>>n>>m;
   for(int i=1;i<=n;i++)
   {scanf("%d",&x); s.insert(x);}
    for(int i=1;i<=m;i++)
    {
       scanf("%d",&x);
      if(s.find(x)!=s.end())
       printf("YES\n");
      else
       printf("YES\n");
    }
    return 0;
}



 
#include <stdio.h>
#define MAXN 3125010 
int vis[MAXN] = {0} ;
int main()
{
	int m , n , x ; 
	int i ;
	scanf("%d%d", &m , &n ) ;
	for( i = 0 ; i < m ; ++i )
	{
		scanf("%d", &x ) ;
		vis[ x / 32 ] |= 1 << x % 32 ;
	}
	for( i = 0 ; i < n ; ++i )
	{
		scanf("%d", &x ) ;
		if( vis[ x / 32 ] & ( 1 << x % 32 ) )
			printf("YES\n");
		else
			printf("NO\n");
	}
	return 0 ;
}        


### C++ STL 练习题及答案 以下是基于引用内容设计的几道 C++ STL 练习题及其解答。 --- #### **练习题一:单词计数器** **题目描述**: 使用 `std::map` 实现一个简单的单词计数器程序。给定一段文字,统计其中每个单词出现的次数并按字母顺序输出结果。 **解决方案**: 为了实现此功能,可以利用 `std::map` 来存储单词与其对应的频率,并通过遍历输入文本完成统计工作[^1]。 ```cpp #include <iostream> #include <sstream> #include <map> #include <algorithm> int main() { std::string text; getline(std::cin, text); // 将所有字符转为小写以便统一处理 for(char& c : text){ c = tolower(c); } std::istringstream iss(text); std::string word; std::map<std::string, int> word_count; while(iss >> word){ ++word_count[word]; } // 输出结果 for(const auto& pair : word_count){ std::cout << pair.first << ": " << pair.second << "\n"; } return 0; } ``` --- #### **练习题二:字符串去重** **题目描述**: 利用 `std::set` 去除重复的字符串项。对于每一条指令,如果操作码为 `0`,表示插入新字符串;如果是 `1`,则查询当前是否存在指定字符串。 **解决方案**: 可以通过 `std::set` 自动去除重复元素的功能来简化逻辑[^2]。 ```cpp #include <bits/stdc++.h> using namespace std; int main(){ int n; cin >> n; set<string> st; for(int i = 0; i < n; ++i){ int opt; string word; cin >> opt >> word; if(opt == 0){ st.insert(word); }else{ if(st.find(word) != st.end()){ cout << "Yes\n"; }else{ cout << "No\n"; } } } return 0; } ``` --- #### **练习题三:消息队列管理** **题目描述**: 设计一个支持优先级的消息队列管理系统。当接收到 `"PUSH"` 指令时,需将带有特定优先级的消息存入队列;而接到 `"POP"` 指令时,则应弹出最高优先级的消息[^3]。 **解决方案**: 采用 `std::priority_queue` 结合自定义比较函数的方式能够高效解决此类问题。 ```cpp #include <iostream> #include <queue> #include <vector> #include <string> struct Message { std::string data; int priority; }; // 定义大顶堆 struct Compare { bool operator()(const Message& m1, const Message& m2) { if(m1.priority != m2.priority) return m1.priority < m2.priority; return true; // 若优先级相同,默认保持原序即可 } }; int main(){ std::priority_queue<Message, std::vector<Message>, Compare> pq; std::string command; while(getline(cin, command)){ if(command.substr(0,4) == "PUSH"){ size_t pos = command.find(' '); std::string str = command.substr(pos+1,command.rfind(' ')-pos-1); int pri = stoi(command.substr(command.rfind(' ')+1)); pq.push(Message{str,pri}); } else if(command == "POP"){ if(pq.empty()) std::cout << "NULL!\n"; else{ std::cout << pq.top().data << '\n'; pq.pop(); } } } return 0; } ``` ---
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值