求出两个字符串的最大公共字符串

例如字符串 str1 = "abcdefg";

str2="cdefbcderg";

最长的公共字符串为 cdef

void getcommon(const char *str1 ,const char *str2)

{

      int i ,j ,len1 ,len2 ,count,max;

      char *pa ,*pb;

      max = 0;

      len1 = strlen(str1);

      len2 = strlen(str2);

      for(i = 0 ;i

      {

             for(j = 0 ;j

            {

                   if(str1[i] == str2[j])

                   {

                            int a = i;

                            int b = j;

                             count = 1;

                          while(str1[++a] == str2[++b]&&(a+1

                          {

                                  count++;

                          }

                          if(count >max)

                           {

                                max = count;

                                pa = str1 +i;

                                pb = str2 +j;

                           }

                   }

            }

      }

      for(i = 0 ;i

     {

           printf("%c",*(pa+i));

      }

       printf("\n");

}

你可能感兴趣的:(算法+数据结构,c)