zoukankan      html  css  js  c++  java
  • LintCode 79 · 最长公共子串

    状态表示:

    (f(i,j)):以(s_1[i])(s_2[j])为结尾的两个子串(s_1[0 sim i])(s_2[0 sim j]),它们的公共子串的长度。

    遍历所有的(i,j),其中最大的(f(i,j))就是答案。

    状态转移:

    [egin{cases} f(i,j) = 0 & s_1[i] e s_2[j] \ f(i,j) = f(i-1,j-1)+1 & s_1[i] = s_2[j] end{cases} ]

    class Solution {
    public:
        /**
         * @param A: A string
         * @param B: A string
         * @return: the length of the longest common substring.
         */
        static const int N=1010;
        int f[N][N];
        int longestCommonSubstring(string &A, string &B) {
            // write your code here
            int n=A.size(),m=B.size();
    
            int res=0;
            memset(f,0,sizeof f);
            for(int i=1;i<=n;i++)
                for(int j=1;j<=m;j++)
                    if(A[i-1] == B[j-1])
                    {
                        f[i][j]=f[i-1][j-1]+1;
                        res=max(res,f[i][j]);
                    }
                        
            return res;
        }
    };
    
  • 相关阅读:
    #include <boost/scoped_ptr.hpp>
    #include <boost/function.hpp>
    #include <boost/bind.hpp>
    8.4散列表查找
    #include <boost/array.hpp>
    异常
    lambda
    #include <amp.h>
    #include <bitset>
    #include <hash_set>
  • 原文地址:https://www.cnblogs.com/fxh0707/p/14660517.html
Copyright © 2011-2022 走看看