Thursday 30 June 2016

LUCKY TIME PROBLEM (Given the time when she completed the code find a lucky time to execute it so that Alice need to wait as little as possible. Time is shown in 24 24 hour format as hh:mm:ss. Time is said to be lucky if all the 6 6 characters (except ':') are different.)

#include <bits/stdc++.h>

using namespace std;

bool vis [10 + 1];

bool rep(int num){

    if(num < 10){               // there is a 0 digit

        if(vis[0]) return true;
        vis[0] = true;
    }

    if(num == 0) return true;   // 2 zeros in the number

    while(num != 0){            // extract the number digit by digit

        int digit = num % 10;
        if(vis[digit]) return true;         // check if digits are repeated
        num /= 10;
        vis[digit] = true;
    }

    return false;
}



int main()
{
    int t, h , m , s;

    scanf("%d", &t);

    while(t--){

        scanf("%d:%d:%d", &h, &m, &s);

        bool found = false;
        bool st = false;

        for(int i = h; i < 24 && !found; i++){

            for(int j = 0; j < 60 && !found; j++){

                if(!st) j = m;

                for(int k = 0; k < 60 && !found; k++){

                    if(!st){

                        k = s;              // start from the given second
                        st = true;
                    }

                    memset(vis, false , sizeof vis);

                    if(!rep(i) && !rep(j) && !rep(k)){          // check if all cells are different

                        if(i < 10) printf("0");       // each number has 2 cells
                        printf("%d:", i);
                        if(j < 10) printf("0", j);
                        printf("%d:", j);
                        if(k < 10) printf("0", k);
                        printf("%d\n", k);
                        found = true;
                    }

                    if(i == 23 && j == 59 && k == 59){      // search in times before the given time

                        i = 0;
                        j = 0;
                        k = 0;
                    }
                }
            }
        }

    }

    return 0;
}

No comments:

Post a Comment