int main() { std::string seq = "abc"; std::vector<std::string> seqs = p_all(seq, seq.length()); for(std::vector<std::string>::iterator iter = seqs.begin(); iter != seqs.end(); ++iter) std::cout<<*iter<<std::endl; return 0; }
int main(void) { int ary[_SIZE_], i, mod; srand((unsigned int)time(NULL)); for (i=0, mod=_SIZE_<<3; i<_SIZE_; i++) ary[i] = rand()%(mod); printf("排序前: "); p_all(ary, _SIZE_); quick_sort(ary, 0, _SIZE_-1); printf("排序後: "); p_all(ary, _SIZE_); return 0; }
std::vector<std::string> p_all(const std::string& seq, int n) { std::vector<std::string> seqs; if(n == 1) { seqs.push_back(std::string(1, seq[0])); return seqs; } std::vector<std::string> sub_seqs = p_all(seq, n-1); for(std::vector<std::string>::iterator iter = sub_seqs.begin(); iter != sub_seqs.end(); ++iter) { std::string sub_seq = *iter; seqs.push_back(seq[n-1] + sub_seq); for(int i = 1; i < n-1; ++i) seqs.push_back(sub_seq.substr(0, i) + seq[n-1] + sub_seq.substr(i, n-1-i)); seqs.push_back(sub_seq + seq[n-1]); } return seqs; }