博客
关于我
2021/04/17 OJ每日一题 1695: 问题H 对称串找最值 python
阅读量:773 次
发布时间:2019-03-22

本文共 931 字,大约阅读时间需要 3 分钟。

为了解决这个问题,我们需要从给定的字符串中找到对称字符串,并找出它们中ASCII码值最大的那一个。

方法思路

  • 读取输入:首先读取输入数据,获取字符串的数量和每个字符串。
  • 筛选对称字符串:检查每个字符串是否是对称的。如果是,对称字符串就会被收集到一个列表中。
  • 计算最大ASCII码:对于每个对称字符串,计算其中的最大ASCII码值。
  • 找出最大值:比较所有对称字符串的最大ASCII码值,找出最大的那个对应的字符串并输出。
  • 这种方法确保我们能够高效地筛选出对称字符串,并在它们中找到ASCII码值最大的那个字符串。

    解决代码

    n = int(input())symmetric_list = []for _ in range(n):    s = input().strip()    if s == s[::-1]:        current_max = max(ord(c) for c in s)        symmetric_list.append((current_max, s))if not symmetric_list:    print("")else:    max_value = max(t[0] for t in symmetric_list)    for pair in symmetric_list:        if pair[0] == max_value:            print(pair[1])            break

    代码解释

  • 读取输入:首先读取整数n,表示输入的字符串数量。然后读取接下来的n行,分别存储每个字符串。
  • 筛选对称字符串:使用循环检查每个字符串是否对称。检查方法是将字符串反转后与原字符串比较。如果相同,则该字符串是对称的。
  • 计算字符ASCII码:对于每个对称字符串,计算其中每个字符的ASCII码值,并找出最大值。
  • 迭代找出最大值:通过遍历对称字符串列表,找到最大的ASCII码值并输出对应的字符串。如果出现多个相同的最大值,第一个遇到的会被打印,但由于问题要求不详细说明这种情况,通常第一个遇到的即可。
  • 这个方法确保了处理过程的高效性和准确性,能够正确找到满足条件的字符串。

    转载地址:http://dlewk.baihongyu.com/

    你可能感兴趣的文章
    NN&DL4.7 Parameters vs Hyperparameters
    查看>>
    NN&DL4.8 What does this have to do with the brain?
    查看>>
    nnU-Net 终极指南
    查看>>
    No 'Access-Control-Allow-Origin' header is present on the requested resource.
    查看>>
    No 'Access-Control-Allow-Origin' header is present on the requested resource.
    查看>>
    NO 157 去掉禅道访问地址中的zentao
    查看>>
    no available service ‘default‘ found, please make sure registry config corre seata
    查看>>
    No compiler is provided in this environment. Perhaps you are running on a JRE rather than a JDK?
    查看>>
    no connection could be made because the target machine actively refused it.问题解决
    查看>>
    No Datastore Session bound to thread, and configuration does not allow creation of non-transactional
    查看>>
    No fallbackFactory instance of type class com.ruoyi---SpringCloud Alibaba_若依微服务框架改造---工作笔记005
    查看>>
    No Feign Client for loadBalancing defined. Did you forget to include spring-cloud-starter-loadbalanc
    查看>>
    No mapping found for HTTP request with URI [/...] in DispatcherServlet with name ...的解决方法
    查看>>
    No mapping found for HTTP request with URI [/logout.do] in DispatcherServlet with name 'springmvc'
    查看>>
    No module named 'crispy_forms'等使用pycharm开发
    查看>>
    No module named 'pandads'
    查看>>
    No module named cv2
    查看>>
    No module named tensorboard.main在安装tensorboardX的时候遇到的问题
    查看>>
    No module named ‘MySQLdb‘错误解决No module named ‘MySQLdb‘错误解决
    查看>>
    No new migrations found. Your system is up-to-date.
    查看>>