简体   繁体   English

如何在python中找到最长的回文?

[英]How to find the longest palindrome in python?

As we all know, a palindrome is a word that equals its reverse.众所周知,回文是一个等于它的反向的词。 Here are some examples of palindromes: malayalam , gag , appa , amma .以下是回文的一些示例: malayalamgagappaamma

We consider any sequence consisting of the letters of the English alphabet to be a word.我们认为任何由英文字母组成的序列都是一个词。 So axxb,abbba and bbbccddx are words for our purpose.所以 axxb、abbba 和 bbbccddx 是我们的目的词。 And aaabbaaa, abbba and bbb are examples of palindromes. aaabbaaa、abbba 和 bbb 是回文的例子。

By a subword of a word, we mean a contiguous subsequence of the word.词的子词是指词的连续子序列。 For example the subwords of the word abbba are a, b, ab, bb, ba, abb, bbb, bba, abbb, bbba and abbba.例如单词abbba的子词是a、b、ab、bb、ba、abb、bbb、bba、abbb、bbba和abbba。

In this task you will given a word and you must find the longest subword of this word that is also a palindrome.在此任务中,您将给出一个单词,并且您必须找到该单词中最长的也是回文的子单词。

For example if the given word is abbba then the answer is abbba.例如,如果给定的单词是 abbba,那么答案就是 abbba。 If the given word is abcbcabbacba then the answer is bcabbacb.如果给定的单词是 abcbcabbacba,那么答案是 bcabbacb。

Solution hint Any subword of w that is a palindrome is also a subword when w is reversed.解决方案提示 w 的任何回文子字在 w 反转时也是子字。

Input format The first line of the input contains a single integer N indicating the length of the word.输入格式 输入的第一行包含一个整数 N,表示单词的长度。 The following line contains a single word of length N, made up of the letters a,b,…, z.下面一行包含一个长度为 N 的单词,由字母 a、b、…、z 组成。

Output format The first line of the output must contain a single integer indicating the length of the longest subword of the given word that is a palindrome.输出格式输出的第一行必须包含一个整数,表示给定单词的最长子字的长度,即回文。 The second line must contain a subword that is a palindrome and which of maximum length.第二行必须包含一个子字,它是一个回文并且最大长度。 If there is more than one subword palindrome of maximum length, print the one that is lexicographically smallest (ie, smallest in dictionary order).如果最大长度的子词回文不止一个,则打印字典序最小的(即字典顺序最小的)。

Test Data: You may assume that 1 ≤ N ≤ 5000. You may further assume that in 30% of the inputs 1 ≤ N ≤ 300.测试数据:您可以假设 1 ≤ N ≤ 5000。您可以进一步假设在 30% 的输入中 1 ≤ N ≤ 300。

Example: We illustrate the input and output format using the above examples:示例:我们使用上面的示例来说明输入和输出格式:

Sample Input 1:
5
abbba
Sample Output 1:
5
abbba
Sample Input 2:
12
abcbcabbacba
Sample Output 2:
8
bcabbacb

I tried a code but it is not working!!我尝试了一个代码,但它不起作用!!

Help me with some other code please!请帮我一些其他的代码!

This is my code but it's not working!这是我的代码,但它不起作用!

n = int(input())
ar = []
bestvals = [] 
best_stored = [] 
for x in range(n): 
    ar.append(int(input())) 
    best_stored.append(0) 
    best_stored[0] = 1 
    for i in range(n): 
        maxval = 1 
        for j in range(i): 
            if ar[i] % ar[j] == 0:
                maxval = max(maxval,(best_stored[j])+1) 
        best_stored[i] = maxval
print(max(best_stored))
def longestPalindrome():
    n = int(input())
    s = input()
    temp = ""
    for i in range(len(s)):
        for j in range(len(s)-1,i-1,-1):
            if s[i] == s[j]:
                m = s[i:j+1]
                if m == m[::-1]:
                    if len(temp) <= len(m):
                        temp = m
    print(len(temp))
    print(temp)

longestPalindrome()   

this should work fine:这应该可以正常工作:

word = input('enter your word: ')
subwords = []
for i in range(len(word)):
    for j in range(len(word)+1):
        subwords.append(word[i:j])

palindromes = []
for subword in subwords:
    if subword == subword[::-1]:
        palindromes.append(subword)


print(len(max(palindromes, key=len)))
print(max(palindromes, key=len))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM