Implementation of diff functions-LCs variants

Source: Internet
Author: User

Where did I go for a written test yesterday? I met a familiar command (diff -- below UBUNTU) that can compare strings, that is, according to the longest public substring problem, if a contains characters not in B, the output format is as follows (-Ch). If a does not, B can output the following format (+ CH ).

 

#include <iostream>#include <cstring>#include <vector>using namespace std;string LCS(string &s1, string &s2){    int row = s1.size();    int col = s2.size();    string table[row + 1][col + 1];    char rowChar[row + 1];    char colChar[col + 1];    int cnt = 0;    rowChar[0] = colChar[0] = ‘\0‘;    for(int i = row - 1, cnt = 1; i >= 0; i--, cnt++)    {        rowChar[cnt] = s1[i];    }    for(int i = col - 1, cnt = 1; i >= 0; i--, cnt++)    {        colChar[cnt] = s2[i];    }    char ch1, ch2;    string str1, str2;    for(int i = 1; i <= row; i++)    {        for(int j = 1; j <= col; j++)        {            ch1 = rowChar[i];            ch2 = colChar[j];            if(ch1 == ch2)                table[i][j] = ch1 + table[i - 1][j - 1];            else            {                str1 = table[i - 1][j];                str2 = table[i][j - 1];                if(str1.size() == str2.size())                    table[i][j] = str1 < str2 ? str2 : str1;                else                    table[i][j] = str1.size() < str2.size() ? str2 : str1;            }        }    }    return table[row][col];}void showDiff(string &s1, string &s2, string sub, vector<string> &ret){    cout << "Sub = " << sub << endl;    int len1 = s1.size();    int len2 = s1.size();    for(int i = 0, j = 0; i < len1; i++)    {        if(s1[i] != sub[j])        {            string str;            str.push_back(‘-‘);            str.push_back(s1[i]);            ret.push_back(str);        }        else            j++;    }    for(int i = 0, j = 0; i < len2; i++)    {        if(s2[i] != sub[j])        {            string str;            str.push_back(‘+‘);            str.push_back(s2[i]);            ret.push_back(str);        }        else            j++;    }}int main(){    string str1, str2;    cin >> str1 >> str2;    string retSub = LCS(str1, str2);    vector<string> ret;    showDiff(str1, str2, retSub, ret);    vector<string>::iterator iter;    for(iter = ret.begin(); iter != ret.end(); iter++)        cout << *iter << endl;    return 0;}

 

Implementation of diff functions-LCs variants

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.