换换换 码题集

文章讲述了如何在C++中使用std::vector动态添加字符串,以及std::map进行字符串查找和交换,以避免for循环导致的时间复杂度过高。通过介绍std::map的用法,如插入、访问、遍历和删除元素,展示了如何提升程序性能和避免超时问题。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

难度:钻石 时间限制:1秒 占用内存:128M
机器人小R在看一个魔术表演,魔术师面前有一排共N个倒扣着的杯子,其中每一个下面都有一个小玩具t; ,并且每个小玩具都是唯一的。 魔术师飞快地变换杯子之后让小R
猜其中M个玩具tj 在哪个杯子里。 由于机器人小R内置的程序只能记录杯子的数量
N,小玩具初始的位置p,以及魔术师每次变换杯子的位置P1,P2 。小R的主人希望你
能写一个程序,帮助小R找出玩具。
在这里插入图片描述
输入
5 5 5
car
van
track
dog
cat
1 2
4 5
3 2
5 1
1 4
van
car
track
dog
cat
输出
5
3
2
4
1
在这里插入图片描述
原始思路:for循环,但会运行超时

#include<bits/stdc++.h> 

using namespace std;
int main(){
	int n,m,t;
	cin>>n>>m>>t;
	string s[n];
	for(int i=0;i<n;i++){
		cin>>s[i];
	}
	for(int i=0;i<t;i++){
		int c;
		int b;
		cin>>c>>b;
		string q=s[c-1];
		s[c-1]=s[b-1];
		s[b-1]=q;
	}
	int p[m];
	for(int i=0;i<m;i++){
		string q;
		cin>>q;
		for(int j=0;j<n;j++){
			if(s[j]==q){
				p[i]=j+1;
				break;
			}
		}
	}
	for(int i=0;i<m;i++){
		cout<<p[i]<<endl;
	} 
	 return 0;
}

vector可以动态添加字符串

#include <vector>  
#include <string>  
  
std::vector<std::string> stringVector; // 可以动态添加字符串  
stringVector.push_back("Hello");  
stringVector.push_back("World");  

// 使用std::array(如果数量是固定的)  
#include <array>  
std::array<std::string, 5> stringArray;  
stringArray[0] = "Hello";  
stringArray[1] = "World";

运用swap()函数,可以省掉一个交换过程代码,运用map,可以省略for循环的一些过程,使得程序不再超时。
在C++中,std::map是一个关联容器,它存储的元素都是键值对,并根据键的值自动排序。std::map中的元素都是唯一的,这意味着任何两个元素都不能有相同的键。

以下是使用std::map的基本步骤:

包含头文件:
首先,你需要包含头文件来使用std::map。
cpp
#include

#include
声明map:
声明一个std::map,指定键和值的类型。
cpp
std::map<int, std::string> myMap; // 键是int类型,值是std::string类型
插入元素:
你可以使用insert成员函数或者operator[]来插入元素。
使用insert函数:

cpp
myMap.insert(std::pair<int, std::string>(1, “one”));
myMap.insert(std::pair<int, std::string>(2, “two”));
或者使用operator[](如果键不存在,会创建它):

cpp
myMap[1] = “one”;
myMap[2] = “two”;
访问元素:
通过键来访问对应的值。
cpp
std::string value = myMap[1]; // value现在是"one"
std::cout << value << std::endl;
注意,使用operator[]访问不存在的键时,会创建一个新的键值对,键是请求的键,值是类型的默认值。如果你想要检查键是否存在而不创建新的键值对,应该使用find成员函数。

遍历map:
可以使用迭代器来遍历std::map中的所有元素。
cpp
for (std::map<int, std::string>::iterator it = myMap.begin(); it != myMap.end(); ++it) {
std::cout << it->first << ": " << it->second << std::endl;
}
或者使用C++11引入的范围for循环:

cpp
for (const auto& kv : myMap) {
std::cout << kv.first << ": " << kv.second << std::endl;
}
删除元素:
使用erase成员函数来删除元素。
cpp
myMap.erase(1); // 删除键为1的元素
或者删除一个迭代器指向的元素:

cpp
std::map<int, std::string>::iterator it = myMap.find(2);
if (it != myMap.end()) {
myMap.erase(it);
}
检查元素是否存在:
使用find成员函数来检查一个键是否在std::map中。
cpp
std::map<int, std::string>::iterator it = myMap.find(3);
if (it != myMap.end()) {
std::cout << "Key 3 found with value: " << it->second << std::endl;
} else {
std::cout << “Key 3 not found.” << std::endl;
}
获取map的大小:
使用size成员函数来获取std::map中元素的数量。
cpp
std::cout << "Size of map: " << myMap.size() << std::endl;
这些是std::map的基本用法。在实际编程中,你可以根据具体需求来使用这些功能。注意,std::map中的元素默认是按键的升序排列的,你也可以通过提供一个比较函数或对象来改变排序规则。

#include<bits/stdc++.h> 

using namespace std;
int main(){
	int n,m,t;
	cin>>n>>m>>t;
	string s[n];
	map<string,int>mp; 
	for(int i=0;i<n;i++){
		cin>>s[i];
		mp[s[i]]=i+1;
	}
	for(int i=0;i<t;i++){
		int c;
		int b;
		cin>>c>>b;
		swap(s[c-1],s[b-1]);
		swap(mp[s[c-1]],mp[s[b-1]]);
	}
	for(int i=0;i<m;i++){
		string q;
		cin>>q;
		cout<<mp[q]<<endl;
		}	 
	 return 0;

}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

且从容.

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值