C language is used to extract substrings and determine the maximum length of symmetric substrings

  • 2020-04-02 03:14:44
  • OfStack

Let's start with a summary of the basic method of extracting substrings from strings using the C language:


#include <stdio.h>



int StrLenU(const char* string)
{
   int len = 0 ;
   const char* p = string;
   while(*p++ != '0')
   {
     if(*p > 0x80 || *p < 0)
     {
      p++;
     }
     len++;
   }
   return len;
}

char* StrSetPosU(const char* string,int pos)
{
   char* result;
   result = string;
   while (result != NULL && *result != '0' && pos > 1)
   {
     if(*result > 0x80 || *result < 0)
     {
       result++;
     }
     result++;
     pos--;
   }
   if(pos!=0)
     return result;
   return '0';
}

int StrLenMemU(const char* string,int size)
{  
   int len = 0 ;
   const char* p = string;
   while(*p++ != '0' && size > 0)
   {
     if(*p > 0x80 || *p < 0)
     {
      p++;
      size--; 
     }
     size-- ;
     len++;
   }
   return len;
}

char* StringSubU(const char* string,int start,int number)
{
   int len = StrLenU(string) ;
   if(start>len)
   {
     printf("Start %d is too big than string length %d!n",start,len);
     return NULL;
   }
   int bufsize = 0;
   int num = number;
   const char* p = string;
   const char* start_char =string;
   
   p = StrSetPosU(string,start);
   start_char = p;
   
   if(number < 0)
   {
     while(*p != '0')
     {
      p++;
      bufsize++;
     }
   }
   else
   {
     while(1)
     {
      
      if(*p == '0' && num > 0)
      {
        printf("Number : %d is to big!n",number);
        break;
      }
      
      else if(num ==0 )
        break;
      
      if(*p > 0x80 || *p < 0)
      {
        bufsize++;
        p++;
      }
      bufsize++;
      p++;
      num--;
     }
   }
   num = bufsize;
   
   char* result ;
   result = (char*)malloc(sizeof(char)*(bufsize+1));
   memset(result,0,sizeof(char)*(bufsize+1));
   
   int i = 0;
   int j = 0;
   while(num != 0)
   {
     result[i++] = start_char[j++];
     num--;
   }
   
   result[bufsize] = '0';
   return result;
}

int main()
{
   
   char* t = "a Ha ha aab and c ha ";
   printf("length: %dn",StrLenU(" Ha ha a ha a ha "));
   printf(" Point to the former %sn After pointing to :%sn",t,StrSetPosU(t,3));
   printf(" Number of characters when full characters :%dn",StrLenMemU(t,6));
   printf(" Number of characters for half a character :%dn",StrLenMemU(t,4)); 
   printf("1. The normal values :%sn",StringSubU("a ha aa ha a",1,2));
   printf("2. Negative values :%sn",StringSubU("a ha aa ha a",-1,2));
   printf("3. Too big a start :%sn",StringSubU("a ha aa ha a",7,2));
   printf("4. High value :%sn",StringSubU("a ha aa ha a",5,3));
   printf("5. Negative values take all :%sn",StringSubU("a ha aa ha a",4,-1));

   return 0;
}

Method to determine the maximum length of a symmetric substring

Palindrome judgment
First override a method to judge the text string, using a pointer, not an array


  #include <stdio.h> 
  #include <stdlib.h> 
  #include <string.h> 
    
  void isSymmetrical(char *str) 
  { 
    char *begin, *end; 
    int flag, len = strlen(str); 
    
    for (begin = str, end = str + len - 1, flag = 1; begin <= end; begin ++, end --) { 
      if (*begin != *end) { 
        flag = 0; 
        break; 
      } 
    } 
    
    if (flag) 
      printf("Yes!n"); 
    else 
      printf("No!n"); 
  } 
    
    
  int main(void) 
  { 
    char str[1001]; 
    
    while (gets(str)) { 
      isSymmetrical(str); 
    } 
    
    return 0; 
  } 
    
  
/ * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
              Problem: 1192
              User: wangzhengyi
              Language: C
              Result: Accepted
              Time: 10 ms
              Memory: 912 KB
      * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * /  


Judge the return substring
To determine whether a substring is palindrome, consider comparing it from the inside out. For example, if the string "Google" is symmetric, we only need to move a bit to the left and a bit to the right to determine whether the next string is symmetric
One thing to note is that there are two cases for each character in the original string:

      The symmetric distribution centered on this character is that the return substring is odd
      A symmetric distribution centered on the character and the preceding character, that is, the return string is even


Time complexity analysis:

The outer layer needs an n-1 loop, and the inner layer traverses from the middle to both sides for each character, which is n, so the total time complexity is O (n * n).

The title

      Title description:  
      Enter a string and output the maximum length of the symmetric substring in that string.  
      For example, the input string "Google", because the longest symmetric substring in the string is "goog", outputs 4.  
      Input:  
      There are multiple sets of data, each set of data line string, the length is not greater than 100.  
      Output:  
      The maximum length of the output return text substring.  
      Sample input:  
      Google  
      Sample output:  
      4  


Ac code

 


  #include <stdio.h> 
  #include <string.h> 
  #include <stdlib.h> 
    
   
  void maxSymmetricalSubstring(char *str) 
  { 
    int maxlength, len; 
    char *pre, *next, *current; 
      
    current = str + 1; 
    maxlength = 0; 
    
    while (*current != '0') { 
      pre = current - 1; 
      next = current + 1; 
    
      while (pre >= str && *next != '0' && *pre == *next) { 
        pre --; 
        next ++;   
      } 
    
      len = (next - 1) - (pre + 1) + 1; 
        
      if (len > maxlength) { 
        maxlength = len; 
      } 
    
      pre = current - 1; 
      next = current; 
    
      while (pre >= str && *next != '0' && *pre == *next) { 
        pre --; 
        next ++; 
      } 
      len = (next - 1) - (pre + 1) + 1; 
    
      if (len > maxlength) { 
        maxlength = len; 
      } 
    
      current ++; 
    } 
      
    printf("%dn", maxlength); 
  }   
    
  int main(void) 
  { 
    char str[101]; 
    
    while (gets(str)) { 
      maxSymmetricalSubstring(str); 
    } 
    
    return 0; 
  } 

      / * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
              Problem: 1252
              User: wangzhengyi
              Language: C
              Result: Accepted
              Time: 0 ms
              Memory: 912 KB
      * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * /  


Related articles: