LCS(2021牛客多校4)
题意:
让你构造三个字符串s1,s2,s3,长度均为n,要求LCS(s1,s2)=a,LCS(s2,s3)=b,LCS(s1,s3)=c
题解:
先考虑三个串互相LCS为x,y,z,且x>=y>=z
显然如果x+y-n>z则无解,反之x+y-n<=z有解
那么就先给三个串加上一个z个a的前缀,然后就变成了x-z,y-z,0,n-z的问题
因为x+y-n<=z,所以(x-z)+(y-z)<=n-z,所以给前两个串一起放上x-z个b,后两个串放上y-z个c即可
代码:
#include<stdio.h>
#include<cstring>
#include<algorithm>
#include<vector>
#include<map>
#include<assert.h>
#include<set>
#include<cmath>
#include<queue>
#include<cstdlib>
#include<iostream>
#include<bitset>
#define pii pair<int,int>
#define fi first
#define se second
#define pb push_back
#define rep(i,j,k) for(int i=(int)(j);i<=(int)(k);i++)
#define per(i,j,k) for(int i=(int)(j);i>=(int)(k);i--)
using namespace std;
typedef long long LL;
typedef double db;
const int N=1005;
int n,a,b,c;
char x[N],y[N],z[N];
void work(int a,int b,int c){rep(i,1,c)x[i]=y[i]=z[i]='a';a-=c;b-=c;rep(i,c+1,c+a){x[i]='b';y[i]='b';z[i]='q';}rep(i,c+a+1,c+a+b){x[i]='p';y[i]='c';z[i]='c';}rep(i,c+a+b+1,n){x[i]='x';y[i]='y';z[i]='z';}
}
void opt(char *a,char *b,char *c){rep(i,1,n)putchar(a[i]);puts("");rep(i,1,n)putchar(b[i]);puts("");rep(i,1,n)putchar(c[i]);puts("");
}
int main(){scanf("%d%d%d%d",&a,&b,&c,&n);if(c<a+b-n||a<b+c-n||b<a+c-n){puts("NO");return 0;}if(c<=a&&c<=b){work(a,b,c);opt(x,y,z);}else if(a<=c&&a<=b){work(b,c,a);opt(z,x,y);}else{work(a,c,b);opt(y,x,z);}return 0;
}