ACM again- Ancient China Mathematics again 表达式计算 只有+和*和括号

ACM again- Ancient China Mathematics again

TimeLimit: 1 Second   MemoryLimit: 32 Megabyte

Totalsubmit: 368   Accepted: 31  

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

 

#include<iostream>
#include<cstdio>
#include<cstring>
#include<cctype>
using namespace std;
int stack[3000],pl;
int in[3000];//判断是否在括号内部:1表示在内部
int main()
{
    int ci;scanf("%d",&ci);
    while(ci--)
    {
        memset(stack,0,sizeof(stack));pl=0;
        memset(in,0,sizeof(in));
        string str;cin>>str;
        for(int i=0,len=str.size();i<len;)
        {
            if(str[i]=='[') stack[pl++]=-1,i++;//-1表示'['
            else if(str[i]=='|')
            {
                int num=0;
                while (i<len&&(str[i]=='|'||str[i]==','))
                {
                    if (str[i]=='|') num++;
                    else num<<=3;
                    i++;
                }
                stack[pl++]=num;
            }
            else //']'
            {
                i++;
                int num=0;
                while(stack[pl-1]!=-1)//因为左右括号是匹配的,所以不会出现stack[-1]这种情况
                {
                    num+=stack[pl-1];//考虑加的情况
                    in[pl-1]=0;//合并数字
                    pl--;
                }
                pl--;
                stack[pl++]=num;
                in[pl-1]=1;//这个数原来是在括号内部
                if(stack[pl-2]!=-1&&in[pl-2])//如果前面是一个数,并且原来是在括号内部,要相乘
                {
                    pl--;
                    stack[pl-1]*=stack[pl];
                    in[pl]=0;//合并数字
                    in[pl-1]=1;//在括号内部
                }
            }
        }
        printf("%d/n",stack[0]);
    }
    return 0;
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值