The largest palindrome substring (ignoring punctuation and spaces)

Code

1  // minimum palindromic substring, ignoring punctuation and spaces 
2 #include <stdio.h>
 . 3 #include < String .h>
 . 4 #include <ctype.h>
 . 5  #define MAXN 5000 + 10
 . 6  
. 7  char buf [MAXN] , S [MAXN];
 . 8  int P [MAXN];
 . 9  int main ()
 10  {
 . 11      int n-, m = 0 ;
 12 is      fgets (buf, the sizeof (S), stdin);
 13 is      n-= strlen (buf);
 14      for ( int I = 0 ; I <n-; I ++ )
15      {
 16          IF (the isalpha (buf [I]))
 . 17          {
 18 is              P [m] = I;
 . 19              S [m ++] = toupper (buf [I]);
 20 is          }
 21 is      }
 22 is      the printf ( " % S \ n- " , S);
 23 is      
24      int max = 0 , X = 0 , Y = 0 ;
 25      
26 is  
27      // No optimization 
28      / * for (I = 0; I <m; I ++) // this approach requires a triple cycle, see if another manner following
 29      {
 30  
31 is         for(int j=i;j<m;j++)
32         {
33             int ok=1,k;
34             for(k=0;k<=(j-i)/2;k++)
35             {
36                 if(s[i+k]!=s[j-k])
37                 {
38                     ok=0;
39                     break;
40                 }
41             }
42             if(ok&&j-i+1>max)
43             {
44                 max=j-i+1;
45                 x=p[i];
46                 y=p[j];
47             }
48         }
 49      } * / 
50  
51 is      // optimization method 
52 is      for (i = 0 ; i <m; i ++)    // this i is in the center, just extension detector 2 cyclic manner to both sides, but two points parity the cycle 
53 is      {
 54 is          int K;
 55          for (K = 0 ; IK> = 0 && I + K <m; K ++) // palindromic sequence has an odd number, s [i] for the intermediate palindromic sequence 
56          {
 57              IF (S [IK] = S [K + I]!) BREAK ;         // IF (! S [IK] = S [K + I])
 58                                               // { 
59              IF ( 2 * K + . 1>max)                    //        if(2*(k-1)+1>max)
60             {                                //        {
61                 max=2*k+1;                   //            max=2*(k-1)+1;
62                 x=p[i-k];                    //            x=p[i-(k-1)];
63                 y=p[i+k];                    //            y=p[i+(k-1)];
64             }                                //        }
65                                              //        break;
66                                              //} If such a sentence written in a block, when the maximum palindromic sequence appears at the beginning or the end, since the loop is first determined for the ik <0 or K + I> m, 
67          }                                     //      causing circulation loop for direct end, can not enter if (2 * (k-1 ) +1> max) statement block, so that the max, x, y update fails, error 
68          
69          for (K = 0 ; IK> = 0 && I + K + . 1 <m; K ++) / / palindromic sequence has an even number, s [i] for the center-left palindromic sequence 
70          {
 71 is              IF (S [IK] S = [I + K +! . 1 ]) BREAK ;         // IF (S [IK]! S = [I + K +. 1])
 72                                                 // { 
73 is              IF ( 2 * K + 2 > max)                       //        IF (2 * (K-. 1) +2> max) 
74              {                                 //         { 
75                  max = 2 * K + 2 ;                      //             max = 2 * (K-. 1) +2; 
76                  X = P [IK];                     / /             X = P [I- (. 1-K)]; 
77                  Y = P [I + K + . 1 ];                     //             Y = P [I + (. 1-K) + 1'd]; 
78              }                                   //         }
 79                                                 //         BREAK ;
 80                                                 // } wrong for the same reason. 
81         }
 82      }
 83  
84  
85  
86      the printf ( " longest palindromic substring (character length only):% D \ n- " , max);
 87      for (I = X; I <= Y; I ++ ) the putchar (buf [I ]);
 88      the putchar ( ' \ n- ' );
 89      return  0 ;
 90 }

 

 

Guess you like

Origin www.cnblogs.com/bboykaku/p/12629867.html