- 论坛徽章:
- 0
|
如果你只是求若干字母的排列组合,可以用下面的代码编译后: echo "abaccedf" | ./mytool
我的机器上求8个字母的全排列也只要0.91秒。
要是不输出到屏幕上,而是重定向到文件里,比如 echo "abcdefgh" | ./mytool > data.txt
只需要0.16秒
求全排列的程序
- #include <iostream>
- #include <vector>
- #include <iterator>
- #include <algorithm>
- int main()
- {
- using namespace std;
- typedef vector<char> vec_char;
- vec_char thestr;
- copy(istream_iterator<char>(cin),istream_iterator<char>(),back_insert_iterator<vec_char>(thestr));
- sort(thestr.begin(),thestr.end());
- while(next_permutation(thestr.begin(),thestr.end()))
- {
- copy(thestr.begin(),thestr.end(),ostreambuf_iterator<char>(cout));
- cout << endl;
- }
- return 0;
- }
复制代码 |
|