原子的数量

问题描述 :
给定一个化学式formula(作为字符串),返回每种原子的数量。
原子总是以一个大写字母开始,接着跟随0个或任意个小写字母,表示原子的名字。如果数量大于 1,原子后会跟着数字表示原子的数量。如果数量等于 1 则不会跟数字。例如,H2O 和 H2O2 是可行的,但 H1O2 这个表达是不可行的。两个化学式连在一起是新的化学式。例如 H2O2He3Mg4 也是化学式。
一个括号中的化学式和数字(可选择性添加)也是化学式。例如 (H2O2) 和 (H2O2)3 是化学式。
给定一个化学式,输出所有原子的数量。格式为:第一个(按字典序)原子的名子,跟着它的数量(如果数量大于 1),然后是第二个原子的名字(按字典序),跟着它的数量(如果数量大于 1),以此类推。

示例 1:
输入:
formula = “H2O”
输出: “H2O”
解释:
原子的数量是 {‘H’: 2, ‘O’: 1}。

示例 2:
输入:
formula = “Mg(OH)2”
输出: “H2MgO2”
解释:
原子的数量是 {‘H’: 2, ‘Mg’: 1, ‘O’: 2}。

示例 3:
输入:
formula = “K4(ON(SO3)2)2”
输出: “K4N2O14S4”
解释:
原子的数量是 {‘K’: 4, ‘N’: 2, ‘O’: 14, ‘S’: 4}。

注意:
所有原子的第一个字母为大写,剩余字母都是小写。
formula的长度在[1, 1000]之间。
formula只包含字母、数字和圆括号,并且题目中给定的是合法的化学式

输入说明 :
输入一个字符串,中间不包含空格

输出说明 :
输出一个字符串,表示结果。

输入范例 :
K4(ON(SO3)2)2

输出范例 :
K4N2O14S4

#include<iostream>
#include<string>
#include<map>
#include<stack>
using namespace std;
struct Atom {
        string tag;
        int count;
    };
    string countOfAtoms(string formula) {
        if (formula.length() && formula[0] != '(')
            formula = "(" + formula + ")";
        stack<Atom> mainSt, subSt;
        int i = 0;
        int atom_start = 0, atom_end = 0;
        int num_start = 0, num_end = 0;
        Atom temp;
        while (i < formula.length()) {
            if (formula[i] == '(') {
                temp.tag = "(";
                temp.count = 0;
                mainSt.push(temp);
                i++;
            }
            else if (formula[i] >= 'A' && formula[i] <= 'Z') {
                atom_start = i;
                while (i + 1 < formula.length() && formula[i+1] >= 'a' && formula[i+1] <= 'z')
                    i++;
                atom_end = i;
                temp.tag = formula.substr(atom_start, atom_end - atom_start + 1);
                i++;
                if (formula[i] >= '0' && formula[i] <= '9') {
                    num_start = i;
                    while (i + 1 < formula.length() && formula[i + 1] >= '0' && formula[i + 1] <= '9')
                        i++;
                    num_end = i;
                    i++;
                    temp.count = stoi(formula.substr(num_start, num_end - num_start + 1));
                }
                else {
                    temp.count = 1;
                }
                mainSt.push(temp);
            }
            else if (formula[i] == ')') {
                while (!mainSt.empty()) {
                    temp = mainSt.top();
                    mainSt.pop();
                    if (temp.tag != "(")
                        subSt.push(temp);
                    else {
                        break;
                    }
                }
                i++;
                int num = 1;
                if (i<formula.length()&&formula[i] >= '0' && formula[i] <= '9') {
                    num_start = i;
                    while (i + 1 < formula.length() && formula[i + 1] >= '0' && formula[i + 1] <= '9')
                        i++;
                    num_end = i;
                    i++;
                    num = stoi(formula.substr(num_start, num_end - num_start + 1));
                }
                while (!subSt.empty()) {
                    temp = subSt.top();
                    subSt.pop();
                    temp.count = temp.count * num;
                    mainSt.push(temp);
                }
            }
        }
        map<string, int> table;
        while (!mainSt.empty()) {
            temp = mainSt.top();
            mainSt.pop();
            table[temp.tag] += temp.count;
        }
        string result;
        for (map<string, int>::iterator iter = table.begin(); iter != table.end(); iter++) {
            result = result + iter->first ;
            if (iter->second > 1)
                result = result + to_string(iter->second);
        }
        return result;
    }

int main(){
	string formula;
	cin>>formula;
	string res=countOfAtoms(formula);
	cout<<res<<endl;
	return 0;
} 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值