More than 2019 cattle off summer school camp (sixth) Is Today Friday?

Time limit: C / C ++ 5 seconds to 10 seconds in other languages

Space limitations: C / C ++ 262144K, other languages 524288K
64bit IO the Format:% LLD

Title Description

No, it's not Friday :(
 
TangTang loves Friday and he has made up a list of n days which are all Friday! Each date in this list is formed as "yyyy/mm/dd", where "yyyy" is a four-digit number representing the year, "mm" is a two-digit number representing the month and "dd" is a two-digit number representing the day. TangTang only considers years between 1600 and 9999 (inclusive), so each year in the list always has four digits, but the months and the days may have leading zeros when it's necessary. For example, "August 3rd, 2019" should be formed as "2019/08/03".
 
To store safely, TangTang ciphered the list using a simple substitution cipher. He substituted each digit by a letter between 'A' and 'J' such that the same digits correspond to the same letter and different digits to different letters.
 
Unfortunately, TangTang forgot which letter corresponds to which digit. Please help him restore the original list.

Enter a description:

There are multiple test cases. The first line contains an integer T (1≤T≤10), indicating the number of test cases. Test cases are given in the following.

For each test case, the first line contains an integer n (1≤n≤1000001), indicating the number of dates.

Each of the next n lines contains a string in the form of "yyyy/mm/dd", representing a ciphered date, where each digit is substituted by an uppercase letter between 'A' and 'J'.

Output Description:

For each test case, output "Case #x: y" in one line (without quotes), where x indicates the case number starting from 1, and y denotes the answer to this test case.

If there is no way to restore the list, then y is "Impossible".

Otherwise, y is a string consisting of ten distinct digits, representing the key to decipher the list. More specifically, the i-th digit in y corresponds to the i-th uppercase letter.

If there are at least two ways, then y is the smallest possible string in the lexicographical order.

Entry

2 
1 
CABJ / AI / AC 
5 
CABJ / AI / AC 
CABJ / AI / AD 
CABJ / AI / EI 
CABJ / AI / AF 
CABJ / AI / AG

Export

#. 1 Case: 0123456789 
Case # 2: Impossible 

meaning of the questions: Given a string consisting of AJ, AJ 0-9 respectively (a correspondence relationship is not fixed), to find a relationship lexicographically smallest such strings are given Friday.

Solution: analog, Zeller's formula .

Code:
#include<bits/stdc++.h>
using namespace std;
int days[]={0,31,28,31,30,31,30,31,31,30,31,30,31};
string s[100005];
int check(int y,int m,int d);
int main()
{
  int i,T,n,a[10],y,m,d,flag,ans,cnt=0;
  scanf("%d",&T);
  while(T--)
  {
    printf("Case #%d: ",++cnt);
    ans=0;
    scanf("%d",&n);
    for(i=0;i<n;i++) cin>>s[i];
    for(i=0;i<10;i++) a[i]=i;
    sort(s,s+n);  //去重,防止超时。
    n=unique(s,s+n)-s;
    do
    {
      flag=1;
      for(i=0;i<n;i++)
      {
        y=1000*a[s[i][0]-'A']+100*a[s[i][1]-'A']+10*a[s[i][2]-'A']+a[s[i][3]-'A'];
        m=10*a[s[i][5]-'A']+a[s[i][6]-'A'];
        d=10*a[s[i][8]-'A']+a[s[i][9]-'A'];
        if(!check(y,m,d)) {flag=0;break;}
      }
      if(flag)
      {
        for(i=0;i<10;i++) printf("%d",a[i]);
        printf("\n");
        ans=1;
        break;
      }
    }
    while(next_permutation(a,a+10));
    if(!ans) printf("Impossible\n");
  }
  system("pause");
  return 0;
}
int check(int y,int m,int d)
{
  if(y<1600) return 0; //检验日期合法性
  if(m<1||m>12) return 0;
  int t=days[m];
  if(m==2&&((y%4==0&&y%100!=0)||(y%400==0))) t++;
  if(d>t) return 0;
  
  if(m<3) y-=1,m+=12; //蔡勒公式
  int c=int(y/100);
  y=y-100*c;
  int w=(c/4)-2*c+y+(y/4)+(26*(m+1)/10)+d-1;
  w=(w%7+7)%7;
  return w==5;
}

Guess you like

Origin www.cnblogs.com/VividBinGo/p/11314458.html