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 ...
随机推荐
- SQL 查询横表变竖表
SQL 查询横表变竖表 /*普通行列转换 假设有张学生成绩表(tb)如下:Name Subject Result张三 语文 74张三 数学 83张三 物理 93李四 语文 74李四 数学 84李四 ...
- paip.提升性能---- 网站并发数的总结.txt
paip.提升性能---- 网站并发数的总结.txt 作者Attilax , EMAIL:1466519819@qq.com 来源:attilax的专栏 地址:http://blog.csdn.n ...
- android125 zhihuibeijing 缓存
## 三级缓存 ## - 内存缓存, 优先加载, 速度最快 - 本地缓存(内存卡), 次优先加载, 速度快 - 网络缓存, 不优先加载, 速度慢,浪费流量 package com.itheima.zh ...
- win7如何共享文件 图文教你设置win7文件共享
点评:win7文件共享已成为网友们之间的热议,接下来为大家分享下如何共享文件,首先开启guest账户( 开始菜单 → 运行 → services.msc → 双击 server 服务项 ,设置启动类型 ...
- Java基本排序
长度为N的数组升序排序 一,冒泡排序 public class BubbleSort { private double[] data; public BubbleSort(double[] data) ...
- 【推理】UVa 10771 - Barbarian tribes
Barbarian tribes In a lost land two primitive tribes coexist: Gareds and Kekas. Every summer sols ...
- 求关注 wcf bindipendpointdelegate 端口限制的功能
我最近也需要实现一个功能:1)一个客户端(192.168.0.15),10个服务端(提供A接口.B接口)如下: 192.168.0.1-5685 192.168.0.2-5685 ...
- Maven初学之经验浅谈
关于Maven这个东西,我不做任何评价. 直接上资料,适合初学者 推荐资料 http://www.imooc.com/learn/443 www.imooc.com/article/15037 还有个 ...
- 关于IO的一些数字
http://static.googleusercontent.com/external_content/untrusted_dlcp/research.google.com/en/us/people ...
- ASP长文章分页的两个方法,函数
'按标识手动分页 function manualPage(str) pages=request.QueryString("page") contentstr=split(str,& ...