1050 String Subtraction
简单模拟
#include <bits/stdc++.h>
using namespace std;
int main()
{
string s, t;
getline(cin, s);
getline(cin, t);
unordered_map<char, int> mp;
for (char c : t) mp[c]++;
for (char c : s) {
if (!mp.count(c)) cout << c;
}
cout << '\n';
return 0;
}
1051 Pop Sequence
树状数组用来判断容量的限制(很容易想到大的数想先出,那比这个数小的一定都入栈了)
set用来判断顺序的合法性,谁出栈就删掉谁。如果这个数比上一个弹出的数大,在容量ok的情况下一定可行。如果弹出的数比上一个数小,那只能是弹出第一个比上一个数小的数。
#include <bits/stdc++.h>
using namespace std;
template<typename T>
class Fenwick {
public:
explicit Fenwick(int _n): n(_n) {
tree.resize(n + 10);
}
/**
* @param index 注意 原数组下标从1~n
*/
void add(int index, T value) {
assert(index >= 1 && index <= n + 1);
for (int i = index; i <= n; i += low_bit(i)) {
tree[i] += value;
}
}
/**
* @return 求 原数组下标范围 [1, index] 的区间和
*/
T sum(int index) {
assert(index >= 0 && index <= n);
T res = 0;
for (int i = index; i; i -= low_bit(i)) {
res += tree[i];
}
return res;
}
private:
static inline int low_bit(int x) {
return x & -x;
}
vector<T> tree;
int n;
};
int main()
{
ios::sync_with_stdio(false);
cin.tie(nullptr), cout.tie(nullptr);
int M, N, K; cin >> M >> N >> K;
vector<int> v;
for (int i = 1; i <= N; i++) v.push_back(i);
for (int i = 0; i < K; i++) {
Fenwick<int> fenwick(N + 1);
set<int> st(v.begin(), v.end());
for (int j = 1; j <= N; j++) fenwick.add(j, 1);
int pre = -1, ok = 1;
for (int j = 0; j < N; j++) {
int x; cin >> x;
if (!ok) continue;
// 判断容量
int sm = fenwick.sum(x);
if (sm > M) ok = 0;
fenwick.add(x, -1);
// 判断次序
if (j == 0) {
pre = x;
st.erase(x);
continue;
}
if (x < pre) {
auto itr = st.lower_bound(pre);
if (itr == st.begin()) {
ok = 0;
continue;
}
itr = prev(itr);
if (*itr ^ x) ok = 0;
}
st.erase(x);
pre = x;
}
if (ok) cout << "YES" << '\n';
else cout << "NO\n";
}
return 0;
}
1052 Linked List Sorting
排序模拟 最后一个样例是链表只有头节点…
#include <bits/stdc++.h>
using namespace std;
int main()
{
ios::sync_with_stdio(false);
cin.tie(nullptr), cout.tie(nullptr);
int n; string head;
cin >> n >> head;
map<string, int> val;
map<string, string> son;
for (int i = 0; i < n; i++) {
string id, v, ne; cin >> id >> v >> ne;
son[id] = ne;
val[id] = stoi(v);
}
vector<pair<int, string>> v;
while (true) {
if (!val.count(head)) break;
v.emplace_back(val[head], head);
head = son[head];
if (head == "-1") break;
}
if (v.empty()) {
cout << "0 -1\n";
return 0;
}
sort(v.begin(), v.end());
cout << v.size() << ' ' << v[0].second << '\n';
auto sz = v.size();
for (int i = 0; i < sz; i++) {
cout << v[i].second << ' ' << v[i].first << ' ';
if (i == sz - 1) cout << -1 << '\n';
else cout << v[i + 1].second << '\n';
}
return 0;
}
1053 Path of Equal Weight
因为是树 两点之间路径唯一 dfs bfs都可以
bfs
#include <bits/stdc++.h>
using namespace std;
int main()
{
ios::sync_with_stdio(false);
cin.tie(nullptr), cout.tie(nullptr);
int n, m, target; cin >> n >> m >> target;
vector<int> w(n);
for (int i = 0; i < n; i++) cin >> w[i];
vector<vector<int>> son(n);
while (m--) {
int fa, cnt; cin >> fa >> cnt;
while (cnt--) {
int s; cin >> s;
son[fa].push_back(s);
}
}
vector<vector<int>> res;
vector<int> fa(n);
fa[0] = -1;
queue<int> qu;
qu.push(0);
while (!qu.empty()) {
auto t = qu.front();
qu.pop();
if (son[t].empty()) {
vector<int> v;
int x = t, sum = 0;
while (true) {
sum += w[x];
v.push_back(w[x]);
x = fa[x];
if (x == -1) break;
}
if (sum ^ target) continue;
reverse(v.begin(), v.end());
res.emplace_back(v);
continue;
}
for (int x : son[t]) {
qu.push(x);
fa[x] = t;
}
}
sort(res.begin(), res.end(), greater<>());
for (const auto& v : res) {
auto zz = v.size();
cout << v[0];
for (int i = 1; i < zz; i++) cout << ' ' << v[i];
cout << '\n';
}
return 0;
}