Kickdown UVa 1588

Description

Download as PDF

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

\epsfbox{p3712a.eps}

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.

\epsfbox{p3712b.eps}

The stripe is made of an expensive alloy, so the engineers want to use as little of it as possible. You need to ï¬nd 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 length of the stripe required to cut off given sections.

Sample Input 

2112112112 
2212112 
12121212 
21212121 
2211221122 
21212

Sample Output 

10 
8 
15

题意:给出两个长度为n1,n2(n1,n2<=100)且每列高度只为1或2的长条。需要将它们放入一个高度为3的容器,求能够容纳它们的最短容器长度。

思路:先将n1固定不动,n2从第一位与n1比较是否两个相加大于四,是的话就将n2向右移动一位,再从头比较,直到没有任何一列相加大于四就记录下这种情况的最短长度;

然后将n2固定不动,执行一样的操作,求出另一个最段长度,最后取最小的。一次AC真开森

代码:

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <string>
using namespace std;
char s1[110],s2[110];
int n1[210],n2[210];
int main()
{
    int i,j;
    while (~scanf("%s%s",s1,s2))
    {
        memset(n1,0,sizeof(n1));
        memset(n2,0,sizeof(n2));
        int len1=strlen(s1);
        int len2=strlen(s2);
        for (i=0;i<len1;i++)  n1[i]=s1[i]-'0';
        for (i=0;i<len2;i++)  n2[i]=s2[i]-'0';
        int minlen=max(len1,len2);
        for (i=0;i<len1;i++)
        {
            for (j=0;j<len2;j++)
            {
                if (n1[i+j]+n2[j]>=4)
                    break;
            }
            if (j==len2)
                break;
        }
        int le1=len2+i;
        for (i=0;i<len2;i++)
        {
            for (j=0;j<len1;j++)
            {
                if (n2[i+j]+n1[j]>=4)
                    break;
            }
            if (j==len1)
                break;
        }
        int le2=i+len1;
        minlen=max(minlen,min(le1,le2));
        printf("%d\n",minlen);
    }
    return 0;
}

转载于:https://www.cnblogs.com/i8888/p/4044021.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值