ZOJ3675: Trim the Nails

Source: Internet
Author: User

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 isNMillimeters wide. And we useNCharacters ('. '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 isMMillimeters wide. How many times at least shocould Robert cut his fingernail?

Input

There will be multiple test cases (about 15). Please process to the end of input.

First line contains one integerN. (1 ≤N≤ 10)

Second line containsNCharacters only consists of '.' and '*'.

Third line contains one integerM. (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****..**46*..***7
Sample Output
12
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. minimum step for output nail scissors
#include 
  
   #include 
   
    #include 
    
     #include 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
     
      =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
      
       =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;}
      
     
    
   
  


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.