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:前缀判断的条 ...
随机推荐
- win7、xp下Meclipse SVN用户名修改
Meclipse SVN用户名修改,在网上查找后发现如下方法: 1.查看你的Eclipse中使用的是什么SVNInterface windows>preference>Team>SV ...
- 在企业级开发中使用Try...Catch...会影响效率吗?
感谢神啊.上帝及老天爷让我失眠,才能够有了本篇文章. 记得不久之前,公司一同事曾经说过:“如果是Winform开发,由于程序是在本地,使用try...catch不会有太大性能问题,可是如果是在web服 ...
- Gprinter Android SDK V2.0 使用说明
佳博特约经销商,此店购买的打印机问题优先解决哟 https://shop107172033.taobao.com/index.htm?spm=2013.1.w5002-9520741823.2.V1p ...
- magic_quotes_gpc、mysql_real_escape_string、addslashes的区别及用法
本篇文章,主要先重点说明magic_quotes_gpc.mysql_real_escape_string.addslashes 三个函数方法的含义.用法,并举例说明.然后阐述下三者间的区别.关系.一 ...
- Java学习笔记——动态代理
所谓动态,也就是说这个东西是可变的,或者说不是一生下来就有的.提到动态就不得不说静态,静态代理,个人觉得是指一个代理在程序中是事先写好的,不能变的,就像上一篇"Java学习笔记——RMI&q ...
- 与 空格的区别
nbsp 是 Non-Breaking SPace的缩写,即“不被折断的空格”,当两个单词使用 连接时,这两个单词就不会被分隔为2行,如下面 <div id="div1" ...
- sql2000
sql 2000简体中文企业版下载(含SP3 SP4 下载地址)安装图解及sp4安装教程图解 2012-07-17 16:24:37| 分类: mssql数据库|字号 订阅 sql 2000 ...
- JSP HTML区别
1.最简单的区别就是,HTML能直接打开,jsp只能发布到Tomact等服务器上才能打开2.定义上HTML页面是静态页面可以直接运行,JSP页面是动态页它运行时需要转换成servlet.3.他们的表头 ...
- 简述安装android开发环境
1, 安装java sdk2, 安装andoroid sdk3, 在环境变量里配置java和andoroid sdk的路径4, 部署eclipse,并在eclipse上安装ADT 补充: 步骤3之后需 ...
- Plan-9效应:为什么东西不坏就不要去修它
http://www.aqee.net/the-plan-9-effect-or-why-you-should-not-fix-it-if-it-aint-broken/ Plan-9是一个很棒的.很 ...