zoukankan      html  css  js  c++  java
  • 求两个字符串的最大公共字符串的长度

    int getCommonStrLength(char * pFirstStr, char * pSecondStr)

    {

      if (pFirstStr == NULL || NULL == pSecondStr)  return 0;    

       int i, len1, len2, len, s1_start, s2_start, idx, curmax, max;

      len1 = strlen(pFirstStr);    

      len2 = strlen(pSecondStr);    

      len = len1 + len2;    

      max = 0;    

      for (i = 0; i < len; i++)

         {        

        s1_start = s2_start = 0;        

        if (i < len1)            

          s1_start = len1 - i;    //每次开始匹配的起始位置         

        else            

          s2_start = i - len1;        

         curmax = 0;        

        for (idx = 0; (s1_start + idx < len1) && (s2_start + idx < len2); idx++)

              {            

          if (toupper(pFirstStr[s1_start + idx]) == toupper(pSecondStr[s2_start + idx]))                

            curmax++;            

          else     //只要有一个不相等,就说明相等的公共字符断了,不连续了,要保存curmax与max中的最大值,并将curmax重置为0             

           {                

             max = curmax > max ? curmax : max;                

            curmax = 0;            

           }        

        }        

        max = curmax > max ? curmax : max;    

      }    

      return max;

    }

  • 相关阅读:
    uva 11078
    hdu1520(树状dp)
    从Markov Process到Markov Decision Process
    剑指Offer系列编程题详解全集
    L1正则和L2正则的区别详解
    协方差详解
    牛顿法和梯度下降法的比较
    C++ const各种用法总结
    Exploration and Exploitation
    RL Algorithm Components
  • 原文地址:https://www.cnblogs.com/kennyael/p/9987783.html
Copyright © 2011-2022 走看看