UVALive 6085|Chemistry|字符串处理
来源:互联网 发布:linux命令日志文件 编辑:程序博客网 时间:2024/06/15 04:49
https://icpcarchive.ecs.baylor.edu/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=4096
题目翻译
一个化学式的定义如下:
M := G | M G // 化学式G := S | S C // 原子团S := A | ’(’ M ’)’ // 原子团C := T | N E // 大于1的整数E := D | D E // 允许前导零的整数T := '2' | ... | '9' // 2 ~ 9N := '1' | ... | '9' // 1 ~ 9D := '0' | ... | '9' // 0 ~ 9A := U | U L | U L L // 元素符号U := 'A' | ... | 'Z' // 大写字母L := 'a' | ... | 'z' // 小写字母
如果一个原子团只有1个,那么角标1可以省略,否则不能省略。
输入
输入有多组数据,EOF结束。
对于每组数据一行一个字符串,表示一种物质的化学式(一定是合法的,不会超过100个字符)。
输出
对于每组数据输出一行。输出一个分子的相应物质中每种元素有几个原子(注意如果只有1个原子那么1省略)。输出不可以有空格。计算过程中所有数字都在32位有符号整数范围内。具体格式参考输出样例
样例输入
H2O(AlC2)3Na4
样例输出
2H+O3Al+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;}
阅读全文
0 0
- UVALive 6085|Chemistry|字符串处理
- 【字符串处理】UVALive
- UVALive 4174 Steganography 字符串处理 密码学
- UVALive - 4882 Parenthesis 表达式处理、字符串处理、栈
- Chemistry
- UVALive 7370--字符串排序
- HDU 5625 Clarke and chemistry(字符串模拟题目)
- Chemistry Journals
- uvalive 5734 字符串最小表示法
- UVALive 2755 Hidden Password(字符串最小表示)
- UvaLive 6439 Pasti Pas! 字符串哈希
- UVALive 4167 Parity (字符串水)
- Uvalive 4513 Stammering Aliens(字符串Hash)
- UVALive
- UVALive
- UVALive
- UVALive
- UVALive
- JSONP原理--解决ajax跨域问题
- Leetcode:Path Sum II
- Android Studio【通用&方便】插件方式打包jar(不包含资源文件)
- 数据仓库
- 网络连接+HttpClient+AsyncTask
- UVALive 6085|Chemistry|字符串处理
- 【BZOJ 3591】 最长上升子序列
- LCA总结
- ROS常用命令
- MVP+Rxjava请求数据
- 蓝桥杯 算法提高 质数的后代
- 2017.11.03
- 宋家皇朝.
- conversion function 转换函数笔记----C++学习之路