Homework New
This commit is contained in:
@@ -1,43 +1,214 @@
|
||||
#include <iostream>
|
||||
#include <string>
|
||||
using namespace std;
|
||||
//<2F>뷨<EFBFBD><EBB7A8><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʽ<EFBFBD><CABD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>м<EFBFBD><D0BC>㣨<EFBFBD><E3A3A8><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
||||
//<2F><>ȻҲ<C8BB><D2B2><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ջ<EFBFBD><D5BB>һ<EFBFBD><D2BB><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>һ<EFBFBD><D2BB><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>֣<EFBFBD><D6A3><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ң<EFBFBD>
|
||||
|
||||
int map(char c) {
|
||||
if (c == '{') return 4;
|
||||
if (c == '[') return 3;
|
||||
if (c == '(') return 2;
|
||||
if (c == '<') return 1;
|
||||
return 0;
|
||||
//<2F><><EFBFBD><EFBFBD>
|
||||
long long fastpow(long long d, long long u){
|
||||
//d^u<><75><EFBFBD>ÿ<EFBFBD><C3BF><EFBFBD><EFBFBD><EFBFBD>
|
||||
long long result = 1, base = d, b = u;
|
||||
while(b > 0){
|
||||
if(b & 1) result *= base;
|
||||
base *= base;
|
||||
b >>= 1;
|
||||
}
|
||||
return result;
|
||||
}
|
||||
int level(char c){
|
||||
switch(c){
|
||||
case '+':
|
||||
case '-': {
|
||||
return 1;
|
||||
break;
|
||||
}
|
||||
case '*':
|
||||
case '/': {
|
||||
return 2;
|
||||
break;
|
||||
}
|
||||
case '^': {
|
||||
return 3;
|
||||
break;
|
||||
}
|
||||
default: {
|
||||
return 0;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
bool match(char a, char b) {
|
||||
return (a == '(' && b == ')') || (a == '[' && b == ']') || (a == '{' && b == '}') || (a == '<' && b == '>');
|
||||
}
|
||||
|
||||
bool check(const string& s) {
|
||||
string front;
|
||||
for (int i = 0; i < s.size(); i++) {
|
||||
if (s[i] == '{' || s[i] == '[' || s[i] == '(' || s[i] == '<') {
|
||||
if (!front.empty() && map(front.back()) <= map(s[i]))
|
||||
return false;
|
||||
front.push_back(s[i]);
|
||||
} else if (s[i] == '}' || s[i] == ']' || s[i] == ')' || s[i] == '>') {
|
||||
if (front.empty() || !match(front.back(), s[i]))
|
||||
return false;
|
||||
front.pop_back();
|
||||
int findexp(const string& s, int l, int r) {
|
||||
int pos = -1;
|
||||
int min_level = 114514;
|
||||
int js = 0;
|
||||
for (int i = r; i >= l; i--) {
|
||||
if (s[i] == ')') js--;
|
||||
else if (s[i] == '(') js++;
|
||||
|
||||
if (js == 0 && level(s[i])) {
|
||||
// <20><><EFBFBD><EFBFBD><EFBFBD>ҽ<EFBFBD><D2BD>ϵ<EFBFBD>^<5E><>ʹ<EFBFBD><CAB9><<3C><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><=
|
||||
if ((s[i] == '^' && level(s[i]) < min_level) ||
|
||||
(s[i] != '^' && level(s[i]) <= min_level)) {
|
||||
min_level = level(s[i]);
|
||||
pos = i;
|
||||
}
|
||||
}
|
||||
}
|
||||
return front.empty();
|
||||
return pos;
|
||||
}
|
||||
|
||||
int main() {
|
||||
int n;
|
||||
cin >> n;
|
||||
for (int i = 0; i < n; i++) {
|
||||
string s;
|
||||
cin >> s;
|
||||
cout << (check(s) ? "Match" : "Fail") << endl;
|
||||
long long toNum(const string& s, int l, int r) {
|
||||
long long tmp = 0;
|
||||
|
||||
// <20><><EFBFBD><EFBFBD><EFBFBD>ո<EFBFBD>
|
||||
while (l <= r && s[l] == ' ') l++;
|
||||
while (l <= r && s[r] == ' ') r--;
|
||||
|
||||
if (l > r) return 0;
|
||||
|
||||
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ҵ<EFBFBD><D2B4><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
||||
for (int i = l; i <= r; i++) {
|
||||
if (isdigit(s[i])) {
|
||||
tmp = tmp * 10 + (s[i] - '0');
|
||||
} else {
|
||||
return 0; // <20>Ƿ<EFBFBD><C7B7>ַ<EFBFBD>
|
||||
}
|
||||
}
|
||||
return 0;
|
||||
|
||||
return tmp;
|
||||
}
|
||||
/*
|
||||
<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ŵİ汾
|
||||
long long toNum(const string& s, int l, int r){
|
||||
long long tmp = 0;
|
||||
bool neg = false;
|
||||
if (s[l] == '-') { neg = true; l++; }
|
||||
for(int i = l; i <= r; i++){
|
||||
if(isdigit(s[i])) tmp = tmp * 10 + (s[i] - '0');
|
||||
}
|
||||
return neg ? -tmp : tmp;
|
||||
}
|
||||
*/
|
||||
|
||||
struct Node{
|
||||
char sign;
|
||||
long long num;
|
||||
Node* left;
|
||||
Node* right;
|
||||
|
||||
Node(): sign('?'), num(-1), left(nullptr), right(nullptr) {}
|
||||
Node(char s): sign(s), num(-1), left(nullptr), right(nullptr) {}
|
||||
Node(long long n): sign('?'), num(n), left(nullptr), right(nullptr) {}
|
||||
};
|
||||
|
||||
class Expression{
|
||||
public:
|
||||
string exp;
|
||||
Node* root;
|
||||
long long result;
|
||||
bool isValid;
|
||||
|
||||
Expression(const string& s): exp(s), root(nullptr), result(-1), isValid(true) {}
|
||||
|
||||
void inorder(Node* r){
|
||||
if(!r) return;
|
||||
inorder(r->left);
|
||||
if (r->sign == '?') cout << r->num;
|
||||
else cout << r->sign;
|
||||
inorder(r->right);
|
||||
}
|
||||
|
||||
Node* build(const string& s, int l, int r){
|
||||
//[l, r]
|
||||
//<2F><><EFBFBD><EFBFBD><EFBFBD>ո<EFBFBD>
|
||||
while(l <= r && s[l] == ' ') l++;
|
||||
while(l <= r && s[r] == ' ') r--;
|
||||
if(l > r) return nullptr;
|
||||
//<2F><><EFBFBD><EFBFBD><EFBFBD>ⲿ<EFBFBD><E2B2BF><EFBFBD><EFBFBD>
|
||||
if(s[l] == '(' && s[r] == ')'){
|
||||
bool hasQuote = true;
|
||||
int js = 0;
|
||||
for(int i = l; i <= r; i++){
|
||||
if(s[i] == '(') js++;
|
||||
else if(s[i] == ')') js--;
|
||||
|
||||
if(js == 0 && i != r){
|
||||
hasQuote = false;
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (hasQuote) return build(s, l + 1, r - 1);
|
||||
}
|
||||
|
||||
int pos = findexp(s, l, r);
|
||||
//<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
||||
if(pos == -1) {
|
||||
long long x = toNum(s, l, r);
|
||||
//cout << "*" << r << endl; <20><>Ϊʲô<CAB2><C3B4><EFBFBD><EFBFBD>0
|
||||
return new Node(x);
|
||||
//<2F><><EFBFBD>·<EFBFBD><C2B7>ڵ<EFBFBD>
|
||||
}
|
||||
Node* tmp = new Node(s[pos]);
|
||||
tmp->left = build(s, l, pos - 1);
|
||||
tmp->right = build(s, pos + 1, r);
|
||||
return tmp;
|
||||
}
|
||||
|
||||
long long calc(Node* r, bool& isValid){
|
||||
if(!r) return 0;
|
||||
//<2F><><EFBFBD><EFBFBD>
|
||||
if(r->sign == '?') return r->num;
|
||||
|
||||
//<2F><><EFBFBD><EFBFBD>
|
||||
long long left = calc(r->left, isValid);
|
||||
long long right = calc(r->right, isValid);
|
||||
if(r->num == -1){
|
||||
char c = r->sign;
|
||||
switch(c){
|
||||
case '+': return left + right;
|
||||
case '-': return left - right;
|
||||
case '*': return left * right;
|
||||
case '/': {
|
||||
if(right == 0) {
|
||||
isValid = false;
|
||||
return 0;
|
||||
}
|
||||
else return left / right;
|
||||
}
|
||||
case '^': {
|
||||
if(left == 0 && right == 0) { isValid = false; return 0; }
|
||||
return fastpow(left, right);
|
||||
break;
|
||||
}
|
||||
default: return 0;
|
||||
}
|
||||
}
|
||||
}
|
||||
void print(){
|
||||
inorder(root);
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
|
||||
int main(){
|
||||
/*
|
||||
string s;
|
||||
getline(cin, s);
|
||||
Expression tree(s);
|
||||
tree.root = tree.build(s, 0, s.length() - 1);
|
||||
//tree.print();
|
||||
cout << tree.calc(tree.root, tree.isValid) << endl;
|
||||
//if(!tree.isValid && result != -1) cout << result <<endl;
|
||||
//else cout << "Invalid" << endl; */
|
||||
string s;
|
||||
while(getline(cin, s)){
|
||||
Expression tree(s);
|
||||
tree.root = tree.build(s, 0, s.length()-1);
|
||||
long long result = tree.calc(tree.root, tree.isValid);
|
||||
if(tree.isValid) cout << result << endl;
|
||||
else cout << "INVALID" << endl;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
0
Exercise/Homework2/Q1.rs
Normal file
0
Exercise/Homework2/Q1.rs
Normal file
191
Exercise/Homework2/Q1双栈做法.cpp
Normal file
191
Exercise/Homework2/Q1双栈做法.cpp
Normal file
@@ -0,0 +1,191 @@
|
||||
#include <bits/stdc++.h>
|
||||
using namespace std;
|
||||
|
||||
//---------------------------------------------
|
||||
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
||||
//---------------------------------------------
|
||||
const long long INF = 1LL << 31; // <20><>ĿҪ<C4BF><D2AA><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Χ [-2^31, 2^31)
|
||||
|
||||
//---------------------------------------------
|
||||
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ȼ<EFBFBD><C8BC><EFBFBD><EFBFBD><EFBFBD>
|
||||
//---------------------------------------------
|
||||
// <20><><EFBFBD><EFBFBD>һ<EFBFBD><D2BB><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ȼ<EFBFBD><C8BC>ȼ<EFBFBD><C8BC><EFBFBD><EFBFBD><EFBFBD>ֵԽ<D6B5><D4BD><EFBFBD><EFBFBD><EFBFBD>ȼ<EFBFBD>Խ<EFBFBD>ߣ<EFBFBD>
|
||||
int level(char op) {
|
||||
switch (op) {
|
||||
case '+': case '-': return 1; // <20>Ӽ<EFBFBD><D3BC><EFBFBD><EFBFBD><EFBFBD>
|
||||
case '*': case '/': return 2; // <20>˳<EFBFBD><CBB3>е<EFBFBD>
|
||||
case '^': return 3; // <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
||||
default: return 0; // <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
||||
}
|
||||
}
|
||||
|
||||
//---------------------------------------------
|
||||
// <20>ж<EFBFBD><D0B6><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ƿ<EFBFBD><C7B7><EFBFBD><EFBFBD>ҽ<EFBFBD><D2BD>ϵ<EFBFBD>
|
||||
//---------------------------------------------
|
||||
// ֻ<><D6BB><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>㣨^<5E><><EFBFBD><EFBFBD><EFBFBD>ҽ<EFBFBD><D2BD>ϣ<EFBFBD>a^b^c <20>ȼ<EFBFBD><C8BC><EFBFBD> a^(b^c)
|
||||
bool rightAssoc(char op) {
|
||||
return op == '^';
|
||||
}
|
||||
|
||||
//---------------------------------------------
|
||||
// <20><>ȫ<EFBFBD><C8AB><EFBFBD><EFBFBD><EFBFBD>ݺ<EFBFBD><DDBA><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>⣩
|
||||
//---------------------------------------------
|
||||
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ϵͳ pow<6F><77><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Լ<EFBFBD>д
|
||||
// ͬʱҪ<CAB1><D2AA><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ƿ<C7B7><F1B3ACB9><EFBFBD>ĿҪ<C4BF><D2AA><EFBFBD><EFBFBD>Χ<EFBFBD><CEA7><EFBFBD><EFBFBD> int32 <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
||||
long long fastpow(long long a, long long b, bool &valid) {
|
||||
long long res = 1;
|
||||
while (b > 0) {
|
||||
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ǰ<EFBFBD><C7B0><EFBFBD><EFBFBD>λ<EFBFBD><CEBB>1<EFBFBD><31><EFBFBD><EFBFBD>Ҫ<EFBFBD><D2AA>һ<EFBFBD><D2BB>
|
||||
if (b & 1) {
|
||||
// <20>ж<EFBFBD><D0B6>Ƿ<EFBFBD><C7B7><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
||||
if (a != 0 && llabs(res) > (INF - 1) / llabs(a)) {
|
||||
valid = false;
|
||||
return 0;
|
||||
}
|
||||
res *= a;
|
||||
}
|
||||
// <><D7BC>ƽ<EFBFBD><C6BD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
||||
b >>= 1;
|
||||
if (b) { // <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>û<EFBFBD><C3BB><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ж<EFBFBD>һ<EFBFBD><D2BB>ƽ<EFBFBD><C6BD><EFBFBD>Ƿ<EFBFBD><C7B7><EFBFBD>ȫ
|
||||
if (llabs(a) > (INF - 1) / llabs(a)) {
|
||||
valid = false;
|
||||
return 0;
|
||||
}
|
||||
a *= a;
|
||||
}
|
||||
}
|
||||
return res;
|
||||
}
|
||||
|
||||
//---------------------------------------------
|
||||
// ִ<><D6B4>һ<EFBFBD><D2BB><EFBFBD><EFBFBD><EFBFBD>㣺<EFBFBD><E3A3BA><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ջ<EFBFBD><D5BB>ȡ<EFBFBD><C8A1><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ͳ<EFBFBD><CDB2><EFBFBD><EFBFBD><EFBFBD>
|
||||
//---------------------------------------------
|
||||
bool apply(stack<long long> &num, stack<char> &op) {
|
||||
if (num.size() < 2 || op.empty()) return false; // <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
||||
long long b = num.top(); num.pop(); // ע<><D7A2>˳<EFBFBD><CBB3><EFBFBD><EFBFBD><EFBFBD>Ҳ<EFBFBD><D2B2><EFBFBD><EFBFBD><EFBFBD>
|
||||
long long a = num.top(); num.pop(); // <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
||||
char c = op.top(); op.pop(); // <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
||||
|
||||
long long res = 0;
|
||||
switch (c) {
|
||||
case '+': res = a + b; break;
|
||||
case '-': res = a - b; break;
|
||||
case '*': res = a * b; break;
|
||||
case '/':
|
||||
if (b == 0) return false; // <20><><EFBFBD>㱨<EFBFBD><E3B1A8>
|
||||
res = a / b; // <20><><EFBFBD><EFBFBD><EFBFBD>Զ<EFBFBD><D4B6><EFBFBD>β
|
||||
break;
|
||||
case '^': {
|
||||
if (b < 0) return false; // <20><>Ŀ<EFBFBD><C4BF>ָ֤<D6A4><D6B8><EFBFBD>Ǹ<EFBFBD><C7B8><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
||||
bool valid = true;
|
||||
res = fastpow(a, b, valid);
|
||||
if (!valid) return false; // <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
||||
break;
|
||||
}
|
||||
default:
|
||||
return false; // <20>Ƿ<EFBFBD><C7B7><EFBFBD><EFBFBD><EFBFBD>
|
||||
}
|
||||
|
||||
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Χ
|
||||
if (res >= INF || res < -INF) return false;
|
||||
|
||||
num.push(res);
|
||||
return true;
|
||||
}
|
||||
|
||||
//---------------------------------------------
|
||||
// <20><><EFBFBD><EFBFBD><EFBFBD>㺯<EFBFBD><E3BAAF><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>һ<EFBFBD>б<EFBFBD><D0B1><EFBFBD>ʽ
|
||||
//---------------------------------------------
|
||||
string calcExpr(const string &s) {
|
||||
stack<long long> num; // <20><><EFBFBD><EFBFBD>ջ
|
||||
stack<char> op; // <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ջ
|
||||
int n = s.size();
|
||||
|
||||
for (int i = 0; i < n; ) {
|
||||
if (isspace(s[i])) { // <20><><EFBFBD><EFBFBD><EFBFBD>ո<EFBFBD>
|
||||
i++;
|
||||
continue;
|
||||
}
|
||||
|
||||
//-------------------------------------
|
||||
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>֣<EFBFBD>֧<EFBFBD>ֶ<EFBFBD>λ<EFBFBD><CEBB><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
||||
//-------------------------------------
|
||||
if (isdigit(s[i])) {
|
||||
long long val = 0;
|
||||
while (i < n && isdigit(s[i])) {
|
||||
val = val * 10 + (s[i] - '0');
|
||||
if (val >= INF) return "INVALID"; // <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Χ
|
||||
i++;
|
||||
}
|
||||
num.push(val);
|
||||
}
|
||||
|
||||
//-------------------------------------
|
||||
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
||||
//-------------------------------------
|
||||
else if (s[i] == '(') {
|
||||
op.push('(');
|
||||
i++;
|
||||
}
|
||||
|
||||
//-------------------------------------
|
||||
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ţ<EFBFBD>Ҫ<EFBFBD><D2AA><EFBFBD>㵽<EFBFBD><E3B5BD>һ<EFBFBD><D2BB>'('Ϊֹ
|
||||
//-------------------------------------
|
||||
else if (s[i] == ')') {
|
||||
while (!op.empty() && op.top() != '(') {
|
||||
if (!apply(num, op)) return "INVALID";
|
||||
}
|
||||
if (op.empty()) return "INVALID"; // <20><><EFBFBD>Ų<EFBFBD>ƥ<EFBFBD><C6A5>
|
||||
op.pop(); // <20><><EFBFBD><EFBFBD> '('
|
||||
i++;
|
||||
}
|
||||
|
||||
//-------------------------------------
|
||||
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
||||
//-------------------------------------
|
||||
else {
|
||||
char now = s[i];
|
||||
if (level(now) == 0) return "INVALID"; // <20>Ƿ<EFBFBD><C7B7>ַ<EFBFBD>
|
||||
|
||||
// ջ<><D5BB><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ȼ<EFBFBD><C8BC><EFBFBD><EFBFBD><EFBFBD><DFBB><EFBFBD>ͬ<EFBFBD><CDAC><EFBFBD>Ҳ<EFBFBD><D2B2><EFBFBD><EFBFBD>ҽ<EFBFBD><D2BD>ϣ<EFBFBD><CFA3><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ȼ<EFBFBD><C8BC><EFBFBD>
|
||||
while (!op.empty() && op.top() != '(' &&
|
||||
(level(op.top()) > level(now) ||
|
||||
(level(op.top()) == level(now) && !rightAssoc(now)))) {
|
||||
if (!apply(num, op)) return "INVALID";
|
||||
}
|
||||
op.push(now);
|
||||
i++;
|
||||
}
|
||||
}
|
||||
|
||||
//-------------------------------------
|
||||
// <20><><EFBFBD><EFBFBD>ջ<EFBFBD><D5BB>ʣ<EFBFBD><CAA3><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
||||
//-------------------------------------
|
||||
while (!op.empty()) {
|
||||
if (!apply(num, op)) return "INVALID";
|
||||
}
|
||||
|
||||
//-------------------------------------
|
||||
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ջ<EFBFBD><D5BB>ֹһ<D6B9><D2BB><EFBFBD><EFBFBD>˵<EFBFBD><CBB5><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʽ<EFBFBD><CABD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
||||
//-------------------------------------
|
||||
if (num.size() != 1) return "INVALID";
|
||||
|
||||
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ս<EFBFBD><D5BD><EFBFBD>
|
||||
return to_string(num.top());
|
||||
}
|
||||
|
||||
//---------------------------------------------
|
||||
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>룬ÿ<EBA3AC><C3BF>һ<EFBFBD><D2BB><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʽ
|
||||
//---------------------------------------------
|
||||
int main() {
|
||||
ios::sync_with_stdio(false);
|
||||
cin.tie(nullptr);
|
||||
|
||||
string s;
|
||||
while (getline(cin, s)) {
|
||||
string ans = calcExpr(s);
|
||||
cout << ans << "\n";
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
194
Exercise/Homework2/Q1过了的版本.cpp
Normal file
194
Exercise/Homework2/Q1过了的版本.cpp
Normal file
@@ -0,0 +1,194 @@
|
||||
#include <iostream>
|
||||
#include <string>
|
||||
using namespace std;
|
||||
|
||||
//<2F><><EFBFBD><EFBFBD>
|
||||
long long fastpow(long long d, long long u){
|
||||
//d^u<><75><EFBFBD>ÿ<EFBFBD><C3BF><EFBFBD><EFBFBD><EFBFBD>
|
||||
long long result = 1, base = d, b = u;
|
||||
while(b > 0){
|
||||
if(b & 1) result *= base;
|
||||
base *= base;
|
||||
b >>= 1;
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
int level(char c){
|
||||
switch(c){
|
||||
case '+':
|
||||
case '-': {
|
||||
return 1;
|
||||
break;
|
||||
}
|
||||
case '*':
|
||||
case '/': {
|
||||
return 2;
|
||||
break;
|
||||
}
|
||||
case '^': {
|
||||
return 3;
|
||||
break;
|
||||
}
|
||||
default: {
|
||||
return 0;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
int findexp(const string& s, int l, int r) {
|
||||
int pos = -1;
|
||||
int min_level = 114514;
|
||||
int js = 0;
|
||||
for (int i = r; i >= l; i--) {
|
||||
if (s[i] == ')') js--;
|
||||
else if (s[i] == '(') js++;
|
||||
|
||||
if (js == 0 && level(s[i])) {
|
||||
int lev = level(s[i]);
|
||||
if (lev < min_level || (lev == min_level && s[i] == '^')) {
|
||||
min_level = lev;
|
||||
pos = i;
|
||||
}
|
||||
}
|
||||
}
|
||||
return pos;
|
||||
}
|
||||
|
||||
long long toNum(const string& s, int l, int r) {
|
||||
long long tmp = 0;
|
||||
|
||||
// <20><><EFBFBD><EFBFBD><EFBFBD>ո<EFBFBD>
|
||||
while (l <= r && s[l] == ' ') l++;
|
||||
while (l <= r && s[r] == ' ') r--;
|
||||
|
||||
if (l > r) return 0;
|
||||
|
||||
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ҵ<EFBFBD><D2B4><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
||||
for (int i = l; i <= r; i++) {
|
||||
if (isdigit(s[i])) {
|
||||
tmp = tmp * 10 + (s[i] - '0');
|
||||
} else {
|
||||
return 0; // <20>Ƿ<EFBFBD><C7B7>ַ<EFBFBD>
|
||||
}
|
||||
}
|
||||
|
||||
return tmp;
|
||||
}
|
||||
|
||||
struct Node{
|
||||
char sign;
|
||||
long long num;
|
||||
Node* left;
|
||||
Node* right;
|
||||
|
||||
Node(): sign('?'), num(-1), left(nullptr), right(nullptr) {}
|
||||
Node(char s): sign(s), num(-1), left(nullptr), right(nullptr) {}
|
||||
Node(long long n): sign('?'), num(n), left(nullptr), right(nullptr) {}
|
||||
};
|
||||
|
||||
class Expression{
|
||||
public:
|
||||
string exp;
|
||||
Node* root;
|
||||
long long result;
|
||||
bool isValid;
|
||||
|
||||
Expression(const string& s): exp(s), root(nullptr), result(-1), isValid(true) {}
|
||||
|
||||
void inorder(Node* r){
|
||||
if(!r) return;
|
||||
inorder(r->left);
|
||||
if (r->sign == '?') cout << r->num;
|
||||
else cout << r->sign;
|
||||
inorder(r->right);
|
||||
}
|
||||
|
||||
Node* build(const string& s, int l, int r){
|
||||
//[l, r]
|
||||
//<2F><><EFBFBD><EFBFBD><EFBFBD>ո<EFBFBD>
|
||||
while(l <= r && s[l] == ' ') l++;
|
||||
while(l <= r && s[r] == ' ') r--;
|
||||
if(l > r) return nullptr;
|
||||
//<2F><><EFBFBD><EFBFBD><EFBFBD>ⲿ<EFBFBD><E2B2BF><EFBFBD><EFBFBD>
|
||||
if(s[l] == '(' && s[r] == ')'){
|
||||
bool hasQuote = true;
|
||||
int js = 0;
|
||||
for(int i = l; i <= r; i++){
|
||||
if(s[i] == '(') js++;
|
||||
else if(s[i] == ')') js--;
|
||||
|
||||
if(js == 0 && i != r){
|
||||
hasQuote = false;
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (hasQuote) return build(s, l + 1, r - 1);
|
||||
}
|
||||
|
||||
int pos = findexp(s, l, r);
|
||||
//<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
||||
if(pos == -1) {
|
||||
long long x = toNum(s, l, r);
|
||||
return new Node(x);
|
||||
}
|
||||
Node* tmp = new Node(s[pos]);
|
||||
tmp->left = build(s, l, pos - 1);
|
||||
tmp->right = build(s, pos + 1, r);
|
||||
return tmp;
|
||||
}
|
||||
|
||||
long long calc(Node* r, bool& isValid){
|
||||
if(!r) return 0;
|
||||
//<2F><><EFBFBD><EFBFBD>
|
||||
if(r->sign == '?') return r->num;
|
||||
|
||||
//<2F><><EFBFBD><EFBFBD>
|
||||
long long left = calc(r->left, isValid);
|
||||
long long right = calc(r->right, isValid);
|
||||
if(r->num == -1){
|
||||
char c = r->sign;
|
||||
switch(c){
|
||||
case '+': return left + right;
|
||||
case '-': return left - right;
|
||||
case '*': return left * right;
|
||||
case '/': {
|
||||
if(right == 0) {
|
||||
isValid = false;
|
||||
return 0;
|
||||
}
|
||||
else return left / right;
|
||||
}
|
||||
case '^': {
|
||||
return fastpow(left, right);
|
||||
}
|
||||
default: return 0;
|
||||
}
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
void print(){
|
||||
inorder(root);
|
||||
}
|
||||
};
|
||||
|
||||
int main(){
|
||||
string s;
|
||||
while(getline(cin, s)){
|
||||
if (s.empty()) {
|
||||
cout << "INVALID" << endl;
|
||||
continue;
|
||||
}
|
||||
Expression tree(s);
|
||||
tree.root = tree.build(s, 0, s.length()-1);
|
||||
if (!tree.root) {
|
||||
cout << "INVALID" << endl;
|
||||
continue;
|
||||
}
|
||||
long long result = tree.calc(tree.root, tree.isValid);
|
||||
if(tree.isValid) cout << result << endl;
|
||||
else cout << "INVALID" << endl;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
@@ -1,50 +1,34 @@
|
||||
use std::io::{self, Read, Write};
|
||||
use std::io;
|
||||
|
||||
fn main() {
|
||||
/*
|
||||
fn main(){
|
||||
let core = "edgnb";
|
||||
let mut s = String::new();
|
||||
io::stdin().read_line(&mut s).unwrap();
|
||||
let n: usize = s.trim().parse().unwrap();
|
||||
let mut n: i32 = s.trim().parse().unwrap();
|
||||
|
||||
for i in 0..n{
|
||||
let mut js = 0;
|
||||
let mut cnt = 0;
|
||||
let mut str = String::new();
|
||||
io::stdin().read_line(&mut str).unwrap();
|
||||
let mut num = String::new();
|
||||
io::stdin().read_line(&mut num).unwrap();
|
||||
let mut m: i32 = num.trim().parse().unwrap();
|
||||
|
||||
let mut nums: Vec<i64> = Vec::new();
|
||||
let mut max: i64 = 0;
|
||||
let mut j = 0;
|
||||
while j <= str.len() - 5{
|
||||
if &str[j..j+5] == core {
|
||||
js += 1;
|
||||
j += 5;
|
||||
if js >= m {
|
||||
cnt += 1;
|
||||
}
|
||||
} else{
|
||||
js = 0;
|
||||
j += 1;
|
||||
}
|
||||
}
|
||||
|
||||
for _ in 0..n {
|
||||
let mut x = String::new();
|
||||
io::stdin().read_line(&mut x).unwrap();
|
||||
let a: i64 = x.trim().parse().unwrap();
|
||||
nums.push(a);
|
||||
if a > max {
|
||||
max = a;
|
||||
}*/
|
||||
// //输入太慢了过不了
|
||||
let mut buf = String::new();
|
||||
io::stdin().read_to_string(&mut buf).unwrap();
|
||||
let mut it = buf.split_whitespace();
|
||||
|
||||
let m: usize = it.next().unwrap().parse().unwrap();
|
||||
let mut vec: Vec<i64> = Vec::with_capacity(m);
|
||||
let mut max: i64 = 0;
|
||||
|
||||
for _ in 0..m {
|
||||
let a: i64 = it.next().unwrap().parse().unwrap();
|
||||
vec.push(a);
|
||||
if a > max { max = a; }
|
||||
println!("{}", cnt);
|
||||
}
|
||||
|
||||
let mut dp: Vec<i64> = vec![0; (max + 1) as usize];
|
||||
if max >= 0 { dp[0] = 1; }
|
||||
if max >= 1 { dp[1] = 1; }
|
||||
if max >= 2 { dp[2] = 2; }
|
||||
if max >= 3 { dp[3] = 4; }
|
||||
|
||||
for i in 4..=max {
|
||||
dp[i as usize] = dp[(i - 1) as usize] + dp[(i - 2) as usize] + dp[(i - 3) as usize] + dp[(i - 4) as usize];
|
||||
}
|
||||
|
||||
let mut out = String::new();
|
||||
for i in vec {
|
||||
out.push_str(&format!("{}\n", dp[i as usize]));
|
||||
}
|
||||
io::stdout().write_all(out.as_bytes()).unwrap();
|
||||
}
|
||||
}
|
||||
@@ -1,39 +1,79 @@
|
||||
use std::io;
|
||||
use std::io::{self, BufRead};
|
||||
|
||||
pub struct Status{
|
||||
pos: i32,
|
||||
a: char, // from
|
||||
b: char, // to
|
||||
c: char // aux
|
||||
|
||||
fn next(s: &Vec<char>, start: usize, end: usize) -> Vec<usize> {
|
||||
let n = end - start;
|
||||
let mut nxt = vec![0; n];
|
||||
let mut j = 0;
|
||||
for i in 1..n {
|
||||
while j > 0 && s[start + i] != s[start + j] {
|
||||
j = nxt[j - 1];
|
||||
}
|
||||
if s[start + i] == s[start + j] {
|
||||
j += 1;
|
||||
}
|
||||
nxt[i] = j;
|
||||
}
|
||||
nxt
|
||||
}
|
||||
|
||||
|
||||
fn main(){
|
||||
let mut s = String::new();
|
||||
io::stdin().read_line(&mut s).unwrap();
|
||||
let n: i32 = s.trim().parse().unwrap();
|
||||
let stdin = io::stdin();
|
||||
for line in stdin.lock().lines() {
|
||||
let s = line.unwrap();
|
||||
if s.trim().is_empty() { continue; }
|
||||
|
||||
let mut stack: Vec<Status> = Vec::new();
|
||||
stack.push(Status{pos: n, a: 'A', b: 'C', c: 'B'});
|
||||
let mut max = 0;
|
||||
let c: Vec<char> = s.trim().chars().collect();
|
||||
let n = c.len();
|
||||
let mut nxt = next(&c, 0, c.len());
|
||||
let mut plen = -1;
|
||||
|
||||
let mut output = String::new(); //输出,要不然太慢总会超时
|
||||
|
||||
while !stack.is_empty() {
|
||||
if stack.len() > max {
|
||||
max = stack.len();
|
||||
//计算P的话可以直接用最长后缀算,使用KMP
|
||||
for len in (1..n).rev() {
|
||||
let mode: Vec<char> = c[n - len..n].to_vec();
|
||||
let tmp: Vec<usize> = next(&mode, 0, mode.len());
|
||||
let mut i = 0;
|
||||
let mut j = 0;
|
||||
while i < n - 1 {
|
||||
while j > 0 && c[i] != mode[j] {
|
||||
j = tmp[j - 1];
|
||||
}
|
||||
if c[i] == mode[j] {
|
||||
j += 1;
|
||||
}
|
||||
if j == mode.len() {
|
||||
plen = mode.len() as i32;
|
||||
break;
|
||||
}
|
||||
if plen > 0 {
|
||||
break;
|
||||
}
|
||||
i += 1;
|
||||
}
|
||||
|
||||
let mut end = stack.pop().unwrap();
|
||||
if end.pos == 1 {
|
||||
output.push_str(&format!("Move disk from {} to {}\n", end.a, end.b));
|
||||
} else {
|
||||
stack.push(Status{pos: end.pos - 1, a: end.c, b: end.b, c: end.a});
|
||||
stack.push(Status{pos: 1, a: end.a, b: end.b, c: end.c});
|
||||
stack.push(Status{pos: end.pos - 1, a: end.a, b: end.c, c: end.b});
|
||||
|
||||
if plen > 0 {
|
||||
break;
|
||||
}
|
||||
}
|
||||
print!("{}", output);
|
||||
println!("{}", max);
|
||||
|
||||
//计算Q,用len1和len2,如果2 * qlen > len(s),则len(Q) = 0
|
||||
|
||||
let mut qlen = 0;
|
||||
if nxt[n - 1] > 0 {
|
||||
qlen = nxt[nxt[n - 1] - 1];
|
||||
}
|
||||
if 2 * qlen > n {
|
||||
qlen = 0;
|
||||
} else {
|
||||
qlen = n - 2 * qlen;
|
||||
}
|
||||
|
||||
if plen < 0 {
|
||||
plen = 0;
|
||||
}
|
||||
let out = qlen as i32 + plen as i32;
|
||||
println!("{}", out);
|
||||
|
||||
//println!("qlen: {}", qlen);
|
||||
//println!("plen: {}", plen);
|
||||
}
|
||||
}
|
||||
153
Exercise/Homework2/grok的表达式树模板.cpp
Normal file
153
Exercise/Homework2/grok的表达式树模板.cpp
Normal file
@@ -0,0 +1,153 @@
|
||||
#include <iostream>
|
||||
#include <memory>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
enum class NodeType {
|
||||
OPERAND, // <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
||||
OPERATOR // <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
||||
};
|
||||
|
||||
class ExprNode {
|
||||
public:
|
||||
NodeType node_type;
|
||||
std::unique_ptr<ExprNode> left;
|
||||
std::unique_ptr<ExprNode> right;
|
||||
|
||||
virtual ~ExprNode() = default;
|
||||
virtual std::string toString() const = 0;
|
||||
};
|
||||
|
||||
class OperandNode : public ExprNode {
|
||||
public:
|
||||
int value;
|
||||
|
||||
OperandNode(int val) : value(val) {
|
||||
node_type = NodeType::OPERAND;
|
||||
}
|
||||
|
||||
std::string toString() const override {
|
||||
return "Operand(" + std::to_string(value) + ")";
|
||||
}
|
||||
};
|
||||
|
||||
class OperatorNode : public ExprNode {
|
||||
public:
|
||||
std::string op;
|
||||
|
||||
OperatorNode(const std::string& oper, std::unique_ptr<ExprNode> l = nullptr, std::unique_ptr<ExprNode> r = nullptr)
|
||||
: op(oper) {
|
||||
node_type = NodeType::OPERATOR;
|
||||
left = std::move(l);
|
||||
right = std::move(r);
|
||||
}
|
||||
|
||||
std::string toString() const override {
|
||||
std::string left_str = left ? " " + left->toString() : "";
|
||||
std::string right_str = right ? " " + right->toString() : "";
|
||||
return "Operator(" + op + left_str + right_str + ")";
|
||||
}
|
||||
};
|
||||
|
||||
class ExprTree {
|
||||
private:
|
||||
std::unique_ptr<ExprNode> root;
|
||||
|
||||
public:
|
||||
ExprTree(std::unique_ptr<ExprNode> r = nullptr) : root(std::move(r)) {}
|
||||
|
||||
void setRoot(std::unique_ptr<ExprNode> r) {
|
||||
root = std::move(r);
|
||||
}
|
||||
|
||||
// ʾ<><CABE><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Inorder Traversal<61><6C><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ڴ<EFBFBD>ӡ<EFBFBD><D3A1><EFBFBD><EFBFBD>ʽ
|
||||
void inorderTraversal(const std::unique_ptr<ExprNode>& node, std::vector<std::string>& result) const {
|
||||
if (!node) return;
|
||||
inorderTraversal(node->left, result);
|
||||
if (node->node_type == NodeType::OPERAND) {
|
||||
auto operand = static_cast<const OperandNode*>(node.get());
|
||||
result.push_back(std::to_string(operand->value));
|
||||
} else {
|
||||
auto oper = static_cast<const OperatorNode*>(node.get());
|
||||
result.push_back(oper->op);
|
||||
}
|
||||
inorderTraversal(node->right, result);
|
||||
}
|
||||
|
||||
std::string toInfixString() const {
|
||||
std::vector<std::string> result;
|
||||
inorderTraversal(root, result);
|
||||
if (result.empty()) return "";
|
||||
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʵ<EFBFBD><CAB5><EFBFBD><EFBFBD><EFBFBD>账<EFBFBD><E8B4A6><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ȷ<EFBFBD><C8B7>ʾ<EFBFBD><CABE><EFBFBD>ȼ<EFBFBD>
|
||||
std::string infix;
|
||||
for (const auto& s : result) {
|
||||
infix += s + " ";
|
||||
}
|
||||
return infix.substr(0, infix.size() - 1); // <20>Ƴ<EFBFBD>ĩβ<C4A9>ո<EFBFBD>
|
||||
}
|
||||
|
||||
// ʾ<><CABE><EFBFBD><EFBFBD><EFBFBD>Ӽ<EFBFBD><D3BC><EFBFBD><EFBFBD><EFBFBD><EFBFBD><D7BA><EFBFBD><EFBFBD>ʽ<EFBFBD><CABD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>棬<F2BBAFB0><E6A3AC>֧<EFBFBD>ֻ<EFBFBD><D6BB><EFBFBD> + - * / <20><><EFBFBD><EFBFBD><EFBFBD>ţ<EFBFBD>
|
||||
// ע<>⣺<EFBFBD><E2A3BA><EFBFBD><EFBFBD>һ<EFBFBD><D2BB><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʵ<EFBFBD>֣<EFBFBD>ʹ<EFBFBD>õݹ<C3B5><DDB9>½<EFBFBD><C2BD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>չ<EFBFBD><D5B9><EFBFBD><EFBFBD>
|
||||
static std::unique_ptr<ExprNode> buildFromInfix(const std::string& expr, size_t& pos) {
|
||||
// <20><><EFBFBD><EFBFBD><EFBFBD>ո<EFBFBD>
|
||||
while (pos < expr.size() && expr[pos] == ' ') ++pos;
|
||||
|
||||
if (pos >= expr.size()) return nullptr;
|
||||
|
||||
char ch = expr[pos];
|
||||
if (isdigit(ch)) {
|
||||
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
||||
int num = 0;
|
||||
while (pos < expr.size() && isdigit(expr[pos])) {
|
||||
num = num * 10 + (expr[pos++] - '0');
|
||||
}
|
||||
return std::make_unique<OperandNode>(num);
|
||||
} else if (ch == '(') {
|
||||
++pos; // <20><><EFBFBD><EFBFBD> '('
|
||||
auto left = buildFromInfix(expr, pos);
|
||||
auto op_node_raw = buildFromInfix(expr, pos); // <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
||||
auto right = buildFromInfix(expr, pos);
|
||||
if (pos < expr.size() && expr[pos] == ')') ++pos;
|
||||
if (op_node_raw && op_node_raw->node_type == NodeType::OPERATOR) {
|
||||
auto op_node = std::unique_ptr<OperatorNode>(static_cast<OperatorNode*>(op_node_raw.release()));
|
||||
op_node->left = std::move(left);
|
||||
op_node->right = std::move(right);
|
||||
return std::move(op_node);
|
||||
}
|
||||
return nullptr; // <20><><EFBFBD><EFBFBD>
|
||||
} else if (ch == '+' || ch == '-' || ch == '*' || ch == '/') {
|
||||
std::string oper(1, expr[pos++]);
|
||||
return std::make_unique<OperatorNode>(oper);
|
||||
}
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
// <20><><EFBFBD>ݷ<EFBFBD><DDB7><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ַ<EFBFBD><D6B7><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
||||
void buildFromInfix(const std::string& expr) {
|
||||
size_t pos = 0;
|
||||
root = buildFromInfix(expr, pos);
|
||||
}
|
||||
};
|
||||
|
||||
// ʾ<><CABE>ʹ<EFBFBD><CAB9>
|
||||
int main() {
|
||||
// <20>ֶ<EFBFBD><D6B6><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>3 + (4 * 5)
|
||||
auto mul = std::make_unique<OperatorNode>("*");
|
||||
mul->left = std::make_unique<OperandNode>(4);
|
||||
mul->right = std::make_unique<OperandNode>(5);
|
||||
|
||||
auto add = std::make_unique<OperatorNode>("+");
|
||||
add->left = std::make_unique<OperandNode>(3);
|
||||
add->right = std::move(mul);
|
||||
|
||||
ExprTree tree(std::move(add));
|
||||
std::cout << "<EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʾ: " << tree.root->toString() << std::endl;
|
||||
std::cout << "<EFBFBD><EFBFBD><EFBFBD><EFBFBD>: " << tree.toInfixString() << std::endl; // <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD> "3 + * 4 5"
|
||||
|
||||
// <20><><EFBFBD>ַ<EFBFBD><D6B7><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><F2BBAFA3><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>֧<EFBFBD>֣<EFBFBD>
|
||||
ExprTree tree2;
|
||||
tree2.buildFromInfix("3+(4*5)"); // <20><>Ҫ<EFBFBD><D2AA><EFBFBD>ƽ<EFBFBD><C6BD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Դ<EFBFBD><D4B4><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʽ
|
||||
std::cout << "<EFBFBD><EFBFBD><EFBFBD>ַ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>: " << (tree2.root ? tree2.root->toString() : "<EFBFBD><EFBFBD>Ч") << std::endl;
|
||||
|
||||
return 0;
|
||||
}
|
||||
120
Exercise/Homework2/readmd.md
Normal file
120
Exercise/Homework2/readmd.md
Normal file
@@ -0,0 +1,120 @@
|
||||
Q1
|
||||
```
|
||||
给定一个中缀表达式,请编写程序计算该表达式的值。表达式包含+、-、*、/、^、(、),所有运算均为二元运算,操作数均为正整数,但可能不止一位,不超过10位。运算结果为整数,值域为[−2
|
||||
31
|
||||
,2
|
||||
31
|
||||
)。除法运算结果若为小数则进行截尾取整。若除法运算中除数为0,则输出INVALID。幂运算须自行实现,不允许调用pow等系统函数。
|
||||
|
||||
测试数据保证幂运算中指数为非负,底数不为0,且不会出现诸如n^a^b连续多次幂运算。
|
||||
|
||||
输入格式:
|
||||
输入为多行,每行为一个长度不超过1000的字符串,表示中缀表达式。
|
||||
|
||||
输出格式:
|
||||
对每个表达式输出一行:为一个整数(表达式的值)或为一个字符串INVALID。
|
||||
|
||||
输入样例:
|
||||
5+(10*2)-6
|
||||
8*(999+1)
|
||||
1+5/(1-1)
|
||||
7*2^3
|
||||
|
||||
输出样例:
|
||||
19
|
||||
8000
|
||||
INVALID
|
||||
56
|
||||
代码长度限制
|
||||
16 KB
|
||||
时间限制
|
||||
50 ms
|
||||
内存限制
|
||||
64 MB
|
||||
栈限制
|
||||
8192 KB
|
||||
```
|
||||
|
||||
Q2
|
||||
```
|
||||
2021年11月6日,英雄联盟全球总决赛打响,中国电子竞技战队Edward Gaming(EDG)以3:2力克韩国强敌DWG KIA(DK)战队,历史上首次夺得全球总冠军。一时间全网沸腾,大家纷纷在社交平台上直呼“edgnb”。现给定一段文本,请编写程序识别出连续的k个“edgnb”组成的字符串在该文本中出现了多少次。
|
||||
|
||||
输入格式:
|
||||
第一行为1个整数T,表示数据组数。对于每组数据,第一行为1个字符串,表示给定的文本。第二行为1个整数k,含义如题目所述。(1≤T≤10。各组数据给定的字符串长度之和不超过10
|
||||
5
|
||||
,且字符串中只包含a-z的小写字母。k≥1且k×5小于给定字符串长度)。
|
||||
|
||||
输出格式:
|
||||
对于每组数据输出一行,为1个整数,表示所求的出现次数。
|
||||
|
||||
输入样例:
|
||||
5
|
||||
xyzedgnbabcedgnb
|
||||
1
|
||||
xyzedgnbabcedgnb
|
||||
2
|
||||
defedgnbedgnbxyz
|
||||
2
|
||||
edgnbedgnbedgnb
|
||||
2
|
||||
fxedgnbedgnbedgnbedgnbmem
|
||||
3
|
||||
|
||||
输出样例:
|
||||
2
|
||||
0
|
||||
1
|
||||
2
|
||||
2
|
||||
|
||||
数据规模:
|
||||
测试点0:5≤T≤10,400≤T个字符串长度之和≤500,k=1
|
||||
测试点1:5≤T≤10,400≤T个字符串长度之和≤500,k≥1
|
||||
测试点2:5≤T≤10,4000≤T个字符串长度之和≤5000,k≥1
|
||||
测试点3:1≤T≤3,90000≤T个字符串长度之和≤100000,k≥1
|
||||
测试点4:1≤T≤3,90000≤T个字符串长度之和≤100000,k≥1
|
||||
|
||||
代码长度限制
|
||||
16 KB
|
||||
时间限制
|
||||
400 ms
|
||||
内存限制
|
||||
64 MB
|
||||
栈限制
|
||||
8192 KB
|
||||
```
|
||||
|
||||
Q3
|
||||
```
|
||||
波比和哈丽在玩一个字母游戏,波比给出一个字符串S,要求哈丽按照一定规则,基于该字符串算出一个数字X。
|
||||
|
||||
规则是:
|
||||
|
||||
(1)求出S的最长重复后缀P(P是S的后缀且在S中出现大于1次,例如yacbacba的最长重复后缀是acba),
|
||||
|
||||
(2)求出在S中去除第二长相等前后缀(S中所有相等的前后缀中第2长者,例如abcabcxxxabcabc中最长相等前后缀是abcabc,第二长的相等前后缀则是abc)后剩下的子串Q(例如abcabcxxxabcabc去除第二长相等前后缀后,剩下abcxxxabc)。
|
||||
|
||||
则X=P的长度+Q的长度。
|
||||
|
||||
注意一个字符串不能称为自己的前缀或后缀。子串Q至少为空串,其长度大于等于0,不能为负数。
|
||||
|
||||
请编写程序帮助哈丽根据给定字符串S,根据上述规则计算出数字X。
|
||||
|
||||
输入格式:
|
||||
输入为若干行,每行为一个字符串,包含不超过100000个字母。
|
||||
|
||||
输出格式:
|
||||
输出为若干行,每行一个整数,表示输入字符串所计算出的数字。
|
||||
|
||||
输入样例:
|
||||
abcabcxxxabcabc
|
||||
xacbacba
|
||||
abc
|
||||
aaa
|
||||
|
||||
输出样例:
|
||||
15
|
||||
12
|
||||
3
|
||||
3
|
||||
```
|
||||
@@ -1,103 +0,0 @@
|
||||
Q1
|
||||
```
|
||||
编写程序检查给定字符串中包含的括号是否正确匹配,本题中的括号有{ }、[ ]、( )、< >四种。另外再加上一个新的约束条件:当有多种括号嵌套时,嵌套的顺序应为{ → [ → ( → <,即a–g+b∗[(d∗<e–f>)]、a+[b+(c–d)∗e]都是正确的匹配,而a+(b∗[c+d])则不是正确匹配。注意本题不允许相同类型括号的嵌套,即a+(b∗(c+d))不是正确匹配。本题不需要判断表达式是否合法,只需判断字符串中包含的括号是否正确匹配。
|
||||
|
||||
输入格式:
|
||||
第一行为一个整数n,表示字符串的个数。接下来n行,每行为一个字符串。1<n≤100,字符串长度不超过1000。
|
||||
|
||||
输出格式:
|
||||
对于每个字符串,若为正确匹配则输出"Match" ,若不匹配则输出"Fail"。
|
||||
|
||||
输入样例1:
|
||||
8
|
||||
a+(b*[c+d])
|
||||
g{b[(<c>)d]e}x
|
||||
[()]
|
||||
((()))
|
||||
<>()[]{}
|
||||
[{}]
|
||||
x=y+{z+(b)}
|
||||
][()
|
||||
输出样例1:
|
||||
Fail
|
||||
Match
|
||||
Match
|
||||
Fail
|
||||
Match
|
||||
Fail
|
||||
Match
|
||||
Fail
|
||||
输入样例2:
|
||||
6
|
||||
{[afds(a<afd>)]}yt
|
||||
[()rew]
|
||||
<>()[wre]{}
|
||||
[{qw}]
|
||||
rew{(weq)}jjk
|
||||
<><{}>[][](){[{}]}
|
||||
输出样例2:
|
||||
Match
|
||||
Match
|
||||
Match
|
||||
Fail
|
||||
Match
|
||||
Fail
|
||||
代码长度限制
|
||||
16 KB
|
||||
时间限制
|
||||
50 ms
|
||||
内存限制
|
||||
30 MB
|
||||
栈限制
|
||||
8192 KB
|
||||
```
|
||||
Q2
|
||||
```
|
||||
从A点到B点有n个格子,小明现在要从A点到B点,小明吃了些东西,补充了一下体力,他可以一步迈一个格子,也可以一步迈两个格子,也可以一步迈3个格子,也可以一步迈4个格子。请编写程序计算小明从A点到B点一共有多少种走法。
|
||||
|
||||
grid2.jpg
|
||||
|
||||
输入格式:
|
||||
输入包含多组数据,第一行为一个整数m,m不超过10000,表示输入数据组数。接下来m行,每行为一个整数n(n不超过100,且保证对应的结果小于2
|
||||
31
|
||||
),表示从A点到B点的格子数。
|
||||
|
||||
输出格式:
|
||||
输出为m个整数,表示对于每组数据小明从A点到B点的走法数。
|
||||
|
||||
输入样例:
|
||||
2
|
||||
5
|
||||
3
|
||||
输出样例:
|
||||
15
|
||||
4
|
||||
```
|
||||
|
||||
Q3
|
||||
```
|
||||
本学期的《数据结构》课上,老师曾结合汉诺塔问题,介绍了使用栈消除递归的方法,从而将汉诺塔问题的递归算法转换为非递归算法,本题请你编程实现上述非递归算法,即使用栈以非递归形式求解汉诺塔问题。将n个圆盘自A柱移至C柱(可途经B柱),并输出求解过中栈的最大容量(最多存储了多少个四元组)。
|
||||
|
||||
备注:本题将在机器评测后进行人工核验,若有同学未按题目要求,仅使用递归程序通过本题,本题记为0分。
|
||||
image.png
|
||||
|
||||
输入格式:
|
||||
输入为一个整数n,表示初始时A柱上的圆盘数目,n不超过20。
|
||||
|
||||
输出格式:
|
||||
按顺序输出各动作,每个动作占一行,格式为“Move disk from x to y”,表示将x柱顶端的圆盘移至y柱。
|
||||
|
||||
最后一行为一个整数,表示求解过程中栈的最大容量。
|
||||
|
||||
输入样例:
|
||||
3
|
||||
输出样例:
|
||||
Move disk from A to C
|
||||
Move disk from A to B
|
||||
Move disk from C to B
|
||||
Move disk from A to C
|
||||
Move disk from B to A
|
||||
Move disk from B to C
|
||||
Move disk from A to C
|
||||
5
|
||||
```
|
||||
146
Exercise/Homework2/表达式树模板.cpp
Normal file
146
Exercise/Homework2/表达式树模板.cpp
Normal file
@@ -0,0 +1,146 @@
|
||||
#include <bits/stdc++.h>
|
||||
using namespace std;
|
||||
|
||||
struct Node {
|
||||
char sign; // <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ҷ<EFBFBD>ӽڵ<D3BD>Ϊ '?'
|
||||
long long num; // <20><><EFBFBD><EFBFBD>
|
||||
Node* left;
|
||||
Node* right;
|
||||
|
||||
Node(): sign('?'), num(-1), left(nullptr), right(nullptr) {}
|
||||
Node(char s): sign(s), num(-1), left(nullptr), right(nullptr) {}
|
||||
Node(long long n): sign('?'), num(n), left(nullptr), right(nullptr) {}
|
||||
};
|
||||
|
||||
// <20><><EFBFBD><EFBFBD><EFBFBD>ݣ<EFBFBD><DDA3><EFBFBD><EFBFBD><EFBFBD>
|
||||
long long fastpow(long long base, long long exp){
|
||||
long long res = 1;
|
||||
while(exp > 0){
|
||||
if(exp & 1) res *= base;
|
||||
base *= base;
|
||||
exp >>= 1;
|
||||
}
|
||||
return res;
|
||||
}
|
||||
|
||||
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ȼ<EFBFBD>
|
||||
int level(char c){
|
||||
switch(c){
|
||||
case '+': case '-': return 1;
|
||||
case '*': case '/': return 2;
|
||||
case '^': return 3;
|
||||
default: return 0;
|
||||
}
|
||||
}
|
||||
|
||||
// <20>ҵ<EFBFBD><D2B5><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ȼ<EFBFBD><C8BC><EFBFBD><EFBFBD>͵<EFBFBD><CDB5><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
||||
int findexp(const string &s, int l, int r){
|
||||
int pos = -1;
|
||||
int minLevel = 114514;
|
||||
int balance = 0; // <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
||||
for(int i = r; i >= l; --i){ // <20>ҽ<EFBFBD><D2BD>ϣ<EFBFBD><CFA3><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ұ<EFBFBD>
|
||||
if(s[i] == ')') balance++;
|
||||
else if(s[i] == '(') balance--;
|
||||
else if(balance == 0 && level(s[i]) > 0){
|
||||
if(level(s[i]) <= minLevel){
|
||||
minLevel = level(s[i]);
|
||||
pos = i;
|
||||
}
|
||||
}
|
||||
}
|
||||
return pos;
|
||||
}
|
||||
|
||||
// <20>ַ<EFBFBD><D6B7><EFBFBD>ת<EFBFBD><D7AA><EFBFBD><EFBFBD>
|
||||
long long toNum(const string &s, int l, int r){
|
||||
long long tmp = 0;
|
||||
while(l <= r && isspace((unsigned char)s[l])) l++;
|
||||
while(r >= l && isspace((unsigned char)s[r])) r--;
|
||||
if(l > r) return 0;
|
||||
for(int i = l; i <= r; ++i){
|
||||
if(!isdigit(s[i])) return 0;
|
||||
tmp = tmp * 10 + (s[i] - '0');
|
||||
}
|
||||
return tmp;
|
||||
}
|
||||
|
||||
class Expression {
|
||||
public:
|
||||
string exp;
|
||||
Node* root;
|
||||
long long result;
|
||||
bool isValid;
|
||||
|
||||
Expression(const string &s): exp(s), root(nullptr), result(0), isValid(true) {}
|
||||
|
||||
Node* build(const string &s, int l, int r){
|
||||
while(l <= r && s[l] == ' ') l++;
|
||||
while(l <= r && s[r] == ' ') r--;
|
||||
if(l > r) return nullptr;
|
||||
|
||||
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ŵ<EFBFBD><C5B4><EFBFBD>
|
||||
if(s[l] == '(' && s[r] == ')'){
|
||||
int balance = 0;
|
||||
bool hasOuter = true;
|
||||
for(int i = l; i <= r; ++i){
|
||||
if(s[i] == '(') balance++;
|
||||
else if(s[i] == ')') balance--;
|
||||
if(balance == 0 && i < r){
|
||||
hasOuter = false;
|
||||
break;
|
||||
}
|
||||
}
|
||||
if(hasOuter) return build(s, l+1, r-1);
|
||||
}
|
||||
|
||||
int pos = findexp(s, l, r);
|
||||
if(pos == -1){
|
||||
long long val = toNum(s, l, r);
|
||||
return new Node(val);
|
||||
}
|
||||
|
||||
Node* tmp = new Node(s[pos]);
|
||||
tmp->left = build(s, l, pos-1);
|
||||
tmp->right = build(s, pos+1, r);
|
||||
return tmp;
|
||||
}
|
||||
|
||||
long long calc(Node* r, bool &v){
|
||||
if(!r) return 0;
|
||||
if(r->sign == '?') return r->num;
|
||||
|
||||
long long left = calc(r->left, v);
|
||||
long long right = calc(r->right, v);
|
||||
|
||||
switch(r->sign){
|
||||
case '+': return left + right;
|
||||
case '-': return left - right;
|
||||
case '*': return left * right;
|
||||
case '/':
|
||||
if(right == 0){ v = false; return 0; }
|
||||
return left / right;
|
||||
case '^':
|
||||
if(right < 0){ v = false; return 0; }
|
||||
return fastpow(left, right);
|
||||
default: return 0;
|
||||
}
|
||||
}
|
||||
|
||||
void calc(){
|
||||
isValid = true;
|
||||
result = calc(root, isValid);
|
||||
}
|
||||
};
|
||||
|
||||
int main(){
|
||||
string s;
|
||||
while(getline(cin, s)){
|
||||
Expression tree(s);
|
||||
tree.root = tree.build(s, 0, s.length()-1);
|
||||
tree.calc();
|
||||
if(tree.isValid) cout << tree.result << endl;
|
||||
else cout << "INVALID" << endl;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user