• 求两个字符串的最大公共子串


     1 #include <stdio.h>
     2 #include <string.h>
     3 
     4 void substr(char *s1, char *s2)
     5 {     
     6      /*    1.设长串开始位置 i, 最大匹配串开始 结束位置 maxStart, maxEnd*/                
     7     char *i = s1, *maxStart = s1, *maxEnd = s1;
     8     char *j = s2; /*  设短串*/
     9     
    10     if(strlen(s1)<strlen(s2))/*    判断长串 短串, 更新指针*/
    11     {
    12         i = maxStart = maxStart = s2;
    13         j = s1;       
    14     }
    15     
    16     char *jt = j;  /*  保存短串开始位置*/
    17     
    18     /*     2.计算最大匹配串 */
    19     for(; *i; i++)/*  长串*/
    20     {      
    21         for(j = jt; *j; j++)/*  短串*/
    22         {
    23             char *indexStart = i,*indexEnd = i;
    24             char *q = j;
    25 
    26             while(*indexEnd == *q && *q)/*  一旦匹配, 长短串指针都后移*/   
    27             {              
    28                 indexEnd++;
    29                 q++;            
    30             }
    31 
    32             if(indexEnd-indexStart > maxEnd-maxStart)/* 更新最大匹配串指针*/
    33             {
    34                maxEnd = indexEnd;
    35                maxStart = indexStart;
    36             }       
    37         }
    38     }
    39     /* 打印最大匹配串 */
    40     while(maxEnd>maxStart)
    41     {
    42         printf("%c",*maxStart++);
    43     }    
    44 } 
    45 
    46 int main()
    47 {
    48     char str1[100] = "abcdfffaebceeeeyyyyy";
    49     char str2[100] = "abcdfqbcabcmxxnn";
    50     substr(str1, str2);
    51     return 0;
    52 }
  • 相关阅读:
    Nginx负载均衡
    MySQL主从复制
    笔记
    tomcat工作原理
    Nginx工作原理
    Loj#6183. 看无可看
    [BZOJ 2759] 一个动态树好题
    5255 -- 【FJOI2016】神秘数
    [NOI2015]寿司晚宴
    [CQOI2017]老C的键盘
  • 原文地址:https://www.cnblogs.com/GoldenEllipsis/p/11605359.html
Copyright © 2020-2023  润新知