zoukankan      html  css  js  c++  java
  • 2021牛客暑期多校训练营4 C. LCS(字符串/构造)

    链接:https://ac.nowcoder.com/acm/contest/11255/C
    来源:牛客网

    题目描述

    Let LCS(s1,s2)LCS(s1,s2) denote the length of the longest common subsequence (not necessary continuity) of string s1s1 and string s2s2.

    Now give you four integers a,b,c,na,b,c,n, you need to find three lowercase character strings s1,s2,s3s1,s2,s3satisfy that ∣s1∣=∣s2∣=∣s3∣=n∣s1∣=∣s2∣=∣s3∣=n
    and LCS(s1,s2)=a,LCS(s2,s3)=b,LCS(s1,s3)=cLCS(s1​,s2​)=a,LCS(s2​,s3​)=b,LCS(s1​,s3​)=c.

    输入描述:

    The first line has four integers a,b,c,na,b,c,n.
    
    0≤a,b,c≤n0≤a,b,c≤n.
    
    1≤n≤10001≤n≤1000.
    

    输出描述:

    If there is no solution, output "NO" (without double quotation marks).
    
    If there exists solutions, you only need to output any one: output three lines, the i-th line has one strings sisi.
    

    示例1

    输入

    复制

    1 2 3 4
    

    输出

    复制

    aqcc
    abpp
    abcc
    

    示例2

    输入

    复制

    1 2 3 3
    

    输出

    复制

    NO
    

    简单构造题。不妨设(aleq b leq c)。考虑如下构造:s1为(a)个'a' + (n - a)个'x',s2为(a)个'a' + (n - a)个'y',s3的前b个字符和s2相同,然后(c - a)个字符和s1相同,剩下的部分填'z'。如果(n - b < c - a)无解。

    最后输出的时候需要根据初始的abc的大小关系确定哪个是真正的s1s2s3。

    #include <bits/stdc++.h>
    using namespace std;
    int n, a, b, c, len[4];
    
    int main() {
    	cin >> a >> b >> c >> n;
    	len[1] = a, len[2] = b, len[3] = c;
    	sort(len + 1, len + 3 + 1);
    	string s1 = "", s2 = "", s3 = "";
    	for(int i = 1; i <= n; i++) {
    		if(i <= len[1]) s1 += 'a';
    		else s1 += 'x';
    	}
    	for(int i = 1; i <= n; i++) {
    		if(i <= len[1]) s2 += 'a';
    		else s2 += 'y';
    	}
    	bool ok = 1;
    	for(int i = 1; i <= n; i++) {
    		if(i <= len[2]) {
    			s3 += s2[i - 1];
    		}
    		else {
    			if(n - len[2] >= (len[3] - len[1])) {
    				for(int j = i + 1; j <= i + (n - len[2] - (len[3] - len[1])); j++) {
    					s3 += 'z';
    				}
    				for(int j = 1; j <= len[3] - len[1]; j++) {
    					s3 += 'x';
    				}
    			} else {
    				ok = 0;
    				break;
    			}
    			break;
    		}
    	}
    	if(!ok) {
    		cout << "NO";
    	} else {
    		if(a <= b && b <= c) {
    			cout << s1 << endl;
    			cout << s2 << endl;
    			cout << s3 << endl;
    		} else if(a <= c && c <= b) {
    			cout << s2 << endl;
    			cout << s1 << endl;
    			cout << s3 << endl;
    		} else if(b <= a && a <= c) {
    			cout << s3 << endl;
    			cout << s2 << endl;
    			cout << s1 << endl;
    		} else if(b <= c && c <= a) {
    			cout << s3 << endl;
    			cout << s1 << endl;
    			cout << s2 << endl;
    		} else if(c <= a && a <= b) {
    			cout << s2 << endl;
    			cout << s3 << endl;
    			cout << s1 << endl;
    		} else {
    			cout << s1 << endl;
    			cout << s3 << endl;
    			cout << s2 << endl;
    		}
    	}
    	return 0;
    }
    
  • 相关阅读:
    运行jar包中的main方法
    (转)如何判断VPS是基于哪种虚拟技术?Xen、OpenVZ、Xen HVM、KVM还是VMware
    centos安装redis
    Jmeter性能测试
    Jmeter脚本录制
    【Tomcat】Tomcat安装及Eclipse配置教程
    【接口测试】【SOAP】简单的接口测试学习
    JMeter性能测试,完整入门篇
    monkey命令详解
    APP专项测试
  • 原文地址:https://www.cnblogs.com/lipoicyclic/p/15062884.html
Copyright © 2011-2022 走看看