ZOJ3675:Trim the Nails
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. 这个题目对于我来说还是太难了,现在对dfs仅仅是知道贴上大牛代码一份,留作复习研究
解法一:
#include <stdio.h>
#include <math.h>
#include <string.h>
#include <algorithm>
using namespace std;
int n,m,right,left,minn;
];
];
void dfs(int fig[],int s,int flag,int step)
{
];
if(step>=minn) return ;
if(flag)
{
; i<=; i++)
tem[i] = fig[i];
for(i = s,j = left; i<=m && j<=n; i++,j++)
{
if(!tem[i])
tem[i] = clp[j];
}
for(i = s; i<=m; i++)
{
if(!tem[i])
{
s = i;
break;
}
}
)
{
if(step<minn)
{
minn = step;
}
return ;
}
dfs(tem,s,,step+);
dfs(tem,s,,step+);
}
else
{
; i<=; i++)
tem[i] = fig[i];
; i++,j--)
{
if(!tem[i])
tem[i] = clp[j];
}
for(i = s; i<=m; i++)
{
if(!tem[i])
{
s = i;
break;
}
}
)
{
if(step<minn)
{
minn = step;
}
return ;
}
dfs(tem,s,,step+);
dfs(tem,s,,step+);
}
}
int main()
{
];
while(~scanf("%d",&n))
{
scanf("%s",str);
memset(clp,,sizeof(clp));
; i<n; i++)
{
if(str[i] == '*')
clp[i+] = ;
}
left = ;
; i<=n; i++)
if(clp[i])
{
left = i;
break;
}
; i--)
if(clp[i])
{
right = i;
break;
}
scanf("%d",&m);
)
{
printf("-1\n");
continue;
}
minn = ;
memset(fig,,sizeof(fig));
dfs(fig,,,);
dfs(fig,,,);
printf("%d\n",minn);
}
;
}
解法二:
首先把 (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还需要分正反情况考虑。
#include <cstdio>
#include <cstring>
#include <iostream>
using namespace std;
#define INF 0x3f3f3f3f
<< ];
],rev[];
int main() {
int n,m,left,right;
while(~scanf("%d%s%d",&n,s,&m))
{
;i<n;i++)
rev[i]=s[n-i-];
left=; right=;
;i<n;i++)
{
left=left<<; right=right<<;
if(s[i]=='*')
left|=;
if(rev[i]=='*')
right|=;
}
;i<<<m;i++)
dp[i]=INF;
dp[(<<m)-]=;
<<m)-;i>=;i--)
{
if(dp[i]==INF)// 由i 剪到 i&(~(left<<j)的状态 若 。。 说明这还没被剪过
continue;
;j<m;j++)
{
dp[i&(~(left<<j))]=min(dp[i&(~(left<<j))],dp[i]+);
dp[i&(~(left>>j))]=min(dp[i&(~(left>>j))],dp[i]+);
dp[i&(~(right<<j))]=min(dp[i&(~(right<<j))],dp[i]+);
dp[i&(~(right>>j))]=min(dp[i&(~(right>>j))],dp[i]+);
}
}
]<INF)
printf(]);
else printf("-1\n");
}
;
}
ZOJ3675:Trim the Nails的更多相关文章
- ZOJ 3675 Trim the Nails
http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=4918 DP+状态压缩. http://www.cnblogs.com/dgsr ...
- 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可知,可用一个二进制数表 ...
- 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源码更详细的注解.感兴趣的可以围观一下 ...
- 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:前缀判断的条 ...
- IE8下String的Trim()方法失效的解决方案
简洁方便 用jquery的trim()方法,$.trim(str)就可以了.
随机推荐
- uva 10692 Huge Mods 超大数取模
vjudge上题目链接:Huge Mods 附上截图: 题意不难理解,因为指数的范围太大,所以我就想是不是需要用求幂大法: AB % C = AB % phi(C) + phi(C) % C ( B ...
- C#_拆箱跟装箱
Net的类型分为两种,一种是值类型,另一种是引用类型.这两个类型的本质区别,值类型数据是分配在栈中,而引用类型数据分配在堆上.那么如果要把一个值类型数据放到堆上,就需要装箱操作:反之,把一个放在堆上的 ...
- 原!!tomcat7.0 配置数据库连接池 SQLServer2000
SQLServer2000所需的3个驱动jar包 msbase.jarmssqlserver.jarmsutil.jar 放入WEB-INF lib文件夹中 1.META-INF 创建一个contex ...
- JSON字符串如何转化成对象?
解析 1.定义:是指将符合 JSON 语法规则的字符串转换成对象的过程. 2.不同的编程语言都提供了解析 JSON 字符串的方法,在这里主要讲解 JavaScript 中的解析方法.主要有三种: 1) ...
- window下安装nodejs
下载nodejs 去https://nodejs.org/en/download/下载nodejs,有.mis(安装版)和.exe(二进制版) .mis(安装版) 一般下载这个就行,简单方便,自带np ...
- OpenCV图像处理中常用函数汇总(1)
//俗话说:好记性不如烂笔头 //用到opencv 中的函数时往往会一时记不起这个函数的具体参数怎么设置,故在此将常用函数做一汇总: Mat srcImage = imread("C:/Us ...
- 指针类型(C# 编程指南)
原文地址:https://msdn.microsoft.com/zh-cn/library/y31yhkeb.aspx 在不安全的上下文中,类型可以是指针类型.值类型或引用类型. 指针类型声明采用下列 ...
- Intellij IDEA中部署Tomcat报错“war exploded: Server is not connected. Deploy is not available”
最近将本机的Tomcat 7卸载重装了Tomcat 9后,在IDEA中运行Tomcat老是报错,一直未找到解决问题的办法. 只能讲Tomcat重新退回到版本7,才重新再IDEA中运行正常.
- 35 个免费创新的响应式 HTML5 模板
HTML5 和响应式都是 Web 开发领域中的热门技术,本文向你推荐 35 个免费的响应式 HTML5 模板,将两种技术完美结合. Mori responsive HTML5 Template Res ...
- [saiku] olap数据源管理
一.应用场景 系统初始化的时候 如果没有创建olap数据源需要先创建olap数据源 否则直接获取所有的数据源存放在全局变量datasources里面以便于后续步骤中获取plap-connections ...