zoukankan      html  css  js  c++  java
  • BUAA 623 Chem is Try!

    http://oj55.bianchengla.com/problem/623/

      好久没写过题解了,昨天做了一道挺恶心的题目,贴一下代码上来。看了一下提交状况,好像我的代码挺短的了,至少我找不到比我短的代码。RE1,AC。

    代码如下:

      1 #include <cctype>
      2 #include <cstdio>
      3 #include <cstring>
      4 #include <map>
      5 #include <string>
      6 #include <iostream>
      7 #include <algorithm>
      8 
      9 using namespace std;
     10 
     11 const int N = 111111;
     12 char buf[N], *p[2];
     13 typedef long long LL;
     14 typedef map<string, LL> MSL;
     15 #define x first
     16 #define y second
     17 
     18 string get_s(int x) {
     19     string ret = "";
     20     ret += *(p[x]++);
     21     while (islower(*p[x])) ret += *p[x], p[x]++;
     22     return ret;
     23 }
     24 
     25 int get_d(int x) {
     26     int ret = 0;
     27     while (isdigit(*p[x])) ret = ret * 10 + *p[x] - '0', p[x]++;
     28     return ret;
     29 }
     30 
     31 MSL merge(MSL a, MSL b) {
     32     MSL c;
     33     for (MSL::iterator mi = a.begin(); mi != a.end(); mi++) c[mi->x] += mi->y;
     34     for (MSL::iterator mi = b.begin(); mi != b.end(); mi++) c[mi->x] += mi->y;
     35     return c;
     36 }
     37 
     38 
     39 void print(MSL &a) { for (MSL::iterator mi = a.begin(); mi != a.end(); mi++) cout << mi->x << '~' << mi->y << ' '; cout << endl; }
     40 void multiply(MSL &a, LL p) { for (MSL::iterator mi = a.begin(); mi != a.end(); mi++) mi->y *= p; }
     41 
     42 MSL dfs(int x, char end) {
     43     MSL cur;
     44     //cout << x << ' ' << p[x] << endl;
     45     while (*p[x] && *p[x] != end && *p[x] != ')') {
     46         //cout << x << ' ' << *p[x] << endl;
     47         if (*p[x] == '+') { p[x]++; continue; }
     48         if (isdigit(*p[x])) {
     49             int t = get_d(x);
     50             MSL tmp = dfs(x, '+');
     51             multiply(tmp, t);
     52             cur = merge(cur, tmp);
     53         } else {
     54             if (*p[x] == '(') {
     55                 p[x]++; MSL tmp = dfs(x, ')');
     56                 if (*p[x] == ')') p[x]++;
     57                 if (isdigit(*p[x])) multiply(tmp, get_d(x));
     58                 cur = merge(cur, tmp);
     59             } else {
     60                 string s = get_s(x);
     61                 if (isdigit(*p[x])) cur[s] += get_d(x);
     62                 else cur[s]++;
     63             }
     64         }
     65     }
     66     return cur;
     67 }
     68 
     69 int main() {
     70     //freopen("in", "r", stdin);
     71     while (~scanf("%s", buf)) {
     72         p[0] = p[1] = buf;
     73         while (*p[1] != '=') p[1]++; *(p[1]++) = 0;
     74         MSL ans1, ans2, tmp;
     75         while (1) {
     76             tmp = dfs(0, '+');
     77             ans1 = merge(ans1, tmp);
     78             if (*p[0] == 0) break;
     79             p[0]++;
     80             //cout << p[0] << endl;
     81         }
     82         while (1) {
     83             tmp = dfs(1, '+');
     84             ans2 = merge(ans2, tmp);
     85             if (*p[1] == 0) break;
     86             p[1]++;
     87             //cout << p[1] << endl;
     88         }
     89         MSL::iterator m1 = ans1.begin(), m2 = ans2.begin();
     90         //print(ans1); print(ans2);
     91         while (1) {
     92             if (m1 == ans1.end()) break;
     93             if (m2 == ans2.end()) break;
     94             if (*m1 != *m2) break;
     95             m1++, m2++;
     96         }
     97         m1 == ans1.end() && m2 == ans2.end() ? puts("YES") : puts("NO");
     98     }
     99     return 0;
    100 }
    View Code

    ——written by Lyon

  • 相关阅读:
    商场活动|简单易用|可下载试用|复用转盘抽奖软件
    js dictionary
    财务大写
    SET ANSI_NULLS ON ……
    批量生成clr脚本
    Git
    CTE递归查询
    jquery 巧用json传参
    个人犯的一个golang routine错误
    .NET实现自动编译
  • 原文地址:https://www.cnblogs.com/LyonLys/p/buaa_623.html
Copyright © 2011-2022 走看看