跟矩阵链乘同类型的题……

输出用%llu不是%I64u……

几组数据:

14
1+2*4+3*4+5*0
0*5*6+7*3+2
3+0+6+7+0+4
4*5+7*1*1+1
2*0+3*4*0+5*6+7+8
1+2+3*1+2*1+0
0+2*2+3*0+4
8*9*0+2
2*0+1+0*3
2*0*3+7+1*0*3
1+3*0*5+2
1+1+1+1+1
2*1+1+2*1+2*1+6
1*2*4*0*6*3

#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <cctype>
#include <algorithm> #define LL unsigned long long int using namespace std; const int MAXN = ; int cntNum, cntOp;
char str[MAXN];
char op[MAXN];
LL num[MAXN];
bool vis[MAXN][MAXN];
LL dpMax[MAXN][MAXN];
LL dpMin[MAXN][MAXN]; void show()
{
printf("All nums: \n");
for ( int i = ; i < cntNum; ++i )
printf( "%I64d ", num[i] ); printf("\nAll Ops:\n");
for ( int i = ; i < cntOp; ++i )
putchar( op[i] );
puts("");
return;
} void chuli()
{
cntNum = cntOp = ;
int len = strlen(str);
for ( int i = ; i < len; ++i )
{
LL tmp = ;
while ( i < len && isdigit( str[i] ) )
{
tmp = tmp * + ( str[i] - '' );
++i;
}
num[ cntNum++ ] = tmp;
if ( i < len )
op[ cntOp++ ] = str[i];
} //show();
return;
} LL DPMAX( int l, int r )
{
LL &res = dpMax[l][r];
if ( vis[l][r] ) return res; vis[l][r] = true;
if ( l == r ) return res = num[l];
res = ; for ( int k = l; k < r; ++k )
{
if ( op[k] == '+' )
res = max( res, DPMAX( l, k ) + DPMAX( k + , r ) );
else if ( op[k] == '*' )
res = max( res, DPMAX( l, k ) * DPMAX( k + , r ) );
}
//printf( "dp[%d][%d]=%I64u\n", l, r, res ); return res;
} LL DPMIN( int l, int r )
{
LL &res = dpMin[l][r];
if ( vis[l][r] ) return res; vis[l][r] = true;
if ( l == r ) return res = num[l];
bool first = true; for ( int k = l; k < r; ++k )
{
if ( op[k] == '+' )
{
if ( first )
{
res = DPMIN( l, k ) + DPMIN( k + , r );
first = false;
}
else
res = min( res, DPMIN( l, k ) + DPMIN( k + , r ) );
}
else if ( op[k] == '*' )
{
if ( first )
{
res = DPMIN( l, k ) * DPMIN( k + , r );
first = false;
}
else
res = min( res, DPMIN( l, k ) * DPMIN( k + , r ) );
}
}
//printf( "dp[%d][%d]=%I64u\n", l, r, res );
return res;
} int main()
{
int T;
scanf( "%d", &T );
while ( T-- )
{
scanf( "%s", str );
chuli();
memset( dpMax, , sizeof(dpMax) );
memset( dpMin, , sizeof(dpMin) ); memset( vis, false, sizeof(vis) );
LL maxx = DPMAX( , cntNum - ); memset( vis, false, sizeof(vis) );
LL minn = DPMIN( , cntNum - ); printf( "%llu %llu\n", maxx, minn );
}
return ;
}

最新文章

  1. noi 6045 开餐馆
  2. java堆内存与栈内存
  3. Dive into python 实例学python (1) —— 函数和测试
  4. 51nod1442 士兵的旅行
  5. 面经-csdn
  6. python 读取机器信息
  7. 小学生之Java中的异常
  8. C++中多重继承构造函数执行顺序
  9. Android 实时文件夹
  10. PHP中判断输入验证码是否一致
  11. ESXi5.0误删除虚拟机还有办法恢复吗?答案是可以!
  12. 数据降维之多维缩放MDS(Multiple Dimensional Scaling)
  13. 【题解】JSOIWC2019 Round1
  14. 64位Win7系统WMware安装Mac OS
  15. memcache分布式布置方案
  16. MySQL(安装,服务,创建用户及授权)
  17. MYSQL 复制详解
  18. [agc011E]Increasing Numbers-[思考题]
  19. linux screen 命令详解(转载)
  20. Stat3—因子分析(Factor Analysis)

热门文章

  1. 线程 task pritce
  2. redis hash类型
  3. 使用maven搭建ssm项目配置+tomact
  4. Mybatis中的DataSource配置
  5. MyEclipse 自动添加 作者 日期 等注解
  6. vue面试常被问到的问题整理
  7. 牛客小白月赛2 J 美 【构造】
  8. 本地通过VMware Workstation创建虚拟机,配置网络环境
  9. nop 插件解析
  10. vue本人常用插件汇总(常更新)