1588 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
anks of the
gears. Now they want to perform some experiments to prove their ndings.
The rst 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 le 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 rst 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
ipped 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.
Universidad de Valladolid OJ: 1588 { Kickdown 2/2
Sample Input
2112112112
2212112
12121212
21212121
2211221122
21212
Sample Output
10
8
15
题意:
给出两个序列,每个序列仅包括‘1’和‘2’。每个序列代表一个零件上的齿口的分布情况:‘2’代表零件在此处是“突出”的,‘1’代表零件在此处是“凹陷”的。序列的长度就是零件的长度。现在将两个序列代表的零件进行对合,一零件的“突出”的位置只能与另一零件的“凹陷”位置进行对合,问对合后的零件组合体的最小长度是多少。
输入:
多组数据,每组两行,每行都是一串‘1’、‘2’序列。
输出:
每组数据输出零件组合体的最小长度。
分析:
简单模拟。其实就是模拟两个零件的对合过程。相对固定一个零件(零件1)的位置,然后将另一零件(零件2)的首位置对其该零件的首位置,查看是否能成功对合,不能的话将零件2移动一个位置,使其首位置对齐零件1的第二个位置并查看是否成功对合。不断重复这样的移动并匹配的操作直到成功对合并计算出组合零件的总长度(零件1+零件2-公共部分)。然后再相对固定零件2,重复上述操作又得到一个总长度,比较两个总长度的值即得到答案。
1 #include <cstdio> 2 #include <cstring> 3 #define LEN 100 4 char h1[LEN + 1],h2[LEN + 1]; 5 int main(){ 6 while(scanf("%s%s",h1,h2) != EOF){ 7 int i = 0,j = 0,t = 0,m,n; 8 int temp,cnt1,cnt2; 9 m = strlen(h1); 10 n = strlen(h2); 11 while(j < m && i < n) 12 if(h1[j] + h2[i] - '0' * 2 <= 3) i++,j++; 13 else t++,i = 0,j = t; 14 cnt1 = m + n - i; 15 i = j = t = 0; 16 while(j < n && i < m) 17 if(h2[j] + h1[i] - '0' * 2 <= 3) i++,j++; 18 else t++,i = 0,j = t; 19 cnt2 = m + n - i; 20 printf("%d ",cnt1 < cnt2 ? cnt1 : cnt2 ); 21 } 22 return 0; 23 }