当前位置:网站首页>E. Two Small Strings
E. Two Small Strings
2022-07-26 09:29:00 【逃夭丶】
传送门:http://codeforces.com/problemset/problem/1213/E
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
input
2
ab
bc
output
YES
acbbac
input
3
aa
bc
output
YES
cacbacbab
input
1
cb
ac
output
YES
abc
题意
询问字符串 str 是否存在,str 由 n 个’a’,n 个’b’,n 个’c’ 组成,并且输入的长度为 n 的字符串 st1,st2 不是 str 的子串,若存在,输出任意情况。
思路
才开始想了好久,没有思路,因为比较菜的缘故,找不到分类点,所以就直接暴力枚举了。
枚举一定数量的 str,然后用 string 的 find 函数,都找不到的话,就输出,return 0;最后找不到的话,就 NO。
枚举的话,因为 str 是由 n 个“abc” 组成,那我们可以 next_permutation,找出“abc”的所有情况,然后 n 倍扩张即可。
#include<cstdio>
#include<iostream>
#include<cmath>
#include<algorithm>
#include<cstring>
#include<cctype>
#include<vector>
#include<stack>
#include<queue>
#include<ctime>
#include<utility>
#include<map>
#define ll long long
#define ld long double
#define ull unsigned long long
using namespace std;
typedef pair<int,int> P;
const int INF = 0x3f3f3f3f3f;
const ll LNF = 0x3f3f3f3f3f3f3f;
const double eps = 1e-6;
const int maxn = 150010;
string abc = "abc";
string st1,st2;
vector<string> st;
int main(void)
{
int n;
cin>>n>>st1>>st2;
do{
string st3;
for(int i=0;i<n;i++)
st3 += abc;
st.push_back(st3);
st.push_back(string(n,abc[0])+string(n,abc[1])+string(n,abc[2]));
}while(next_permutation(abc.begin(),abc.end()));
vector<string>::iterator it = st.begin();
while(it!=st.end()){
string st4 = *it;
if(st4.find(st1)==-1&&st4.find(st2)==-1){
printf("YES\n");
cout<<st4<<endl;
return 0;
}
it++;
}
printf("NO\n");
return 0;
}
边栏推荐
猜你喜欢
mysql5.7.25主从复制(单向)
arc-gis基础操作3
OFDM Lecture 16 - OFDM
Arc GIS basic operation 3
arc-gis的基本使用2
PMM(Percona Monitoring and Management )安装记录
Windows backs up the database locally by command
js中树与数组的相互转化(树的子节点若为空隐藏children字段)
2022 Shanghai safety officer C certificate examination questions and mock examination
MySQL transaction
随机推荐
官方颁发的SSL证书与自签名证书结合实现网站双向认证
[Online deadlock analysis] by index_ Deadlock event caused by merge
简单行人重识别代码到88%准确率 郑哲东 准备工作
Source code analysis of object wait notify notifyAll
安卓 实现缓存机制,多种数据类型缓存
Mysql事务
Fiddler下载安装
dll中的全局变量
(二)面扫描仪与机械臂的手眼标定(眼在手外:九点标定)
csdn空格用什么表示
Table extraction for opencv table recognition (2)
php执行shell脚本
antUI中a-modal 拖拽功能制作
Order based evaluation index (especially for recommendation system and multi label learning)
nodejs服务后台执行(forever)
MySQL transaction
Personality test system V1.0
点击input时,不显示边框!
Exception handling mechanism II
MySql5.7.25源码安装记录