HackerRank - Play with words
2015-03-20 01:15
405 查看
Kinda similar with another palindrome DP from LeetCode. Feel it, it is a bottom-up DP - palindrome subsequence.
str = input() slen = len(str) dp = [[0 for x in range(slen)] for x in range(slen)] for clen in range(1, slen + 1): for i in range(0, slen - clen + 1): if clen == 1: dp[i][i] = 1 elif clen == 2: dp[i][i + 1] = 2 if str[i] == str[i+1] else 1 else: if str[i] == str[i + clen - 1]: dp[i][i + clen - 1] = dp[i + 1][i + clen - 2] + 2 else: dp[i][i + clen - 1] = max(dp[i + 1][i + clen - 2], dp[i + 1][i + clen - 1], dp[i][i + clen - 2]) #print (str[i:i+clen], dp[i][i + clen - 1]) ret = 1 for i in range(0, slen - 1): len1 = dp[0][i] len2 = dp[i + 1][slen - 1] ret = max(ret, len1 * len2) print (ret)
相关文章推荐
- HackerRank: Play with words
- 【Hacker Rank】01.Say "Hello, World!" With Python
- Bricks Game(HackerRank play-game)
- uva10129(play with words)
- HackerRank "Playing with numbers"
- HDOJ 3487 Play with Chain【splay】
- leetcode 30:Substring with Concatenation of All Words
- [Hackerrank]hourrank 19-What Are the Odds?
- HackerRank Week of Code 23——Enclosure(等周定理+余弦定理)
- hdu 1116 Play on Words(欧拉图判断模板)
- Leetcode: Substring with Concatenation of All Words
- Substring with Concatenation of All Words
- UVA 10129 Play on Words
- POJ 1386 Play on Words(有向欧拉通路 连通图)
- hackerrank Lucky Numbers(扩展gcd/规律)
- poj 1386 Play on Words
- 30. Substring with Concatenation of All Words
- UVA10673---Play With Floor And Ceil
- UVA 10129 Play on Words(欧拉道路)
- HDU-3487 Play with Chain (splay好题 带翻转 cut-link操作)