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);
for (int i = 1; i <=4; i++) {
for (int j = 0; j < m; j++) {
if (i >= vec[j][0] && i <= vec[j][1]) {
sum += vec[j][2];
break;
}
}
}
cout << sum<<endl;
system("pause");
return 0;
}
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;
}
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;
}
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;
}
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);
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;
int n;
n = str.find('@');
str1 = str.substr(0, n);
n = str.length() - n;
str2 = str.substr(str.find('@')+1, n);
if (str2.size() == 0) {
cout << str3;
system("pause");
return 0;
}
map<char, int> mp;
int wz;
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++) {
if ((*it).second == 0)
continue;
cout << (*it).first << ":" << (*it).second;
if (num < mp.size()) {
cout << ",";
num++;
}
}
system("pause");
return 0;
}