全排列和find函数以及vector的应用

C++: string 中find函数的用法以及string::npos的含义:https://blog.csdn.net/linwh8/article/details/50752733
c++ 中for (auto &str : vec) for (auto &c : str) c = toupper©;是什么意思为什么要用两层for
第一层for是遍历vec向量里的各个字符串,第二层for遍历字符串里的各个字符,并把字符转换为大写
输出序列{1,2,3,4}字典序的全排列。

#include<iostream>
#include<algorithm>
using namespace std;
int main()
{
	int ans[4]={1,2,3,4};
	sort(ans,ans+4);    /* 这个sort可以不用,因为{1,2,3,4}已经排好序*/
	do                             /*注意这步,如果是while循环,则需要提前输出*/
	{
		for(int i=0;i<4;++i)
			cout<<ans[i]<<" ";
		cout<<endl;
	}while(next_permutation(ans,ans+4));
	return 0;
}

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
inputCopy
2
ab
bc
outputCopy
YES
acbbac
inputCopy
3
aa
bc
outputCopy
YES
cacbacbab
inputCopy
1
cb
ac
outputCopy
YES
abc
代码:

#include <bits/stdc++.h>
using namespace std;
int main()
{
    int n;
    string s, t;
    cin >> n >> s >> t;
    string abc = "abc";
    vector<string> res;
    do
    {
        string cur;
        for (int i = 0; i < n; ++i)
            cur += abc;
        res.push_back(cur);
        res.push_back(string(n, abc[0]) + string(n, abc[1]) + string(n, abc[2]));
    }
    while (next_permutation(abc.begin(), abc.end()));
 
    for (auto str : res)
    {
        //cout<<str<<"---"<<endl;
        if (str.find(s) == string::npos && str.find(t) == string::npos)
        {
            cout << "YES" << endl << str << endl;
            return 0;
        }
    }
    assert(false);
    cout << "NO" << endl;
 
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值