zoukankan      html  css  js  c++  java
  • 寻找两个字符串中最长的公共部分字符串

    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
    ", sstr);
    41         }
    42         else
    43         {
    44             for (int i = 0; i < strlen(str1); i++)
    45             {
    46                 //printf("%s
    
    ", 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
    ", 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
    ", 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
    ", temp);
    63                             //printf("strLen[%d][%d] = %d
    ", 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.
    ");
    76             }
    77 
    78             // printf("count: %d
    ", count);
    79             for (int i = 0; i < count; i++)
    80             {
    81                 index = strLen[temp][i];
    82                 if (index != 0)
    83                 {
    84                     printf("%s
    ", strMem[index]);
    85                 }
    86             }
    87         }
    88     }
    89 
    90     return 0; // will not jump here
    91 
    92 }
    --------------------------------------------- Study needs record, record needs review ---------------------------------------------
  • 相关阅读:
    Asp.Net Core 轻松学-被低估的过滤器
    Asp.Net Core 轻松学-利用文件监视进行快速测试开发
    Asp.Net Core 轻松学-利用xUnit进行主机级别的网络集成测试
    Asp.Net Core 轻松学-HttpClient的演进和避坑
    Asp.Net Core 轻松学-基于微服务的后台任务调度管理器
    Asp.Net Core 轻松学-一行代码搞定文件上传
    .NET Core 2.2 新增部分功能使用尝鲜
    Asp.NetCore轻松学-业务重点-实现一个简单的手机号码验证
    Asp.Net Core 轻松学-实现跨平台的自定义Json数据包
    Asp.Net Core 轻松学-利用 Swagger 自动生成接口文档
  • 原文地址:https://www.cnblogs.com/georgemxx/p/12593892.html
Copyright © 2011-2022 走看看