对策字符串的最大长度

来源:互联网 发布:网络语言橘子什么意思 编辑:程序博客网 时间:2024/04/30 22:41

73.对策字符串的最大长度。 题目:输入一个字符串,输出该字符串中对称的子字符串的最大长度。比如输入字符串“google”,由于该字符串里最长的对称子字符串是“goog”,因此输出4。


void main()
{
 char str[]="google";
 int len = strlen(str);
 
 int i,j,m,n,k,tmp,temp,st;
 int count[100]={0};
 int count_1[100]={0};
 int count_2[100]={0};
 k = 0;
 for(i =0;i<len;i++)
 {
  for( j =(len-1);j>i;j--)
  {
   if(str[i] == str[j])
   {  
    m =i;
    n =j;
    while((str[m]== str[n]) && (n>m))
    {
     m++;
     n--;
    }
    if( (m-n)==1 ) //是作为对称的子串
    {
                    count[k] = (j-i+1);
     count_1[k] = i;
     count_2[k] = j;
     k++;
    }
    
   }
  }
 }

 
 tmp = k;
 for(i=0;i<k;i++)
 {
  printf("%2d",count[i]);
 }

 for(i = 0 ; i<k-1;i++) //找出最大的值
 {
  if(count[i]>count[i+1])
  {
             temp = count[i];
    count[i] = count[i+1];
    count[i+1] = temp;
  }
 }
   
    for(i =0;i<k ; i++)
 {
  if(count[k-1] == (count_2[i] - count_1[i] +1))
  {
   st = i; //找出了最大长度的开始位置
  }
 }
   
 for(i=st; i<(st+count[k-1]); i++)
 {
  printf("%c",str[i]);
 }


}

 

 

原创粉丝点击