ZOJ 3675 Trim the Nails quiz

Source: Internet
Author: User

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 shoshould 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.

A pair of scissors has an open mouth. It means an open mouth * Indicates a usable place. using scissors to cut your nails is equal. How many times do I need to cut all the nails?

Input n indicates the scissors length. Input n characters. Input m indicates the nail length.
During the competition, I only gave a glance and did not think carefully. I directly engaged in other questions with a high question rate. I wasted white points. The main reason is that I was too slow.
Be confident, be quick, and have a problem. Have an idea. Don't be afraid of it. Just give up on other questions and make time.

In addition, there are several detailed errors in the question. These errors should be avoided in the shooting code, but they are always not rigorous and careful, so we need to strengthen them specially.

We also need to strengthen the code capability and discover the importance of the Code capability.


[Cpp]
# Include <stdio. h>
# Include <string. h>
Int a [100];
 
Int n, m, min, left, right;
Void DFS (int B [], int s, int flag, int step)
{
Int I, j, c [100];
If (step> = min) return;
If (flag)
{
For (I = 1; I <= 25; I ++)
C [I] = B [I];
For (I = s, j = left; I <= m & j <= n; I ++, j ++)
If (c [I] = 0)
C [I] = a [j];
For (I = s; I <= m; I ++)
If (c [I] = 0)
{
S = I; break;
}
If (I = m + 1)
{
If (step <min) min = step;
Return;
}
DFS (c, s, 1, step + 1 );
DFS (c, s, 0, step + 1 );
}
Else
{
For (I = 1; I <= 25; I ++)
C [I] = B [I];
For (I = s, j = right; I <= m & j> 0; I ++, j --)
If (c [I] = 0)
C [I] = a [j];
For (I = s; I <= m; I ++)
If (c [I] = 0)
{
S = I; break;
}
If (I = m + 1)
{
If (step <min) min = step;
Return;
}
DFS (c, s, 0, step + 1 );
DFS (c, s, 1, step + 1 );
}
}
Int main ()
{
Int I, B [100];
Char ch;
While (scanf ("% d", & n )! = EOF)
{
Getchar (); // getchar is required here; otherwise, the following character input will be affected.
For (I = 1; I <= n; I ++)
{
Scanf ("% c", & ch );
If (ch = '*') a [I] = 1;
Else a [I] = 0;
}
Left = 0;
For (I = 1; I <= n; I ++)
If (a [I] = 1)
{
Left = I;
Break;
}
For (I = n; I> 0; I --)
If (a [I] = 1)
{
Right = I;
Break;
}
// For (I = 1; I <= n; I ++) printf ("% d", a [I]);
Scanf ("% d", & m );
// Printf ("m = % d \ n", m );
If (n = 0) {printf ("0 \ n"); continue ;}
If (left = 0) {printf ("-1 \ n"); continue ;}
Memset (B, 0, sizeof (B ));
Min = 999999999;
DFS (B, 1, 1 );
DFS (B, 1, 0, 1 );
Printf ("% d \ n", min );
}
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.