题目翻译
一个化学式的定义如下:
M := G | M G // 化学式
G := S | S C // 原子团
S := A | ’(’ M ’)’ // 原子团
C := T | N E // 大于1的整数
E := D | D E // 允许前导零的整数
T := '2' | ... | '9' // 2 ~ 9
N := '1' | ... | '9' // 1 ~ 9
D := '0' | ... | '9' // 0 ~ 9
A := U | U L | U L L // 元素符号
U := 'A' | ... | 'Z' // 大写字母
L := 'a' | ... | 'z' // 小写字母
如果一个原子团只有1个,那么角标1可以省略,否则不能省略。
输入
输入有多组数据,EOF结束。
对于每组数据一行一个字符串,表示一种物质的化学式(一定是合法的,不会超过100个字符)。
输出
对于每组数据输出一行。输出一个分子的相应物质中每种元素有几个原子(注意如果只有1个原子那么1省略)。输出不可以有空格。计算过程中所有数字都在32位有符号整数范围内。具体格式参考输出样例
样例输入
H2O
(AlC2)3Na4
样例输出
2H+O
3Al+6C+4Na
题解
work(str,mul)表示当前解析的字符串为str,str内的所有元素的原子个数都要倍乘mul。
那么就有递归关系了。
work(str,mul) -> work(substr, mul*submul)
substr是work的一个合法的子串,一定是一种原子或原子团的合法表示,如(Al2CO3)2
是合法的。
比如work("(Al2CO3)2", 1)->work("Al2CO3", 2)
具体的实现满满的”Java”风。。特长,废话特多。
// UVALive 6085
#include <cstdio>
#include <cctype>
#include <cstring>
#include <map>
#include <sstream>
#include <algorithm>
using namespace std;
#define foreach(it,c) for(auto it=c.begin();it!=c.end();++it)
const int N = 128;
int partner[N];
string substring(char *str, int beginIndex, int length) {
stringstream ss;
string res;
for (int i = beginIndex; i < beginIndex + length; ++i)
ss << str[i];
ss >> res;
return res;
}
/**
* Extract a valid element symbol starting at beginIndex of the given string.
* @return The word length, 0 if not a right position.
*/
int findElement(char *str, int beginIndex) {
int i;
if (str[beginIndex] < 'A' || str[beginIndex] > 'Z')
return 0;
for (i = beginIndex + 1; str[i]; ++i)
if (str[i] < 'a' || str[i] > 'z')
break;
return i - beginIndex;
}
/**
* Extract the number starting at beginIndex of the given string.
*/
int findDigit(char *str, int beginIndex) {
int i, res = 0;
for (i = beginIndex; str[i] != 0 && isdigit(str[i]); ++i)
res = res * 10 + str[i] - '0';
return res;
}
/**
* Scan the string and find paired parenthesis.
*/
void scanParenthesis(char *str) {
static int sk[N];
int top = 1, i;
for (i = 0; str[i]; ++i) {
if (str[i] == '(') sk[++top] = i;
else if (str[i] == ')') partner[sk[top--]] = i;
}
}
/**
* Parse the given chemical formula. Compute the occupation of each element in the formula.
* @param res The result map, containing pairs: element -> occupation
* @param str The whole string
* @param len The length of str
* @param from beginIndex
* @param to endIndex
* @param mul the multiplication
*/
void work(map<string, int> &res, char *str, int len, int from, int to, int mul = 1) {
if (from > to) return;
for (int i = from; i <= to; ) {
if (str[i] == '(') {
int m = 1;
if (isdigit(str[partner[i] + 1]))
m = findDigit(str, partner[i] + 1);
work(res, str, len, i + 1, partner[i] - 1, mul * m);
i = partner[i] + 1;
} else {
int e = findElement(str, i);
if (e == 0) { // not a valid element description.
++i;
continue;
}
// check the subscript
int cnt = 1;
if (i + e < len && isdigit(str[i + e]))
cnt = findDigit(str, i + e);
res[substring(str, i, e)] += mul * cnt;
i += e;
}
}
}
int main() {
static char str[N];
static pair<string, int> pairs[N];
while (scanf("%s", str) != EOF) {
scanParenthesis(str);
int len = strlen(str), tot = 0;
map<string, int> res;
work(res, str, strlen(str), 0, len - 1);
foreach(it, res) {
if (it != res.begin())
putchar('+');
if (it->second > 1)
printf("%d", it->second);
printf("%s", it->first.c_str());
}
putchar('\n');
}
return 0;
}