回文串判断与查找

来源:互联网 发布:生命数组 编辑:程序博客网 时间:2024/05/26 05:53

1> 判断一个字符串是否为回文串?  

例如 :"abcdcba"

 
 直接在字符串头部和尾部放置两个指针(p,q),两个指针分别向中间移动,若遇见不想等的情况直接return false;循环条件设置为p<q;
 

2> 不区分大小写和其余字符判断一串字符串是否为回文字符串

例如:"A man, a plan, a canal, Panama"

该字符串就是一个回文的字符串,除去逗号和空格,忽略字母的大小写!!!

/*======================================================    > File Name: enhancedPalin.c    > Author: panlu    > E-mail:      > Other : 加强版回文串的判断     > Created Time: 2016年02月27日 星期六 22时59分42秒 =======================================================*/#include<stdio.h>#include<string.h>#include<ctype.h>//判断字符是否为字母和数字,int isLetNum(char c){    if((c > '0'&& c < '9') || (c > 'A'&&c < 'Z')){        return 1;    }    return 0;}int isPalindrome(char *s){    int i,j = 0;    //将字符串中所有的字母都转换为小写的字母,方便统一比较    for(; i < sizeof(s); i++){        s[i] = tolower(s[i]);    }    for(i=0,j=sizeof(s)-1; i<j; i++,j++){        //如果在遍历的过程中遇到除字母和数字外的其余字符直接跳过        while((i < j) && !(isLetNum(s[i]))){            i++;        }        while((i < j) && !(isLetNum(s[j]))){            j--;        }        if(s[i]!=s[j]){            return 0;        }    }    return 1;}int main(){    int result;    char s1[] = "A man, a plan, a canal, Panama";    if(isPalindrome(s1)){        printf("是回文串!\n");    }else{        printf("不是回文串!\n");    }    return 0;}

3> 回文数字的判断

例如:9867689

应该循环取从后面取一个数和从前面取一个数进行比较,直到数字的中间位置。

代码还没写呢^_^!

4> 一个字符串中最长的回文子串的查询

例如:"abcdeflmnnmlopehdnsgu"    这个字符串里面最长的回文子串就是lmnnml了。

使用暴力方法解决的话就是查找出所有的子串,然后判断每个子串是否为回文串。

但是可以这样,遍历一遍字符串,假设每个字符都将会是回文子串的中间字符,这样向两边延伸,找到一个回文子串,然后找到最长的那一个!!!

/*======================================================    > File Name: lps.c    > Author: panlu    > E-mail:      > Other : 最长回文子串,一个字符串中拥有许多子串,如何获取最长的一个回文子串     > Created Time: 2016年02月27日 星期六 23时46分19秒 =======================================================*/#include<stdio.h>#include<string.h>//传进去的后两个参数是为了返回最长回文子串的起始位置和长度void longestPalindromicSub(char *s,int *mystart,int *mylen){    int i = 0;    int maxLeft = 0;    int maxRight = 0;    int max = 1;    int n = strlen(s);    int start,end,len,left,right;    for(i = 0;i < n; i++){    //假设最长子串为偶数个的情况        start = i;        end = i+1;        len = 0;        left = start;        right = end;        while(start >= 0 && end < n){            if(s[start] == s[end]){                len = len+2;                left = start;                right = end;                start--;         //向两边延伸了                end++;            }else{                break;            }        }        if(max < len){            maxLeft = left;            maxRight = right;            max = len;        }        //假设最长子串为奇数个的情况        start = i-1;        end = i+1;        len = 1;        left = start;        right = end;        while(start >= 0 && end < n){            if(s[start] == s[end]){                len = len+2;                left = start;                right = end;                start--;                end++;            }else{                break;            }        }        if(max < len){            maxLeft = left;            maxRight = right;            max = len;        }    }    *mystart = maxLeft;    *mylen = max;}int main(){    char s1[] = "abcdeflmnnmlopehdnsgu";    char s2[100];    int mystart,mylen = 0;    longestPalindromicSub(s1,&mystart,&mylen);    printf("%d  %d\n",mystart,mylen);    strncpy(s2,&s1[mystart],mylen);    printf("%s\n",s2);    return 0;}
0 0
原创粉丝点击