字符串 manacher 最长回文子串
2016-09-28 13:02
169 查看
学习一下:
这个感觉好理解一些→戳这里
这个代码比较好→戳这里
题目:hdu3068
最终id 的值为 找到的最大回文的中间值(扩展后的坐标,即 还没 除2)
maxlen-1为 最大回文长度
1.如果要求,回文长度最长,且下标最后?那就是下面的代码
2.如果要求,回文长度最长,且要首次出现的?那就把串预处理,倒转一下 函数: reverse( str.begin(),str.end() );
3.如果要求,回文长度最长,输出最长回文? 知道中间值和长度,返回到原来的数组中,就可以输出。
4.如果要求,回文长度最长,输出最长回文,且首字母最小? 做个标记。在替换最大值时,比较首字母,并记录id
这个感觉好理解一些→戳这里
这个代码比较好→戳这里
题目:hdu3068
最终id 的值为 找到的最大回文的中间值(扩展后的坐标,即 还没 除2)
maxlen-1为 最大回文长度
1.如果要求,回文长度最长,且下标最后?那就是下面的代码
2.如果要求,回文长度最长,且要首次出现的?那就把串预处理,倒转一下 函数: reverse( str.begin(),str.end() );
3.如果要求,回文长度最长,输出最长回文? 知道中间值和长度,返回到原来的数组中,就可以输出。
4.如果要求,回文长度最长,输出最长回文,且首字母最小? 做个标记。在替换最大值时,比较首字母,并记录id
#include <iostream> #include <cstdio> #include <algorithm> #include <math.h> #include <cstring> using namespace std; const int maxn=110010; char str[maxn*2]; int p[maxn*2]; int main() { while(~scanf("%s",str)){ int len=strlen(str); int id=0,maxlen=0; for(int i=len;i>=0;--i){ str[i+i+2]=str[i]; str[i+i+1]='#'; } str[0]='*'; memset(p,0,sizeof(p)); for(int i=2;i<2*len+1;++i){ if(p[id]+id>i) p[i]=min(p[2*id-i],p[id]+id-i); else p[i]=1; //向左右展开匹配 while(str[i-p[i]] == str[i+p[i]]) ++p[i]; if(id+p[id]<i+p[i]) id=i; //最大长度存入maxlen if(maxlen<p[i]) maxlen=p[i]; } printf("%d\n",maxlen-1); } return 0; }
相关文章推荐
- Manacher's ALGORITHM: O(n)时间求字符串的最长回文子串
- Manacher's ALGORITHM: O(n)时间求字符串的最长回文子串
- Manacher's ALGORITHM: O(n)时间求字符串的最长回文子串
- Manacher's ALGORITHM: O(n)时间求字符串的最长回文子串
- Manacher算法求字符串的最长回文子串
- Manacher's ALGORITHM: O(n)时间求字符串的最长回文子串
- Manacher's ALGORITHM: O(n)时间求字符串的最长回文子串
- Manacher's ALGORITHM: O(n)时间求字符串的最长回文子串
- Manacher's ALGORITHM: O(n)时间求字符串的最长回文子串
- 求解字符串的最长回文子串的Manacher’s Algorithm
- 串子串Manacher算法: O(n)时间求字符串的最长回文子串
- Manacher's ALGORITHM: O(n)时间求字符串的最长回文子串
- Manacher's ALGORITHM: O(n)时间求字符串的最长回文子串
- Manacher's ALGORITHM: O(n)时间求字符串的最长回文子串
- Manacher's ALGORITHM: O(n)时间求字符串的最长回文子串
- 数组字符串那些经典算法:最大子序列和,最长递增子序列,最长公共子串,最长公共子序列,字符串编辑距离,最长不重复子串,最长回文子串
- 字符串之最长回文子串 manacher算法
- manacher(马拉车)求最长回文子串长度
- 详解最长回文子串Manacher
- 最长回文子串——Manacher 算法