Chapter III Array and string UVa1588 Kickdown

Source: Internet
Author: User

Title: The length of the given lengths is N1,n2 (n1,n2<=100) and the height of each column is only 1 or 2 of the strip. They need to be placed in a container with a height of 3, asking for the shortest container length that can hold them.

Analysis:

    1. There are two obvious ways to do this: first, fix a string, and move the other from left to right. Second, fixed a string A, another string B moved, and then fixed B, take the minimum length value.
    2. Both methods I have passed my own implementation, I think the second kind of code is more concise and elegant, but also has a certain degree of extensibility.

Use examples to illustrate it ~

Arrays 1 and 2 are the two arrays we are going to compare, (of course, arrays of strings), and we'll fix any one of them, put them below, and let the other move up and down in the pan left, to compare

When the first comparison occurs, the example

At this point, starting from the head element of array 1 and the head element of the group 2 is compared, when the occurrence of two 2, or the matching portion is completed, this comparison is completed, with a state variable to mark the results of this comparison.

As the legend is aware, this comparison stops at the element labeled 1.

The above array is then shifted right, which is equivalent to moving the array to the left and making a second comparison.

By the legend, this comparison is successful, with two array of total length n1+n2 minus the length of the matching part, that is, the required length len, if Len is less than the minimum length of the original record, the minimum length will be updated.

Keep repeating the process until this happens:

So we can complete half of the comparison task, then, the position of the array 1 and 2 is reversed, repeat the above process, take two times the minimum value, you can get the final result

Reverse and repeat the above process:

Understanding the idea, let's give a concise code implementation:

1#include <cstdio>2#include <cstring>3 Const intmaxn= -;4 Chara[maxn+1],b[maxn+1];5 intn1,n2;6 intMinConst int&i,Const int&j) {7     returnI<j?i:j;8 }9 intMinlen (Char(SS,Char*S2,int&n) {//N is the length of the S1Ten     intSumlen=n1+n2,minn=min (N1,N2), len=Sumlen; One      for(intI=0; i<n;i++){ A         intok=1, Fix=min (N-i,minn);//fix calculation is a difficult point -          for(intj=0; j<fix;j++)if(s1[i+j]=='2'&&s2[j]=='2'){ -ok=0; Break; the         } -         if(Ok&&len>sumlen-fix) len=sumlen-fix; -     } -     returnLen; + } - intMain () { +      while(SCANF ("%s%s", &a,&b) = =2){ AN1=strlen (a), N2=strlen (b);//The comma operator was inadvertently used atprintf"%d\n", Min (Minlen (a,b,n1), Minlen (B,A,N2)));//minimum value of two results with min function -     } -}

All right, here we go today, stick to the victory and the portal, everyone refueling Oh ~ ~ ~ ~ ~ (ω ') o

Topic Portal:https://uva.onlinejudge.org/index.php?option=com_onlinejudge&itemid=8&page=show_problem& problem=4463

Chapter III Array and string UVa1588 Kickdown

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.