第十七届中国计量大学程序设计竞赛 K Known-Well Palindrome Date-Easy Version

K Known-Well Palindrome Date-Easy Version
链接:https://ac.nowcoder.com/acm/contest/7591/K
来源:牛客网

题目描述
This problem is almost the same as the next one, but has different requirement and constraints.

As time flies by, the composition of our society is always changing. Where the post-1980s and post-1990s generations were almost considered as the era of the past, the reverse is true for the post-1995 and post-2000s generations: the rising of new generations is bound to bring more surprises.
In this year, the brand new post-2020s generation was recently created. On 2nd Feb 2020, it was reported by People’s Daily that babies who were born in Changyi District, Jilin City, Jilin Province on this day, would receive a unique identification number only made up of digit ‘2’ and ‘0’. That is mainly because the date “20200202” is a Palindrome Date.
Nowadays, Little Gyro considers that this kind of phenomenon of Palindrome Date is quite well-known among all the people and decides to play with it. Moreover, Little Gyro made the following new regulations on it:
The Palindrome Date should be a valid date, according to the rules of Gregorian calendar(公历历法). Furthermore, it should satisfy the format of “YYYYMMDD”, where YYYY, MM and DD represent the year, month and day, respectively.
The Palindrome Date should conform the format of “ABCDDCBA”, where A, B, C and D represent the digit number at the range of ‘0’ to ‘9’. Furthermore, it is allowed to be duplicated among each of them.

Now given a series of identification number periods, Little Gyro wants to calculate the number of times if and only if a consecutive Palindrome Date appears from these given number periods within the specific given order. Please help him.

Please note that all the Palindrome Dates should at the range of 1st Jan, 0001 and 31st Dec, 9999 (both inclusive).
输入描述:
There are multiple test cases. Each case consists a string in one line, indicating several identification number periods, and every two of them is separated by a space.

The length of the string in each line will not exceed 10^5
. And in one line only containing the single character ‘#’ terminates the input and this test case is not to be processed.

It’s guaranteed that the string can only be made up by number digits ‘0’-‘9’ and spaces, and the sum of the length of the string of all test cases will not exceed 2×10^6
.
输出描述:
For each test case output one integer in one line, indicating the number of times when a consecutive Palindrome Date appears.
输入

130402202002021234
320124202332021862
110110116711302020 020208196711301866
022002202002025678
220202202002022021 120202202002022020
#

输出

1
0
1
2
2

题意
题意也就是给你数字找回文串,而这回文串为8位,是年月日,需要你判断是否合法以及其中是否有空格(有空格不算)。是个和麻烦很恶心的题,我也wa好好多次。
代码

#include<iostream>
#include<vector>
#include<map>
#include<string>
#include<cstring>
#include<queue>
#include<algorithm>
#include<cstring>
#include<set>
#include<stack>
using namespace std;

const int maxn = 4e6 + 10;
char s[maxn];
int d[] = {0,31,28,31,30,31,30,31,31,30,31,30,31};
bool check(int l,int r){
    int year = 0;
    for(int i = l,j = r;i <= j; i++,j--){
        year = year * 10 + (s[i] - '0');
        if(s[i] != s[j])return false;
    }
    int month = ((s[l + 4] - '0') * 10) + (s[l + 5] - '0');
    int day = ((s[l + 6] - '0') * 10) + (s[l + 7] - '0');
    if(year < 1 || year > 9999)return false;
    if(month < 0 || month > 12)return false;
    if((year % 4 == 0 && year % 100 != 0) || year % 400 == 0){
        if(month == 2){
            return day >= 1 && day <= 29;
        }else{
            return day >= 1 && day <= d[month];
        }
    }
    return day >= 1 && day <= d[month];
    return true;
}
void solved(){
    int n = strlen(s + 1); 
    int ans = 0;
    for(int i = 1; i + 7 <= n; i++){
        int l = i;
        int r = i + 7;
        if(check(l,r)){
            ans++;
        }
    }
    printf("%d\n",ans);
}
int main(){
    while(cin.getline(s+1,maxn) ){
        if(s[1] == '#')break; 
        solved();
    }
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值