C++标程

// input code here
#include<bits/stdc++.h>
#define maxn 100000
#define mod 10007
using namespace std;
char ss[maxn+100],s[maxn+100];
int l,top,a[maxn+100],b[maxn+100];
void add()
{
    int k,k0,k1;
    k=a[0];
    k0=a[k-1]*a[k];
    k1=a[k-1]*b[k]+b[k-1]*a[k]+b[k-1]*b[k];
    a[--a[0]]=k0%mod,b[a[0]]=k1%mod;
}
void multiply()
{
    int k,k0,k1;
    k=a[0];
    k0=a[k-1]*a[k]+a[k-1]*b[k]+b[k-1]*a[k];
    k1=b[k-1]*b[k];
    a[--a[0]]=k0%mod,b[a[0]]=k1%mod;
}
int main()
{
    int i,j,k;
    char tmp;
    scanf("%d%s\n",&l,ss);
    a[0]=b[0]=top=0;
    for(i=0; i<l; i++)
    {
        tmp=ss[i];
        switch(tmp)
        {
            case '(':
                s[++top]=tmp;
                break;
            case '+':
            {
                if(i==0 || (i>0 && ss[i-1]!=')'))
                    a[++a[0]]=1,b[a[0]]=1;

                while(top>0 && s[top]!='(')
                {
                    if(s[top]=='+')add();
                    else multiply();
                    top--;
                }
                s[++top]=tmp;
                break;
            }
            case '*':
            {
                if((i>0 && ss[i-1]!=')') || i==0)
                    a[++a[0]]=1,b[a[0]]=1;
                while(top>0 && s[top]=='*')
                    multiply(),top--;
                s[++top]=tmp;
                break;
            }
            default:
            {
                if(ss[i-1]!=')')
                    a[++a[0]]=1,b[a[0]]=1;
                while(s[top]!='(')
                {
                    if(s[top]=='+')add();
                    else multiply();
                    top--;
                }
                top--;
                break;
            }
        }
    }
    if(top>0 && ss[l-1]!=')')
        a[++a[0]]=1,b[a[0]]=1;
    while(top>0)
    {
        if(s[top]=='+')add();
        else multiply();
        top--;
    }
    if(l==0)printf("%d\n",1);
    else printf("%d\n",a[a[0]]);
    return 0;
}

0 条评论

目前还没有评论...

信息

ID
1808
难度
6
分类
(无)
标签
递交数
748
已通过
212
通过率
28%
被复制
14
上传者