D - Dreamer Kattis - dreamer(全排列)
Bash just woke up from his sweetest dream ever. In his dream, he became the best Pokenom trainer — like no one ever was. It was on the date …
Unfortunately, Bash forgot the exact date. He only remembered that the date was written in format ‘DD MM YYYY’ with exactly 8 digits. He also remembers these 8 digits (but he does not remember their order). Of course, the date must be a valid date.
Since Bash was born on Jan 1st, 2000, he knows that the date was on or after Jan 1st, 2000. (Note that the date can be Jan 1st, 2000 — it means Bash is destined to be the best Pokenom trainer since he was born!).
Bash really wants to know the date when he become the best Pokenom trainer. How many possible valid dates could there be? What is the earliest valid date that Bash could become the best Pokenom trainer?
Notes
On a leap year, February has 29 days. Following are the rules for determining leap years:
A year divisible by 400 is a leap year,
A year divisible by 100 but not by 400 is NOT a leap year,
A year divisible by 4 but not by 100 is a leap year,
A year not divisible by 4 is NOT a leap year.
Input
The first line contains one integer t (1≤t≤50) — the number of test cases.
Each of the next t lines describes one test case, contains eight digits in the format ‘XX XX XXXX’ (eight digits, separated by two blank spaces). Note that the input might not represent a valid date.
Note that the first month of the year is represented by 01, and the first day of the month by 01.
Output
For each test case, output a single line containing the number of possible dates and the earliest date which Bash could become the best Pokenom trainer, in the format ‘DD MM YYYY’. If there are no valid dates, print a single line containing ‘0’ (zero) instead.
Sample Input 1
3
04 11 2018
23 45 6789
01 01 0002
Sample Output 1
524 18 11 2004
0
4 01 01 2000
关键: 利用一个全排列公式next_permutation;
#include<bits/stdc++.h>
using namespace std;
int main()
{
int t;
cin >> t;
while(t--)
{
int y0 = 10000, m0 = 13, d0 = 32;
char s[15];
scanf("%s %s %s", s, s+2, s+4);
sort(s, s+8);
int num = 0;
while(next_permutation(s, s+8))
{
int yy = (s[0]-'0')*1000+(s[1]-'0')*100+(s[2]-'0')*10+(s[3]-'0');
if(yy>=2000)
{
int mm = (s[4]-'0')*10+(s[5]-'0');
if(mm<=12&&mm>=1)
{
int dd = (s[6]-'0')*10+(s[7]-'0');
if(mm==1||mm==3||mm==5||mm==7||mm==8||mm==10||mm==12)
{
if(dd<=31&&dd>=1)
{
num++;
if(yy<y0)
{
y0 = yy;
m0 = mm;
d0 = dd;
}
else if(yy==y0)
{
if(mm<m0)
{
y0 = yy;
m0 = mm;
d0 = dd;
}
else if(mm == m0)
{
if(dd<d0)
{
y0 = yy;
m0 = mm;
d0 = dd;
}
}
}
}
}
else if(mm==4||mm==6||mm==9||mm==11)
{
if(dd<=30&&dd>=1)
{
num++;
if(yy<y0)
{
y0 = yy;
m0 = mm;
d0 = dd;
}
else if(yy==y0)
{
if(mm<m0)
{
y0 = yy;
m0 = mm;
d0 = dd;
}
else if(mm == m0)
{
if(dd<d0)
{
y0 = yy;
m0 = mm;
d0 = dd;
}
}
}
}
}
else if(mm == 2)
{
if((yy%4==0&&yy%100!=0)||yy%400==0)
{
if(dd<=29&&dd>=1)
{
num++;
if(yy<y0)
{
y0 = yy;
m0 = mm;
d0 = dd;
}
else if(yy==y0)
{
if(mm<m0)
{
y0 = yy;
m0 = mm;
d0 = dd;
}
else if(mm == m0)
{
if(dd<d0)
{
y0 = yy;
m0 = mm;
d0 = dd;
}
}
}
}
}
else
{
if(dd<=28&&dd>=1)
{
num++;
if(yy<y0)
{
y0 = yy;
m0 = mm;
d0 = dd;
}
else if(yy==y0)
{
if(mm<m0)
{
y0 = yy;
m0 = mm;
d0 = dd;
}
else if(mm == m0)
{
if(dd<d0)
{
y0 = yy;
m0 = mm;
d0 = dd;
}
}
}
}
}
}
}
}
}
if(num==0) cout << num << endl;
else
{
cout << num << ' ';
if(d0<10) cout << '0' << d0 << ' ';
else cout << d0 << ' ';
if(m0<10) cout << '0' << m0 << ' ';
else cout << m0 << ' ';
cout << y0 <<endl;
}
}
return 0;
}