Cod sursa(job #1591976)

Utilizator cautionPopescu Teodor caution Data 6 februarie 2016 22:18:51
Problema Invers Scor 20
Compilator cpp Status done
Runda Arhiva de probleme Marime 2.28 kb
#include <fstream>
#include <iostream>
#include <string>

std::string number;

bool isSatisfyingProperty(int p1, int x1, int p2, int x2, bool ugly_boolean)
{
  //  std::cout<<"DEBUG :"<<p1<<' '<<x1<<' '<<p2<<' '<<x2<<'\n';
    int r = 0;
    //INCORRECT :/ this shit will definetly not work in all cases, but whatever
    if(x2 == -1) {
        x2 = 9;
        r = -1;
    }
    if(p1 > p2) return true;
    else if(p1 == p2) {
        if(x1%2) return false;
        else return true;
    }
    else {
        if(x1 == 1) {
            //0+1 case
            if(ugly_boolean && x2 == 1 && isSatisfyingProperty(p1+1, number[p1+1]-'0', p2-1, number[p2-1]-'0', true)) return true;
            //compound case
            else {
                if(p1+1 < p2) {
                    int aux = 10+number[p1+1]-'0';
                    if(aux == 10) {
                        if(x2 == 9) return isSatisfyingProperty(p1+1, 1, p2-1, number[p2-1]-'0'+r, false);
                        else if(x2 == 0) return isSatisfyingProperty(p1+2, number[p1+2]-'0', p2-1, number[p2-1]-'0'+r-1, true); //r must be 0 here, anyways
                        else return false;
                    }
                    else {
                        if(aux%10 == x2) return isSatisfyingProperty(p1+2, number[p1+2]-'0', p2-1, number[p2-1]-'0'+r-1, true);
                        else if((aux-1)%10 == x2) return isSatisfyingProperty(p1+1, 1, p2-1, number[p2-1]-'0'+r-1, false);
                    }
                }
                else if(p1+1 == p2) {
                    if(x2%2) return false;
                    else return true;
                }
                else return false;
            }
        }
        else {
            if(x1 == x2) return isSatisfyingProperty(p1+1, number[p1+1]-'0', p2-1, number[p2-1]-'0'+r, true);
            else if(x1 == x2+1) return isSatisfyingProperty(p1, 1, p2-1, number[p2-1]-'0'+r, false);
            else return false;
        }
    }
    return false;
}

int main()
{
    std::ifstream in("invers.in");
    std::ofstream out("invers.out");
    int n;
    in>>n;
    for(int i = 1; i <= n; ++i) {
      //  std::cout<<'\n';
        in>>number;
        if(isSatisfyingProperty(0, number[0]-'0', number.size()-1, number[number.size()-1]-'0', true)) out<<"DA\n";
        else out<<"NU\n";
    }
    return 0;
}