1. 程式人生 > >1061 Dating (20 分)字串的處理

1061 Dating (20 分)字串的處理

題目

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

解題思路

  題目大意: 給你兩對字串,按照如下規則從中解碼出時間——
  1. 第一對兒字串的第一個相同的大寫字母,其所在字母表順序表示星期數,如A是第一個字母,表示第一天,即星期一。我們也可以由此推出,這個所謂的第一個相同的大寫字母有兩個隱含條件,第一個是位置也要相同,第二個是有效取值範圍在A~G之間(畢竟一週只有7天);
  2.第一對兒字串的第二個相同的大寫字母

,其對應24進位制(0 ~ 9,A ~ N)的代表小時數,如字母A,表示10點;同1,這裡隱含一個條件,即有效的取值範圍;
  3. 第二對兒字串第一個相同的字母,其所在位置代表分鐘數。這裡也隱含了一個條件,並不區分大小寫。
  4. 輸出格式:按照DAY HH:MM的形式輸出,注意補零。
  解題思路: 題意弄清楚了之後,其實沒什麼難度,就是隱含條件有點坑,題目並沒有說明資料的合法性,所以就不清楚要不要做判斷,經過資料1、2、4的調教,才知道,原來需要做資料合法性檢測。

/*
** @Brief:No.1061 of PAT advanced level.
** @Author:Jason.Lee
** @Date:2018-12-27
** @Solution: https://blog.csdn.net/CV_Jason/article/details/85319249
*/

#include<iostream>
#include<string>
#include<algorithm>

using namespace std;

string week[7] = {"MON","TUE","WED","THU","FRI","SAT","SUN"};
int main(){
	string str1,str2,str3,str4;
	while(cin>>str1>>str2>>str3>>str4){
		char letter[2];
		int index = 0;
		for(int i=0,j=0;i<str1.length()&&j<str2.length()&&index<2;i++,j++){
			if(str1[i]==str2[j]){
				if(index==0&&(str1[i]>='A'&&str1[i]<='G'&&str2[i]>='A'&&str2[i]<='G')){
					letter[index++] = str1[i];
				}else if(index==1&&((str1[i]>='A'&&str1[i]<='N'&&str2[i]>='A'&&str2[i]<='N')||
					str1[i]>='0'&&str1[i]<='9'&&str2[i]>='0'&&str2[i]<='9')){
					letter[index++] = str1[i];
				}
			}
		}
		int minute = 0;
		for(int i=0,j=0;i<str3.length()&&j<str4.length();i++,j++){
			if(str3[i]==str4[j]){
				if((str3[i]>='A'&&str3[i]<='Z'&&str4[i]>='A'&&str4[i]<='Z')
					||(str3[i]>='a'&&str3[i]<='z'&&str4[i]>='a'&&str4[i]<='z')){
					minute = i;
				}
			}
		}
		int hours = (letter[1]>='A'?(letter[1]-'A'+10):(letter[1]-'0'));
		printf("%s %02d:%02d\n",week[letter[0]-'A'].c_str(),hours,minute);
	}
	return 0;
} 

在這裡插入圖片描述

總結

  我只能說這道題很坑,題目沒有告訴,或者保證資料的合法性,那麼就要考慮資料是否是有問題的,可是測試點不過,我們可能第一反應還是自己哪裡出bug了,結果時間花在錯誤的方向上,很可惜。