PAT (Advanced Level) Practice 1061 Dating (20 分) 凌宸1642
题目描述:
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.
译:夏洛克·福尔摩斯收到了一张带有奇怪字符串的纸条 :Let's date! 3485djDkxh4hhGE 2984akDfkkkkggEdsb s&hgsfdk d&Hyscvnm
. 他只用了一分钟来找出那些奇怪的字符串实际上是指的是编码时间Thursday 14:04
——y因为第一对相同的英文大写字母(区分大小写)前面两个字符串是第 4 个大写字母D
,代表在一周内的第四天 ; 第二个相同的字符是第 5 个大写字母 E
,表示第 14 个小时(因此,一天中 0 到 23 小时分别用 0 到 9和 A 到 N 表示);最后两个字符串相同的英文字母是 s
在第 4 位,代表第 4 分钟。现在有两组字符串,你要帮夏洛克破译约会时间 。
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.
译:每个测试文件包含一个测试用例,每个用例在 4 行中给定不超过60个字符且没有空格的非空字符串。
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.
译:对于每个测试用例,在一行中打印破译的时间,格式为:DAY HH:MM
, 其中 DAY 是一个是 3 字符的一周中某一天的缩写 。也就是说,MON
代表周一 , TUE
代表周二, WED
代表周三, THU
代表周四, FRI
代表周五, SAT
代表周六, SUN
代表周日。题目保证对于每个用例结果唯一。
Sample Input (样例输入):
3485djDkxh4hhGE
2984akDfkkkkggEdsb
s&hgsfdk
d&Hyscvnm
Sample Output (样例输出):
THU 14:04
The Idea:
这里的坑点就在与 第一组的两个字符串中,我们需要先确定好 找到第一对相同的 大写英文字母 ,才能继续往下去寻找第二对相同的字符。证据就是 示例中第一组中第一对相同的字符是 3 号位置上的 8 ,但是此时还没有找到第一对相同的大写英文字符,所以这对是不起作用的,因为示例中选择的时间是第一对相同字符 ( D )后面的相同字符 ( E )。
The Codes:
#include<bits/stdc++.h>
using namespace std ;
string s[4] ;
string week[7] = {"MON","TUE","WED","THU","FRI","SAT","SUN"} ;
int main(){
for(int i = 0 ; i < 4 ; i ++) getline(cin , s[i]) ; // 输入 4 个字符串
int lenMin1 = min(s[0].size() , s[1].size()) ; // 得到第一组字符串中长度小的
int lenMin2 = min(s[2].size() , s[3].size()) ; // 得到第二组字符串中长度小的
bool flag = false ; // 标志是否找到了第一对 相同 的大写字母。
int index1 , index2 , index3 ;
for(int i = 0 ; i < lenMin1 ; i ++){
// 找 第一组相同的 大写字母
if((s[0][i] == s[1][i])&&(s[0][i] >= 'A' && s[0][i] <= 'G')&& !flag){
flag = true;
index1 = s[0][i] - 'A' ;
i ++ ; // 找到了直接往后挪
}
// 找到第一对相同的大写英文字符之后,找第二对相同的字符 [0 - 9 , A - N]
if(flag && s[0][i] == s[1][i]){
if(s[0][i] >= '0' && s[0][i] <= '9'){
index2 = s[0][i] - '0' ;
break ;
}
if(s[0][i] >= 'A'&& s[0][i] <='N'){
index2 = s[0][i] - 'A' + 10 ;
break ;
}
}
}
// 在第二组字符串中 找第一对相同的字符
for(int j = 0 ; j < lenMin2 ; j ++){
if((s[2][j] == s[3][j])&&((s[2][j] >= 'a' && s[2][j] <= 'z')||(s[2][j] >= 'A' && s[2][j] <= 'Z')) ){
index3 = j ;
break ;
}
}
printf("%s %02d:%02d\n",(week[index1]).c_str(),index2,index3);
return 0;
}