[codeforces 260]B. Ancient Prophesy

[codeforces 260]B. Ancient Prophesy

试题描述

A recently found Ancient Prophesy is believed to contain the exact Apocalypse date. The prophesy is a string that only consists of digits and characters "-".

We'll say that some date is mentioned in the Prophesy if there is a substring in the Prophesy that is the date's record in the format "dd-mm-yyyy". We'll say that the number of the date's occurrences is the number of such substrings in the Prophesy. For example, the Prophesy "0012-10-2012-10-2012" mentions date 12-10-2012 twice (first time as "0012-10-2012-10-2012", second time as "0012-10-2012-10-2012").

The date of the Apocalypse is such correct date that the number of times it is mentioned in the Prophesy is strictly larger than that of any other correct date.

A date is correct if the year lies in the range from 2013 to 2015, the month is from 1 to 12, and the number of the day is strictly more than a zero and doesn't exceed the number of days in the current month. Note that a date is written in the format "dd-mm-yyyy", that means that leading zeroes may be added to the numbers of the months or days if needed. In other words, date "1-1-2013" isn't recorded in the format "dd-mm-yyyy", and date "01-01-2013" is recorded in it.

Notice, that any year between 2013 and 2015 is not a leap year.

输入

The first line contains the Prophesy: a non-empty string that only consists of digits and characters "-". The length of the Prophesy doesn't exceed 105 characters.

输出

In a single line print the date of the Apocalypse. It is guaranteed that such date exists and is unique.

输入示例

777-444---21-12-2013-12-2013-12-2013---444-777

输出示例

13-12-2013

数据规模及约定

见“输入

题解

扫一遍字符串,然后 string, map STL 大法好,反正 cf 测评机快的飞起。

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

const int BufferSize = 1 << 16;
char buffer[BufferSize], *Head, *Tail;
inline char Getchar() {
    if(Head == Tail) {
        int l = fread(buffer, 1, BufferSize, stdin);
        Tail = (Head = buffer) + l;
    }
    return *Head++;
}
int read() {
    int x = 0, f = 1; char c = getchar();
    while(!isdigit(c)){ if(c == '-') f = -1; c = getchar(); }
    while(isdigit(c)){ x = x * 10 + c - '0'; c = getchar(); }
    return x * f;
}

#define maxn 100010
#define id isdigit
int n;
char S[maxn], tmp[15];
map <string, int> M;

bool jud(char* s) {
//	printf("%s
", s + 1);
	int d, m, y;
	if(!id(s[1]) || !id(s[2]) || s[3] != '-' || !id(s[4]) || !id(s[5]) || s[6] != '-' || !id(s[7]) || !id(s[8]) || !id(s[9]) || !id(s[10]))
		return 0;
	d = (s[1] - '0') * 10 + s[2] - '0';
	m = (s[4] - '0') * 10 + s[5] - '0';
	y = (s[7] - '0') * 1000 + (s[8] - '0') * 100 + (s[9] - '0') * 10 + s[10] - '0';
//	printf("%d %d %d
", d, m, y);
	if(y < 2013 || y > 2015) return 0;
	if(m < 1 || m > 12 || d < 1) return 0;
	if(m == 2 && d > 28) return 0;
	if(m <= 7 && (m & 1) && d > 31) return 0;
	if(m <= 7 && !(m & 1) && d > 30) return 0;
	if(m > 7 && !(m & 1) && d > 31) return 0;
	if(m > 7 && (m & 1) && d > 30) return 0;
	return 1;
}

int main() {
	scanf("%s", S + 1); n = strlen(S + 1);
	
	int mx = 0; string mxs = "";
	for(int i = 1; i <= n - 9; i++) {
		memset(tmp, 0, sizeof(tmp));
		for(int j = i; j <= i + 9; j++) tmp[j-i+1] = S[j];
		if(jud(tmp)) {
			string tt = string(tmp + 1);
			if(!M.count(tt)) M[tt] = 1;
			else M[tt]++;
			if(mx < M[tt]) mx = M[tt], mxs = tt;
		}
	}
	
	cout << mxs << endl;
	
	return 0;
}
原文地址:https://www.cnblogs.com/xiao-ju-ruo-xjr/p/5814759.html