2019牛客暑期多校训练营(第六场) Is Today Friday?

时间限制:C/C++ 5秒,其他语言10秒

空间限制:C/C++ 262144K,其他语言524288K
64bit IO Format: %lld

题目描述

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.

输入描述:

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'.

输出描述:

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.

输入

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

输出

Case #1: 0123456789
Case #2: Impossible

题意:给定由A-J组成的字符串,A-J分别对应0-9(对应关系不固定),寻找一个字典序最小的关系,使得给定的字符串都是星期五。

题解:模拟,蔡勒公式

代码:
#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;
}
posted @ 2019-08-07 11:55  Vivid-BinGo  阅读(198)  评论(0编辑  收藏  举报