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;
}

 

posted @ 2014-05-11 22:32  疯狂的癫子  阅读(201)  评论(0编辑  收藏  举报