1061 Dating (20 分)字符串的处理

版权声明:假装这里有个版权声明…… https://blog.csdn.net/CV_Jason/article/details/85319249

题目

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了,结果时间花在错误的方向上,很可惜。

猜你喜欢

转载自blog.csdn.net/CV_Jason/article/details/85319249
今日推荐