Cod sursa(job #593810)

Utilizator cont_de_testeCont Teste cont_de_teste Data 4 iunie 2011 16:08:51
Problema Invers Scor 60
Compilator cpp Status done
Runda Arhiva de probleme Marime 2.49 kb
# include <cstdio>
# include <cstring>
# include <cassert>

const char *FIN = "invers.in", *FOU = "invers.out" ;

char S[10005], S2[10005];
int T, N ;

int check1(int l, int n) {
    int i, j, k;
    for (i = l, j = n - 1; 1; i++, j--) {
        if (i == j)
            return S[i] % 2 == 0;
        if (j - i == 1)
            return S[i] == S[j] || (S[i] == S[j] + 11);
        switch (S[i] - S[j]) {
        case 0:
            break;
        case 1:
            S[i + 1] += 10;
            break;
        case 11:
            S[i + 1] += 10;
        case 10:
            if (S[j] == 9) return 0;
            for (S[k = j - 1]--; S[k] < 0 && k >= i; )
                S[k] += 10, S[--k]--;
            if (k == i) return 0;
            break;
        default:
            return 0;
        }
    }
}
int check (int L, int N) {
    for (int i = L, j = N - 1, k; ; ++i, --j) {
        if (i == j) {
            return (S[i] & 1) == 0 ;
        } else if (j - i == 1) {
            return (S[i] == S[j] || (S[i] == S[j] + 11)) ;
        } else {
            if (S[i] - S[j] == 0) {
                continue ;
            } else if (S[i] - S[j] == 1) {
                S[i + 1] += 10 ;
            } else if (S[i] - S[j] == 11) {
                S[i + 1] += 10 ;
                if (S[i] - S[j] == 10) assert (0), assert (1) ;
                return 0 ;
            } else if (S[i] - S[j] == 10) {
                if (S[j] == 9) return 0;
                for (S[k = j - 1]-- ; S[k] < 0 && k >= i ; S[--k]--)
                    S[k] += 10 ;
                if (k == i) return 0;
            } else {
                return 0 ;
            }
        }
    }
}

const char *solve (int N) {
    if (N == 1) return (S[0] & 1) ? "NU" : "DA";
    else if (N == 2) return (S[0] == S[1] || (S[0] == 1 && (S[1] & 1) == 0)) ? "DA" : "NU" ;
    else if (S[0] == 1) {
        memcpy (S2, S, sizeof (S)) ;
        S[1] += 10 ;
        if (check (1, N)) return "DA" ;
        else if (S[0] == 1 && S[N - 1] == 0) {
            return "NU" ;
        } else {
            memcpy (S, S2, sizeof (S2)) ;
            return check (0, N) ? "DA" : "NU" ;
        }
    } else {
        return check (0, N) ? "DA" : "NU" ;
    }
    return "NU";
}

int main (void) {
    freopen (FIN, "r", stdin) ;
    freopen (FOU, "w", stdout) ;

    for (scanf ("%d", &T); T; --T) {
        scanf ("%s", S) ;
        for (N = 0; S[N]; S[N++] -= '0') ;
        printf ("%s\n", solve (N)) ;
    }
}