zoj3829Known Notation 贪心

//给出一个序列,最少需要多少步使得其成为
//后缀表达式
//对于这个序列如果数字个数小于"*"那么就采用插入的方式
//如果数字大于"*",就将最后一个数字和现在的'*'交换
#include<cstdio>
#include<cstring>
#include<iostream>
using namespace std ;
const int maxn = 1010 ;
char str[maxn] ;
int main()
{
    int t ;
    scanf("%d" , &t) ;
    while(t--)
    {
        scanf("%s" , str) ;
        int len = strlen(str) ;
        int d = 0  , x = 0 ;
        for(int i = 0 ;i < len;i++)
        if(str[i] != '*')
        d++ ;
        else
        x++ ;
        int ans = 0 ;
        int sumd = 0 ;
        int sumx = 0 ;
        for(int i = 0;i < len;i++)
        {
            if(str[i] == '*')
            sumx++ ;
            else
            sumd++ ;
            while(sumx+1 > sumd)
            {
                if(d > x)
                {
                    ans++ ;
                    for(int j = len-1 ;j > i;j--)
                    if(str[j] != '*')
                    {
                         swap(str[i] , str[j]) ;
                         break ;
                    }
                    sumx-- ;
                    sumd++ ;
                }
                else
                {
                    d++ ;
                    sumd++ ;
                    ans++ ;
                }
            }
        }
        cout<<ans<<endl;
    }
    return 0 ;
}


你可能感兴趣的:(zoj3829Known Notation 贪心)