(PAT 1061) Dating (字符串处理)
1061 Dating (20 point(s))
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 (大写字母)
小时范围:0-9 A-N (大写字母)
分钟范围:大小写字母
#include <iostream>
#include <algorithm>
#include <string>
using namespace std;
string Date[7] = { "MON","TUE","WED","THU","FRI","SAT","SUN" };
int main() {
string codes[4];
for (int i = 0; i < 4; ++i) {
cin >> codes[i];
}
int pointer1 = 0;
int pointer2 = 0;
int pointer3 = 0;
int pointer4 = 0;
int num1 = 0, num2 = 0, num3 = 0;
while (pointer1 < codes[0].length() && pointer2 < codes[1].length()) {
if (isupper(codes[0][pointer1]) && isupper(codes[1][pointer2]) && codes[0][pointer1] <= 71 && codes[0][pointer1] == codes[1][pointer2]) {
num1 = codes[0][pointer1] - 65;
pointer1++;
pointer2++;
break;
}
pointer1++;
pointer2++;
}
while (pointer1 < codes[0].length() && pointer2 < codes[1].length()) {
if ((isupper(codes[0][pointer1]) && isupper(codes[1][pointer2]) && codes[0][pointer1] <= 78) || (isdigit(codes[0][pointer1]) && isdigit(codes[1][pointer2])) && codes[0][pointer1] == codes[1][pointer2]) {
if (isdigit(codes[0][pointer1])) {
num2 = codes[0][pointer1] - 48;
}
else {
num2 = codes[0][pointer1] - 65 + 10;
}
break;
}
pointer1++;
pointer2++;
}
while (pointer3 < codes[2].length() && pointer4 < codes[3].length()) {
if (isalpha(codes[2][pointer3]) && isalpha(codes[3][pointer4]) && codes[2][pointer3] == codes[3][pointer4]) {
num3 = pointer3;
break;
}
pointer3++;
pointer4++;
}
printf("%s %02d:%02d", Date[num1].c_str(), num2, num3);
return 0;
}
还没有评论,来说两句吧...