. A: find the longest common string (string)

1 title

. A: find the longest common string (string)
[proposition al: External Import]
Time limit: 1.000 sec memory limit: 128 MB

Title Description
find the longest common substring using a string s and t are stored in sequential structure string, there is no output if false, if the longest of the plurality of output that string appears first.

Input
Input two strings

Output
output common substring

Sample input the Copy
abcdef
adbcef
sample output the Copy
BC

2 parsed

2.1 the meaning of problems

Two strings find the longest common substring

2.2 ideas

Two for each sub-string obtained hash value string (while recording a longest length, the length of the start, end length), and find the corresponding sub-string piles hash values are equal to those, you can find the maximum length and the maximum corresponding to the length of the substring starting and ending positions. The time complexity is O (n- 2 + m 2 ), n-m and the length of the two strings.

3 reference code

#include <iostream>
#include <vector>
#include <string>
#include <algorithm>

using std::max;
using std::string;
using std::vector;
using std::cin;


typedef long long LL;
struct Node{
    int hashValue, length, start, end;//字符串hash值、长度、起始为止、终止位置
    Node(int _hashValue, int _length, int _start, int _end): hashValue(_hashValue), length(_length), start(_start), end(_end){}
};

const int P = 10000019;
const int MOD = 1000000007;
const int MAXN = 1010;
LL PowP[MAXN];//powP存放P^i%MOD
LL H1[MAXN], H2[MAXN];//str1、str2的hash值
vector<Node> pr1, pr2;//<子串hash值,子串长度>

//初始化PowP
void Init(int len){
    PowP[0] = 1;
    for (int i = 1; i <= len; ++i)
    {
        PowP[i] = (PowP[i-1] * P) % MOD;
    }
}

//str的hash
void calH(LL H[],string &str){
    H[0] = str[0];
    for (int i = 1; i < str.length(); ++i)
    {
        H[i] = (H[i-1] * P + str[i]) % MOD;
    }
}

//计算H[i...j]
int calSingleSubH(LL H[], int i, int j){
    if(i == 0) return H[j];
    else return ((H[j] - H[i-1] * PowP[j - i + 1]) % MOD + MOD)% MOD;
}

//计算所有子串的hash值,并将<子串hash,子串长度>存入pr
void calSubH(LL H[], int len, vector<Node>& pr){
    for (int i = 0; i < len; ++i)
    {
        for (int j = i; j < len; ++j)
        {
            int hashValue = calSingleSubH(H, i, j);
            pr.push_back(Node(hashValue, j - i + 1, i , j));
        }
    }
}


//计算pr1和pr2中相同hash值,维护最大长度
string getMax(string str1){
    int ans = 0;
    string str;
    for (int i = 0; i < pr1.size(); ++i)
    {
        for (int j = 0; j < pr2.size(); ++j)
        {
            if(pr1[i].hashValue == pr2[j].hashValue){
                if(pr1[i].length > ans){
                    ans = pr1[i].length;
                    str = str1.substr(pr1[i].start, pr1[i].end);
                }
            }
        }
    }
    return str;
}

int main(int argc, char const *argv[])
{
    string str1, str2;
    getline(cin, str1);
    getline(cin, str2);

    Init(max(str1.length(), str2.length()));
    calH(H1, str1);
    calH(H2, str2);

    calSubH(H1, str1.length(), pr1);
    calSubH(H2, str2.length(), pr2);

    string res = getMax(str1);
    if(res.size() == 0){
        printf("false\n");
    }else{
        std::cout << res << std::endl;
    }

    return 0;
}
Published 377 original articles · won praise 52 · views 50000 +

Guess you like

Origin blog.csdn.net/qq_33375598/article/details/104472568