小红书 第一题第三题AC代码
第一题
#include <iostream>
#include <string>
using namespace std;
int main() {
string str; cin >> str;
string res = "";
int count = 0;
for(char ch : str){
if(ch == '('){
++count;
}else if(ch == ')'){
--count;
}else if(count == 0){
if(ch == '<'){
if(res.size() > 0) res.pop_back();
}else {
res.push_back(ch);
}
}
}
cout << res << endl;
return 0;
} 第三题,竟然不支持使用lambda表达式,真是难受
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
bool cmp(const pair<int, int> &a, const pair<int, int> &b){
return a.first == b.first ? a.second < b.second : a.first < b.first;
}
int main() {
int N; cin >> N;
vector<pair<int, int> > data(N);
for(int i = 0; i < N; ++i)
cin >> data[i].first >> data[i].second;
sort(data.begin(), data.end(), cmp);
vector<int> ivec;
for (int i = 0; i < N; ++i) {
if (ivec.size() == 0 || ivec.back() <= data[i].second)
ivec.push_back(data[i].second);
else {
int low = 0, high = ivec.size() - 1;
while (low < high) {
int mid = (low + high) / 2;
if (ivec[mid] <= data[i].second)
low = mid + 1;
else
high = mid;
}
ivec[low] = data[i].second;
}
}
cout << ivec.size() << endl;
return 0;
} #小红书##笔试题目##题解#