碎碎念
今天交作业,完成了50%,P1175,真气人
今天分享两道题的AC代码
题目原文请移步下面的链接
OI
、模拟
、字符串
、数据结构
、栈
普及-
#include <bits/stdc++.h>
using namespace std;
#define endl '\n';
void best_coder() {
string s;
cin >> s;
vector<string> c(55);
int j = 0, t = 0, l = 0;
for (int i = 0; i < s.size() - 1; ++i) {
if (s[i] == '.') {
c[j] = s.substr(t, i - t);
t = i + 1;
++l;
++j;
} else if (s[i] < '0' || s[i] > '9') {
c[j] = s[i];
++j;
++l;
t = i + 1;
}
}
stack<int> ans;
ans.push(stoi(c[0]));
int i = 1;
for (; i < l; ++i) {
if (c[i] < "0" || c[i] > "9") {
int a = ans.top();
ans.pop();
int b = ans.top();
ans.pop();
if (c[i] == "+") {
ans.push(a + b);
} else if (c[i] == "-") {
ans.push(b - a);
} else if (c[i] == "*") {
ans.push(a * b);
} else if (c[i] == "/") {
ans.push(b / a);
}
} else {
ans.push(stoi(c[i]));
}
}
cout << ans.top();
}
void happy_coder() {
}
int main() {
// // 提升cin、cout效率
// ios::sync_with_stdio(false);
// cin.tie(nullptr);
// cout.tie(nullptr);
// 小码匠
best_coder();
// 最优解
// happy_coder();
// 返回
return 0;
}
/**
* 8.3.2.6.*+5./-4.+@
*/
题目原文请移步下面的链接
OI
、模拟
、字符串
、数据结构
、栈
普及/提高-
#include <bits/stdc++.h>
using namespace std;
#define endl '\n';
void best_coder() {
int q;
cin >> q;
while (q--) {
int n;
cin >> n;
vector<int> a(n);
vector<int> b(n);
for (int i = 0; i < n; ++i) {
cin >> a[i];
}
for (int i = 0; i < n; ++i) {
cin >> b[i];
}
stack<int> s;
int j = 0;
for (int i = 0; i < n; ++i) {
s.push(a[i]);
while (!s.empty() && s.top() == b[j]) {
s.pop();
++j;
}
}
if (s.empty()) {
cout << "Yes" << endl;
} else {
cout << "No" << endl;
}
}
}
void happy_coder() {
}
int main() {
// // 提升cin、cout效率
// ios::sync_with_stdio(false);
// cin.tie(nullptr);
// cout.tie(nullptr);
// 小码匠
best_coder();
// 最优解
// happy_coder();
// 返回
return 0;
}
END