UVA13075 题解
简要题意
给定
题目思路
通过 DFS 来枚举出每一位,当所有
这里有个问题,如何实现字典序,题目中提到这个字符串只会存在
注:UVA 格式都很恶心,这题格式需要注意行末无空格,具体实现看代码。
代码实现
通过记录
#include<iostream>
#include<bits/stdc++.h>
using namespace std;
bool is_first;
void dfs(int left_a,int left_b,string str){
if(left_a==0 && left_b==0){
if(is_first){ // 判断字符串不为第一个
cout<<' '; // 否:输出前一个字符串的空格
}else{
is_first=1;
}
cout<<str; // 避免如果是最后一个,不用输出空格
return;
}
if(left_a>0){ // 如果还能放C,就优先放C
dfs(left_a-1,left_b,str+'C');
}
if(left_b>0){ //注意不要加else
dfs(left_a,left_b-1,str+'V');
}
}
int main(){
int T;
cin>>T;
int a,b;
while(T--){ // 多组数据记得清空
cin>>a>>b;
is_first=0;
string ss="";
dfs(a,b,ss);
cout<<endl;
}
return 0;
}