【題目】http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=5383spa
【報告】get
很明顯,*是不用加的,只用加數字就好了。數字最多加 符號的個數+1減數字的個數。class
至於交換,就按照後綴表達式那樣掃一遍,碰見一個數字就計數器+1,趕上符號就-1,若是計數器=0了,就把當前位置的*和從後往前第一個數字交換。程序
證實啥的,很明顯是吧,很理所固然是吧……word
【程序】di
#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;
}
while