为何自己做的时候没做出来。。。
题意:
略
思路:
由于一个 * 能消除2个数字,然后生成一个数字。因此首先要保证数字个数大于等于 ‘*’ 个数+1。
如果数字个数不够,就在最前面添加。
然后开始从头开始遍历,遇到*就看当前的数字个数是否有两个或以上.不够的话,从最后面开始找第一个不是‘*’的进行交换;够则直接消掉。
code:
/* **********************************************
Created Time: 2014-10-14 14:48:16
File Name : zoj3829.cpp
*********************************************** */
//#pragma comment(linker, "/STACK:102400000,102400000")
#include <iostream>
#include <fstream>
#include <cstring>
#include <climits>
#include <deque>
#include <cmath>
#include <queue>
#include <stack>
#include <list>
#include <map>
#include <set>
#include <utility>
#include <sstream>
#include <complex>
#include <string>
#include <vector>
#include <cstdio>
#include <bitset>
#include <functional>
#include <algorithm>
using namespace std;
typedef long long LL;
const int MAXN = 1005;
string str;
int main()
{
ios::sync_with_stdio(false);
int T;
cin>>T;
while(T--)
{
int res = 0;
cin>>str;
int n = str.length();
int cots = 0, cotn = 0;
for(int i = 0 ;i < n; i++)
{
if(str[i] >= '0' && str[i] <= '9')
cotn++;
else
cots++;
}
int sta = 0;
if(cotn < cots+1)
{
res += cots+1 - cotn;
sta = res;
}
//
for(int i = 0;i < n ;i++)
{
if(str[i] == '*')
{
if(sta >= 2)
sta--;
else
{
for(int j = n-1;j > i; j--)
{
if(str[j] != '*')
{
swap(str[j], str[i]);
res++;
sta++;
break;
}
}
}
}
else
sta++;
}
cout<<res<<endl;
}
return 0;
}