ZOJ2829——贪心——Known Notation

http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3829

/*
题意:要求变成合法的串需要操作多少次
有两个操作
1.添加   2.交换
先扫一遍得到肯定要添的数字
从前往后,如果发现有数字n++,如果n<=1 时,因为一个*对应两个数字,所以n++,ans++,补一次添加操作,否则就补一次交换操作
*/
/************************************************
* Author        :Powatr
* Created Time  :2015-8-18 9:57:17
* File Name     :ZOJ3829.cpp
 ************************************************/

#include <cstdio>
#include <algorithm>
#include <iostream>
#include <sstream>
#include <cstring>
#include <cmath>
#include <string>
#include <vector>
#include <queue>
#include <deque>
#include <stack>
#include <list>
#include <map>
#include <set>
#include <bitset>
#include <cstdlib>
#include <ctime>
using namespace std;

#define lson l, mid, rt << 1
#define rson mid + 1, r, rt << 1 | 1
typedef long long ll;
const int MAXN = 1e3 + 10;
const int INF = 0x3f3f3f3f;
const int MOD = 1e9 + 7;

char s[MAXN];
int main(){
    int T;
    scanf("%d", &T);
    getchar();
    while(T--){
        scanf("%s", s+1);
        int len = strlen(s+1);
        int ans = 0;
        int x = 0;
        for(int i = 1; i <= len ;i++){
            if(s[i] == '*') x++;
        }
        if(x == 0) {
            puts("0");
            continue;
        }
         ans = max((x + 1 - (len - x)),0);
        int n = ans;
        for(int i = 1; i <= len; i++){
            if(s[i] == '*'){
                if(n <= 1) { n++, ans++;}
                else n--;
            }
            else n++;
        }
        printf("%d
", ans);
    }
    return 0;
}
                    

  

原文地址:https://www.cnblogs.com/zero-begin/p/4738680.html