zoukankan      html  css  js  c++  java
  • C 找出最长的回文子串(不区分大小写)

    #include <stdio.h>
     #include <stdlib.h>
     #include <string.h>
     
     #define CH "%c"
     #define DG "%d"
     
     //求最长的回文字串 判断回文不算符号
     //但是最后输出的时候需要计算符号
     //样例输入
     //She say:Madam,I'm Adam.
     
     //样例输出
     //Madam,I'm Adam
     
     int isHui(char str[],int start, int endi);
     int isLetter(char ch);
     int charEqual(char ch1,char ch2);
     
     int main()
     {   int maxLength=0;
         int maxStart=0;
     
         //char str[]="abbdcd:aa?dcbcdc";
         char str[]="She say:Madam,I'm Adam";
         int i=0;
         //求最长的符合某条件的字串
         //首先应得到所有子串
         //从第一个字母开始的子串有ab  abb  abbd ..
         //从第二个字母开始的子串有bb  bbd bbda...
         for(i=0; i<strlen(str); i++)
         {
             int j=i;
             for(; j<strlen(str); j++)
             {
                 int rs=isHui(str,i,j);
                 if(rs){
                    int length=j-i+1;
                    //printf(" length %d\n",length);
                    if(length > maxLength){
                         printf("-----------max start i %d\n",i);
                         maxLength=length;//记录下长度和开始位置
                         maxStart=i;
                    }
     
                 }
             }
         }
     
         //打印该语句
         i=maxStart;
         printf("-----------\n");
         //printf(DG,maxLength);
         //int countChar=0;
         while(i<maxLength+maxStart){
             printf("%c",str[i]);
             i++;
         }
         //printf(DG,isHui(str,4,11));
         //printf(DG,charEqual('a','a'));
     }
     
     /**
     判断指定字串区域是不是回文
     */
     int isHui(char str[],int start,int ends)
     {
         //printf(DG,start);
         //printf(DG,ends);
         int yes=1;
         int i=start;
         int j=ends;
         for(i=start;i<=ends;i++){
             printf(CH,str[i]);
         }
         i=start;
         while( i<=j )
         {
             if(isLetter(str[i])&&isLetter(str[j]))
             {
                // printf("str %d=%c",i,str[i]);
                 //printf("str %d=%c",j,str[j]);
                 if(!charEqual(str[i],str[j]))
                 //if(str[i]!=str[j])
                 {
                     yes=0;
                     break;
                 }
                 i++;
                 j--;
     
             }
             else if(isLetter(str[i])==0 &&isLetter(str[j])==1){
                 i++;
             }else if(isLetter(str[i])==1 && isLetter(str[j])==0){
                 j--;
             }else if(isLetter(str[i])==0 && isLetter(str[j])==0){
                 i++;
                 j--;
             }
         }
         if(yes){
             printf("    hui\n");
         }else {
             printf(" \n");
         }
         return yes;
     
     }
     
     int charEqual(char ch1,char ch2){
         int yes=0;
         //先前已经判断了ch1 ch2都是字母
         //不需要当都是大写 都是小写 一个大写 一个小写 这么复杂
         if(ch1==ch2){
             yes=1;
         }else if((ch1-ch2)==32|| (ch2-ch1)==32  ){
             yes=1;
         }else {}
         return yes;
     
     
     }
     
     int isLetter(char ch)
     {
         if((ch>='A'&& ch<='Z')|| (ch>='a'&& ch<='z')  )
         {
             return 1;
     
         }
         else
         {
             return 0;
         }
     }
  • 相关阅读:
    Good Substrings CodeForces
    Watto and Mechanism Codeforces Round #291 (Div. 2)
    Codeforces Round #487 (Div. 2) A Mist of Florescence (暴力构造)
    Oulipo HDU
    POJ
    求值2 组合数公式题目
    URAL
    SCU
    【转】phpcms授课学习
    WordPress文章浏览历史插件
  • 原文地址:https://www.cnblogs.com/cart55free99/p/2966102.html
Copyright © 2011-2022 走看看