K

来源:互联网 发布:mac的俄语键盘逗号 编辑:程序博客网 时间:2024/04/29 08:12

题意:给两种操作:

(1)在表达式中填1;

(2)交换任意两个元素;

要求用最少的操作使字符串构成逆波兰表达式。

思路:贪心。

(1)先让numof(*)==numof(1)-1;

(2)从前往后扫,如果遇到*,numof(*)++;

else numof(1)++;

if numof(*)>=numof(1)   需要把后边的数字交换到前边的*,

即numof(*)--;

numof(1)++;

op++;

#include <iostream>#include <algorithm>#include <cstdio>#include <cmath>#include <cstring>#include <string>#include <string.h>#include <map>#include <set>#include <queue>#include <deque>#include <list>#include <bitset>#include <stack>#include <stdlib.h>#define lowbit(x) (x&-x)#define e exp(1.0)//ios::sync_with_stdio(false);typedef long long ll;typedef long long LL;using namespace std;int main(){    int T;    ios::sync_with_stdio(false);    cin>>T;    string s;    while(T--)    {        cin>>s;        int num = 0,starnum = 0;        for(int i=0;i<s.length();i++)        {            if(s[i]=='*') starnum++;            else num++;        }        int ans = starnum-num+1;        if(ans<0) ans = 0;        starnum = 0;        num = ans;        for(int i=0;i<s.length();i++)        {            if(s[i]=='*') starnum++;            else  num++;            if(starnum>=num)            {                ans++;                starnum--;                num++;            }        }        cout<<ans<<endl;    }    return 0;}


原创粉丝点击