[算法竞赛入门经典]Kickdown ACM/ICPC NEERC 2004,UVa1587

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.

Sample Input

2112112112

2212112

12121212

21212121

2211221122

21212

Sample Output

10

8

15

Code

复杂版,模拟算法。此题用模拟来算的话会非常麻烦,要考虑很多边界问题

#include <stdio.h>
#include <string.h>
#define MAX 1000
char top[MAX];
char temp[MAX];
char down[MAX];

int main()
{
    while(~scanf("%s %s",top,down)){
        int len1 = (int)strlen(top),len2 = (int)strlen(down);
        int min = len1 + len2,max = len1 + len2,
                ss = len1,xl = len2;
        if(len1>len2){
            strcpy(temp,down); strcpy(down,top); strcpy(top,temp); ss = len2; xl = len1;
        }
        int j = 0;
        int jmp = 0;
        while(j < xl){
            int ok = 1;
            for(int x = j,y = 0;x < xl && y < ss;x ++,y ++){
                if((top[y] - ‘0‘)+(down[x] - ‘0‘) > 3){ ok = 0; break;}
            }
            if(ok) { min = xl; jmp = 1; break; }
            ++ j;
        }
        if(jmp)
            printf("%d\n",min);
        else{
            j = ss - 1;
            while(j > 0){
                int sub = 0;
                for(int x = j,y = 0;x < ss && y < ss - j;x ++,y ++){
                    if((top[x] - ‘0‘) + (down[y] - ‘0‘) <= 3){
                        ++ sub;
                    }else{
                        sub = 0;
                        break;
                    }
                }
                int t = max - sub;
                if(t < min) min = t;
                -- j;
            }
            j = xl - ss + 1;
            while(j < xl){
                int sub = 0;
                for(int x = j,y = 0;x < xl && y < xl - j;x ++,y ++){
                    if((top[y] - ‘0‘) + (down[x] - ‘0‘) <= 3){
                        ++ sub;
                    }else { sub = 0; break; }
                }
                int t = max - sub;
                if(t < min) min = t;
                ++ j;
            }
            printf("%d\n",min);
        }
    }
    return 0;
}

原文地址:https://www.cnblogs.com/1Kasshole/p/9691030.html

时间: 2024-10-29 14:48:29

[算法竞赛入门经典]Kickdown ACM/ICPC NEERC 2004,UVa1587的相关文章

《算法竞赛入门经典(第二版)》pdf

下载地址:网盘下载 内容简介  · · · · · · <算法竞赛入门经典(第2版)>是一本算法竞赛的入门与提高教材,把C/C++语言.算法和解题有机地结合在一起,淡化理论,注重学习方法和实践技巧.全书内容分为12 章,包括程序设计入门.循环结构程序设计.数组和字符串.函数和递归.C++与STL入门.数据结构基础.暴力求解法.高效算法设计.动态规划初步.数学概念与方法.图论模型与算法.高级专题等内容,覆盖了算法竞赛入门和提高所需的主要知识点,并含有大量例题和习题.书中的代码规范.简洁.易懂,不

《算法竞赛入门经典第二版》 P35 习题2-4 子序列的和(subsequence)

/* <算法竞赛入门经典第二版> P35 习题2-4: 输入两个正整数 n < m < 10^6,输出 (1/n)^2 + 1/(n+1)^2 +……+ 1/m^2,保留5位小数. 输入包含多组数据,结束标志为 m=n=0. 有错欢迎指出^_^ */ #include<stdio.h> int main() { int m,n,i,j=1; while(scanf("%d%d",&m,&n) != EOF) { double sum

算法竞赛入门经典_4.3_递归

看代码 #include <stdio.h> int f(int n){ return n == 0?1:f(n-1)*n; } int main() { printf("%d\n", f(5)); return 0; } 上面f函数使用了递归,递归由两部分组成,一是递归头,二是递归体. 我们使用gcc调试工具 H:\编程书籍学习\算法竞赛入门经典2代码\算法入门经典第四章>b f 'b' 不是内部或外部命令,也不是可运行的程序 或批处理文件. H:\编程书籍学习\算

《算法竞赛入门经典》动态规划复习

codevs 4979 数塔 1 #define N 100 2 #include<iostream> 3 using namespace std; 4 #include<cstdio> 5 int a[N][N],b[N][N],n; 6 int main() 7 { 8 scanf("%d",&n); 9 for(int i=1;i<=n;++i) 10 for(int j=1;j<=i;++j) 11 { 12 scanf("

算法竞赛入门经典训练指南

最近在看算法竞赛入门经典训练指南这本书,书中不错的算法我将在博客中发布,和大家共同学习. 题目: 在你的王国里有一条n个头的恶龙,你希望雇一些骑士把它杀死(即砍掉所有头).村里有m个骑士可以雇佣,一个能力值为m的骑士可以砍掉一个直径不超过x的头,且需要支付x个金币.如何雇佣骑士才能砍掉恶龙的所有头,且需要支付的金币最少?注意,一个骑士只能砍一个头(且不能被雇佣两次). 输入格式: 输入包含多组数据.每组数据的第一行为正整数m和n(1<=m,n<=20 000):以下m行每行为一个整数,即恶龙每

算法竞赛入门经典-训练指南(10881-Piotr&#39;s Ants)

题目大意: 一根长度为L的木棍一堆蚂蚁爬,向左或向右,速度都为1,若两蚂蚁碰撞则同时转头(转身时间忽略不计),问T时间之后每只蚂蚁的位置: 输入:t,(t个样例),每个样例输入 L,T,n,接下来是n行每行两个数据,一个POS(位置),一个dir(方向): 输出:按输入顺序输出每只蚂蚁的最终位置,若处于碰撞状态则输出Turning,掉下去输出"Fell off": 解题思路: 本题类似于<挑战程序设计>的一道水题(POJ -1852  Ants),思路题:不过本题输入并不一

算法竞赛入门经典+挑战编程+USACO

下面给出的题目共计560道,去掉重复的也有近500题,作为ACMer Training Step1,用1年到1年半年时间完成.打牢基础,厚积薄发.   一.UVaOJ http://uva.onlinejudge.org  西班牙Valladolid大学的程序在线评测系统,是历史最悠久.最著名的OJ.   二.<算法竞赛入门经典> 刘汝佳  (UVaOJ  351道题)  以下部分内容摘自:http://sdkdacm.5d6d.com/thread-6-1-1.html   "AO

棋盘覆盖问题(算法竞赛入门经典)

在一个 2^k * 2^k 个方格组成的棋盘中,若恰有一个方格与其它方格不同,则称该方格为一特殊方格,称该棋盘为一特殊棋盘.显然特殊方格在棋盘上出现的位置有 4^k 种情形.因而对任何 k>=0 ,有 4^k 种不同的特殊棋盘.下图所示的特殊棋盘为 k=2 时 16 个特殊棋盘中的一个. 在棋盘覆盖问题中,要用下图中 4 中不同形态的 L 型骨牌覆盖一个给定的特殊棋牌上除特殊方格以外的所有方格,且任何 2 个 L 型骨牌不得重叠覆盖.易知,在任何一个 2^k * 2^k 的棋盘中,用到的 L 型

【算法竞赛入门经典】【第三章】课后习题(第二部分)

自从蓝桥杯之后,都没写博客了.今天将之前第三章还差的一部分习题答案补上. 3-4整数相加 这一题题目有提示,说选择合适的输入方式,即可简化问题.刚开始没想到cin,结果还用字符串来做,多亏别人提醒我一下,我才想起cin.惭愧啊.. #include <iostream> using namespace std; int main() { int a,b; char op; while(cin>>a>>op>>b){ switch(op){ case '+':