当前位置:网站首页>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;
}
边栏推荐
猜你喜欢
Ext4 file system opens dir_ After nlink feature, link_ Use link after count exceeds 65000_ Count=1 means the quantity is unknown
电机转速模糊pid控制
[online problem] timeout waiting for connection from pool problem troubleshooting
ZXing简化版,转载
Basic use of Arc GIS 2
Neural network and deep learning-6-support vector machine 1-pytorch
docker配置mysql集群
JS output diamond on the console
PMM(Percona Monitoring and Management )安装记录
arcgis的基本使用1
随机推荐
2020-12-29
Great reward for interview questions
大二上第一周学习笔记
arcgis的基本使用1
Under a directory of ext3 file system, subfolders cannot be created, but files can be created
【Mysql数据库】mysql基本操作集锦-看得会的基础(增删改查)
TableviewCell高度自适应
青少年软件编程等级考试标准解读_二级
QT随手笔记(六)——更新界面、截图、文件对话框
Jmeter配置元件之CSV数据文件设置
解决IE7 & IE8 存储cookie问题
Add DLL
VectorTileLayer更换style
【Flutter -- 布局】Align、Center、Padding 使用详解
ie7设置overflow属性失效解决方法
docker配置mysql集群
OpenCV 表格识别之表格提取(二)
C managed and unmanaged
The problem of the sum of leetcode three numbers
matlab simulink实现模糊pid对中央空调时延温度控制系统控制