c_cpp 从给定模式生成所有二进制字符串
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了c_cpp 从给定模式生成所有二进制字符串相关的知识,希望对你有一定的参考价值。
/*
http://ideone.com/9enXkm
http://www.geeksforgeeks.org/generate-all-binary-strings-from-given-pattern/
http://www.practice.geeksforgeeks.org/problem-page.php?pid=884
*/
#include <iostream>
#include <string>
#include <queue>
using namespace std;
void printBinary(string s){
queue<string> q;
q.push(s);
while(!q.empty()){
string str = q.front();
size_t i = str.find('?');
if(i != string::npos){
str[i] = '0';
q.push(str);
str[i] = '1';
q.push(str);
}
else
cout << str << " ";
q.pop();
}
}
int main() {
int t;
cin >> t;
while(t--){
string s;
cin >> s;
printBinary(s);
cout << endl;
}
return 0;
}
/*
http://ideone.com/PKJTxq
http://www.geeksforgeeks.org/generate-all-binary-strings-from-given-pattern/
http://www.practice.geeksforgeeks.org/problem-page.php?pid=884
*/
#include <iostream>
#include <string>
#include <vector>
using namespace std;
void printBinary(string s, int i){
if(i == s.size()){
cout << s << " ";
return;
}
if(s[i] == '?'){
s[i] = '0';
printBinary(s, i+1);
s[i] = '1';
printBinary(s, i+1);
}
else
printBinary(s, i+1);
}
int main() {
int t;
cin >> t;
while(t--){
string s;
cin >> s;
printBinary(s, 0);
cout << endl;
}
return 0;
}
以上是关于c_cpp 从给定模式生成所有二进制字符串的主要内容,如果未能解决你的问题,请参考以下文章