1006 ACM again- Ancient China Mathematics again //The ACM/ICPC Asia Harbin Online First Round Contest 2010 Warmup-2
来源:互联网 发布:淘宝买东西怎么支付 编辑:程序博客网 时间:2024/04/29 01:10
ACM again- Ancient China Mathematics again
TimeLimit: 1 Second MemoryLimit: 32 Megabyte
Totalsubmit: 341 Accepted: 25
Description
ACM again- Ancient China Mathematics again
ACM again- Ancient China Mathematics again
Dr.K has published a essay on ACM(Ancient China Mathematics), he asked me to thank you for your help what really helped a lot. But another problem raised that the ACM is not as simple as we descripted last time.
In order to help you understand it, we should first go over the ancient people's counting system again:
1、They use only 7 digits to represent numbers, and numbers are as follow:
| -> 1
|| -> 2
||| -> 3
|||| -> 4
||||| -> 5
|||||| -> 6
||||||| -> 7
It is a pity that they have not developed "0" before these people disapeared.
2、If a number is greater than 7, they use digit place just as we do today. Between each two digit places, there is a ",".
eg:
|||||,|| -> 42 (5x8+2)
|||,||,|||| -> 212 (3*64+2*8+4)
In Dr.K's further study, he noticed that, there comes another two symbols - "[" and "]". They are often used together and there is always an expression between them. To see it more clearly, we define it as a grammer expression:
expression = [exp1]
exp1 = [exp1] | [exp1]exp1 | exp1[exp1] | Digit
Digit = "|" | "||" | "|||" | "||||" | "|||||" | "||||||" | "|||||||"
For example:
[||,||||]
[[||][||,|]]
[||[|||][|||||,|]]
are all acceptable expressions.
But what does these expressions mean? That really confused Dr.K a long time, fortunately, he conquered it. He found that:
1、"[]" is used in pairs and all the time with an expersion between them.
2、An expersion is always a number (certainly in their "|||..." form) or another expression.
3、An "exp1[exp2]" or "[exp1]exp2" expersion means exp1 + exp2.
4、An "[exp1][exp2]" expersion means exp1 x exp2.
For example:
[||[||]] = 2+2 = 4
[|] = 1
[[||][|,|]] = 2x9 = 18
[||[[||]|][||]|,||] = 2+(2+1)x2+10 = 18
As same as mordern mathematics, multiplication is piror to addition, and expersion in a "[]" should be count piror to the outer expersion.
For another time, he turns to you for help, it is sure that you will not let him down.
Input
The first line of standard input contains an integer N, the number of test cases. N lines followed.
In each line a squence is given, it is guaranteed that the length of the squence will not exceed 1024 characters and the outcome number will not be greater than 1000000.
Output
For each case you are to output a line giving the number in today's decimal counting system.
Sample Input
3
[||[||]]
[|]
[[||][|,|]]
Sample Output
4
1
18
先ORZ秦牛
#include<stdio.h>
#include<string.h>
char t[1026];
int stack[1026];
bool sc[1026];
int p;
int main()
{
int T;
scanf("%d",&T);
while(T--)
{
p=0;
memset(sc,0,sizeof(sc));
scanf("%s",t);
int l=strlen(t);
for(int i=0;i<l;i++)
{
char a=t[i];
switch (a)
{
case '[':stack[p++]=-1;break;
case '|':
{
int num=0;
while((a=='|'||a==',')&&i<l)
{
if(a=='|') num++;
else num=num<<3;
i++;
a=t[i];
}
i--;
stack[p++]=num;
break;
}
case ']':
{
int num=0;
while(p>0&&stack[p-1]!=-1)
{
num+=stack[p-1];
sc[p-1]=false;
p--;
}
p--;
stack[p]=num;
sc[p++]=true;
if(p-2>=0&&stack[p-2]!=-1&&sc[p-2])
{
p--;
stack[p-1]*=stack[p];
sc[p-1]=true;
sc[p]=false;
}
break;
}
}
}
int num=0;
while(p>0) num+=stack[--p];
printf("%d/n",num);
}
return 0;
}
- 1006 ACM again- Ancient China Mathematics again //The ACM/ICPC Asia Harbin Online First Round Contest 2010 Warmup-2
- The ACM/ICPC Asia Harbin Online First Round Contest 2010 Warmup-2
- 1005 not sub sequence //The ACM/ICPC Asia Harbin Online First Round Contest 2010 Warmup-2
- The ACM/ICPC Asia Harbin Online First Round Contest 2010 Warmup-1
- The ACM/ICPC Asia Harbin Online First Round Contest 2010
- ACM - Ancient China Mathematics
- 2012 ACM/ICPC Asia Regional Online Warmup-1006
- 2010 Asia ACM/ICPC Regional Harbin
- 2012 ACM/ICPC Asia Regional Online Warmup-1003
- 2012 ACM/ICPC Asia Regional Online Warmup-1004
- 2012 ACM/ICPC Asia Regional Online Warmup-1007
- 2013 ACM/ICPC Asia Regional Online —— Warmup 1002
- 2013 ACM/ICPC Asia Regional Online — Warmup 个人总结
- 2013 ACM/ICPC Asia Regional Online —— Warmup HDU4707
- The 2014 ACM-ICPC Asia Mudanjiang Regional First Round C
- The 2014 ACM-ICPC Asia Mudanjiang Regional First Round A
- The 2014 ACM-ICPC Asia Mudanjiang Regional First Round J
- The 2014 ACM-ICPC Asia Mudanjiang Regional First Round小记
- returning into(update,delete,insert)
- 使用rmmod会出现 rmmod : chdir(/lib/modules): No such file or directory ?
- U-BOOT环境变量实现
- ASP网页数据库连接
- MyEclipse and Tomcat
- 1006 ACM again- Ancient China Mathematics again //The ACM/ICPC Asia Harbin Online First Round Contest 2010 Warmup-2
- 如何实现ASP.NET中网站访问量的统计
- PHP汉字验证码的实现
- PHP的学习颈瓶如何跨越
- 关于文字内容溢出用点点点(…)省略号表示
- php 程序员
- joj 1989 Vampire Numbers
- 360安全卫士2011校园招聘信息
- iphone开发 简介