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
orMM/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
- If 1 \leq x \leq 12, the answer is
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')