BUAA 623 Chem is Try!
2024-09-06 13:51:16
http://oj55.bianchengla.com/problem/623/
好久没写过题解了,昨天做了一道挺恶心的题目,贴一下代码上来。看了一下提交状况,好像我的代码挺短的了,至少我找不到比我短的代码。RE1,AC。
代码如下:
#include <cctype>
#include <cstdio>
#include <cstring>
#include <map>
#include <string>
#include <iostream>
#include <algorithm> using namespace std; const int N = ;
char buf[N], *p[];
typedef long long LL;
typedef map<string, LL> MSL;
#define x first
#define y second string get_s(int x) {
string ret = "";
ret += *(p[x]++);
while (islower(*p[x])) ret += *p[x], p[x]++;
return ret;
} int get_d(int x) {
int ret = ;
while (isdigit(*p[x])) ret = ret * + *p[x] - '', p[x]++;
return ret;
} MSL merge(MSL a, MSL b) {
MSL c;
for (MSL::iterator mi = a.begin(); mi != a.end(); mi++) c[mi->x] += mi->y;
for (MSL::iterator mi = b.begin(); mi != b.end(); mi++) c[mi->x] += mi->y;
return c;
} void print(MSL &a) { for (MSL::iterator mi = a.begin(); mi != a.end(); mi++) cout << mi->x << '~' << mi->y << ' '; cout << endl; }
void multiply(MSL &a, LL p) { for (MSL::iterator mi = a.begin(); mi != a.end(); mi++) mi->y *= p; } MSL dfs(int x, char end) {
MSL cur;
//cout << x << ' ' << p[x] << endl;
while (*p[x] && *p[x] != end && *p[x] != ')') {
//cout << x << ' ' << *p[x] << endl;
if (*p[x] == '+') { p[x]++; continue; }
if (isdigit(*p[x])) {
int t = get_d(x);
MSL tmp = dfs(x, '+');
multiply(tmp, t);
cur = merge(cur, tmp);
} else {
if (*p[x] == '(') {
p[x]++; MSL tmp = dfs(x, ')');
if (*p[x] == ')') p[x]++;
if (isdigit(*p[x])) multiply(tmp, get_d(x));
cur = merge(cur, tmp);
} else {
string s = get_s(x);
if (isdigit(*p[x])) cur[s] += get_d(x);
else cur[s]++;
}
}
}
return cur;
} int main() {
//freopen("in", "r", stdin);
while (~scanf("%s", buf)) {
p[] = p[] = buf;
while (*p[] != '=') p[]++; *(p[]++) = ;
MSL ans1, ans2, tmp;
while () {
tmp = dfs(, '+');
ans1 = merge(ans1, tmp);
if (*p[] == ) break;
p[]++;
//cout << p[0] << endl;
}
while () {
tmp = dfs(, '+');
ans2 = merge(ans2, tmp);
if (*p[] == ) break;
p[]++;
//cout << p[1] << endl;
}
MSL::iterator m1 = ans1.begin(), m2 = ans2.begin();
//print(ans1); print(ans2);
while () {
if (m1 == ans1.end()) break;
if (m2 == ans2.end()) break;
if (*m1 != *m2) break;
m1++, m2++;
}
m1 == ans1.end() && m2 == ans2.end() ? puts("YES") : puts("NO");
}
return ;
}
——written by Lyon
最新文章
- python下如何安装biopython
- win10系统下点击关机却自动重启的问题解决思路
- 关于浏览器URL中出现会话验证字符说明
- BZOJ1012——[JSOI2008]最大数maxnumber
- jquery 判断手势滑动方向(上下左右)
- Android:改变Activity切换方式(转载)
- python中字符串\r的奇怪问题
- jQuery EasyUI动态添加控件或者ajax加载页面后不能自动渲染问题的解决方法
- 框架搭建资源 (二) 添加M(模型)
- Swiper.js使用方法
- 【原创】Linux基础之vi
- linux下ping命令出现ping: sendto: Network is unreachable
- 正确理解python中的赋值语句:a, b = b, a + b
- PriorityBlockingQueue 原理分析
- Java笔记Spring(三)
- Liferay7 BPM门户开发之12:acitiviti和liferay用户权限体系集成
- DL中epoch、batch等的意义【转载】
- 你写的什么垃圾代码让Vsync命令不能及时处理呢?(2)
- 面试总结之MISC(操作系统,网络,数学,软件开发,测试,工具,系统设计,算法)
- C++实现线程同步的几种方式