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

题目要求从前两个字符串中找出最早出现的公共字符,第一个公共字符为A~G,代表星期一到星期日;第二个为0~9、A~N代表0~23小时。

在后两个字符串中找出第一个公共字母的位置,作为分钟。

代码如下:

#include <iostream>
#include <stdio.h>
#include <map>
#include <string>

using namespace std;

char* weeks[] = {"MON","TUE","WED","THU","FRI","SAT","SUN"};

int char2hour(char c){
    if(c >= '0' && c <= '9'){
        return c - '0';
    }else{
        return c - 'A' + 10;
    }

}

int char2week(char c){
    return c - 'A';
}

int main()
{
    string str1,str2,str3,str4;
    cin >> str1 >> str2 >> str3 >> str4;
    int dataCnt = 0;
    int len1 = min(str1.length(),str2.length());
    for(int i = 0; i < len1; i++){
        char c1 = str1[i];
        char c2 = str2[i];
        if(c1 == c2){
            if(dataCnt == 0){ // find week. A ~ G
                if(c1 >= 'A' && c1 <= 'G'){
                    printf("%s ",weeks[char2week(c1)]);
                    dataCnt++;
                }
            }else{ // find hour
                if((c1 >= '0' && c1 <= '9') || (c1 >= 'A' && c1 <= 'N')){
                    printf("%02d:",char2hour(c1));
                    break;
                }
            }
        }
    }
    int len2 = min(str3.length(),str4.length());
    for(int i = 0; i < len2; i++){
        char c1 = str3[i];
        char c2 = str4[i];
        if(c1 == c2 && isalpha(c1)){
            printf("%02d
",i);
            break;
        }
    }
    return 0;
}


原文地址:https://www.cnblogs.com/aiwz/p/6154101.html