当前位置:网站首页>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;
}
边栏推荐
猜你喜欢

电机转速模糊pid控制

神经网络与深度学习-6- 支持向量机1 -PyTorch

keepalived 实现mysql自动故障切换

MySql5.7.25源码安装记录

matlab中的AR模型短时预测交通流

【Flutter -- 布局】Align、Center、Padding 使用详解

Innovus is stuck, prompting x error:

JS output diamond on the console

matlab simulink实现模糊pid对中央空调时延温度控制系统控制

2022 tea artist (intermediate) special operation certificate examination question bank simulated examination platform operation
随机推荐
Smart gourmet C language
大二上第三周学习笔记
登录模块用例编写
Process32first returns false, error x message 24
Zxing simplified version, reprinted
Windows backs up the database locally by command
Wechat applet avatarcropper avatar clipping
青少年软件编程等级考试标准解读_二级
Global variables in DLL
VS2019配置opencv
keepalived 实现mysql自动故障切换
Malloc failed to allocate space and did not return null
Bloom filter
opencv图像处理
js在控制台输出菱形
面试题目大赏
sublime 安装插件
Use of OpenCV class
EOJ 2020 1月月赛 E数的变换
您的登录IP不在管理员配置的登录掩码范围内