2000分
题目
题意:
给定长度n和两个长度为2的字符串a1、a2,要求构造一个字符串,含有n个a,n个b,n个c,且子串中不包括a1、a2。a1,a2中只包含a、b或c。
1
≤
n
≤
1
0
5
1≤n≤10^5
1≤n≤105
分析:
我们可以考虑a,b,c都连着算,这样子串种类是最少的。有六种排列,暴力判断子串是否不包含a1,a2。再处理一下a1,a2包含连续的字符的情况,用abc或cba这样排列n次即可。
#include <iostream>
#include <string>
using namespace std;
string z[] = {"abc","acb","bac","bca","cab","cba"};
int main()
{
ios::sync_with_stdio(false);
cin.tie(0);
int n;
cin >> n;
string a1,a2;
cin >> a1 >> a2;
string res;
if( a2[0] == a2[1] ) swap(a1,a2);
if( a1[0] == a1[1] )
{
if( a2[0] == a2[1] )
{
string temp = "abc";
for (int i = 1; i <= n; i++)
{
res += temp;
}
}else
{
string temp;
if( a2 == "ab" || a2 == "bc" || a2 == "ca" ) temp = "cba";
else temp = "abc";
for (int i = 1; i <= n; i++)
{
res += temp;
}
}
cout << "YES" << '\n';
cout << res << '\n';
return 0;
}
for (int i = 0; i < 6; i++)
{
string temp = z[i];
int flag = 0;
for (int j = 0; j < 2; j++)
{
string s1;
s1 += temp[j],s1 += temp[j+1];
if( s1 == a1 || s1 == a2 )
{
flag = 1;
break;
}
}
if( flag == 0 )
{
for (int j = 0; j < 3; j++)
{
for (int k = 1; k <= n; k++)
{
res += temp[j];
}
}
break;
}
}
cout << "YES" << '\n';
cout << res << '\n';
return 0;
}