zoukankan      html  css  js  c++  java
  • longest common str

    #include <vector>
    #include <iostream>
    #include <string>
    using namespace std;
    
    int longest_common_string(string& str_a, string& str_b) {
        int len_a = str_a.length();
        int len_b = str_b.length();
        if (0 == len_a || 0 == len_b) {
            return 0;
        }
        int rows = len_a + 1;
        int cols = len_b + 1;
        int ** comm_len_array = new int * [rows];
        int i = 0;
        for (i=0; i < rows; i++) {
            comm_len_array[i] = new int[cols];
        }
    
        int j = 0;
        for (i = 0; i < rows; i++) {
            for (j = 0; j < cols; j++) {
                comm_len_array[i][j] = 0;
            }
        }
    
        int max = INT_MIN;
        int end_sub_str_a = -1;
        int end_sub_str_b = -1;
        for (i = 1; i < rows; i++) {
            for (j = 1; j < cols; j++) {
                if (str_a[i - 1] == str_b[j - 1]) {
                    comm_len_array[i][j] = comm_len_array[i - 1][j - 1] + 1;
                } else {
                    comm_len_array[i][j] = 0;
                }
    
                if (comm_len_array[i][j] > max) {
                    max = comm_len_array[i][j];
                    end_sub_str_a = i;
                    end_sub_str_b = j;
                }
            }
        }
    
        for (i=0; i < rows; i++) {
            delete[] comm_len_array[i];
        }
        delete[] comm_len_array;
    
        return max;
    }
    
    
    int main() {
        string stra = "abcedefg";
        string strb = "abcf";
        cout << longest_common_string(stra, strb) << endl;
    }
  • 相关阅读:
    窗体间传值
    winform 导出datagridview 到excel
    单击单元格任意地方事件
    CLR via 随书笔记
    值类型和引用类型的区别
    System.Object简介
    装箱与拆箱
    静态类
    关于Linq2Sql有外键表的更新引发的问题。
    滑动切换页面
  • 原文地址:https://www.cnblogs.com/SpeakSoftlyLove/p/5280363.html
Copyright © 2011-2022 走看看