【ZOJ】3829 Known Notation

【题目】http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=5383

【报告】

       很明显,*是不用加的,只用加数字就行了。数字最多加 符号的个数+1减数字的个数。

       至于交换,就按照后缀表达式那样扫一遍,遇见一个数字就计数器+1,遇上符号就-1,如果计数器=0了,就把当前位置的*和从后往前第一个数字交换。

       证明啥的,很明显是吧,很理所当然是吧……

【程序】

#include
#include
#include
#include
#include
using namespace std;
const int Len = 1000;
char str[Len+1];
int main()
{
    int T;
    scanf("%d",&T);
    while (T--)
    {
        scanf("%s",str);
        int a,b;
        a=b=0;
        for (int i=0;i
            if (str[i]=='*')
                b++;
            else a++;
        if (b==0){printf("0\n");continue;}
        int num=0,ans=0;;
        if (b>=a) num+=b+1-a,ans+=b+1-a;  // 补足
        for (int i=0,k=strlen(str)-1;i
            if (str[i]!='*') num++;
            else if (num>1) num--;
            else
            {
                while (k>=i&&str[k]=='*') k--;
                if (k>i){swap(str[k],str[i]);i--;ans++;}
                else num++,ans++;
            }
        printf("%d\n",ans);
    }
    return 0;
}

posted @ 2014-10-15 16:38  为美好世界献上珂学  阅读(99)  评论(0编辑  收藏  举报