仅供参考 #第二题 (1369)#include<iostream> #include<vector> using namespace std; void DFS(int nowDepth, int maxDepth, int n, int k, vector<string> &ans, string tmpSeq){ if(nowDepth >= maxDepth){ ans.push_back(tmpSeq); return; } if(n > 0){ // choose "*" tmpSeq += "*"; DFS(nowDepth+1, maxDepth, n-1, k, ans, tmpSeq); tmpSeq.pop_back(); } if(k > 0){ // choose "|" tmpSeq += "|"; DFS(nowDepth+1, maxDepth, n, k-1, ans, tmpSeq); tmpSeq.pop_back(); } } int main(){ int n, k; cin >> n >> k; vector<string> ans; string tmpSeq = ""; DFS(0, n+k-1, n, k-1, ans, tmpSeq); cout << ans.size() << endl; for(auto str: ans){ cout << str << endl; } system("pause"); return 0; }
点赞 2

相关推荐

01-07 15:50
四川大学 Java
看日出看日落:好好背八股,做算法。我身边跟你bg差不多的基本都大厂暑期
点赞 评论 收藏
分享
牛客网
牛客企业服务