解题思路: 对于全排列的问题,STL,提供了一个强大的函数,
bool next_permutation(iterator.begin() , iterator.end());
对于当前的序列如果不是最后一个序列则返回真,否则返回假。
uva 10098 - Generating Fast
题目链接: 点击打开链接
代码:
#include <iostream> #include <cstdio> #include <string> #include <cstdlib> #include <algorithm> using namespace std; string str; int main(){ int n; scanf("%d" , &n); while(n--){ string str; cin>>str; while(next_permutation(str.begin() , str.end())){ cout<<str<<endl; } cout<<endl; } return 0 ; }
uva 729 - The Hamming Distance Problem
题目链接:点击打开链接
代码:
#include <iostream> #include <cstdio> #include <cstring> #include <cstdlib> #include <algorithm> using namespace std; int main() { int Case , n, h; bool first = true; scanf("%d" , &Case); while(Case--){ if(first) first = false; else printf("\n"); string str; scanf("%d%d", &n, &h); for (int i = 0; i < n; i++) { if (i >= n - h) str += '1'; else str += '0'; } cout<<str<<endl; while (next_permutation(str.begin(), str.end())) cout << str << endl; } return 0; }
uva 146 - ID Codes
题目链接:点击打开链接
代码:
#include <iostream> #include <cstdio> #include <cstring> #include <cstdlib> #include <string> #include <algorithm> using namespace std; string str; int main(){ while(cin>>str){ if(str == "#") break; if(next_permutation(str.begin(),str.end())) cout<<str<<endl; else printf("No Successor\n"); } return 0; }