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的更多相关文章

  1. ZOJ 3675 Trim the Nails(bfs)

    Trim the Nails Time Limit: 2 Seconds      Memory Limit: 65536 KB Robert is clipping his fingernails. ...

  2. 2014 Super Training #6 G Trim the Nails --状态压缩+BFS

    原题: ZOJ 3675 http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3675 由m<=20可知,可用一个二进制数表 ...

  3. ZOJ3675:Trim the Nails

    Robert is clipping his fingernails. But the nail clipper is old and the edge of the nail clipper is ...

  4. ZOJ Monthly, November 2012

    A.ZOJ 3666 Alice and Bob 组合博弈,SG函数应用 #include<vector> #include<cstdio> #include<cstri ...

  5. [PHP源码阅读]trim、rtrim、ltrim函数

    trim系列函数是用于去除字符串中首尾的空格或其他字符.ltrim函数只去除掉字符串首部的字符,rtrim函数只去除字符串尾部的字符. 我在github有对PHP源码更详细的注解.感兴趣的可以围观一下 ...

  6. ZOJ People Counting

    第十三届浙江省大学生程序设计竞赛 I 题, 一道模拟题. ZOJ  3944http://www.icpc.moe/onlinejudge/showProblem.do?problemCode=394 ...

  7. php的empty(),trim(),strlen()方法

    如果empty()函数的参数是非空或非零的值,则empty()返回FALSE.换句话说,"".0."0".NULL.array().var$var:以及没有任何 ...

  8. ORACLE中的LTRIM、RTRIM和TRIM

    LTRIM.RTRIM和TRIM在ORACLE中的用法:1.LTRIM(C1,C2)其中C1和C2都可以字符串,例如C1是'Miss Liu',C2'MisL'等等.这是第一个和SQL SERVER不 ...

  9. mybatis : trim标签, “等于==”经验, CDATA标签 ,模糊查询CONCAT,LIKE

    一.My Batis trim标签有点类似于replace效果. trim 属性, prefix:前缀覆盖并增加其内容 suffix:后缀覆盖并增加其内容 prefixOverrides:前缀判断的条 ...

随机推荐

  1. R cannot be resolved to a variable

    1. 检查Android 的SDK是否丢失需要重新下载,检查build path,把需要导入的JAR包确认都导入成功 2. 确保class没有import Android.R,注意是不能有Androi ...

  2. Shell - 文件运算符

    文件运算符  文件运算符  描述 -b file  检测 file 是否为块设备文件 -c file  检测 file 是否为字符设备文件  -d file  检测 file 是否为目录 -e fil ...

  3. (转)VS2012网站发布详细步骤

    2.弹出网站发布设置面板,点击<新建..>,创建新的发布配置文件: 4. 在配置中,要选择“Release”——发布模式(Release   称为发布版本,它往往是进行了各种优化,使得程序 ...

  4. Adb connect监听指定的主机和端口/Adb监听Visual Studio Emulator for Android模拟器

    语法: adb connect <host>[:<port>] 使用实例: adb connect //如果连接成功则返回 connected to 说明 在使用Visual ...

  5. sql - 以半月,每月 分组

    按半月:完整代码: SELECT siteNumber [站点], CONVERT(VARCHAR(7),day,120)+'-'+ case when day(day) between 1 and ...

  6. redhat6.4 配置centos6 yum替换

    1.卸载掉系统redhat自带的yum   rpm -qa |grep yum |xargs rpm -e --nodeps 2 下载相关的centos yum插件   主要有python-inipa ...

  7. T-SQL语言基础

    1.T-SQL语言 CREATE:创建新对象,包括数据库.表.视图.过程.触发器和函数等常见数据库对象. ALTER:修改已有对象的结构. DROP:用来删除已有的对象.有些对象是无法删除的,因为它们 ...

  8. Python自动化运维之29、Bottle框架

    Bottle 官网:http://bottlepy.org/docs/dev/index.html Bottle是一个快速.简洁.轻量级的基于WSIG的微型Web框架,此框架只由一个 .py 文件,除 ...

  9. const关键字与指针

    const与指针在一起的几种情况. const int *p1; //表示p1本身不是const,指向的变量是const. const *int p2; //语法错误 int const *p3; / ...

  10. 关于Look and Say序列的感想

    今天无意间翻到了<PHP经典实例>中字符串章节中关于Look and Say序列的那个程序: <?php function lookandsay($s) { //将保存返回值的变量初 ...