Love丶FFC's Blog

PAT乙级:1014 福尔摩斯的约会

2020-02-05 07:43:15
阅读:704   •   评论:11
标签:,

大侦探福尔摩斯接到一张奇怪的字条:我们约会吧! 3485djDkxh4hhGE 2984akDfkkkkggEdsb s&hgsfdk d&Hyscvnm。大侦探很快就明白了,字条上奇怪的乱码实际上就是约会的时间星期四 14:04,因为前面两字符串中第 1 对相同的大写英文字母(大小写有区分)是第 4 个字母 D,代表星期四;第 2 对相同的字符是 E ,那是第 5 个英文字母,代表一天里的第 14 个钟头(于是一天的 0 点到 23 点由数字 0 到 9、以及大写字母 A到 N 表示);后面两字符串第 1 对相同的英文字母 s 出现在第 4 个位置(从 0 开始计数)上,代表第 4 分钟。现给定两对字符串,请帮助福尔摩斯解码得到约会的时间。

输入格式:

输入在 4 行中分别给出 4 个非空、不包含空格、且长度不超过 60 的字符串。

输出格式:

在一行中输出约会的时间,格式为 DAY HH:MM,其中 DAY 是某星期的 3 字符缩写,即 MON 表示星期一,TUE 表示星期二,WED 表示星期三,THU 表示星期四,FRI 表示星期五,SAT 表示星期六,SUN 表示星期日。题目输入保证每个测试存在唯一解。

输入样例:

3485djDkxh4hhGE 
2984akDfkkkkggEdsb 
s&hgsfdk 
d&Hyscvnm

输出样例:

THU 14:04

编程语言:Python

解题思想:

1.利用字典实现A~G与Day的对应

2.先判断Day,再判断Hour

3.注意Hour和Min在小于10的情况下需要补足0,例如0时6分需要补足为00:06

4.Hour=列表索引的值

可进行的优化:字符匹配不用从头开始,可以从第一个符合条件的位置开始

时间复杂度:O(n)

代码如下:

  1. String = list()
  2. for i in range(4):
  3. String.append(input())
  4.  
  5. DayLetter = {'A': "MON", 'B': "TUE", 'C': "WED", 'D': "THU", 'E': "FRI", 'F': "SAT", 'G': "SUN"}
  6. HourLetter = ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K',
  7. 'L', 'M', 'N']
  8.  
  9. # 进行前两对字符串匹配
  10. MatchIndex = 0 # 用于记录满足条件的索引
  11. for i in range(min(len(String[0]), len(String[1]))):
  12. if String[0][i] == String[1][i] and String[0][i] in DayLetter:
  13. Day = String[0][i] # 第一个相同的大写字母
  14. MatchIndex = i + 1 # 判断Hour时在此基础上向前一位
  15. break
  16. for j in range(MatchIndex, min(len(String[0]), len(String[1]))):
  17. if String[0][j] == String[1][j] and String[0][j] in HourLetter:
  18. HH = String[0][j] # 第二个相同的字符
  19. break
  20.  
  21. # 进行后两对字符串匹配
  22. for i in range(min(len(String[2]), len(String[3]))):
  23. if String[2][i] == String[3][i] and String[2][i].isalpha():
  24. MM = i # 第一个相同的字母
  25. break
  26.  
  27. Day = DayLetter[Day]
  28. HH = HourLetter.index(HH)
  29. if HH < 10:
  30. HH = '0' + str(HH)
  31. if MM < 10:
  32. MM = '0' + str(MM)
  33. print(str(Day) + ' ' + str(HH) + ':' + str(MM))

评论板

共有 11 条评论

  1. cheap louis vuitton bags

    I just wanted to thank you for the fast service. as well as they look great. I received them a day earlier than expected. for instance the I will definitely continue to buy from this site. you decide I will recommend this site to my friends. Thanks!
    cheap louis vuitton bags https://www.louisvuittonsoutletstore.com/

  2. cheap louis vuitton outlet

    I just wanted to thank you for the fast service. actually they look great. I received them a day earlier than expected. as good as the I will definitely continue to buy from this site. you decide I will recommend this site to my friends. Thanks!
    cheap louis vuitton outlet https://www.bestlouisvuittonoutlet.com/

  3. cheap louis vuitton outlet

    I just wanted to thank you for the fast service. or it may be they look great. I received them a day earlier than expected. particularly the I will definitely continue to buy from this site. situation I will recommend this site to my friends. Thanks!
    cheap louis vuitton outlet https://www.cheapreallouisvuitton.com/

  4. Jorcift

    3 Lactated Ringer s LR Still a physiologically sensible choice cheap cialis online pharmacy Goldhirsch, consulting and lecture fees from GlaxoSmithKline, Novartis, AstraZeneca, Roche, and Schering Plough

  5. jordans for cheap

    I just wanted to thank you for the fast service. or even a they look great. I received them a day earlier than expected. much like the I will definitely continue to buy from this site. situation I will recommend this site to my friends. Thanks!
    jordans for cheap https://www.realjordansretro.com/

  6. cheap jordans online

    I just wanted to thank you for the fast service. or even they look great. I received them a day earlier than expected. most notably I will definitely continue to buy from this site. you ultimately choose I will recommend this site to my friends. Thanks!
    cheap jordans online https://www.cheaprealjordan.com/

  7. cheap real jordans

    Read reviews and was a little hesitant since I had already inputted my order. or else but thank god, I had no issues. such as the received item in a timely matter, they are in new condition. no matter what so happy I made the purchase. Will be definitely be purchasing again.
    cheap real jordans https://www.authenticcheapjordans.com/

  8. cheap jordans online

    Read reviews and was a little hesitant since I had already inputted my order. or possibly but thank god, I had no issues. prefer received item in a timely matter, they are in new condition. regardless so happy I made the purchase. Will be definitely be purchasing again.
    cheap jordans online https://www.realcheapretrojordanshoes.com/

  9. authentic cheap jordans

    Read reviews and was a little hesitant since I had already inputted my order. as well but thank god, I had no issues. for example the received item in a timely matter, they are in new condition. in any event so happy I made the purchase. Will be definitely be purchasing again.
    authentic cheap jordans https://www.realjordansshoes.com/

  10. intuido

    priligy ebay Diets low in fruits particularly oranges, apples, and grapefruit and cruciferous vegetables broccoli, cabbage, and brussel sprouts may result in a relative deficiency of calcium D glucarate and its metabolites

--------查看该分类下最新文章--------
^
新版博客正在完善中!域名:http://www.loveffc:8080,点击跳转,完全移植后将去除端口号。

Copyright © 2018 - 2021 FFC的小站 - 滇 ICP 备 18010780 号 - 1

- Powered by WordPress & AliYun · Theme by FFC -

- Environment by Windows & XAMPP · Designed by WebStorm & VSCode -

已运行:

访问量:492742