本文主要是介绍ural 1297. Palindrome dp,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
1297. Palindrome
Time limit: 1.0 second
Memory limit: 64 MB
Memory limit: 64 MB
The “U.S. Robots” HQ has just received a rather alarming anonymous letter. It states that the agent from the competing «Robots Unlimited» has infiltrated into “U.S. Robotics”. «U.S. Robots» security service would have already started an undercover operation to establish the agent’s identity, but, fortunately, the letter describes communication channel the agent uses. He will publish articles containing stolen data to the “Solaris” almanac. Obviously, he will obfuscate the data, so “Robots Unlimited” will have to use a special descrambler (“Robots Unlimited” part number NPRx8086, specifications are kept secret).
Having read the letter, the “U.S. Robots” president recalled having hired the “Robots Unlimited” ex-employee John Pupkin. President knows he can trust John, because John is still angry at being mistreated by “Robots Unlimited”. Unfortunately, he was fired just before his team has finished work on the NPRx8086 design.
So, the president has assigned the task of agent’s message interception to John. At first, John felt rather embarrassed, because revealing the hidden message isn’t any easier than finding a needle in a haystack. However, after he struggled the problem for a while, he remembered that the design of NPRx8086 was still incomplete. “Robots Unlimited” fired John when he was working on a specific module, the text direction detector. Nobody else could finish that module, so the descrambler will choose the text scanning direction at random. To ensure the correct descrambling of the message by NPRx8086, agent must encode the information in such a way that the resulting secret message reads the same both forwards and backwards.
In addition, it is reasonable to assume that the agent will be sending a very long message, so John has simply to find the longest message satisfying the mentioned property.
In addition, it is reasonable to assume that the agent will be sending a very long message, so John has simply to find the longest message satisfying the mentioned property.
Your task is to help John Pupkin by writing a program to find the secret message in the text of a given article. As NPRx8086 ignores white spaces and punctuation marks, John will remove them from the text before feeding it into the program.
Input
The input consists of a single line, which contains a string of Latin alphabet letters (no other characters will appear in the string). String length will not exceed 1000 characters.
Output
The longest substring with mentioned property. If there are several such strings you should output the first of them.
Sample
input | output |
---|---|
ThesampletextthatcouldbereadedthesameinbothordersArozaupalanalapuazorA | ArozaupalanalapuazorA |
Problem Author: Eugene Krokhalev
Problem Source: IX Open Collegiate Programming Contest of the High School Pupils (13.03.2004)
import math
class InputReader():def nextInt(self):return int(input().strip())def nextSplitInts(self):a = self.nextInts()return tuple(a)def nextLine(self):return input()def nextString(self):return input().strip()def nextInts(self):ints = []str = input().strip().split()for s in str:if (s is None) or (len(s) == 0):continueints.append(int(s))return intsdef nextFloats(self):floats = []str = input().strip().split()for s in str:if (s is None) or (len(s) == 0):continuefloats.append(float(s))return floatsif __name__ == '__main__':reader = InputReader()s = reader.nextString()n = len(s)dp = [[False]*n for i in range(n)]mxLen = 1idx = 0for i in range(n):dp[i][i] = Trueif i+1 < n and s[i] == s[i+1]:dp[i][i+1] = Trueif mxLen < 2 :mxLen = 2idx = ifor d in range(2 , n):for i in range(0 , n):j = i + dif j > n-1 :breakif s[i] == s[j] and (d == 2 or dp[i+1][j-1]):dp[i][j] = Trueif mxLen < d + 1:mxLen = d + 1idx = iprint(s[idx:idx+mxLen])
这篇关于ural 1297. Palindrome dp的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!