2021牛客暑期多校训练营4:C-LCS

题目链接

题意:三个长为n的字符串 s 1 , s 2 , s 3 s1,s2,s3 s1,s2,s3,已知 L C S ( s 1 , s 2 ) = a , L C S ( s 2 , s 3 ) = b , L C S ( s 1 , s 3 ) = c LCS(s1,s2)=a,LCS(s2,s3)=b,LCS(s1,s3)=c LCS(s1,s2)=a,LCS(s2,s3)=b,LCS(s1,s3)=c,请你构造出符合条件的 s 1 , s 2 , s 3 s1,s2,s3 s1,s2,s3

解析:我们先让 s 1 , s 2 , s 3 s1,s2,s3 s1,s2,s3同时在首部放入 m i n ( a , b , c ) min(a,b,c) min(a,b,c)的’a’,然后我们以 a = m i n ( a , b , c ) a=min(a,b,c) a=min(a,b,c)的情况进行研究:由于 L C S ( s 1 , s 3 ) = c LCS(s1,s3)=c LCS(s1,s3)=c,所以我们可以继续在 s 1 , s 3 s1,s3 s1,s3的末尾加入 c − a c-a ca个’b’, L C S ( s 2 , s 3 ) = b LCS(s2,s3)=b LCS(s2,s3)=b,我们可以在 s 2 , s 3 s2,s3 s2,s3末尾加入 b − a b-a ba个’c’,此时 s 1 , s 2 , s 3 s1,s2,s3 s1,s2,s3满足 L C S ( s 1 , s 2 ) = a , L C S ( s 2 , s 3 ) = b , L C S ( s 1 , s 3 ) = c LCS(s1,s2)=a,LCS(s2,s3)=b,LCS(s1,s3)=c LCS(s1,s2)=a,LCS(s2,s3)=b,LCS(s1,s3)=c,所以我们可以得出字符串长度 n n n最小为 a + c − a + b − a = b + c − a a+c-a+b-a=b+c-a a+ca+ba=b+ca,最后在 s 1 , s 2 , s 3 s1,s2,s3 s1,s2,s3尾部分别加入不同的字符,如 s 1 s1 s1添加’x’, s 2 s2 s2添加’y’, s 3 s3 s3添加’z’至长度为 n n n,其他情况同样分析即可

AcCode:

#include 
#include
#include
#include
#define int long long

using namespace std;

signed main(){
    cin.sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    int a, b, c, n;
    cin >> a >> b >> c >> n;
    string str1, str2, str3;
    int arr[4] = { 0,a,b,c };
    sort(arr + 1, arr + 4);
    int lmin = arr[2] + arr[3] - arr[1];
    if (lmin > n) {
        cout << "NO" << endl;
        return 0;
    }
    for (int i = 0; i < arr[1]; i++) {
        str1.push_back('a');
        str2.push_back('a');
        str3.push_back('a');
    }
    if (arr[1] == a) {
        int len1 = b - arr[1];
        for (int i = 0; i < len1; i++) {
            str2.push_back('b');
            str3.push_back('b');
        }
        len1 = c - arr[1];
        for (int i = 0; i < len1; i++) {
            str1.push_back('c');
            str3.push_back('c');
        }
    }
    else if (arr[1] == b) {
        int len1 = c - arr[1];
        for (int i = 0; i < len1; i++) {
            str3.push_back('b');
            str1.push_back('b');
        }
        len1 = a - arr[1];
        for (int i = 0; i < len1; i++) {
            str1.push_back('c');
            str2.push_back('c');
        }
    }
    else {
        int len1 = a - arr[1];
        for (int i = 0; i < len1; i++) {
            str2.push_back('b');
            str1.push_back('b');
        }
        len1 = b - arr[1];
        for (int i = 0; i < len1; i++) {
            str2.push_back('c');
            str3.push_back('c');
        }
    }
    while (str1.length() < n) str1.push_back('d');
    while (str2.length() < n) str2.push_back('e');
    while (str3.length() < n) str3.push_back('f');
    cout << str1 << endl;
    cout << str2 << endl;
    cout << str3 << endl;

}

你可能感兴趣的:(题解,补题)