bol_in 2021-12-17 19:59 采纳率: 64.6%
浏览 46
已结题

Python 回文子字串問題


求該題目程式碼

回文子字串(Palindromic Substring):
輸入一個字串(不得有空格),找出所有不重複的回文子字串,且依字典順序排序後,將這些回文子字串以”#”相隔印出。

範例
==============
Sample input1:
abbabcc

Sample output1:
a#abba#b#bab#bb#c#cc
==============
Sample input2:
a
Sample output2:
a
==============
Sample input3:
wandnaVision

Sample output3:
V#a#andna#d#i#isi#n#ndn#o#s#w
==============
Sample input4:
89811086

Sample output4:
0#1#11#6#8#898#9

==============
Sample input5:
2020XX0221hahah

Sample output5:
0#020#0XX0#1#2#202#20XX02#22#X#XX#a#aha#h#hah#hahah
  • 写回答

1条回答 默认 最新

  • chuifengde 2021-12-17 20:33
    关注
    s = input(">>>")
    result = []
    
    for i in range(1, len(s) + 1):    
        for j in range(len(s)):
            if j + i > len(s):
                break
            t = s[j:j+i]
            if t == t[::-1]:
                result.append(t)
    
    print('#'.join(sorted(set(result))))
    '''
    --result
    >>>abbabcc
    a#abba#b#bab#bb#c#cc
    '''
    
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

问题事件

  • 已结题 (查看结题原因) 12月20日
  • 已采纳回答 12月20日
  • 创建了问题 12月17日

悬赏问题

  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?
  • ¥15 c++头文件不能识别CDialog