linux kernel data struct : find longest sub string

今天我们来看看面试 C 中常见,而且比较刁钻的题目:

找出两个字符串中的最大相同子串,如:

str1: "mmmabckkk"
str2: "oabclll"

那么最大相同子串为:abc

以下为我与同事讨论最后的思路及解法,希望对大家有所启发。

基本思路为:短串中以最大的长度作为窗口,以后窗口依次减一;
            遍历短串的窗口字符
            遍历长串的窗口字符
            比较


以下为C实现,调试环境为Dev-C:

#include
#include

typedef unsigned int uint;

struct longest_sub_str {
       uint l_cursor;
       uint s_cursor;
       uint window;
       }LSS;

//s1 == s2, return 1, else return 0      
uint strEqual(char *s1, char *s2, uint count)
{
     printf("==========\n");
     printf("s1: %s\n",s1);
     printf("s2: %s\n",s2);
     printf("==========\n");
     while(count--)
     {
         if(*(s1+count) != *(s2+count)) return 0;
     }
     return 1;
}
      
struct longest_sub_str *find_longest_sub_str(char *s1, char *s2)
{
     uint m,window,n;
     char *l_str_p, *s_str_p;
     uint l_str_len, s_str_len;
    
     uint len1 = strlen(s1);
     uint len2 = strlen(s2);
    
     //judge long and short string
     if(len1 >= len2)
     {
             l_str_len = len1;
             s_str_len = len2;
            
             l_str_p = s1;
             s_str_p = s2;
     }
     else
     {
             l_str_len = len2;
             s_str_len = len1;
            
             l_str_p = s2;
             s_str_p = s1;        
     }
    
     printf("long string is: %s. length is: %d\n",l_str_p,l_str_len);
     printf("short string is: %s. length is: %d\n",s_str_p,s_str_len);
    
     //The next three "for" are very important
     //window
     for(window = s_str_len; window > 0; window--)     
     {   //short string cursor
         for(m = 0; window+m <= s_str_len; m++)
         {    //long string cursor
              for(n = 0; n < l_str_len; n++)
              {
                    if(strEqual(l_str_p+n, s_str_p+m, window))
                    {
                        LSS.l_cursor = n;
                        LSS.s_cursor = m;
                        LSS.window = window;
                       
                        return &LSS;
                    }
              }//long string cursor end   
         }//short string cursor end
     }//window end
    
     return 0;    
}

main()
{
 char *str2 = "mmabcm";
 char *str1 = "oabcl";
 
 //char *str2 = "deidkvnkadslldisead;iiswefdvcxlse";
 //char *str1 = "mmmmswefdxxx";
 
 printf("Hello world !\n\n");
 
 printf("*********************\n");
 printf("str1 = %s\n",str1);
 printf("str2 = %s\n",str2);
 
 LSS.l_cursor = 0;
 LSS.s_cursor = 0;
 LSS.window = 0;
     
 if(find_longest_sub_str(str1, str2))
 {
  printf("longest sub string is: \n");
  printf("long str cursor: %d\n", LSS.l_cursor);
  printf("short str cursor: %d\n", LSS.s_cursor);
  printf("window is: %d\n", LSS.window);
  printf("*********************\n"); 
 }
 else
 {
  printf("no longest sub string\n");
 }  
     
 system("pause");
}

 

运行结果为:

Hello world !

*********************
str1 = oabcl
str2 = mmabcm
long string is: mmabcm. length is: 6
short string is: oabcl. length is: 5
==========
s1: mmabcm
s2: oabcl
==========
==========
s1: mabcm
s2: oabcl
==========
==========
s1: abcm
s2: oabcl
==========
==========
s1: bcm
s2: oabcl
==========
==========
s1: cm
s2: oabcl
==========
==========
s1: m
s2: oabcl
==========
==========
s1: mmabcm
s2: oabcl
==========
==========
s1: mabcm
s2: oabcl
==========
==========
s1: abcm
s2: oabcl
==========
==========
s1: bcm
s2: oabcl
==========
==========
s1: cm
s2: oabcl
==========
==========
s1: m
s2: oabcl
==========
==========
s1: mmabcm
s2: abcl
==========
==========
s1: mabcm
s2: abcl
==========
==========
s1: abcm
s2: abcl
==========
==========
s1: bcm
s2: abcl
==========
==========
s1: cm
s2: abcl
==========
==========
s1: m
s2: abcl
==========
==========
s1: mmabcm
s2: oabcl
==========
==========
s1: mabcm
s2: oabcl
==========
==========
s1: abcm
s2: oabcl
==========
==========
s1: bcm
s2: oabcl
==========
==========
s1: cm
s2: oabcl
==========
==========
s1: m
s2: oabcl
==========
==========
s1: mmabcm
s2: abcl
==========
==========
s1: mabcm
s2: abcl
==========
==========
s1: abcm
s2: abcl
==========
longest sub string is:
long str cursor: 2
short str cursor: 1
window is: 3
*********************
请按任意键继续. . .

 

你可能感兴趣的:(linux,kernel,data,struct,C,find,sub,string)