zoj 3829 Known Notation 贪心

本文详细阐述了在数字消除游戏中,如何通过优化策略确保游戏胜利。重点在于利用*符号进行数字消除,确保数字数量足够与*符号匹配,并通过遍历与交换操作实现目标。文章提供了解决此类问题的算法实现,包括输入输出处理、状态维护与消除逻辑。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

为何自己做的时候没做出来。。。

题意:

思路:

由于一个 * 能消除2个数字,然后生成一个数字。因此首先要保证数字个数大于等于 ‘*’ 个数+1。

如果数字个数不够,就在最前面添加。

然后开始从头开始遍历,遇到*就看当前的数字个数是否有两个或以上.不够的话,从最后面开始找第一个不是‘*’的进行交换;够则直接消掉。

code:

/* **********************************************
Created Time: 2014-10-14 14:48:16
File Name   : zoj3829.cpp
*********************************************** */
//#pragma comment(linker, "/STACK:102400000,102400000")
#include <iostream>
#include <fstream>
#include <cstring>
#include <climits>
#include <deque>
#include <cmath>
#include <queue>
#include <stack>
#include <list>
#include <map>
#include <set>
#include <utility>
#include <sstream>
#include <complex>
#include <string>
#include <vector>
#include <cstdio>
#include <bitset>
#include <functional>
#include <algorithm>
using namespace std;
typedef long long LL;
const int MAXN = 1005;
string str;
int main()
{
        ios::sync_with_stdio(false);
        int T;
        cin>>T;
        while(T--)
        {
                int res = 0;
                cin>>str;
                int n = str.length();
                int cots = 0, cotn = 0;
                for(int i = 0 ;i < n; i++)
                {
                        if(str[i] >= '0' && str[i] <= '9')
                                cotn++;
                        else
                                cots++;
                }
                int sta = 0;
                if(cotn < cots+1)
                {
                        res += cots+1 - cotn;
                        sta = res;
                }
                //
                for(int i = 0;i < n ;i++)
                {
                        if(str[i] == '*')
                        {
                                if(sta >= 2)
                                        sta--;
                                else
                                {
                                        for(int j = n-1;j > i; j--)
                                        {
                                                if(str[j] != '*')
                                                {
                                                        swap(str[j], str[i]);
                                                        res++;
                                                        sta++;
                                                        break;
                                                }
                                        }
                                }
                        }
                        else
                                sta++;
                }
                cout<<res<<endl;
        }
        return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值