字符串中查找最长回文子串完整代码
2013-08-20 08:29
363 查看
参考原文:http://blog.csdn.net/yzl_rex/article/details/7908259
还有一种方法简单易懂,就是时间消耗更大,需要O(n*n)
#include <stdio.h>
#include <string.h>
#include <malloc.h>
char* huiwen(char *s)
{
if(s == NULL){
return NULL;
}
char *p;
p = (char *)malloc(100);
memset(p,0,100);
int i,j;
for(i=0;s[i] != '\0';i++){
p[i]=1;
if(s[i] != s[i+1]){
for(j=1;i>=j && s[i+j] != '\0' && s[i-j] == s[i+j] ;j++){
p[i]++;
}
}else{
p[i]=2;
for(j=1;i>=j && s[i+j+1] != '\0' && s[i-j] == s[i+j+1] ;j++){
p[i]++;
}
}
}
return p;
}
int findMax( char *s)
{
if(s == NULL){
return 0;
}
int max=0;
int i=0;
for(i=1;i<strlen(s);i++){
if(s[i]>s[max]){
max = i;
}
}
return max;
}
int main(void)
{
char *s;
s = (char *) malloc(100);
memset(s,0,100);
scanf("%s",s);
char *p = huiwen(s);
int i=0;
while(p[i] != '\0'){
printf("%d",p[i]);
i++;
}
int max = findMax(p);
//printf("%d\n", max);
//printf("%s\n", s);
if(s[max] != s[max+1]){
for(i= max-(p[max]-1); i<= max+(p[max]-1);i++)
printf("%c",s[i]);
}else{
for(i= max-p[max]+2;i<= max+1+(p[max]-1);i++){
printf("%c",s[i]);
}
}
printf("\n");
free(s);
free(p);
return 0;
}
#include <stdio.h> #include <string.h> #include <malloc.h> int Min(int a,int b) { return ((a-b)?b:a); } char* huiwen(char *s) { if(NULL == s){ return 0; } int id; int mx=0; int i=0; char *p; p = (char *)malloc(100); memset(p,0,sizeof(100)); for(i=0;i<strlen(s);i++){ if(mx>1){ p[i] = Min(mx-i,p[2*id-i]); }else{ p[i] = 1; } for(;s[i+p[i]]==s[i-p[i]];p[i]++); if(p[i]+i>mx){ mx = p[i]+i; id = i; } } return p; } int findMax( char *s) { if(s == NULL){ return 0; } int max=0; int i=0; for(i=1;i<strlen(s);i++){ if(s[i]>s[max]){ max = i; } } return max; } int main(void) { // char *s="1234321"; int i=0; char *s; s = (char *)malloc(50); memset(s,0,10); scanf("%s",s); char *l; l = (char *)malloc(100); memset(l,0,100); *(l+i++)='$'; char *d = s; while(*s != '\0'){ *(l+i++) = '#'; *(l+i++) = *s; s++; } *(l+i) = '#'; char *p = huiwen(l); if(p == NULL){ return -1; } printf("%s\n",l); for(i=0;p[i] != '\0';i++) printf("%d",p[i]); printf("\n"); int max = findMax(p); int id = max/2 - 1; if(l[max] !='#'){ for(i=id-(p[max]-1)/2;i<=id+(p[max]-1)/2;i++) printf("%c",d[i]); }else{ for(i=id-(p[max]-2)/2;i<=id+(p[max])/2;i++) printf("%c",d[i]); } printf("\n"); free(p); free(l); free(d); return 0; }
还有一种方法简单易懂,就是时间消耗更大,需要O(n*n)
#include <stdio.h>
#include <string.h>
#include <malloc.h>
char* huiwen(char *s)
{
if(s == NULL){
return NULL;
}
char *p;
p = (char *)malloc(100);
memset(p,0,100);
int i,j;
for(i=0;s[i] != '\0';i++){
p[i]=1;
if(s[i] != s[i+1]){
for(j=1;i>=j && s[i+j] != '\0' && s[i-j] == s[i+j] ;j++){
p[i]++;
}
}else{
p[i]=2;
for(j=1;i>=j && s[i+j+1] != '\0' && s[i-j] == s[i+j+1] ;j++){
p[i]++;
}
}
}
return p;
}
int findMax( char *s)
{
if(s == NULL){
return 0;
}
int max=0;
int i=0;
for(i=1;i<strlen(s);i++){
if(s[i]>s[max]){
max = i;
}
}
return max;
}
int main(void)
{
char *s;
s = (char *) malloc(100);
memset(s,0,100);
scanf("%s",s);
char *p = huiwen(s);
int i=0;
while(p[i] != '\0'){
printf("%d",p[i]);
i++;
}
int max = findMax(p);
//printf("%d\n", max);
//printf("%s\n", s);
if(s[max] != s[max+1]){
for(i= max-(p[max]-1); i<= max+(p[max]-1);i++)
printf("%c",s[i]);
}else{
for(i= max-p[max]+2;i<= max+1+(p[max]-1);i++){
printf("%c",s[i]);
}
}
printf("\n");
free(s);
free(p);
return 0;
}
相关文章推荐
- JAVA代码—算法基础:给定一个字符串查找最长回文子串
- 在字符串中查找最长重复子串的探讨
- 最长回文子串(Manacher算法模板题)&&对称字符串问题
- 华为OJ——查找两个字符串a,b中的最长公共子串
- hihoCoder - 1032 - 最长回文子串 (字符串~)
- leetcode 5. Longest Palindromic Substring 最长回文子串的查找 + 按照length做DP
- POJ3974 求字符串的最长回文子串的长度
- 【字符串】最长回文子串
- 查找字符串中最长的连续数字子串
- 数组字符串那些经典算法:最大子序列和,最长递增子序列,最长公共子串,最长公共子序列,字符串编辑距离,最长不重复子串,最长回文子串
- lc#5字符串的最长回文子串
- O(n)时间求字符串的最长回文子串
- 查找两个字符串a,b中的最长公共子串
- 华为OJ(查找两个字符串a,b中的最长公共子串)
- 查找字符串最长回文
- 第5题 查找字符串中的最长回文字符串---Manacher算法
- 【华为OJ】【081-查找两个字符串a,b中的最长公共子串】
- 华为OJ中级题-查找两个字符串a,b中的最长公共子串
- 最长回文子串的查找
- 华为机试---查找两个字符串a,b中的最长公共子串