zoukankan      html  css  js  c++  java
  • 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("
    ");
            ans=1;
            break;
          }
        }
        while(next_permutation(a,a+10));
        if(!ans) printf("Impossible
    ");
      }
      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;
    }
    本博客仅为本人学习,总结,归纳,交流所用,若文章中存在错误或有不当之处,十分抱歉,劳烦指出,不胜感激!!!
  • 相关阅读:
    PostMan-NewMan运行参数
    shell脚本学习简单记录笔记
    android开发okhttp-4.9.1源码大致流程解读
    android开发获取键盘高度以及判断键盘是否显示(兼容分屏模式)
    Android开发The style on this component requires your app theme to be Theme.AppCompat (or a descendant)的解决方法
    Linux开发Ubuntu安装man手册
    Android开发源码解读四大组件源码解读简单梳理
    Android开发涉及到的AMS类和ActivityThread类源码解读
    Android开发为什么主线程可以一直运行而不会退出来
    前端CryptoJS加密、后端解密代码实现参考
  • 原文地址:https://www.cnblogs.com/VividBinGo/p/11314458.html
Copyright © 2011-2022 走看看