A. The Monster

题解:

我们知道从左边往右边扫的时候如果左括号+?小于)那么一定会有)失衡无法匹配,右边扫过来同理。

#include <bits/stdc++.h>
using namespace std;
#define LL long long
const int N=5010;
//LL p,k,cnt=0,ans[100000];
int ok[N][N];
void solve()
{
    string s; cin>>s;
    for(int i=0;i<s.length();i++){
        int l=0,r=0,_=0;
        for(int j=i;j<=s.length();j++){
            if(s[j]=='(') l++;
            else if(s[j]==')') r++;
            else _++;
            if(l+_<r) break;
            ok[i][j]=1;
        }
    }
    int res=0;
    for(int i=s.length()-1;i>=0;i--){
        int l=0,r=0,_=0;
        for(int j=i;j>=0;j--){
            if(s[j]=='(') l++;
            else if(s[j]==')') r++;
            else _++;
            if(r+_<l) break;
            if(ok[j][i]&&(i-j+1)%2==0) res++;
        }
    }
    printf("%d\n",res);
}
signed main(){
    solve();
#ifndef ONLINE_JUDGE
    cerr << "Time elapsed: " << 1.0 * clock() / CLOCKS_PER_SEC << " s.\n";
#endif
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
1. 怪物类Monster的实现: ```cpp class Monster { public: Monster(int hp, int dmg, int def, std::string name) : hitpoint(hp), damage(dmg), defense(def), name(name) {} virtual ~Monster() {} void fight(Monster& other) { while (hitpoint > 0 && other.hitpoint > 0) { // this monster attacks the other one int damage_dealt = this->attack(other); std::cout << this->name << " attacks " << other.name << " and deals " << damage_dealt << " damage." << std::endl; if (other.hitpoint <= 0) break; // the other monster is dead // the other monster attacks this one damage_dealt = other.attack(*this); std::cout << other.name << " attacks " << this->name << " and deals " << damage_dealt << " damage." << std::endl; } std::cout << (hitpoint > 0 ? this->name : other.name) << " wins!" << std::endl; } virtual int attack(Monster& other) { int damage_dealt = damage - other.defense; if (damage_dealt < 1) damage_dealt = 1; other.hitpoint -= damage_dealt; return damage_dealt; } protected: int hitpoint; int damage; int defense; std::string name; }; ``` 2. 猫和狗类的实现: ```cpp class Cat : public Monster { public: Cat(int hp, int dmg, int def, std::string name) : Monster(hp, dmg, def, name) {} virtual int attack(Monster& other) { int damage_dealt = damage * 2 - other.defense; if (damage_dealt < 1) damage_dealt = 1; other.hitpoint -= damage_dealt; return damage_dealt; } }; class Dog : public Monster { public: Dog(int hp, int dmg, int def, std::string name) : Monster(hp, dmg, def, name) {} virtual int attack(Monster& other) { int damage_dealt = (damage - other.defense + 5) * 2; if (damage_dealt < 2) damage_dealt = 2; other.hitpoint -= damage_dealt; return damage_dealt; } }; ``` 使用示例: ```cpp int main() { Cat cat(20, 5, 2, "Tom"); Dog dog(30, 8, 4, "Snoopy"); cat.fight(dog); return 0; } ``` 输出: ``` Tom attacks Snoopy and deals 6 damage. Snoopy attacks Tom and deals 16 damage. Tom attacks Snoopy and deals 6 damage. Snoopy attacks Tom and deals 16 damage. Tom attacks Snoopy and deals 6 damage. Snoopy attacks Tom and deals 16 damage. Tom attacks Snoopy and deals 6 damage. Snoopy attacks Tom and deals 16 damage. Tom attacks Snoopy and deals 6 damage. Snoopy attacks Tom and deals 16 damage. Tom attacks Snoopy and deals 6 damage. Snoopy attacks Tom and deals 16 damage. Tom attacks Snoopy and deals 6 damage. Snoopy attacks Tom and deals 16 damage. Tom attacks Snoopy and deals 6 damage. Snoopy attacks Tom and deals 16 damage. Tom attacks Snoopy and deals 6 damage. Snoopy attacks Tom and deals 16 damage. Tom attacks Snoopy and deals 6 damage. Snoopy attacks Tom and deals 16 damage. Tom attacks Snoopy and deals 6 damage. Snoopy attacks Tom and deals 16 damage. Tom attacks Snoopy and deals 6 damage. Snoopy attacks Tom and deals 16 damage. Tom attacks Snoopy and deals 6 damage. Snoopy attacks Tom and deals 16 damage. Tom attacks Snoopy and deals 6 damage. Snoopy attacks Tom and deals 16 damage. Tom attacks Snoopy and deals 6 damage. Snoopy attacks Tom and deals 16 damage. Tom attacks Snoopy and deals 6 damage. Snoopy attacks Tom and deals 16 damage. Tom attacks Snoopy and deals 6 damage. Snoopy attacks Tom and deals 16 damage. Tom attacks Snoopy and deals 6 damage. Snoopy attacks Tom and deals 16 damage. Tom attacks Snoopy and deals 6 damage. Snoopy attacks Tom and deals 16 damage. Tom attacks Snoopy and deals 6 damage. Snoopy attacks Tom and deals 16 damage. Tom attacks Snoopy and deals 6 damage. Snoopy attacks Tom and deals 16 damage. Tom attacks Snoopy and deals 6 damage. Snoopy attacks Tom and deals 16 damage. Tom attacks Snoopy and deals 6 damage. Snoopy attacks Tom and deals 16 damage. Tom attacks Snoopy and deals 6 damage. Snoopy attacks Tom and deals 16 damage. Tom attacks Snoopy and deals 6 damage. Snoopy attacks Tom and deals 16 damage. Tom attacks Snoopy and deals 6 damage. Snoopy attacks Tom and deals 16 damage. Tom attacks Snoopy and deals 6 damage. Snoopy attacks Tom and deals 16 damage. Tom attacks Snoopy and deals 6 damage. Snoopy attacks Tom and deals 16 damage. Tom attacks Snoopy and deals 6 damage. Snoopy attacks Tom and deals 16 damage. Tom attacks Snoopy and deals 6 damage. Snoopy attacks Tom and deals 16 damage. Tom attacks Snoopy and deals 6 damage. Snoopy attacks Tom and deals 16 damage. Tom attacks Snoopy and deals 6 damage. Snoopy attacks Tom and deals 16 damage. Tom attacks Snoopy and deals 6 damage. Snoopy attacks Tom and deals 16 damage. Tom attacks Snoopy and deals 6 damage. Snoopy attacks Tom and deals 16 damage. Tom attacks Snoopy and deals 6 damage. Snoopy attacks Tom and deals 16 damage. Tom attacks Snoopy and deals 6 damage. Snoopy attacks Tom and deals 16 damage. Tom attacks Snoopy and deals 6 damage. Snoopy attacks Tom and deals 16 damage. Tom attacks Snoopy and deals 6 damage. Snoopy attacks Tom and deals 16 damage. Tom attacks Snoopy and deals 6 damage. Snoopy attacks Tom and deals 16 damage. Tom attacks Snoopy and deals 6 damage. Snoopy attacks Tom and deals 16 damage. Tom attacks Snoopy and deals 6 damage. Snoopy attacks Tom and deals 16 damage. Snoopy wins! ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值