Codeforces 115E Unambiguous Arithmetic Expression

D. Unambiguous Arithmetic Expression
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

Let's define an unambiguous arithmetic expression (UAE) as follows.

  • All non-negative integers are UAE's. Integers may have leading zeroes (for example, 0000 and 0010 are considered valid integers).
  • If X and Y are two UAE's, then "(X) + (Y)", "(X) - (Y)", "(X) * (Y)", and "(X) / (Y)" (all without the double quotes) are UAE's.
  • If X is an UAE, then " - (X)" and " + (X)" (both without the double quotes) are UAE's.

    You are given a string consisting only of digits ("0" - "9") and characters "-", "+", "*", and "/". Your task is to compute the number of different possible unambiguous arithmetic expressions such that if all brackets (characters "(" and ")") of that unambiguous arithmetic expression are removed, it becomes the input string. Since the answer may be very large, print it modulo 1000003 (106 + 3).

Input

The first line is a non-empty string consisting of digits ('0'-'9') and characters '-', '+', '*', and/or '/'. Its length will not exceed 2000. The line doesn't contain any spaces.

Output

Print a single integer representing the number of different unambiguous arithmetic expressions modulo 1000003 (106 + 3) such that if all its brackets are removed, it becomes equal to the input string (character-by-character).

Examples
input
1+2*3
output
2
input
03+-30+40
output
3
input
5//4
output
0
input
5/0
output
1
input
1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1
output
100728
Note

For the first example, the two possible unambiguous arithmetic expressions are:

((1) + (2)) * (3)
(1) + ((2) * (3))

For the second example, the three possible unambiguous arithmetic expressions are:

(03) + (( - (30)) + (40))
(03) + ( - ((30) + (40)))
((03) + ( - (30))) + (40)

题目大意:这题看上去很花哨,实际上大多都是废话。大致意思就是说,一些数(可以有前导零和正负号)构成的不同的代数式有多少种。

思路:我们先来分析几种不可能的情况,(1)第一位有*或/,这显然;(2)最后一位有符号,显然;(3)连续出现两个符号且第二个符号是*或/,我们思考,若+-和+-挨着,可能造成的情况是加上一个正数或加上一个负数这种情况(因为一个数可以有正负号),而如果第二位是个*/,就一定不可能。

求方案数,一眼就知道是个DP,我们设置状态为f[i]表示处理到前i个符号的方案数,提前预处理处连续两个+-的位置,向后转移,若此处有连续的加减,则方案由j+1向j转移,否则由j向j转移。

#include<cstdio>
#include<cstring>
#include<string>
#include<algorithm>
#include<iostream>
#include<cmath>
#include<cstdlib>
#include<ctime>

using namespace std;

const int MAXN=2010;
const int Mod=1e6+3;
char s[MAXN];
int f[MAXN][MAXN],g[MAXN][MAXN];
int pos[MAXN];

int main()
{
	scanf("%s",s);
	int len=strlen(s);
	if (s[0]=='*' || s[0]=='/' || s[len-1]<'0' | s[len-1]>'9')
	{
		cout<<0;
		return 0;
	}	
	int num=0;
	for (int i=0;i<len;i++)
	{
		if (s[i]<'0' || s[i]>'9')
		{
			if (s[i+1]=='*' || s[i+1]=='/')
			{
				cout<<0;
				return 0;
			}
			else if (s[i+1]=='+' || s[i+1]=='-')
			{
				pos[num]=1;
			}
			num++;
		}	
	} 
	f[0][0]=1;
	for (int i=0;i<num;i++)
	{
		for (int j=0;j<=num;j++)
		{
			g[i][j]=f[i][j];
			if (j)
			{
				g[i][j]+=g[i][j-1];
			}
			g[i][j]%=Mod;
		}
		if (!pos[i])
		{
			for (int j=0;j<num;j++)
			{
				f[i+1][j]+=g[i][j+1];
				f[i+1][j]%=Mod;
			}
		}
		else
		{
			for (int j=1;j<num;j++)
			{
				f[i+1][j]+=g[i][j];
				f[i+1][j]%=Mod;
			}
		}
	}
	cout<<f[num][0];
    return 0;
}


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值