作者:jostree 转载请说明出处 http://www.cnblogs.com/jostree/p/4020792.html

题目链接: zoj 3829 Known Notation

使用贪心+模拟。由于没有数字之间没有空格,因此该题有如下性质:

1.如果该字符串全部为数字,则无需操作,直接输出0。

2.连续的n个数字后面接连续的m个*,当n>m时,一定是有效的答案。从而最终的目的就是把最后的数字尽量向前移动,把最前面非法的*尽量向后移动,因此insert操作添加数字时,只需添加在最前面即可。

3.*的个数一定要小于数字的个数,因为每个*号需要消耗掉一个数字,并且首个*消耗掉2个数字。因此如果发现数字的个数比*的个数少k个,那么需要直接在字符串首添加k+1个数字。

4.如果字符串非全数字,那么结尾的数字一定要被替换成*,否则串无效。因此遇到第一个*时,直接与结尾的数字交换。

5.遍历整个串,当发现*的个数大于等于数字的个数时,直接把该位置的*与最后的数字进行替换。

代码如下:

 #include <cstdio>
#include <cstdlib>
#include <iostream>
#include <cstring>
using namespace std;
char str[];
void solve()
{
int len = strlen(str);
int nnum = , nstar = ;
int pnum = len-;
int res = ;
int leftnum = ;
for( int i = ; i < len ; i++ )
{
if( str[i] == '*' ) nstar++;
else nnum++;
}
if( nnum == len )
{
printf("0\n");
return;
}
if( nnum - nstar <= )
{
leftnum = nstar -nnum + ;
res += leftnum;
}
for( int i = ; i < len ; i++ )
{
while( i < pnum && str[pnum] == '*') pnum--;
if( str[i] == '*' )
{
leftnum--;
if( pnum == len- && str[pnum] != '*' )
{
str[i] = str[pnum];
str[pnum] = '*';
leftnum += ;
res++;
pnum--;
}
else if( leftnum <= )
{
str[i] = str[pnum];
str[pnum] = '*';
res++;
leftnum += ;
}
}
else leftnum++;
}
printf ( "%d\n", res );
}
int main(int argc, char *argv[])
{
int T;
scanf("%d", &T);
while( T-- )
{
scanf ( "%s", str );
solve();
}
}

另附测试用例:

1***1
**1
1*1*1*
1*1*1*1
1**
1111*
1111*1
111**1
*111*
**1111*
***111111*
*1**1
*1**1*
*1111**
*1111**1
*111**11
***111
***
**1
*1*
*11
1*
*1
1** 3
3
1
1
2
0
1
1
1
2
2
3
3
1
1
1
3
4
3
2
1
1
2
2

zoj 3829 Known Notation的更多相关文章

  1. 贪心+模拟 ZOJ 3829 Known Notation

    题目传送门 /* 题意:一串字符串,问要最少操作数使得成为合法的后缀表达式 贪心+模拟:数字个数 >= *个数+1 所以若数字少了先补上在前面,然后把不合法的*和最后的数字交换,记录次数 岛娘的 ...

  2. ZOJ 3829 Known Notation 贪心

    Known Notation Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://acm.zju.edu.cn/onlinejudge/showPro ...

  3. ZOJ 3829 Known Notation (2014牡丹江H称号)

    主题链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do? problemId=5383 Known Notation Time Limit: 2 S ...

  4. ZOJ 3829 Known Notation 乱搞

    乱搞: 1.数字的个数要比*的个数多一个,假设数字不足须要先把数字补满 2.最优的结构应该是数字都在左边,*都在右边 3.从左往右扫一遍,遇到数字+1,遇到*-1,假设当前值<1则把这个*和最后 ...

  5. zoj 3829 Known Notation(2014在牡丹江区域赛k称号)

    Known Notation Time Limit: 2 Seconds      Memory Limit: 131072 KB Do you know reverse Polish notatio ...

  6. ZOJ 3829 Known Notation 贪心 难度:0

    Known Notation Time Limit: 2 Seconds      Memory Limit: 65536 KB Do you know reverse Polish notation ...

  7. ACM学习历程——ZOJ 3829 Known Notation (2014牡丹江区域赛K题)(策略,栈)

    Description Do you know reverse Polish notation (RPN)? It is a known notation in the area of mathema ...

  8. ZOJ - 3829 Known Notation(模拟+贪心)

    http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3829 给定一个字符串(只包含数字和星号)可以在字符串的任意位置添加一个数字 ...

  9. ZOJ 3829 Known Notation(字符串处理 数学 牡丹江现场赛)

    题目链接:problemId=5383">http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=5383 Do you ...

随机推荐

  1. 如何用 iptables 禁止某个ip?

    国内私募机构九鼎控股打造APP,来就送 20元现金领取地址:http://jdb.jiudingcapital.com/phone.html内部邀请码:C8E245J (不写邀请码,没有现金送)国内私 ...

  2. tableview 上拉时 标题行出现在顶部不动效果

    类似这种效果: 其实很简单,利用tableview 的plain属性,然后使用section,其实滑上去不动的是  section的headView. -(NSInteger)numberOfSect ...

  3. Activity的生命周期,BACK键和HOME键生命周期

    Activity的生命周期模型在Google提供的官方文档上有比较详细的一个图示 public class HelloActivity extends Activity { public static ...

  4. 如何限制input只能输入数字

    在input上增加onkeyup和onafterpaste事件,事件中用正则表达式替换其它字符,测试没有问题. <input type="text" value=" ...

  5. MySQL的数据类型(转)

    MySQL的数据类型 1.整数 TINYINT: 8 bit 存储空间 SMALLINT: 16 bit 存储空间 MEDIUMINT: 24 bit 存储空间 INT: 32 bit 存储空间 BI ...

  6. 不同linux系统添加开机启动程序的命令

    see http://phpcj.org/blog/%E4%B8%8D%E5%90%8Clinux%E7%B3%BB%E7%BB%9F%E6%B7%BB%E5%8A%A0%E5%BC%80%E6%9C ...

  7. Android(java)学习笔记129:Tab标签的使用

    1.案例1---TabProject (1)首先是main.xml文件: <?xml version="1.0" encoding="utf-8"?> ...

  8. Http抓包工具Charlse使用教程

    [原文]http://blog.csdn.net/wildfireli/article/details/19829479 Charles是目前最强大的http调试工具,在界面和功能上远强于Fiddle ...

  9. ubuntu on win VS ubuntu(virtual box)VS Cygwin

    执行命令粗略估计执行时间: date --rfc-3339='ns';seq 100000000 | grep 8 | wc -w; date --rfc-3339='ns' ubuntu 14.04 ...

  10. rsync配置

    一.rsync 简介 Rsync(remote synchronize)是一个远程数据同步工具,可通过LAN/WAN快速同步多台主机间的文件,也可以使用 Rsync 同步本地硬盘中的不同目录. Rsy ...