6.21 学习c++

1.vector 贪心

#include<iostream>
#include<algorithm>
#include<vector>
using namespace std;
bool cmp(vector<int> vec1, vector<int> vec2) {
	return vec1[2] < vec2[2];
}
int main01() {
	int n, m;
	cin >> n >> m;
	int num = 0;
	vector<vector<int>> vec;
	for (int i = 0; i < m; i++){
		vec.push_back(vector<int>());
	for (int j = 0; j < 3; j++) {
		cin >> num;
		vec[i].push_back(num);
	}
}
	int sum = 0;
	sort(vec.begin(), vec.end(),cmp);
	//cout << endl;
/*	for (int i = 0; i < m; i++) {
		for (int j = 0; j < 3; j++) {
			cout << vec[i][j] << " ";
			
		}
		cout << endl;
	}*/
	for (int i = 1; i <=4; i++) {
		for (int j = 0; j < m; j++) {
			if (i >= vec[j][0] && i <= vec[j][1]) {
				//cout << vec[j][2] << " ";
				sum += vec[j][2];
				break;
			}				
		}
}			
		cout << sum<<endl;
	system("pause");
	return 0;
}
//4
//6
//2 3 10
//2 4 20
//1 3 15
//1 4 25
//3 4 8
//1 4 16


2.最大括号镶嵌深度 stack

#include<iostream>
#include<string>
#include<algorithm>
#include<stack>
using namespace std;
int main() {
	string str;
	stack<char> sta;
	getline(cin, str);
	int mm = 0;
	int i=0;
	for (i = 0; i < str.length(); i++) {
		char f = str[i];
		if (f == '(' ||f == '[' || f == '{') {
			sta.push(f);
			int a = sta.size();
			mm = max(mm, a);
		}
		else
		{
			if (sta.size()== 0) {
				break;
			}
			if (f == ')') {
				if (sta.top() == '(') {
					sta.pop();
					continue;
				}	
			}
			else if (f == ']') {
				if (sta.top() == '[') {
					sta.pop();
					continue;
				}
			}
			else {
				if (sta.top() == '{') {
					sta.pop();
					continue;
				}
			}
			break;
		}
	}
	if (i == str.length() && sta.size() == 0)
		cout << mm << endl;
	else
		cout << 0<<endl;
	system("pause");
	return 0;
}
//输入描述:
//一个只包括 ‘(’, ‘)’, ‘ { ’, ‘ }’, ‘[’, ']'的字符串
//输出描述:
//一个整数,最大的括号深度
//
//示例 1:
//输入
//[]
//输出
//1
//说明
//有效字符串,最大嵌套深度为1
//
//示例 2:
//输入
//([] {()})
//输出
//3
//说明
//有效字符串,最大嵌套深度为3
//
//示例 3:
//输入
//(]
//输出
//0
//说明
//无效字符串,有两种类型的左右括号数量不相等
//
//示例 4:
//输入
//([)]
//输出
//0
//说明
//无效字符串,存在未按正确顺序闭合的括号
//原文链接:https ://blog.csdn.net/weixin_44052055/article/details/123946643

3.滑动窗口最大值

#include<iostream>
#include<queue>
#include<algorithm>
#include<cmath>
using namespace std;
int main() {
	int n;
	cin >> n;
	int* nums = new int[n];
	queue<int> que;
	int nn;
	int num;
	for (int i = 0; i < n; i++) {
		cin >> num;
		que.push(num);
	}
	int fast = 0, low = 0;
	int m;
	cin >> m;
	int sum = 0;
	int mmax = 0;
	for (int i = 0; i < m; i++) {
		nums[i] = que.front();
		que.pop();
		sum += nums[i];
		mmax = sum;
	}
	for (int i = m; i < n; i++) {
		nums[i] = que.front();
		que.pop();
		sum += nums[i] - nums[i - m];
		mmax = max(mmax,sum);
	}
	cout << mmax << endl;
	system("pause");
	return 0;
}
//输入描述:
//第一行输入一个正整数N,表示整数个数。(0 < N < 100000)
//第二行输入N个整数,整数的取值范围为[-100, 100]。
//第三行输入一个正整数M,M代表窗口的大小,M <= 100000,且M <= N。
//输出描述:
//窗口滑动产生所有窗口和的最大值。
//示例 1 输入输出示例仅供调试,后台判题数据一般不包含示例
//
//输入
//6
//12 10 20 30 15 23
//3
//输出
//68
//原文链接:https ://blog.csdn.net/weixin_44052055/article/details/123901424

4.DNA序列 queue

#include<iostream>
#include<string>
#include<queue>
#include<algorithm>
using namespace std;
int main() {
	string str;
	int n;
	getline(cin, str);
	cin >> n;
	int xb = 0;
	int num=0;
	int num_max=0;
	queue<int> que;
	for (int i = 0; i < n; i++) {
		if (str[i] == 'C' || str[i] == 'G') {
			num++;
			num_max = max(num_max, num);
			xb = i - n + 1;
		}
		que.push(str[i]);
	}
	for (int i = n; i < str.length(); i++) {
		if (str[i] == 'C' || str[i] == 'G')
			num++;
		if (que.front() == 'C' || que.front() == 'G')
			num--;
		if (num > num_max) {
			num_max = num;
			xb = i - n + 1;
		}
		que.pop();
		que.push(str[i]);
	}
	for (int i = xb; i < xb + n; i++)
		cout << str[i];
	cout << endl;
	system("pause");
	return 0;
}
//输入描述:
//输入一个string型基因序列,和int型子串的长度
//输出描述:
//找出GC比例最高的子串, 如果有多个则输出第一个的子串
//
//示例1:
//输入
//ACGT
//2
//输出
//CG
//说明
//ACGT长度为2的子串有AC, CG, GT3个,其中AC和GT2个的GC - Ratio都为0.5,CG为1,故输出CG
//
//示例2:
//输入
//AACTGTGCACGACCTGA
//5
//输出
//GCACG
//说明
//虽然CGACC的GC - Ratio也是最高,但它是从左往右找到的GC - Ratio最高的第2个子串,所以只能输出GCACG。
//
//原文链接:https ://blog.csdn.net/weixin_44052055/article/details/124121185

5.数字涂色

#include<iostream>
#include<algorithm>
using namespace std;
int main() {
	int n;
	cin >> n;
	int* nn = new int[n];
	for (int i = 0; i < n; i++)
		cin >> nn[i];
	sort(nn, nn + n);
	int num = 0;
	for (int i = 0; i < n; i++) {
		for (int j = 0; j < n; j++) {
			if (nn[i] % nn[j] == 0&&nn[i]!=nn[j]) {
				num++;
				break;
			}
		}
	}
	cout << n-num<< endl;
	system("pause");
	return 0;
}

6.字符串统计(全量和占用字符集)

#include<iostream>
#include<algorithm>
#include<map>
#include<string>
using namespace std;
int cco(string str) {
	str[0] = '0';
	int ss;
	ss = stoi(str);
	//cout << "stoi:  " << str << endl;
	//cout << " stoi= " << ss << endl;
	return ss;
}
int main() {
	string str;
	getline(cin, str);
	string str1,str2,str3;
	str3 = str;
	for (int i = 0; i < str.length(); i++)
		if (str[i] == ':')
			str[i] = NULL;
	//cout << "str: " << str << endl;
	int n;
	n = str.find('@');
	str1 = str.substr(0, n);
	//cout << "str1: " << str1 << endl;
	n = str.length() - n;
	str2 = str.substr(str.find('@')+1, n);
	//cout << "str2: " << str2 << endl;
	if (str2.size() == 0) {
		cout << str3;
		system("pause");
		return 0;
	}
	map<char, int> mp;
	int wz;
	//cco(str1.substr(wz + 1, str1.find(',', wz)-1));
	wz = 0;
	while (wz!=str1.length())
	{
		int pos;
		pos = str1.find(',', wz)+1;
		if (pos == 0)
			pos = str1.length();
		mp[str1[wz]] = cco(str1.substr(wz+1,pos-1));
		wz = pos;
	}
	wz = 0;
	while (wz != str2.length())
	{
		int pos;
		pos = str2.find(',', wz) + 1;
		if (pos == 0)
			pos = str2.length();
		mp[str2[wz]] -= cco(str2.substr(wz + 1, pos - 1));
		wz = pos;
	}
	int num = 1;
	for (map<char, int>::iterator it = mp.begin(); it != mp.end(); it++) {
		//cout << "char:  " << (*it).first << "   val:   " << (*it).second << endl;
		if ((*it).second == 0)
			continue;
		cout << (*it).first << ":" << (*it).second;
		if (num < mp.size()) {
			cout << ",";
			num++;
		}
	}
	//cout << mp.size() << endl;
	system("pause");
	return 0;
}
//输入描述:
//输入一个字符串 一定包含@,@前为全量字符集 @后的为已占用字符集
//已占用字符集中的字符,一定是全量字符集中的字符,字符集中的字符跟字符之间使用英文逗号隔开
//每个字符都表示为字符 + 数字的形式,用英文冒号分隔,比如a:1标识一个a字符
//字符只考虑英文字母,区分大小写,数字只考虑正整型 不超过100
//如果一个字符都没被占用,@标识仍存在
//输出描述 :
//输出可用字符集,
//不同的输出字符集之间用回车换行,
//注意:输出的字符顺序要跟输入的一致,不能输出b : 3, a : 2, c : 2
//如果某个字符已全部占用,则不需要再输出
//
//示例 1:
//输入
//a : 3, b : 5, c : 2@a:1, b:2
//输出
//a : 2, b : 3, c : 2
//说明:
//全量字符集为三个a,5个b,2个c
//已占用字符集为1个a,2个b
//由于已占用字符不能再使用
//因此剩余可用字符为2个a,3个b,2个c
//
//示例 2:
//输入
//a : 3, b : 5, c : 2@
//输出
//a : 3, b : 5, c : 2@
//原文链接:https ://blog.csdn.net/weixin_44052055/article/details/123926523
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值