提交记录 11160


用户 题目 状态 得分 用时 内存 语言 代码长度
kongren test. 自定义测试 Compile Error 0 0 ns 0 KB C++ 2.68 KB
提交时间 评测时间
2019-11-01 11:00:03 2023-09-03 19:38:59
#include <string>
#include <iostream>
#include <unordered_map>
using namespace std;

#include <sstream>
typedef unordered_map<string, int> atoms;
typedef atoms::value_type record;
class node{
public:
    string name;
    int count;
    node(const string& _name,int _count):name(_name),count(_count){}
    bool operator<(const node& o)const{
        return name < o.name;
    }
};
class Solution {
    void addRes(atoms &res, atoms &group, string &name, int &count){
        if(count==0)
            count=1;
        if(name!="")
            res[name]+=count;
        else{
            for(auto p:group){
                res[p.first]+=p.second * count;
            }
        }
        count=0;
        name="";
        group.clear();
    }
    unordered_map<string, int> test(const string &formula, string::iterator &itor){
        atoms res, tmp;
        string eleName;
        int eleCount=0;
        for(;itor!=formula.end();itor++){
            auto c=*itor;
            if(c>='a'&&c<='z')
                eleName.push_back(c);
            else if(c>='0'&&c<='9'){
                if(eleCount==0)
                    eleCount = (int)(c-'0');
                else
                    eleCount = eleCount*10+(int)(c-'0');
            }
            else if(c>='A'&&c<='Z'){
                addRes(res, tmp, eleName, eleCount);
                eleName=c;
            }
            else if(c=='('){
                addRes(res, tmp, eleName, eleCount);
                itor++;
                tmp=test(formula, itor);
            }else if(c==')'){
                break;
            }
        }
        addRes(res, tmp, eleName, eleCount);
        return res;
    }
public:
    string countOfAtoms(string formula) {
        auto itor=formula.begin();
        vector<node> vec;
        for(auto p:test(formula, itor)){
            vec.emplace_back(p.first, p.second);
        }
        sort(vec.begin(), vec.end());
        stringstream out;
        for(auto &r:vec){
            out<<r.name;
            if(r.count>1)
                out<<r.count;
        }
        return out.str();
    }
};

int main(){
    string formula = "((Bi16LrSc30As49As23La13La(Ba21Re28BkRf40DbP29SeSe41La27Cn16)48(TcRn)49(Sb28Ti4Po35Mn16)21(Ta36Tl2Br5Dy21S41)6(Bh37Li36O20Tb48)24Cr43Pd11Pu39OgYb43Zr35FmHe44(Rh35BiLaCf4RnHLi10RgLuRg)3(Cn15PbS44Nd18)8)25((Hs11B41Rf46)20(At48Te45)32(Cs15Mt19OgHs34Ts5La33Ga23Np50Dy33O24)4)13((Po21ZnPdK27Pm16TlCo34Nd30Y4N)16(Nh2BaNa28Ga15LuAl38)17(Rb23ReRf2Rf33I32Te48Bh)50(Cf37Ne32W33BeRgIr21Cs34Mc17Zn43)43(Ho23ArEs38Er40Tb8DyIn41Tc36Hg21Cl9)42(Y8B25Ts16S10Fr2Lv22Po6)2(Zn46N34Ds7Sg20HoRf31P25ZrIHo22)40(FeRh50Kr9ThPt49)37(TaLrKr35Kr12SrCd26Xe28Mt26CnFl)43)23)17";
    Solution s;
    cout<<s.countOfAtoms(formula)<<endl;
    return 0;
}

CompilationN/AN/ACompile ErrorScore: N/A


Judge Duck Online | 评测鸭在线
Server Time: 2026-03-28 06:44:51 | Loaded in 1 ms | Server Status
个人娱乐项目,仅供学习交流使用 | 捐赠