UVA1588 Kickdown

两个齿轮接合的最小长度,高度限制 3。
固定下方齿轮,起始点设置为 101,不断移动上方齿轮。

const int SIZE = 100+1;
int main(int argc, const char * argv[]) {
    string bottom,top;
    int H[SIZE*3], b_btm = SIZE, b_top = 0;
    while (cin >> bottom >> top) {
        memset(H, 0, sizeof(H));
        int len_btm = bottom.size(), len_top = top.size();
        for (int i = 0; i < len_btm; ++i)
            H[i+SIZE] = bottom[i] - '0';
        int ans = len_btm + len_top;
        for (b_top = b_btm-len_top; b_top <= b_btm + len_btm; ++b_top) {
            bool flag = true;
            for (int i = 0; i < len_top; ++i) {
                if (top[i]-'0' + H[b_top+i] > 3) { flag = false; break; }
            }
            if (flag) ans = min(ans, max(b_top+len_top, b_btm+len_btm) - min(b_top, b_btm));
        }
        cout <<ans<<endl;
    }
    
    
    return 0;
}

原文地址:https://www.cnblogs.com/i-8023-/p/12763318.html