K1 Known-Well Palindrome Date - Easy Version
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 '222' and '000'. That is mainly because the date "202002022020020220200202" 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 "YYYYMMDDYYYYMMDDYYYYMMDD", where YYYYYYYYYYYY, MMMMMM and DDDDDD represent the year, month and day, respectively.
- The Palindrome Date should conform the format of "ABCDDCBAABCDDCBAABCDDCBA", where AAA, BBB, CCC and DDD represent the digit number at the range of '000' to '999'. 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).
Input Specification:
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 10510^5105. 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 '000'-'999' and spaces, and the sum of the length of the string of all test cases will not exceed 2×10610^6106.
Output Specification:
For each test case output one integer in one line, indicating the number of times when a consecutive Palindrome Date appears.
Sample Input:
130402202002021234 320124202332021862 110110116711302020 020208196711301866 022002202002025678 220202202002022021 120202202002022020 #
Sample Output:
1 0 1 2 2
Hint:
For the first sample, Little Gyro can find 111 Palindrome Date "202002022020020220200202", from the 7th7th7th digit to the 14th14th14th digit.
For the second sample, Little Gyro can not find any Palindrome Date, although there contains a Palindrome Subsequence "202332022023320220233202", it is not a valid date.
For the third sample, Little Gyro can find 111 Palindrome Date "110110111101101111011011", from the 1st1st1st digit to the 8th8th8th digit. Although there also contains a Palindrome Date "202002022020020220200202", it is separate by a space.
For the fourth sample, Little Gyro can find 222 Palindrome Dates. Such as "022002200220022002200220", from the 1st1st1st digit to the 8th8th8th digit, and "202002022020020220200202", from the 7th7th7th digit to the 14th14th14th digit, respectively.
题意
找出一个字符串中出现的回文日期的次数,需要满足回文的条件以及满足历法的要求。
简析
简单模拟即可。
参考代码
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N = 1e6 + 10;
string s;
int n;
const int Day[] = { 0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
int IsP(string &s) {
for (int i = 0; i < 4; i++) {
if (s[i] == ' ') return false;
if (s[i] != s[7 - i]) return false;
}
return true;
}
int IsN(int n) {
return ((n % 400 == 0) || (n % 100 != 0 && n % 4 == 0));
}
int IsE(string &s) {
int y = 0;
for (int i = 0; i <= 3; i++) {
y = y * 10 + (s[i] - '0');
}
int m = 0;
for (int i = 4; i <= 5; i++) {
m = m * 10 + (s[i] - '0');
}
int d = 0;
for (int i = 6; i <= 7; i++) {
d = d * 10 + (s[i] - '0');
}
if (m > 12 || m < 1) return false;
int true_d = Day[m];
if (m == 2 && IsN(y)) true_d++;
if (d > true_d || d < 1) return false;
return true;
}
int solve() {
int cnt = 0;
for (int i = 0; i <= n - 8; i++) {
string t = s.substr(i, 8);
// cout << t << endl;
if (!IsP(t))continue;
cnt += IsE(t);
}
return cnt;
}
int main() {
// freopen("in.txt", "r", stdin);
while (true) {
getline(cin, s);
n = s.size();
if (s == "#") break;
printf("%d\n", solve());
}
return 0;
}
Tips
数据有很多很离谱,比如空格的影响需要消除。然后要判断闰年。