您的位置:首页 > 其它

UVA - 1588 Kickdown

2015-03-18 00:49 375 查看
Kickdown

Time Limit: 3000MSMemory Limit: Unknown64bit IO Format: %lld & %llu
Submit Status

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


自己的代码:

想法是用短的串从开头语长串对齐开始,不能满足条件就要右移,最后的答案为 i+lenb。

要注意的是除了上面的情况还有从右端开始匹配的。于是两个串都转了一下,两个都要转!!自己就因为只转了一串wrong了好多次。。。

需要些空间想象力什么的。
#include<iostream>
#include<cstring>
#include<string>
#include<algorithm>
using namespace std;
int main()
{
	string aa;
	string bb;
	while (cin >> aa >> bb)
	{
		if (aa.length() < bb.length())
		{
			string t;
			t = aa;
			aa = bb; 
			bb = t;
		}

		int lena = aa.length();
		int lenb = bb.length();

		int a[200] = { 0 };
		int b[200] = { 0 };
		for (int i = 0; i < lena; i++)
		{
			if (aa[i] == '1') a[i] = 1;
			else if (aa[i] == '2') a[i] = 2;
		}
		for (int i = 0; i < lenb; i++)
		{
			if (bb[i] == '1') b[i] = 1;
			else if (bb[i] == '2') b[i] = 2;
		}

		int ans1;
		for (int ii = 0; ii <= lena; ii++)
		{
			bool ok = true;
			for (int j = 0; j < lenb; j++)
			{
				if (a[ii + j] + b[j]>3) ok = false;
			}
			if (ok)
			{
				ans1 = ii + lenb;
				if (ans1 < lena) ans1 = lena;
				break;
			}
		}
		
		
		int ad[200] = { 0 };              
		for (int i = 0; i < lena; i++)    
			ad[i] = a[lena - i - 1];      
		int bd[200] = { 0 };
		for (int i = 0; i < lenb; i++)
			bd[i] = b[lenb - i - 1];
	
		int ans2;
		for (int ii = 0; ii <= lena; ii++)
		{
			bool ok = true;
			for (int j = 0; j < lenb; j++)
			{
				if (ad[ii + j] + bd[j]>3) ok = false;
			}
			if (ok)
			{
				ans2 = ii + lenb;
				if (ans2 < lena) ans2 = lena;
				break;
			}
		}

		int ans = min(ans1, ans2);
		cout << ans << endl;
	}
}


再贴一下别人家的代码。

两个串相互匹配了下,代码简单。
#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;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: