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 ---------------------------------------------
  • 相关阅读:
    HTML <form> 标签的 accept-charset 属性
    SpringMVC中Controller跳转到另一个Controller方法
    DIV层+CSS实现锁屏
    工作组模式下专用队列(Private Queue)如何引用远程队列路径
    stl之deque双端队列容器
    百度之星资格赛——Disk Schedule(双调旅行商问题)
    android 无线模式下使用ADB调试
    HDU
    NoSQL 数据库产品学习总结(一)
    ThinkPad E431怎样关闭触摸板
  • 原文地址:https://www.cnblogs.com/georgemxx/p/12593892.html
Copyright © 2011-2022 走看看