UVa 1588 Kickdown

不知道为什么总是超时,基本都是两重循环。

#include 
#include 

char top[101];
char bottom[101];

int main() {



    while(scanf("%s%s", top, bottom)) {
        //printf("%s\n", top);
        //printf("%s\n", bottom);
        int tlen = strlen(top);
        int blen = strlen(bottom);
        //printf("%d %d\n", tlen, blen);
        int minlen = tlen + blen;

        int maxlen = tlen > blen ? tlen : blen;

        for(int i = 0; i < tlen; i++) {
            for(int j = i; j < tlen && j < blen + i; j++) {
                if(top[j] == '2' && bottom[j-i] == '2') {
                    break;
                }
                if(j == tlen - 1 || j == blen - 1 + i) {
                    //printf("%d %d %d\n", i, j, 0);
                    if(minlen > maxlen + i) {
                        int diff = tlen - i < blen ? 0 : tlen - i - blen;
                        if(diff)
                          minlen = maxlen;
                        else
                          minlen = maxlen + i;
                    }
                }
            }
        }

        for(int i = 0; i < blen; i++) {
            for(int j = i; j < tlen + i && j < blen; j++) {
                if(bottom[j] == '2' && top[j-i] == '2') {
                    break;
                }
                if(j == tlen - 1 + i || j == blen - 1) {
                    //printf("%d %d %d\n", i, j, 1);
                    if(minlen > maxlen + i) {
                        int diff = blen - i < tlen ? 0 : blen - i - tlen;
                        if(diff)
                          minlen = maxlen;
                        else
                          minlen = maxlen + i;
                    }
                }
            }
        }

        printf("%d\n", minlen);

        memset(top, 0, sizeof(top));
        memset(bottom, 0, sizeof(bottom));
    }

    return 0;
}  

你可能感兴趣的:(ACM)