Description
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 �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
这个题因为一开始没注意细节的处理,导致提交7次WA。。。另外吐槽一下POJ的数据,太水了好吧,一个有BUG的程序竟然还能完美AC,醉了。。。= =
这个题的意思是给出两个高度只为1或2的不规则齿状长条 ,需要将他们啮合后装入高度为3的容器内,求出可装入容器的啮合后的最短长度。
一开始欠考虑了,觉得短的右端跟长的左端对齐,然后短的在长的上面一点点向右移动,出现各列小于等于3的情况在短条完全被长条包含和短条在长条两端这几种情况做一下比较得出的就是最短长度了(有点乱= =),但多次WA后想了想移动的时候有两个方向啊,然后自己出了几组测试数据,果然是错的,然后整改程序,两个方向分别进行比较,得出当前方向的两个数据后存下来,再跟两个长条的长度进行各种取大小值,最后就得出结果了。。。可是最后对数组的处理又出问题了(但是在POJ真的A了啊)。。= =后来有重理了一下思路,改了几个细节(数组清零),终于AC了。。T_T
#include <iostream>
#include <cstring>
using namespace std;
char str1[222];
char str2[222];
int main()
{
int i,j,len1,len2,x1,x2;
while(cin>>str1>>str2)
{
len1=strlen(str1);
len2=strlen(str2);
memset(str1+len1,0,sizeof(str1)-len1); //输入数据后,将空余的元素(空余的元素!!!)全变成0,方便计算
memset(str2+len2,0,sizeof(str2)-len2);
for(i=0;i<len1;i++) //将字符换算成数字
str1[i]-=48;
for(i=0;i<len2;i++)
str2[i]-=48;
for(i=0;i<len1;i++)
{
for(j=0;j<len2;j++)
if(str1[i+j]+str2[j]>3)
break;
if(j==len2) //内循环结束后直接跳出
break;
}
x1=len2+i; //经过一个方向的比较得到一个长度值
for(i=0;i<len2;i++)
{
for(j=0;j<len1;j++)
if(str2[i+j]+str1[j]>3)
break;
if(j==len1) //内循环结束后直接跳出
break;
}
x2=len1+i; //经过另一个方向的比较得到的长度值
cout<<max(max(len1,len2),min(x1,x2))<<endl; //这个复合取大小值的真心好用,简单粗暴
}
return 0;
}