Click Here to go back to the homepage.

Dreamer Solution:


#include <bits/stdc++.h>
using namespace std;

int daysInMonth[12] = {31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};

bool validDate(int day, int month, int year){
    if(year < 2000 || month > 12 || month == 0 || day == 0){
        return false;
    }
    //determine leap year
    daysInMonth[1] = (year % 400 == 0 || (year % 4 == 0 && year % 100 != 0)) ? 29 : 28;
    if(daysInMonth[month - 1] < day){
        return false;
    }
    return true;
}

int main(){
    // #ifndef TESTING
    // freopen("input.txt", "r", stdin);
    // freopen("output.txt", "w", stdout);
    // #endif

    string line;
    getline(cin, line);
    int cases = stoi(line);
    while(cases--){
        getline(cin, line);
        line.erase(remove(line.begin(), line.end(), ' '), line.end());
        sort(line.begin(), line.end());
        int count = 0;
        int earliest[3] = {32, 13, 10000};
        do {
            int day = stoi(line.substr(0, 2));
            int month = stoi(line.substr(2, 2));
            int year = stoi(line.substr(4));
            if(validDate(day, month, year)){
                count++;
                if(year < earliest[2] || (year == earliest[2] && month < earliest[1]) ||
                 (year == earliest[2] && month == earliest[1] && day < earliest[0])){
                    earliest[0] = day;
                    earliest[1] = month;
                    earliest[2] = year;
                }
            }
        } while(next_permutation(line.begin(), line.end()));
        cout << count << " ";
        if(earliest[2] < 10000){
            cout << setfill('0') << setw(2) << earliest[0] << " " << setfill('0') << setw(2) << earliest[1] << " " << earliest[2];
        }
        cout << endl;
    }

    return 0;   
}