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.

输出剪指甲的最小步数
#include <stdio.h>
#include <math.h>
#include <string.h>
#include <algorithm>
using namespace std;

int n,m,right,left,minn;
char str[20];
int clp[20];

void dfs(int fig[],int s,int flag,int step)
{
    int i,j,k,tem[50];
    if(step>=minn) return ;
    if(flag)
    {
        for(i = 0; i<=25; 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(i == m+1)
        {
            if(step<minn)
            {
                minn = step;
            }
            return ;
        }
        dfs(tem,s,1,step+1);
        dfs(tem,s,0,step+1);
    }
    else
    {
        for(i = 0; i<=25; i++)
            tem[i] = fig[i];
        for(i = s,j = right; i<=m && j>=1; i++,j--)
        {
            if(!tem[i])
                tem[i] = clp[j];
        }
        for(i = s; i<=m; i++)
        {
            if(!tem[i])
            {
                s = i;
                break;
            }
        }
        if(i == m+1)
        {
            if(step<minn)
            {
                minn = step;
            }
            return ;
        }
        dfs(tem,s,0,step+1);
        dfs(tem,s,1,step+1);
    }
}

int main()
{
    int i,j,k,len,fig[50];
    while(~scanf("%d",&n))
    {
        scanf("%s",str);
        memset(clp,0,sizeof(clp));
        for(i = 0; i<n; i++)
        {
            if(str[i] == ‘*‘)
                clp[i+1] = 1;
        }
        left = 0;
        for(i = 1; i<=n; i++)
            if(clp[i])
            {
                left = i;
                break;
            }
        for(i = n; i>=1; i--)
            if(clp[i])
            {
                right = i;
                break;
            }
        scanf("%d",&m);
        if(left==0)
        {
            printf("-1\n");
            continue;
        }
        minn = 999999999;
        memset(fig,0,sizeof(fig));
        dfs(fig,1,1,1);
        dfs(fig,1,0,1);
        printf("%d\n",minn);
    }

    return 0;
}

ZOJ3675:Trim the Nails,布布扣,bubuko.com

时间: 2024-12-10 18:26:32

ZOJ3675:Trim the Nails的相关文章

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

原题: ZOJ 3675 http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3675 由m<=20可知,可用一个二进制数表示指甲的状态,最多2^20,初始状态为0,表示指甲都没剪,然后BFS找解,每次枚举剪刀的两个方向,枚举移动的位数进行扩展状态即可. 代码: #include <iostream> #include <cstdio> #include <cstring> #include &

ZOJ 3675 Trim the Nails(bfs)

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('.'

ZOJ Monthly, November 2012

A.ZOJ 3666 Alice and Bob 组合博弈,SG函数应用 #include<vector> #include<cstdio> #include<cstring> #include<algorithm> using namespace std; const int maxn = 10000 + 100; int SG[maxn]; vector<int> g[maxn]; int mex(int u) { //minimal exc

Mybatis中运用小技巧 trim标签的使用

作者:death05的博客推荐:路在脚下trim元素的主要功能是可以在自己包含的内容钱加上某些前缀,也可以在其后加上某写后缀,与之对应的属性是prefix和suffix: 可以把包含内容的首部某些内容覆盖,即忽略,也可以把尾部的某些内容覆盖,对应的属性是prefixOverrides和suffixOverrides.以下举例: 1.代码为: select * from user <trim prefix="WHERE" prefixoverride="AND |OR&q

swift -- 定义空字符串 hasPrefix hasSuffix trim split join range

// 定义空的字符串 var str1 = "" var str2 = String() str1.isEmpty      // 判断字符串是否为空 // 输出字符串中所有的字符 var str3 = "As god name" for c in str3{ println(c) } Int.max   // Int类型的最大值 Int.min   // Int类型的最小值 var arr1 = ["c", "oc", &q

练习使用Trim()函数规范名字输入

Java中的Trim()函数可以去除字符串的空白前缀和空白后缀,可用来规范用户输入的内容,具体这样用: String s="   Hello world  ".trim(); 然后s就是"Hello world". 处理名字输入小练: import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; public class testTrim {

My Batis mapper.xml中 动态SQL中使用trim标签 if end的场景

trim标签有点类似于replace效果. trim 属性 prefix:前缀覆盖并增加其内容 suffix:后缀覆盖并增加其内容 prefixOverrides:前缀判断的条件 suffixOverrides:后缀判断的条件 <!-- 修改 --> <update id="updateTest" >        UPDATE test         <trim prefix="SET" suffixOverrides="

mybatis &lt;where&gt;、&lt;set&gt;、&lt;trim&gt;、&lt;sql&gt;、&lt;foreach&gt;标签的使用

mybatis where标签的使用 where后面跟查询条件 简化sql语句中判断条件的书写 例: <select id="user" parameterType="user" resultType="User"> select * from user <where> <if test="id!=null and id!=''"> id=#{id} </if> <if t

什么是SSD TRIM (by quqi99)

作者:张华  发表于:2016-03-23 版权声明:可以任意转载,转载时请务必以超链接形式标明文章原始出处和作者信息及本版权声明 ( http://blog.csdn.net/quqi99 ) 普通硬盘并不真正从硬盘里删除数据,只是在操作系统标记为删除,下次直接覆盖写:但SSD物理覆盖写却是很慢的(对于SSD,可以以4KB大小的页(128个page组成一个block)来读写数据,但却只能以512KB(128 page)大小来删除.要覆盖写一个4KB page的话,先要把512KB block全