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;
    }
    
  • 相关阅读:
    HTML编码规范(转)
    ASP.NET连接MySQL数据库方法(测试可行)
    Redis源码解析05: 压缩列表
    Redis源码解析04: 跳跃表
    Redis源码解析03: 字典的遍历
    Redis源码解析02: 字典
    Redis源码解析01: 简单动态字符串SDS
    小象垃圾分类小程序从开始到结束
    spring boot踩坑记
    spring boot打包问题
  • 原文地址:https://www.cnblogs.com/lipoicyclic/p/15062884.html
Copyright © 2011-2022 走看看