zoukankan      html  css  js  c++  java
  • [CF552E] Vanya and Brackets

    Description

    给定一个只有 +* 和一位整数构成的表达式,你可以添加一对括号,使得这个表达式的值最大。* 的个数不超过 (15)

    Solution

    显然 ( 一定出现在式子最左边或者某个 * 的右边

    显然 ) 一定出现在式子最右边或者某个 * 的左边

    于是我们可以暴力枚举要将哪一段套括号,然后生成一个新的表达式,暴力计算即可

    手写表达式计算器太烦了,于是直接 Python

    src_str=input()
    lenstr=len(src_str)
    str=list(src_str)
    
    listMulPos=[]
    for i,c in enumerate(str):
        if c=='*':
            listMulPos.append(i)
    listLeftParPos=[0]
    for i in listMulPos:
        listLeftParPos.append(i+1)
    listRightParPos=[lenstr]
    for i in listMulPos:
        listRightParPos.append(i)
    
    ans=0
    
    # 枚举所有可行的表达式并计算
    for leftPos in listLeftParPos:
        for rightPos in listRightParPos:
            tmp=[]
            for i in str:
                tmp.append(i)
            if(leftPos<rightPos+1):
                tmp.insert(leftPos,'(')
                tmp.insert(rightPos+1,')')
                tmpval=eval(''.join(tmp))
                ans=max(ans,tmpval)
    
    print(ans)
    
    
  • 相关阅读:
    ubuntu install gobgp
    ubunut install golang
    Using GoBGP as an IXP connecting router
    400 行 C 代码实现一个虚拟机
    IPv6 Segment Routing (SRv6)
    How to Install VPP in ubuntu x86 or arm64
    mpls + sr + bgp
    ospf sr
    520了,用32做个简单的小程序
    FPGA设计经验总结
  • 原文地址:https://www.cnblogs.com/mollnn/p/13644040.html
Copyright © 2011-2022 走看看