您的位置:首页 > 其它

贪心+模拟 ZOJ 3829 Known Notation

2015-08-16 20:42 183 查看
题目传送门

 /*
题意:一串字符串,问要最少操作数使得成为合法的后缀表达式
贪心+模拟:数字个数 >= *个数+1 所以若数字少了先补上在前面,然后把不合法的*和最后的数字交换,记录次数
岛娘的代码实在难懂啊~
*/
/************************************************
* Author        :Running_Time
* Created Time  :2015-8-16 14:29:49
* File Name     :K.cpp
************************************************/

#include <cstdio>
#include <algorithm>
#include <iostream>
#include <sstream>
#include <cstring>
#include <cmath>
#include <string>
#include <vector>
#include <queue>
#include <deque>
#include <stack>
#include <list>
#include <map>
#include <set>
#include <bitset>
#include <cstdlib>
#include <ctime>
using namespace std;

#define lson l, mid, rt << 1
#define rson mid + 1, r, rt << 1 | 1
typedef long long ll;
const int MAXN = 1e3 + 10;
const int INF = 0x3f3f3f3f;
const int MOD = 1e9 + 7;
char str[MAXN];

int main(void)    {     //ZOJ 3829 Known Notation
int T;  scanf ("%d", &T);
while (T--) {
scanf ("%s", str);
int x = 0, len = strlen (str);
for (int i=0; i<len; ++i)  {
if (str[i] == '*')  x++;
}
if (x == 0) {
puts ("0"); continue;
}
int ans = max (x + 1 - (len - x), 0);   int n = ans;
for (int i=0; i<len; ++i)   {
if (str[i] == '*')  {
if (n <= 1) n++, ans++;
else    n--;
}
else    n++;
}
printf ("%d\n", ans);
}

return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: