E. Two Small Strings(分析+模拟)

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 1n105

分析:

    我们可以考虑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;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值