寻找两个字符串中最长的公共部分字符串

For instance:

If input two strings like ‘abc’ and ‘adc’, the output will be ‘a’ & ‘c’, because the longest common part is ‘a’ and ‘c’.

If input two strings like ‘abdaad’ and ‘daadc’, the output is ‘daad’

If input two strings like ‘abcd’ and ‘abc’, the output is ‘abc’ which is used another branch

No bullshit, please refer to the code directly.

 1 /* find the longest common string in both str1 and str2 */
 2 
 3 #define _CRT_SECURE_NO_WARNINGS
 4 #include <stdio.h>
 5 #include <string.h>
 6 
 7 int main(void)
 8 
 9 {
10 
11 #define ROW 100
12 #define COL 100
13 
14     char str1[COL];
15     char str2[COL];
16     char dst[COL];
17     char strMem[ROW][COL];
18     int strLen[20][20];
19 
20     while (scanf("%s %s", str1, str2) != EOF)
21     {
22         int index = 1;
23         int count = 0;
24         int len;
25         int temp = 0;
26         
27         /* clear two-dimensional array */
28         for (int i = 0; i < 20; i++)
29         {
30             for (int j = 0; j < 20; j++)
31             {
32                 strLen[i][j] = 0;
33             }
34         }
35 
36         char *sstr = ((strlen(str1) > strlen(str2)) ? str2 : str1);
37         char *lstr = ((strlen(str1) > strlen(str2)) ? str1 : str2);
38         if (strstr(lstr, sstr) != NULL)
39         {
40             printf("%s\n", sstr);
41         }
42         else
43         {
44             for (int i = 0; i < strlen(str1); i++)
45             {
46                 //printf("%s\n\n", str1 + i);
47                 memset(dst, 0, strlen(str1)); /* clear the array */
48                 for (int j = 1; j <= (strlen(str1) - i); j++)
49                 {
50                     strncpy(dst, str1 + i, j);
51                     //printf("dst = %s  len = %d, size = %d, dst[1] = %c\n", dst, strlen(dst), sizeof(dst), dst[1]);
52 
53                    if (strstr(str2, dst) != NULL) /* 从str2中查找dst字符串 */
54                     {
55                         strcpy(strMem[index], dst); /* 将dst字符串赋值给strMem二维数组 */
56                         //printf("strMem[%d] = %s\n", index, strMem[index]);
57                         len = strlen(strMem[index]);
58                         if (len >= temp)
59                         {
60                             temp = len;
61                             strLen[temp][count] = index;
62                             //printf("temp: %d\n", temp);
63                             //printf("strLen[%d][%d] = %d\n", temp, count, index);
64                         }
65 
66                         count++;
67                     }
68 
69                     index++;
70                 }
71             }
72 
73             if (count == 0)
74             {
75                 printf("there's no same string.\n");
76             }
77 
78             // printf("count: %d\n", count);
79             for (int i = 0; i < count; i++)
80             {
81                 index = strLen[temp][i];
82                 if (index != 0)
83                 {
84                     printf("%s\n", strMem[index]);
85                 }
86             }
87         }
88     }
89 
90     return 0; // will not jump here
91 
92 }

猜你喜欢

转载自www.cnblogs.com/georgemxx/p/12593892.html