DDMMORMMDD - Editorial

PROBLEM LINK:

Practice
Contest: Division 1
Contest: Division 2
Contest: Division 3
Contest: Division 4

Author: the_hyp0cr1t3
Tester & Editorialist: iceknight1093

DIFFICULTY:

992

PREREQUISITES:

None

PROBLEM:

Given a date string, decide whether it’s of the form DD/MM/YYYY, MM/DD/YYYY, or can be either.
It’s guaranteed that the given date is valid in at least one of the forms.

EXPLANATION:

Since the given date is valid in at least one form, there’s a rather simple solution that doesn’t involve any ugly casework with days-per-month and such.

Let x denote the number formed by the first two digits in the input, and y denote the number formed by the next two digits.
So for example, if S = 12/24/2051 then we have x = 12 and y = 24.

Then,

  • If 1 \leq x \leq 12 and 1 \leq y \leq 12, the date can be both DD/MM/YYYY or MM/DD/YYYY.
  • Otherwise, exactly one of x and y must denote the month, so:
    • If 1 \leq x \leq 12, the answer is MM/DD/YYYY
    • Else, the answer is DD/MM/YYYY

TIME COMPLEXITY

\mathcal{O}(1) per test case.

CODE:

Author's code (C++)
#include <iostream>

int main() {
    int tests;
    std::cin >> tests;
    while (tests--) {
        std::string s;
        std::cin >> s;
        int x = (s[0] - '0') * 10 + (s[1] - '0');
        int y = (s[3] - '0') * 10 + (s[4] - '0');
        if (x <= 12 and y <= 12)
            std::cout << "BOTH" << '\n';
        else if (y <= 12)
            std::cout << "DD/MM/YYYY" << '\n';
        else
            std::cout << "MM/DD/YYYY" << '\n';
    }
}
Editorialist's code (Python)
for _ in range(int(input())):
    s = input()
    x, y = int(s[0:2]), int(s[3:5])
    if 1 <= x <= 12 and 1 <= y <= 12: print('Both')
    elif 1 <= x <= 12: print('MM/DD/YYYY')
    else: print('DD/MM/YYYY')

Another very simple approach.

t = int(input())

for i in range(t):
    a,b,c = list(map(int,input().split("/")))
    if b <= 12 and a <= 12:
        print("BOTH")
    elif a > 12:
        print("DD/MM/YYYY")
    else:
        print("MM/DD/YYYY")

Split the 3 numbers indicating either DD,MM and YYYY or MM,DD and YYYY and stored them in a, b and c. These variables have then been used to check if format is DD,MM or MM,DD by checking if variable a > 12 (Max months exceeded) then print out the format DD,MM,YYYY or else print MM,DD,YYYY and in case a and b both are less than equal to 12 then print both.

Hope it helps! :grinning: