PAT甲级 1061. Dating

前言

这个题目不难,是第一道题,稳中求进。这个题目麻烦一点的在于题目的意思我感觉有一点含糊不清,我一开始就是可能误解题目意思了导致条件有疏忽。

题目原文

Sherlock Holmes received a note with some strange strings: Let’s date! 3485djDkxh4hhGE 2984akDfkkkkggEdsb s&hgsfdk d&Hyscvnm. It took him only a minute to figure out that those strange strings are actually referring to the coded time Thursday 14:04 – since the first common capital English letter (case sensitive) shared by the first two strings is the 4th capital letter D, representing the 4th day in a week; the second common character is the 5th capital letter E, representing the 14th hour (hence the hours from 0 to 23 in a day are represented by the numbers from 0 to 9 and the capital letters from A to N, respectively); and the English letter shared by the last two strings is s at the 4th position, representing the 4th minute. Now given two pairs of strings, you are supposed to help Sherlock decode the dating time.

Input Specification:
Each input file contains one test case. Each case gives 4 non-empty strings of no more than 60 characters without white space in 4 lines.

Output Specification:
For each test case, print the decoded time in one line, in the format DAY HH:MM, where DAY is a 3-character abbreviation for the days in a week – that is, MON for Monday, TUE for Tuesday, WED for Wednesday, THU for Thursday, FRI for Friday, SAT for Saturday, and SUN for Sunday. It is guaranteed that the result is unique for each case.

Sample Input:
3485djDkxh4hhGE
2984akDfkkkkggEdsb
s&hgsfdk
d&Hyscvnm

Sample Output:
THU 14:04

解决思路

题目大概是给定四个字符串,然后给出一个时间点。

第一个信息点是要破译出星期几。根据例子,前两个字符串中第一次出现同位相同的字母时,如果这个字母是A到G中的一个,就说明它在这几个字母中的顺序就是一周中的第几天,比如D是第四位,那么就代表星期四。

第二个信息点是要破译出第几小时。它提到第二个在同一位置相同的字母是E,代表14,因为0到9代表了零点到九点,而从A开始到N就是代表十点到二十三点。题目中这个相同的E是出现在前两个字符串中的,它没提及到是否有可能前两个中可能不存在,那么是不是要在后两个字符串中去找,因为这涉及到第三个信息点。

第三个信息点是,后两个字符串中如果在同一个位置出现了s这个字母,注意只认s这个字母,然后它在后两个字符串中的位置(从0开始)是4,那么就是代表第四分钟。似乎可以知道了,如果前两个字符串中没有找到小时的破译点(也就是第二对相同位置的相同字母且在A和N之间),那么就在后两个字符串中去找。由于分钟的定义不是后两个字符串中相同位置的相同字母给出,而是固定的s的位置给出,那么就不会和第二个信息点发生冲突。

源代码

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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
#include <iostream>
#include <cstdio>
#include <string>
#include <algorithm>
#include <cctype>
#include <vector>

using namespace std;

int main(void) {
string s1, s2, s3, s4;
cin >> s1 >> s2 >> s3 >> s4;

vector<char> commons;
int M;
// 找到第一个信息点以及可能存在的第二个信息点
for(int i = 0; i < min(s1.size(), s2.size()); i++) {
if(s1[i] == s2[i]) {
commons.push_back(s1[i]);
}
}
// 找到
for(int i = 0; i < min(s3.size(), s4.size()); i++) {
if(s3[i] == s4[i]) {
commons.push_back(s3[i]);
}
}

for(int i = 0 ;i < min(s3.size(), s4.size()); i++) {
if(s3[i] == s4[i] && isalpha(s3[i])) {
M = i;
}
}
char D, H;
bool isDFind = false;
for(int i = 0; i < commons.size(); i++) {
if(!isDFind) {
if(commons[i] <= 'G' && commons[i] >= 'A') {
D = commons[i];
isDFind = true;
continue;
}
} else {
if(commons[i] >= '0' && commons[i] <= '9') {
H = commons[i];
break;
}
if(commons[i] >= 'A' && commons[i] <= 'N') {
H = commons[i];
break;
}
}
}

if(D == 'A') {
printf("MON ");
} else if(D == 'B') {
printf("TUE ");
} else if(D == 'C') {
printf("WED ");
} else if(D == 'D') {
printf("THU ");
} else if(D == 'E') {
printf("FRI ");
} else if(D == 'F') {
printf("SAT ");
} else if(D == 'G') {
printf("SUN ");
}

int Hi;
if(isdigit(H)) {
Hi = (int)(H - '0');
} else {
Hi = (int)(H - 'A' + 10);
}
printf("%02d:", Hi);

printf("%02d", M);
return 0;
}

后记

我这个写复杂了,当时AC了就没管了,还可以简化的,确实有点不优雅…