题解:P14603 [NWRRC 2025] Defense Distance
这是一道可癌且毒瘤的构造题。
思路
根据莱文斯坦距离的性质,
那么我们就可以列出三个二元一次方程:
-
x + y = a -
x + z = b -
y + z = c
根据初中数学知识,得:
-
x = \frac{a + b - c}{2} -
y = \frac{a + c - b}{2} -
z = \frac{b + c - a}{2}
但是如果
注意要特判 0 0 0 的情况。
时间复杂度是线性的。
Code
#include<bits/stdc++.h>
using namespace std;
int a,b,c;
int main(){
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
cin >> a >> b >> c;
if(a == 0 and b == 0 and c == 0){
cout << "Yes\na\na\na";
return 0;
}
if(a + b < c or a + c < b or b + c < a){
cout << "No";
return 0;
}
cout << "Yes\n";
string s,u,v;
if((a + b + c) % 2 == 1){
s += 'a';
u += 'b';
v += 'c';
a --;
b --;
c --;
if(a == 0 and b == 0 and c == 0){
cout << s << "\n" << u << "\n" << v;
return 0;
}
}
int x = (a + b - c) / 2;
int y = (a + c - b) / 2;
int z = (b + c - a) / 2;
s += string(x,'a');
u += string(x,'b');
v += string(x,'b');
s += string(y,'a');
u += string(y,'b');
v += string(y,'a');
s += string(z,'a');
u += string(z,'a');
v += string(z,'b');
cout << s << "\n" << u << "\n" << v;
return 0;
}