ZOJ 3675 Trim the Nails
http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=4918
DP+状态压缩。
http://www.cnblogs.com/dgsrz/articles/2791363.html
首先把 (1<<m)-1 作为指甲没剪时的初态(全是1),dp[x]保存的就是对于每个状态,需要剪的最少次数。
当前状态x以二进制表示时,出现的1表示这一位置还留有指甲,0就是已剪去。而对于指甲钳,又可以将其以二进制表示,对于案例:****..**,不妨用11110011代替,1表示当前位置刀锋完好,0相反。于是对每一位分析:
| 原来指甲情况 A | 指甲钳刀锋 B | 最终指甲情况 Y |
| 0 | 0 | 0 |
| 0 | 1 | 0 |
| 1 | 0 | 1 |
| 1 | 1 | 0 |
化简一下得到每一位上的关系:Y = A & ~B
所以递推方程就是:dp[x & (~B)] = min(dp[x & (~B)], dp[x] + 1);
问题是,指甲钳并不总是和指甲最左端对齐,所以还需要对指甲钳进行移动。反应在上式,就是对B进行左右各m次的移位操作。另外,指甲钳可以反着用,于是B还需要分正反情况考虑。
Trim the Nails
Time Limit: 2 Seconds Memory Limit: 65536 KB
Robert is clipping his fingernails. But the nail clipper is old and the edge of the nail clipper is potholed.
The nail clipper's edge is N millimeters wide. And we use N characters('.' or '*') to represent the potholed nail clipper. '.' represents 1 bad millimeter edge, and '*' represents 1 good millimeter edge.(eg. "*****" is a 5 millimeters nail clipper with the whole edge good. "***..." is a 6 millimeters nail clipper with half of its edge good and half of its edge bad.)
Notice Robert can turn over the clipper. Turning over a "**...*"-nail clipper will make a "*...**"-nail clipper.
One-millimeter good edge will cut down Robert's one-millimeter fingernail. But bad one will not. It will keep the one-millimeter unclipped.
Robert's fingernail is M millimeters wide. How many times at least should Robert cut his fingernail?
Input
There will be multiple test cases(about 15). Please process to the end of input.
First line contains one integer N.(1≤N≤10)
Second line contains N characters only consists of '.' and '*'.
Third line contains one integer M.(1≤M≤20)
Output
One line for each case containing only one integer which is the least number of cuts. If Robert cannot clipper his fingernail then output -1.
Sample Input
8
****..**
4
6
*..***
7
Sample Output
1
2
Hint
We use '-' to present the fingernail.
For sample 1:
fingernail: ----
nail clipper: ****..**
Requires one cut. For sample 2:
fingernail: -------
nail clipper: *..***
nail clipper turned over: ***..*
Requires two cuts.
#include<stdio.h>
#include<string.h>
#define inf 999999
int dp[1<<20];
int min(int x,int y)
{
if(x>y)
return y;
else
return x;
}
int main()
{
int i,n,bin,rbin,m,j;
char str[100];
while(~scanf("%d",&n))
{
memset(dp,inf,sizeof(dp));
bin=rbin=0;
getchar();
scanf("%s%d",str,&m);
for(i=0;i<n;i++)
{
bin=bin<<1;
if(str[i]=='*')
bin=bin|1;
}
for(i=n-1;str[i]!=0;i--)
{
rbin=rbin<<1;
if(str[i]=='*')
rbin=rbin|1;
}
dp[(1 << m) - 1] = 0;
for (i = (1 << m) - 1; i >= 0; i--)//后面更新。
{
for (j = 0; j < m; j++)
{
dp[i & (~(bin << j))] = min(dp[i & (~(bin << j))], dp[i] + 1);
dp[i & (~(rbin << j))] = min(dp[i & (~(rbin << j))], dp[i] + 1);
dp[i & (~(bin >> j))] = min(dp[i & (~(bin >> j))], dp[i] + 1);
dp[i & (~(rbin >> j))] = min(dp[i & (~(rbin >> j))], dp[i] + 1);
}
}
if(dp[0]<inf)
printf("%d\n",dp[0]);
else
printf("-1\n");
}
return 0;
}
ZOJ 3675 Trim the Nails的更多相关文章
- ZOJ 3675 Trim the Nails(bfs)
Trim the Nails Time Limit: 2 Seconds Memory Limit: 65536 KB Robert is clipping his fingernails. ...
- 2014 Super Training #6 G Trim the Nails --状态压缩+BFS
原题: ZOJ 3675 http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3675 由m<=20可知,可用一个二进制数表 ...
- ZOJ3675:Trim the Nails
Robert is clipping his fingernails. But the nail clipper is old and the edge of the nail clipper is ...
- ZOJ Monthly, November 2012
A.ZOJ 3666 Alice and Bob 组合博弈,SG函数应用 #include<vector> #include<cstdio> #include<cstri ...
- [PHP源码阅读]trim、rtrim、ltrim函数
trim系列函数是用于去除字符串中首尾的空格或其他字符.ltrim函数只去除掉字符串首部的字符,rtrim函数只去除字符串尾部的字符. 我在github有对PHP源码更详细的注解.感兴趣的可以围观一下 ...
- ZOJ People Counting
第十三届浙江省大学生程序设计竞赛 I 题, 一道模拟题. ZOJ 3944http://www.icpc.moe/onlinejudge/showProblem.do?problemCode=394 ...
- php的empty(),trim(),strlen()方法
如果empty()函数的参数是非空或非零的值,则empty()返回FALSE.换句话说,"".0."0".NULL.array().var$var:以及没有任何 ...
- ORACLE中的LTRIM、RTRIM和TRIM
LTRIM.RTRIM和TRIM在ORACLE中的用法:1.LTRIM(C1,C2)其中C1和C2都可以字符串,例如C1是'Miss Liu',C2'MisL'等等.这是第一个和SQL SERVER不 ...
- mybatis : trim标签, “等于==”经验, CDATA标签 ,模糊查询CONCAT,LIKE
一.My Batis trim标签有点类似于replace效果. trim 属性, prefix:前缀覆盖并增加其内容 suffix:后缀覆盖并增加其内容 prefixOverrides:前缀判断的条 ...
随机推荐
- 如何使用node中的buffer
介绍:Buffer类是一个全局类,是一个比较罕见不需要require( ‘buffer’ )就可以使用的类,Buffer类似与数组也有length, 它里面的元素为16进制的两位数,即 0-255的数 ...
- inner join
select Person.LastName,Person.FirstName,Orders.OrderNo from Persons INNER JOIN Orders ON Person.Id_P ...
- 转 sqlserver字段描述相关操作sql
可以自己查询系统表: SELECT o.name AS tableName, c.name AS columnName, p.[value] AS Description FROM sysproper ...
- HTML5 WebAudioAPI-实例(二)
简单播放实例1: var url='../content/audio/海阔天空.mp3'; if (!window.AudioContext) { alert('您的浏览器不支持AudioContex ...
- php100视频原始地址列表整理:
php100视频原始地址列表整理: 教程名称 . 1:环境配置与代码调试 2:PHP的数据类型与源码调试 3:常用PHP运算类型介绍与应用 4: PHP条件语句介绍与应用 5:PHP循环语句的介绍与应 ...
- Ubuntu系统配置日志/var/log/message
ubuntu系统默认不生成/var/log/messages文件,有时候想查看相关日志就很不方便,于是我们可以设置使系统生成此文件. 1.先安装 apt-get install rsyslog2.用v ...
- 2014年10月16号--for语句实例
Console.WriteLine("一对小兔一个月之后长成大兔,再过一个月后生新的一对兔子,且两年之后有多少对兔子,就是三兔子幼兔,小兔,成兔"); Console.WriteL ...
- gulp安装
1. npm install gulp -g 全局安装 npm install gulp --save-dev 安装文件内,纪录于package.json 接著安装插件,完成下列任务 ...
- wordpress version
version info /readme.html /wp-includes/version.php remove copyright 1.wp-login.php //<h1><a ...
- 给id赋值
var div = document.getElementByTagName('div') div.id="mydiv";div.setAttribute("id&quo ...