题目大意
题目链接
大意: 给你一串字符串, 让你找出现次数最多的合法日期, 日期格式dd-mm-yyyy
,其中yyyy 范围是2013 to 2015(因为这个降低了不少难度)
思路: 每次取10个字母判断是不是合法的, 同时用map计数。
具体实现看代码
AC代码
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65
| #include <set> #include <map> #include <ctime> #include <queue> #include <cmath> #include <stack> #include <bitset> #include <vector> #include <cstdio> #include <sstream> #include <cstring> #include <cstdlib> #include <iostream> #include <algorithm> #define eps 1e-8 using namespace std;
typedef long long ll; const int MAX = 2e5 + 5; char s[MAX]; char tmp[15]; string ans; int month[13] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}; map<string, int> mp; map<string, int>::iterator it;
int main() { cin >> s; int len = strlen(s); for (int i = 0; i <= len - 10; i++) { for (int j = 0; j < 10; j++) { tmp[j] = s[j + i]; } tmp[10] = '\0'; if (tmp[6] != '2' || tmp[7] != '0' || tmp[8] != '1') continue; if (tmp[9] != '3' && tmp[9] != '4' && tmp[9] != '5') continue;
if (tmp[2] != '-' || tmp[5] != '-') continue; if (!isdigit(tmp[0]) || !isdigit(tmp[1]) || !isdigit(tmp[3]) || !isdigit(tmp[4]) || !isdigit(tmp[6]) || !isdigit(tmp[7]) || !isdigit(tmp[8]) || !isdigit(tmp[9])) continue; int yue = (tmp[3] - '0') * 10 + tmp[4] - '0'; if (yue <= 0 || yue > 12) continue; int day = (tmp[0] - '0') * 10 + tmp[1] - '0'; if (day > month[yue] || day <= 0) continue; mp[tmp]++; } it = mp.begin(); int maxx = 0; for (; it != mp.end(); ++it) { if (maxx < (it->second)) { maxx = it->second; ans = it->first; } } cout << ans << "\n"; return 0; }
|
1
| 恰似你一低头的温柔,娇弱水莲花不胜寒风的娇羞, 我的心为你悸动不休。
|