zoj 3829 Known Notation 贪心

来源:互联网 发布:精乌胶囊知乎 编辑:程序博客网 时间:2024/05/21 16:58

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

题意:

思路:

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

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

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

code:

/* **********************************************Created Time: 2014-10-14 14:48:16File 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;}


0 0
原创粉丝点击