NOIP 2011 表达式的值

题目描述
对于1 位二进制变量定义两种运算:
这里写图片描述
运算的优先级是:
先计算括号内的,再计算括号外的。
“× ”运算优先于“⊕”运算,即计算表达式时,先计算× 运算,再计算⊕运算。例如:计算表达式A⊕B × C时,先计算 B × C,其结果再与 A 做⊕运算。
现给定一个未完成的表达式,例如+(*_),请你在横线处填入数字0 或者1 ,请问有多少种填法可以使得表达式的值为0 。


【题目分析】
递推


【代码】

#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;
const int mod=10007;
struct node{int one,zer;}unit,next;
node num[100010];
char sym[100010],s[100010];
int i,cnt=0,p=0,l=0;
void push()
{sym[++p]=s[i];}
void pop()
{
    switch (sym[p--])
    {
        case'+':
            next.one=(num[p].one*num[p+1].one+num[p].zer*num[p+1].one+num[p].one*num[p+1].zer)%mod;
            next.zer=(num[p].zer*num[p+1].zer)%mod;
            num[p]=next;
            break;
        case'*':
            next.zer=(num[p].zer*num[p+1].zer+num[p].zer*num[p+1].one+num[p].one*num[p+1].zer)%mod;
            next.one=(num[p].one*num[p+1].one)%mod;
            num[p]=next;
            break;
    }
}
bool can()
{
    if (s[i]=='+'&&sym[p]!='(') return 1;
    if (s[i]=='*'&&sym[p]!='('&&sym[p]!='+') return 1;
    return false;
}
int main()
{
    unit.one=unit.zer=1;
    scanf("%d",&l);
    scanf("%s",s+1);
    sym[++p]='(';
    l++;
    s[l]=')';
//    printf("%s",s+1);
    i=1;
    while (i<=l)
    {
        while (s[i]=='(') {push(); i++;}
        num[p]=unit;
        do
        {
            if (s[i]==')')
            {
                while (sym[p]!='(') pop();
                p--;
                num[p]=num[p+1];
            }
            else
            {
                while (can()) pop();
                push();
            }
            i++;
        }while(i<=l&&s[i-1]==')');
    }
    printf("%d\n",num[0].zer);
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值