2022 RoboCom 世界机器人开发者大赛-本科组(省赛)
T3 跑团
两种写法大同小异:
1.
#include <iostream>
#include <algorithm>
#include <cstring>
#include <cmath>
#include <cstdio>
#define Mod 7452454545
#include <map>
#include <set>
#include <unordered_map>
#include <vector>
#include <bits/stdc++.h>
#define LL long long
#define PII pair<int, int>
#define PLL pair<long, long>
#define x first
#define y second
#define INF 0x3f3f3f3f
#define ULL unsigned long long
#define kanm7 ios::sync_with_stdio(false), cin.tie(0), cout.tie(0)
#define endl '\n'
using namespace std;
map<int,int> st;
int main() {
string s; cin >> s;
int ma = 0, mn = 0;
if(s[0] != '+' && s[0] != '-') s.insert(s.begin(), '+');
int n = s.size();
for(int i = 0; i < n; ) {
int j = i + 1; // 运算数的开头
while(j < n && s[j] != '+' && s[j] != '-') j ++;
if(s[i] == '-') {
int a = 0, b = 0, f = 1;
for(int k = i + 1; k < j; k ++) {
if(s[k] == 'd') {
f = 0;
continue;
}
if(f) a = a * 10 + s[k] - '0';
else b = b * 10 + s[k] - '0';
}
if(f) {
ma -= a, mn -= a;
} else {
if(!a) a ++;
st[b] += a;
ma -= 1 * a, mn -= b * a;
}
} else {
int a = 0, b = 0, f = 1;
for(int k = i + 1; k < j; k ++) {
if(s[k] == 'd') {
f = 0;
continue;
}
if(f) a = a * 10 + s[k] - '0';
else b = b * 10 + s[k] - '0';
}
if(f) {
ma += a, mn += a;
} else {
if(!a) a ++;
st[b] += a;
ma += b * a, mn += 1 * a;
}
}
i = j;
}
for(auto it : st) {
cout << it.first << ' ' << it.y << endl;
}
cout << mn << ' ' << ma << endl;
return 0;
}
2.就是第二种写法,decltype这个关键字用法还不是很熟。
#include <iostream>
#include <algorithm>
#include <cstring>
#include <cmath>
#include <cstdio>
#define Mod 7452454545
#include <map>
#include <set>
#include <unordered_map>
#include <vector>
#include <bits/stdc++.h>
#define LL long long
#define PII pair<int, int>
#define PLL pair<long, long>
#define x first
#define y second
#define INF 0x3f3f3f3f
#define ULL unsigned long long
#define kanm7 ios::sync_with_stdio(false), cin.tie(0), cout.tie(0)
#define endl '\n'
using namespace std;
map<int,int> st;
vector<string> add, sub;
int get(string s) {
int res = 0;
for(int i = 0; i < s.size(); i ++) {
res = res * 10 + s[i] - '0';
}
return res;
}
int main() {
string s; cin >> s;
// 把加减分类
if(s[0] != '-') {
s.insert(s.begin(), '+');
}
// cout << s << endl;
int n = s.size();
for(int i = 0; i < n; ) {
int j = i + 1;
while(j < n && (s[j] != '+' && s[j] != '-')) j ++;
if(s[i] == '+') add.push_back(s.substr(i + 1, j - i - 1));
else {
sub.push_back(s.substr(i + 1, j - i - 1));
}
i = j;
}
// 把dy这样的前面补上1
int s1 = 0, s2 = 0;
for(int i = 0; i < add.size(); i ++) {
regex r("^d");
add[i] = regex_replace(add[i], r, "1d");
regex r1("d");
sregex_token_iterator pos(add[i].begin(), add[i].end(), r1, -1);
decltype(pos) end;
int f = 0;
int x = 0, y = 0;
for(; pos != end; pos ++) {
if(!f) {
f ++;
x = get(pos->str());
} else {
y = get(pos->str());
}
}
if(!y) {
s1 += x, s2 += x;
continue;
}
s1 += x * 1, s2 += x * y;
st[y] += x;
}
for(int i = 0; i < sub.size(); i ++) {
regex r("^d");
sub[i] = regex_replace(sub[i], r, "1d");
regex r1("d");
sregex_token_iterator pos(sub[i].begin(), sub[i].end(), r1, -1);
decltype(pos) end;
int f = 0;
int x = 0, y = 0;
for(; pos != end; pos ++) {
if(!f) {
f ++;
x = get(pos->str());
} else {
y = get(pos->str());
}
}
if(!y) {
s1 -= x, s2 -= x;
continue;
}
s1 -= x * y;
s2 -= x * 1;
st[y] += x;
}
for(auto it : st) {
cout << it.first << ' ' << it.second << endl;
}
cout << s1 << ' ' << s2 << endl;
return 0;
}