HUNNU11342:Chemistry(模拟)

时间:2022-08-22 21:09:28

http://acm.hunnu.edu.cn/online/?action=problem&type=show&id=11342

Problem description

The chemical formula of a molecule M describes its atomic make-up. Chemical formulas obey the following grammar:

      M := G | M G
G := S | S C
S := A | '(' M ')'
C := T | N E
E := D | D E
T := '2' | ... | '9'
N := '1' | ... | '9'
D := '0' | .. | '9'
A := U | U L | U L L
U := 'A' | .. | 'Z'
L := 'a' | .. | 'z'

The count C represents a multiplier for the subgroup S that precedes it. For example, H2O has two H (hydrogen) and one O (oxygen) atoms, and (AlC2)3Na4 contains 3 Al (aluminum), 6 C (carbon) and 4 Na (sodium) atoms.

Input
  The input will contain data for one or more test cases. For each test case, there will be one line of input, containing a valid chemical formula. Each line will have no more than 100 characters. 
Output
  For each line of input there will be one line of output which is the atomic decomposition of the chemical in the form of a sum as shown in the sample output. The atoms are listed in lexicographical order, and a count of 1 is implied and not explicitly written. There are no blank spaces in the output. All of the counts in the correct output will be representable in 32-bit signed integers. 
Sample Input
H2O
(AlC2)3Na4
Sample Output
2H+O
3Al+6C+4Na
Problem Source

2012 Rocky Mountain Regional Contest

题意:按字典序升序输出所有元素

思路:一道恶心的模拟题啊,暴力模拟即可,弄了好久啊,一开始是全部算出来在进行叠加,但是超时了,于是就每次计算完一个元素后都放进去,终于过了,0ms

#include <stdio.h>
#include <string.h>
#include <algorithm>
using namespace std; struct node
{
char word[105];
int cnt;
} a[105]; char str[105]; int cmp(node x,node y)
{
return strcmp(x.word,y.word)<0;
} int main()
{
while(~scanf("%s",str))
{
int len = strlen(str),flag = 1;
int l = -1,ll = 0;
int i,j,k,zuo,t;
for(i = 0; i<=100; i++)
memset(a[i].word,'\0',sizeof(a[i].word));
for(i = 0; i<len; i++)
{
if(str[i]>='A' && str[i]<='Z')
{
if(l!=-1)
a[l].word[ll] = '\0';
flag = 1;
ll = 0;
if(l!=-1)
{
int tt = 0,c;
for(j = k; j<len; j++)//从该元素往后走,找括号
{
if(str[j] == '(')//找左括号
tt++;
else if(str[j] == ')')
{
if(tt)//与左括号匹配,消去一个无用的右括号
tt--;
else
{
zuo--;//匹配包含该元素的左括号
j++;
c = 0;
while(str[j]>='0' && str[j]<='9')//计算括号后面的数字
{
c = c*10+str[j]-'0';
j++;
}
j--;
if(c)//非0则与原来的数字相乘
a[l].cnt*=c;
}
}
if(!zuo)//没有包含该元素的左括号则跳出
break; }
}
for(j = 0; j<l; j++)
{
if(!strcmp(a[j].word,a[l].word))//有与该元素匹配的就直接将该元素的数目累加,并且长度减1,没有则不管
{
a[j].cnt+=a[l].cnt;
l--;
break;
}
}
l++;//下一个元素
a[l].word[ll++] = str[i];//放进结构体
k = i;//记录位置
a[l].cnt = 1;//初始化个数为1
t = zuo = 0;
for(j = i; j>=0; j--)//记录左边将此元素包裹在内的左括号的数量
{
if(str[j] == ')')
t++;
else if(str[j] == '(')
{
if(t)
t--;
else
zuo++;
}
}
}
else if(str[i]>='a' && str[i]<='z')
a[l].word[ll++] = str[i];
else if(str[i]>='0' && str[i]<='9' && flag && (str[i-1]>='A' && str[i-1]<='Z' || str[i-1]>='a' && str[i-1]<='z') )
{//这条件很重要,这是计算紧跟在元素后的数字,但不能算括号后面的数字
flag = 0;
int c = 0;
while(str[i]>='0' && str[i]<='9')
{
c = c*10+str[i]-'0';
i++;
}
i--;
if(c)
a[l].cnt*=c;
}
}
while(zuo)//这是计算最后一个元素,因为最后一个元素可能没有算
{
int tt = 0,c;
for(j = k; j<len; j++)
{
if(str[j] == '(')
tt++;
else if(str[j] == ')')
{
if(tt)
tt--;
else
{
zuo--;
j++;
c = 0;
while(str[j]>='0' && str[j]<='9')
{
c = c*10+str[j]-'0';
j++;
}
j--;
if(c)
a[l].cnt*=c;
}
}
}
}
for(j = 0; j<l; j++)
{//医务室计算最后一个元素
if(!strcmp(a[j].word,a[l].word))
{
a[j].cnt+=a[l].cnt;
l--;
break;
}
}
sort(a,a+l+1,cmp);
if(a[0].cnt!=1)
printf("%d%s",a[0].cnt,a[0].word);
else
printf("%s",a[0].word);
for(i = 1; i<=l; i++)
{
if(a[i].cnt!=1)
printf("+%d%s",a[i].cnt,a[i].word);
else
printf("+%s",a[i].word);
}
printf("\n");
} return 0;
}

HUNNU11342:Chemistry(模拟)的更多相关文章

  1. 「模拟8&period;29」chinese&lpar;性质&rpar;&&num;183&semi;physics&&num;183&semi;chemistry&lpar;概率期望&rpar;

    T1  chinese 根据他的问题i*f[i]我们容易联想到,答案其实是每种方案中每个点的贡献为1的加和 我们可以转变问题,每个点在所有方案的贡献 进而其实询问就是1-k的取值,有多少中方案再取个和 ...

  2. &lbrack;CSP-S模拟测试&rsqb;&colon;chemistry(期望DP&plus;组合数学)

    题目传送门(内部题27) 输入格式 第一行有$4$个整数$n,k,p,q$.第二行有$n$个整数$a_i$.接下来有$n-1$行,每行有两个整数$u,v$,表示$u$与$v$之间通过化学单键连接. 输 ...

  3. URAL 2046 A - The First Day at School 模拟题

    A - The First Day at SchoolTime Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://acm.hust.edu.cn/vjudg ...

  4. yyy loves Easter&lowbar;Egg I(恶心的字符串模拟)

    题目背景 Soha的出题效率着实让人大吃一惊.OI,数学,化学的题目都出好了,物理的题还没有一道.于是,Huntfire,absi2011,redbag对soha进行轮番炸,准备炸到soha出来,不料 ...

  5. App开发:模拟服务器数据接口 - MockApi

    为了方便app开发过程中,不受服务器接口的限制,便于客户端功能的快速测试,可以在客户端实现一个模拟服务器数据接口的MockApi模块.本篇文章就尝试为使用gradle的android项目设计实现Moc ...

  6. 故障重现&comma; JAVA进程内存不够时突然挂掉模拟

    背景,服务器上的一个JAVA服务进程突然挂掉,查看产生了崩溃日志,如下: # Set larger code cache with -XX:ReservedCodeCacheSize= # This ...

  7. Python 爬虫模拟登陆知乎

    在之前写过一篇使用python爬虫爬取电影天堂资源的博客,重点是如何解析页面和提高爬虫的效率.由于电影天堂上的资源获取权限是所有人都一样的,所以不需要进行登录验证操作,写完那篇文章后又花了些时间研究了 ...

  8. HTML 事件&lpar;四&rpar; 模拟事件操作

    本篇主要介绍HTML DOM中事件的模拟操作. 其他事件文章 1. HTML 事件(一) 事件的介绍 2. HTML 事件(二) 事件的注册与注销 3. HTML 事件(三) 事件流与事件委托 4.  ...

  9. 模拟AngularJS之依赖注入

    一.概述 AngularJS有一经典之处就是依赖注入,对于什么是依赖注入,熟悉spring的同学应该都非常了解了,但,对于前端而言,还是比较新颖的. 依赖注入,简而言之,就是解除硬编码,达到解偶的目的 ...

随机推荐

  1. 【Cocos2d-x for WP8 学习整理】(4)CCTableView 实现《天天爱消除》中的得分榜

    接上回 CCScrollView 继续,在GUI 里还有个 CCScrollView 的子类---CCTableView . 这个名字应该是从 IOS 里的 UITableView来的,其实是跟WP8 ...

  2. python简单文件操作

    写软件著作申请,需要将所有源代码代码贴入一个word中,在源代码前后要添加一些标记,如下: //////////////////////////// //filename1 ///////////// ...

  3. &lbrack;转&rsqb;C&num;操作注册表

    原文链接:http://www.cnblogs.com/txw1958/archive/2012/08/01/csharp-regidit.html 下面我们就来用.NET下托管语言C#注册表操作,主 ...

  4. MongoDB学习(2)—Node&period;js与MongoDB的基本连接示例

    前提 已经安装了node.js和MongoDB,本文使用的node.js是v0.12.0,MongoDB是3.0.0. 初始化数据 启动MongoDB服务,在test数据库中插入一条实例数据: db. ...

  5. 【转】C&plus;&plus; static关键字

    原文:http://blog.csdn.net/hackbuteer1/article/details/7487694 C++的static有两种用法:面向过程程序设计中的static和面向对象程序设 ...

  6. 软件测试实验二----selenium、katalon、junit

    1.安装firefox和seleniumIDE.katalon 安装按成后在Firefox中有seleniumIDE.katalon的图标 2.使用katalon导出测试脚本 点击katalon的插件 ...

  7. Java Design Pattern&lpar;Factory&comma;Singleton&comma;Prototype&comma;Proxy&rpar;

    一.Factory 设计模式: the most common pattern,create a new object ,eg. A a=new A();工厂模式的好处:工厂模式可以做到把创建对象单独 ...

  8. windows清除访问共享文件夹的登陆信息

    https://jingyan.baidu.com/article/c843ea0b70797e77931e4a96.html 当在命令提示窗口输入net use命令时,会显示本机缓存的共享登录信息, ...

  9. 【Cesium】天空盒子

    skyBox: new Cesium.SkyBox({ sources: { positiveX: 'static/image/skyBox/posx.jpg', negativeX: 'static ...

  10. 通过例子来理解python闭包。

    闭包:就是内部函数对enclosing作用域的变量进行引用.(可先参考python函数作用域LEGB) 通过一个例子体会 def func_150(val): passline = 90 if val ...