UVa1588 Kickdown (换抵挡装置)

A research laboratory of a world-leading automobile company has received an order to create a special transmission mechanism, which allows for incredibly efficient kickdown | an operation of switching to lower gear. After several months of research engineers found that the most efficient solution requires special gears with teeth and cavities placed non-uniformly. They calculated the optimal flanks of the gears. Now they want to perform some experiments to prove their findings.The first phase of the experiment is done with planar toothed sections, not round-shaped gears. A section of length n consists of n units. The unit is either a cavity of height h or a tooth of height 2h. Two sections are required for the experiment: one to emulate master gear (with teeth at the bottom)
and one for the driven gear (with teeth at the top). There is a long stripe of width 3h in the laboratory and its length is enough for cutting two engaged sections together. The sections are irregular but they may still be put together if shifted along each
other. The stripe is made of an expensive alloy, so the engineers want to use as little of it as possible. You need to find the minimal length of the stripe which is enough for cutting both sections simultaneously.

Input
The input file contains several test cases, each of them as described below.There are two lines in the input, each contains a string to describe a section. The first line describes master section (teeth at the bottom) and the second line describes driven section (teeth at the top).Each character in a string represents one section unit | 1 for a cavity and 2 for a tooth. The sections can not be flipped or rotated.Each string is non-empty and its length does not exceed 100.

Output
For each test case, write to the output a line containing a single integer number | the minimal lengthof the stripe required to cut off given sections.


Sample Input
2112112112
2212112
12121212
21212121
2211221122
21212
Sample Output
10
8
15

解答:

#include<bits/stdc++.h>
using namespace std;
char aa[1120],bb[1120];
int la,lb,ans1,ans2,a[1210],b[1120];
bool finda(int x)
{
    for(int i=1;i<=lb;i++)
    {
        if(a[x+i-1]+b[i]>3) return 0;
        if(x+i-1>la) return 1;
    }
    return 1;
}
bool findb(int x)
{
    for(int i=1;i<=la;i++)
    {
        if(b[x+i-1]+a[i]>3) return 0;
        if(x+i-1>lb) return 1;
    }
    return 1;
}
int main()
{
    //freopen("1.txt","r",stdin);
    //freopen("2.txt","w",stdout);
    while(1)
    {
        if(!(cin>>aa[1])) return 0;
        memset(a,0,sizeof(a));
        memset(b,0,sizeof(b));
        ans1=ans2=990000;
        a[1]=aa[1]-'0';
        for(la=2;la;la++)
        {
            aa[la]=getchar();
            if(aa[la]=='\n')
            {
                la--;
                break;
            }
            a[la]=aa[la]-'0';
        }
        for(lb=1;lb;lb++)
        {
            bb[lb]=getchar();
            if(bb[lb]>'9'||bb[lb]<'0')
            {
                lb--;
                break;
            }
            b[lb]=bb[lb]-'0';
        }
        //if(la>lb)
        //{
            for(int s=1;s<=la;s++)
            {
                if(a[s]+b[1]<=3)
                {
                    if(finda(s)) 
                    {
                        ans1=max(la,lb+s-1);
                        break;
                    }
                }
            }
    //    }
        //else
        //{
            for(int s=1;s<=lb;s++)
            {
                if(b[s]+a[1]<=3)
                {
                    if(findb(s))
                    {
                        ans2=max(lb,la+s-1);
                        break;
                    }
                }
            }
        //}
        printf("%d\n",min(min(ans1,ans2),la+lb));
    }
 } 

猜你喜欢

转载自www.cnblogs.com/satans/p/11112935.html
今日推荐