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;
}