zoj 3829 Known Notation
作者: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的更多相关文章
- 贪心+模拟 ZOJ 3829 Known Notation
题目传送门 /* 题意:一串字符串,问要最少操作数使得成为合法的后缀表达式 贪心+模拟:数字个数 >= *个数+1 所以若数字少了先补上在前面,然后把不合法的*和最后的数字交换,记录次数 岛娘的 ...
- ZOJ 3829 Known Notation 贪心
Known Notation Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://acm.zju.edu.cn/onlinejudge/showPro ...
- ZOJ 3829 Known Notation (2014牡丹江H称号)
主题链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do? problemId=5383 Known Notation Time Limit: 2 S ...
- ZOJ 3829 Known Notation 乱搞
乱搞: 1.数字的个数要比*的个数多一个,假设数字不足须要先把数字补满 2.最优的结构应该是数字都在左边,*都在右边 3.从左往右扫一遍,遇到数字+1,遇到*-1,假设当前值<1则把这个*和最后 ...
- zoj 3829 Known Notation(2014在牡丹江区域赛k称号)
Known Notation Time Limit: 2 Seconds Memory Limit: 131072 KB Do you know reverse Polish notatio ...
- ZOJ 3829 Known Notation 贪心 难度:0
Known Notation Time Limit: 2 Seconds Memory Limit: 65536 KB Do you know reverse Polish notation ...
- 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 ...
- ZOJ - 3829 Known Notation(模拟+贪心)
http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3829 给定一个字符串(只包含数字和星号)可以在字符串的任意位置添加一个数字 ...
- ZOJ 3829 Known Notation(字符串处理 数学 牡丹江现场赛)
题目链接:problemId=5383">http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=5383 Do you ...
随机推荐
- PS Studio调用.exe输出错误信息的解决办法
在一个button_click下调用了如下外部可执行文件: $button1_Click = { #TODO: Place custom script here .\PsExec.exe \\192. ...
- 修改hosts文件(判断是否为管理员/以管理员权限运行脚本)
将以下命令保存为 HostsModify.ps1,然后执行即可 #该脚本用来添加hosts解析记录.脚本在执行的时候会判断当前用户是否为管理员,如果不是则弹出提示框口,要求输入相应密码 If (-NO ...
- Tomcat 生产服务器性能优化
虑一下这种场景,你开发了一个应用,它有十分优秀的布局设计,最新的特性以及其它的优秀特点.但是在性能这方面欠缺,不管这个应用如何都会遭到客户拒绝.客户总是期望它们的应用应该有更好的性能.如果你在产品中使 ...
- ios开发——实用技术OC-Swift篇&触摸与手势识别
iOS开发学习之触摸事件和手势识别 iOS的输入事件 触摸事件 手势识别 手机摇晃 一.iOS的输入事件 触摸事件(滑动.点击) 运动事件(摇一摇.手机倾斜.行走),不需要人为参与的 远程控制 ...
- 《Linux内核修炼之道》 之 高效学习Linux内核
http://blog.csdn.net/fudan_abc/article/details/5738436
- 王帅:深入PHP内核
[问底]王帅:深入PHP内核(三)——内核利器哈希表与哈希碰撞攻击 [问底]王帅:深入PHP内核(二)——SAPI探究 [问底]王帅:深入PHP内核(一)——弱类型变量原理探究
- [JavaScript]JS对select动态options操作[IE&FireFox兼容]
<select id="ddlResourceType" onchange="getvalue(this)"></select> ...
- 文件I/O(不带缓冲)之write函数
调用write函数向打开的文件写数据. #include <unistd.h> ssize_t write( int filedes, const void *buf, size_t nb ...
- qt helper
qt帮助文档(中文版) http://www.kuqin.com/qtdocument/index.html qt基础 http://www.devbean.net/2012/08/qt-study- ...
- 第一章 00 StringUtil.cpp和StringUtil.hh分析
/* * StringUtil.hh * * Copyright 2002, Log4cpp Project. All rights reserved. * * See the COPYING fil ...