You are given two strings s and t both of length 2 and both consisting only of characters ‘a’, ‘b’ and ‘c’.
Possible examples of strings s and t: “ab”, “ca”, “bb”.
You have to find a string res consisting of 3n characters, n characters should be ‘a’, n characters should be ‘b’ and n characters should be ‘c’ and s and t should not occur in res as substrings.
A substring of a string is a contiguous subsequence of that string. So, the strings “ab”, “ac” and “cc” are substrings of the string “abacc”, but the strings “bc”, “aa” and “cb” are not substrings of the string “abacc”.
If there are multiple answers, you can print any of them.
Input
The first line of the input contains one integer n (1≤n≤105) — the number of characters ‘a’, ‘b’ and ‘c’ in the resulting string.
The second line of the input contains one string s of length 2 consisting of characters ‘a’, ‘b’ and ‘c’.
The third line of the input contains one string t of length 2 consisting of characters ‘a’, ‘b’ and ‘c’.
Output
If it is impossible to find the suitable string, print “NO” on the first line.
Otherwise print “YES” on the first line and string res on the second line. res should consist of 3n characters, n characters should be ‘a’, n characters should be ‘b’ and n characters should be ‘c’ and s and t should not occur in res as substrings.
If there are multiple answers, you can print any of them.
Examples
inputCopy
2
ab
bc
outputCopy
YES
acbbac
inputCopy
3
aa
bc
outputCopy
YES
cacbacbab
inputCopy
1
cb
ac
outputCopy
YES
abc
题意: 有两个长度为2的由‘a’ ‘b’ ‘c’三个字母构成的字符串,现在需要构造一个由’a’,‘b’,‘c’,三个字母各n个组成的字符串,且构成的字符串不包含前两个字符串的子串。
思路: 构造可以分两种情况,可以形成自循环和不能形成自循环。其中不能形成自循环又有三种情况。
ab
ac
当前情况a后不能接b和c,所以只能放在a之前,可以构成bbccaa的形式。
ac
bc
当前情况c前不能接a和b,所以只能放在c之后,可以构成ccaabb形式。
ab
ba
当前情况a和b不能放在一起,所以中间要间隔一个c,因此可以构成acacbb形式。
除次三种情况外,其他都可以构成自循环。
ab
bc
可以构成acbacb的形式。
所以,对于不能构成循环的形式,可以通过特判判断,反之通过全排列a,b,c三个字母,查看是否满足要求即可。
#include
using namespace std;
int main()
{
int n;
char a,b,c,d;
string s,c1,c2;
scanf("%d",&n);
cin>>a>>b>>c>>d;
c1 += a; c1 += b;
c2 += c; c2 += d;
int tbd = b - 'a';
int tac = a - 'a';
cout<<"YES"<<endl;
if(b == d && ((a == (tbd + 1) % 3 + 'a' && c == (tbd + 2) % 3 + 'a') || (a == (tbd + 2) % 3 + 'a' && c == (tbd + 1) % 3 + 'a'))){
for(int i = 1; i <= n; ++i) s += b;
for(int i = 1; i <= n; ++i) s += (tbd + 1) % 3 + 'a';
for(int i = 1; i <= n; ++i) s += (tbd + 2) % 3 + 'a';
cout<<s<<endl;
}
else if(a == c && ((b == (tac + 1) % 3 + 'a' && d == (tac + 2) % 3 + 'a') || (b == (tac + 2) % 3 + 'a' && d == (tac + 1) % 3 + 'a'))){
for(int i = 1; i <= n; ++i) s += (tac + 1) % 3 + 'a';
for(int i = 1; i <= n; ++i) s += (tac + 2) % 3 + 'a';
for(int i = 1; i <= n; ++i) s += a;
cout<<s<<endl;
}
else if(a == d && c == b && a != c){
for(int i = 1; i <= n; ++i)
printf("%c%c",a,(a - 'a' + 1) % 3 + 'a' == b ? (a - 'a' + 2) % 3 + 'a' : (a - 'a' + 1) % 3 + 'a');
for(int i = 1; i <= n; ++i)
printf("%c",b);
printf("\n");
}
else{
char ch[5] = {'a','b','c'};
do
{
string s1,s2,s3;
s1 += ch[0]; s1 += ch[1];
s2 += ch[1]; s2 += ch[2];
s3 += ch[2]; s3 += ch[0];
if(s1 == c1 || s1 == c2 || s2 == c1 || s2 == c2 || s3 == c1 || s3 == c2) continue;
else break;
}while(next_permutation(ch,ch+3));
for(int i = 1; i <= n; ++i)
for(int j = 0; j < 3; ++j)
printf("%c",ch[j]);
printf("\n");
}
return 0;
}