算法_Longest Palindromic Substring(寻找最长回文字串)
题目:Given a string S, find the longest palindromic substring in S. You may assume that the maximum length of S is 1000, and there exists one unique longest palindromic substring.
题解:首先需要清楚什么是“回文“(不知道这个翻译对不对?)字符串!回文字符串关于某一个字符对称,在左右两边与中心相同距离的字符相等。
那么还需要注意的是对称中心可以有多个相同的字符组成,如下图:
所以编程实现时可以先从对称中心入手,从字符串位置0开始作为对称中心依次验证。时间复杂度O(n2);
char* longestPalindrome(char* s) {
int len=strlen(s);
int begin=,maxlen=;
for(int i=;i<len;i++)
{
int left=i,right=i;
while(right<len- && s[right]==s[right+])right++;
while(left> && right<len- && s[left-]==s[right+]){left--;right++;}
if((right-left+)>maxlen)
{
maxlen=right-left+;
begin=left;
}
}
char* s_out=malloc((maxlen+)*sizeof(char));
for(int i=;i<maxlen;i++)
{
s_out[i]=s[begin++];
}
s_out[maxlen]='\0';
return s_out;
}
算法_Longest Palindromic Substring(寻找最长回文字串)的更多相关文章
- 面试常用算法——Longest Palindromic Substring(最长回文子串)
第一种: public static void main(String[] args) { String s = "abcbaaaaabcdcba"; int n,m; Strin ...
- POJ 3974 最长回文字串(manacher算法)
题意:给出一个字符串,求出最长回文字串. 思路:一开始我直接上了后缀数组DC3的解法,然后MLE了.看了DISCUSS发现还有一种计算回文字串更加优越的算法,就是manacher算法.就去学习了一下, ...
- 最长回文字串——manacher算法
时间复杂度:O(n) 参考:https://segmentfault.com/a/1190000003914228 1.问题定义 最长回文子串问题:给定一个字符串,求它的最长回文子串长度. 如果一个字 ...
- 求字符串的最长回文字串 O(n)
昨天参加了某公司的校园招聘的笔试题,做得惨不忍睹,其中就有这么一道算法设计题:求一个字符串的最长回文字串.我在ACM校队选拔赛上遇到过这道题,当时用的后缀数组AC的,但是模板忘了没写出代码来. 回头我 ...
- hihocoder 第一周 最长回文字串
题目1 : 最长回文子串 时间限制:1000ms 单点时限:1000ms 内存限制:64MB 描述 小Hi和小Ho是一对好朋友,出生在信息化社会的他们对编程产生了莫大的兴趣,他们约定好互相帮助,在编程 ...
- 21.Longest Palindromic Substring(最长回文子串)
Level: Medium 题目描述: Given a string s, find the longest palindromic substring in s. You may assume ...
- Hdu 3068 最长回文字串Manacher算法
题目链接 最长回文 Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total S ...
- 5. Longest Palindromic Substring[M]最长回文子串
题目 Given a string s, find the longest palindromic substring in s. You may assume that the maximum le ...
- 【LeetCode每天一题】Longest Palindromic Substring(最长回文字串)
Given a string s, find the longest palindromic substring in s. You may assume that the maximum lengt ...
随机推荐
- Combination Sum,Combination Sum II,Combination Sum III
39. Combination Sum Given a set of candidate numbers (C) and a target number (T), find all unique co ...
- C#中弹出新窗口
1.在主窗体程序中定义对应别的窗体的对象 Form_a_class form1 = Form_a_class test_delegate(); 2.调用显示 form1.ShowDialog();
- hdu5347 MZL's chemistry(打表)
转载请注明出处: http://www.cnblogs.com/fraud/ ——by fraud MZL's chemistry Time Limit: 2000/1000 MS ...
- Java中list<Object>集合去重实例
一:Java中list去重的方法很多,下面说一下其中一种方法:把list里的对象遍历一遍,用list.contain(),如果不存在就放入到另外一个list集合中: 二:实例 这里需要注意的是:使用c ...
- webview 上 postUrl 发送参数过程中数据丢失或错误 的问题
用到了android 的 webview 来展示页面.webview需要用post来传递参数.于是问题出现了,后台解析中发现参数错误. 之前有因为String 和byte[]转行时,数据丢失的问题,于 ...
- [FML]学习笔记二 PAC Learning Model
对于一个concept class C,如果存在一个算法A和一个多项式poly(.,.,.,.),有对于任意的ε>0.δ>0以及X的任意分布D和任何target concept C,当sa ...
- 使用grid++report打印选中行
接上一篇<hibernate+spring+mvc+Easyui框架模式下使用grid++report的总结>对grid++report做进一步开发 先写一下实现流程: 1.默认为全部载入 ...
- jsp跳转到servlet
web.xml中url-pattern的值必须和相关联的jsp页面form中的action的值一样,才会从jsp页面跳转到servlet.
- jstat
http://hi.baidu.com/savagert/item/6a056619d25bb6426926bb38
- Linux上的设备管理器
一般windows上我们用它自带的“设备管理器”来查看,管理,安装,卸载驱动. 那么问题来了,Linux上用什么命令来看呢? 可以用: lshw lsusb lspci lsmod 查看特定模块. ...