华为OJ中级题-查找两个字符串a,b中的最长公共子串

查找两个字符串a,b中的最长公共子串。

void hwOJ(){
    string a = "abcdefghijklmnop";
    string b = "abcsafjklmnopqrstuvw";
    int lena = a.length();
    int lenb = b.length();
    int st1 = 0, st2 = 0,st=0;
    int count = 0, MAx = 0;
    string tmp = "", longStr = "";
    for (int i = 0; i < lena; ++i){
        for(int j = 0; j < lenb; ++j){
            st1 = i; st2 = j;
            while (a[st1] == b[st2]&&st1<lena&&st2<lenb){
                ++st1; ++st2;
                ++count;
            }
            if (count > MAx){
                MAx = count;
                st = i;
            }
            count = 0;
}
    }
    string str;
    for (int i = 0; i < MAx; ++i){
        str.push_back(a[i + st]);
    }
    cout << str;
}

你可能感兴趣的:(C++)