紫书:Kickdown

日常做题

Kickdown

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 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 length of the stripe required to cut off given sections.

Samples
2112112112
2212112
12121212
21212121
2211221122
21212

10
8
15

题目大意
给你两个字符串(由1和2组成)
这两个字符串分别描述的是两个零件的形状
其中1和2分别表示该零件在该位置的高度

现在要求把两个字符串叠在一起,但是每一个单位的高度不能超过3,求两个零件叠在一起之后的最小总长度!

情况分析
为了思路更加清晰:我将两个零件叠在一起的情况分了3类
1.完全匹配成功在这里插入图片描述
2.部分匹配成功
在这里插入图片描述
3.完全不能匹配
在这里插入图片描述
这样将情况搞明白有助于思路不乱!!

我们先假设长块零件在下面:
1.如果完全匹配成功:则答案长度不就是长条零件的长度吗
2.如果完全不匹配成功,答案长度不就是两个零件加起来的长度吗
3.如果不完全匹配:
假如两个零件相交与长条零件上长度为i的地方:
那么答案长度不就是 (i + 短零件长度 )

值得注意的问题
我们之前的所有猜想都是在长零件在下面的基础上,不完全匹配
会在长零件的右边多出来,但是如果有没有可能短零件会慢慢的左移,最后从长零件左边多出来呢
如图:在这里插入图片描述
此时我们发现,我们还要计算一次长字符串从右边端点开始遍历查找的情况,这样显得有些麻烦!不如写一个函数。
分别以1串,2串,1串为主串,在下面,然后遍历主串(从左到右进行查找)然后输出两次,分别以短(长)串为主串,查找一次,这样就不会忽略这里说的这种情况!
//主串不动,副串逐渐向右移动查找…

代码

#include<bits/stdc++.h>
using namespace std;
char a[100001];
char b[100001];
int judge(char a[],char b[])
{
	int w1=strlen(a);
	int w2=strlen(b);
	for(int i=0;i<w1;i++)
	{
		int flag=1;
		for(int j=i;j<w1&&j<i+w2;j++)
		{
			if(a[j]=='2'&&b[j-i]=='2')
			{
				flag=0;
			}
		}
		if(flag==1)
		return max(w1,i+w2);
	}
	return w1+w2;
}
int main()
{
	while(cin>>a)
	{
		cin>>b;
		int ans=min(judge(a,b),judge(b,a));
		cout<<ans<<endl;
	}
	return 0;
}

细节点
1.之前在分类讨论时,说过如果完全匹配,答案直接就是长串的长度,但是这里我们只规定了主串和副串,所以这里用一个
max(w1,i+w2)可以概括匹配的所有情况
2.只有两个位置长度同时为2则为不匹配。

反思
一开始没什么思路
胡乱写写,最后把自己写乱了,
字符串一直是我的弱项,每次都会把边界弄乱
一言难尽
这个题解也时看别人博客才总结出来的皮毛

原博客
这个题并不难,只是觉得看了大佬的博客之后,感觉大佬的代码很美观,很简洁就把问题解决了,要是我自己写即便写出来了也特别乱

  • 3
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值