Two Small Strings(简单的模拟+构造)

题目描述:

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.

输入描述

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'.

输出描述

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.

样例输入 1 

2
ab
bc

样例输出 1 

YES
acbbac

样例输入 2 

3
aa
bc

样例输出 2 

YES
cacbacbab

样例输入 3 

1
cb
ac

样例输出 3 

YES
abc

思路:看到的第一眼,是想,将abc三个两两全排列,这样数也没有多少,给出谁,就不可以用谁,看看之后的组合有什么规律没有,这个想法很明显失败之后就看到abc都要用到,那就很自然想到是不是abc全排列,我不断加上就可以,abc全排列只有六种,所以即使嵌套for循环时间也不会很长,同样模拟题干意思,不能出现已经给出的样式,那我就写一个bool函数,判断一下,但是要注意啊数组默认是从0开始,这样写出来以后样例它通过了,但是WA了,debug一下,肯定是漏情况了,试着令s1=ab  s2=ba发现,abc全排列不行了只能是aaacccbbb,所以又增加了一种可能性,其实事后再琢磨这道题,倒着想,结果不是乱排的就是整整齐齐这样的,代码如下:

 

#include<iostream>
#include<cstdio>
#include<string>
#include<cstring>
using namespace std;
string s[6]={"abc","bac","acb","bca","cba","cab"};
 string s1,s2,str;
bool check(){
    for(int i=1;i<str.size();i++){
        if(str[i-1]==s1[0]&&str[i]==s1[1]||str[i-1]==s2[0]&&str[i]==s2[1]){return false;}
    }
    return true;
}
int main(){
   
    ios::sync_with_stdio(false);

     std::cin.tie(0);
     std::cout.tie(0);
   //为了节省时间,可以不用写快读快写,也可以像这个一样关闭同步流
    int n;
    cin>>n;
    cin>>s1>>s2;
    for(int i=0;i<6;i++){
        str.clear();//记得初始化,否则会保留上次循环的结果
        for(int j=1;j<=n;j++){
            str+=s[i];
        }
        if(check()){
            cout<<"YES"<<endl;
            cout<<str<<endl;
            return 0;//记得此时结果都出来了就结束程序吧
        }
        str.clear();//下面是在增加一种情况比如s1=ab  s2=ba,那么abc的全排列已经不能满足,只能是出现aaacccbbb这种情况
        for(int j=1;j<=n;j++){str+=s[i][0];}
        for(int j=1;j<=n;j++){str+=s[i][1];}
        for(int j=1;j<=n;j++){str+=s[i][2];}
        if(check()){
            cout<<"YES"<<endl;
            cout<<str<<endl;
            return 0;
        }
    }
    cout<<"NO"<<endl;//都不满足就是no
    
    
    
    
    
    
    
    return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值