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.


题意:  Robert须要剪指甲。可是他的指甲刀有缺陷,有些是剪不到的,

他的指甲刀形如是一个字符串。符号'.'代表指甲刀这处有缺陷这处的指甲不能修剪到,

符号'*'代表这处是完善的,这处的能够修剪到。如指甲刀**..**,要剪长度为6的指甲,

则剪出来的指甲(1代表该处指甲已修剪。0则没有)是110011,这须要再剪一次;

指甲刀能够左右移动,还能够翻转;

题解:一直从最左端有指甲的位置開始 剪,指甲钳分正反两种状态剪指甲,bfs求出最小步数。

-1的情况全是‘.’。

能够先处理出两把指甲钳,正反各一把,用一个数表示。指甲原始状态能够用(1<<L)-1表示,即所有

都是1,然后进行位运算,把剩下的状态求出来。知直到0.

#include<cstring>
#include<algorithm>
#include<iostream>
#include<cstdio>
#include<cmath>
#include<queue> using namespace std; int n,L,ans;
char s[14];
int Jidong,_Jidong; struct node
{
int nail;
int step;
};
queue<node>que;
bool vis[(1<<20)+50]; void debug(int x)
{
while(x)
{
printf("%d",x&1 );
x>>=1;
}
printf("\n");
}
void bfs()
{
while(que.size())que.pop();
memset(vis,false,sizeof vis);
node it;
it.nail=L;
it.step=0;
que.push(it);
vis[L]=true;
while(que.size())
{
it=que.front();
if(it.nail==0)
{
ans=it.step;
return;
}
que.pop();
while((it.nail&1)==0)
{
it.nail>>=1;
}
int nit=it.nail,_nit=it.nail;
for(int i=0;i<=20;i++)
{
if((nit&(1<<i))&&(Jidong&(1<<i)))
nit^=(1<<i);
if((_nit&(1<<i))&&(_Jidong&(1<<i)))
_nit^=(1<<i);
}
node _it;
_it.nail=nit;
_it.step=it.step+1;
if(!vis[nit])
{
vis[nit]=true;
que.push(_it);
}
_it.nail=_nit;
if(!vis[_nit])
{
vis[_nit]=true;
que.push(_it);
}
}
} int main()
{
//freopen("test.in","r",stdin);
while(cin>>n)
{
scanf("%s",s);
cin>>L;
Jidong=0,_Jidong=0;
int length=strlen(s);
for(int i=0,j=length-1;i<length;i++,j--)
{
if(s[i]=='*')
{
Jidong|=(1<<i);
_Jidong|=(1<<j);
}
}
//debug(Jidong);
if(!Jidong)
{
printf("-1\n");
continue;
}
while((Jidong&1)==0)Jidong>>=1;
while((_Jidong&1)==0)_Jidong>>=1;
L=(1<<L)-1;
ans=-1;
bfs();
printf("%d\n",ans );
}
return 0;
}

最新文章

  1. Thinking in Unity3D
  2. ubuntu 系统下搭建Java的环境
  3. 自己搭建云存储(WIFI路由器上接硬盘)
  4. Linux的IO性能监控工具iostat详解
  5. Java 中文字符判断 中文标点符号判断
  6. pip是用国内镜像源
  7. C#-datagriview的表头高度的设置
  8. Kafka 协议实现中的内存优化
  9. selenium2使用记录
  10. 关联查询一张小表。对性能有影响吗(mysql)
  11. eclipse导入项目之后有感叹号
  12. ●SCOI2018 AFO
  13. c# pda
  14. IE浏览器兼容性调整总结技巧
  15. 创建 elasticsearch 用户
  16. 四:(之三)制作镜像和一些docker命令
  17. SET FMTONLY ON
  18. DQN(Deep Reiforcement Learning) 发展历程(一)
  19. bzoj2467生成树
  20. Ionic3与Angular4新特性

热门文章

  1. Java 8 (3) Stream 流 - 简介
  2. SpringBoot 2.x (1):手动创建项目与自动创建项目
  3. [ NOI 2001 ] 食物链
  4. python自动化--接口请求及封装
  5. ubantu MongoDB安装
  6. Qt 5.8.3 部署/添加 Crypto++第三方库(5.6.5版本)
  7. 扩增子分析解读6进化树 Alpha Beta多样性
  8. 05Oracle Database 表空间查看,创建,修改及删除
  9. oracle文件 结构01
  10. 在TWaver的Tree节点上画线