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
简单构造题。不妨设。考虑如下构造:s1为个'a' + 个'x',s2为个'a' + 个'y',s3的前b个字符和s2相同,然后个字符和s1相同,剩下的部分填'z'。如果无解。
最后输出的时候需要根据初始的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;
}
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· 单线程的Redis速度为什么快?
· SQL Server 2025 AI相关能力初探
· AI编程工具终极对决:字节Trae VS Cursor,谁才是开发者新宠?
· 展开说说关于C#中ORM框架的用法!
2020-07-26 外一章