关于c++:全排列生成BI-生成序列基础上机试题

5次阅读

共计 1728 个字符,预计需要花费 5 分钟才能阅读完成。

在计算机科学畛域,如何高效生成指定的序列是一个十分重要的问题。当初给你一个字符串,且这个字符串外部的字符曾经升序有序。请你找出由这些字符形成的所有的序列。
输出
输出的第一行是一个整数 n,示意测试数据组数。
接下来 n 行,每行输出一个字符升序有序的字符串。字符串中只蕴含小写字母,长度不超过 8。
输入
对于每组输出,输入由输出的字符串中的字符形成的所有序列,按字典序升序排列,后果中不能呈现雷同的序列。
每组输入前面跟一个空行。
样例输出 Copy
3
ab
abc
bca
样例输入 Copy
ab
ba

abc
acb
bac
bca
cab
cba

abc
acb
bac
bca
cab
cba

next_permutation 用法参考 https://blog.csdn.net/ac_gibs…

//#include<iostream>
//#include<string>
//#include<string.h>
//#include<algorithm>
//#include<cstring>
//using namespace std;
//
//int main(){
//    int t = 0;
//    scanf("%d",&t);
//    while(t--){
//        string s;
//        cin >> s;
//        if(s.length() <= 8){//            sort(s.begin(),s.end());
//            do// 全排列 
//            {
//                cout<< s <<endl;
//            }while(next_permutation(s.begin(),s.end()));
//            cout << endl;
//        }        
//    }
//    return 0;
//}
//// 求字符循环流动的算法如下 
////void change(string &s) { // 在 while 循环中会不停调用此函数不停变换短串 s2
////    char t;
////    int i;
////    t = s[0];// 用哨兵暂存储第一个字符
////    for(i = 0; i < s.length() - 1; i++) { // 不要超界
////        s[i] = s[i + 1];// 将前面的字符赋值给后面,不停实现轮换以测验变换后的多个 s2...... 是否会在 s1 中呈现
////    }
////    s[i] = t;// 将哨兵中的字符赋值给字符串最初一个地位
////}
//// 排序形式能够为疾速排序,这里写一遍快排(严蔚敏)////void Quick_Sort(int a[], int low, int high){////    if(low < high){////        int pivot = Partiton(a, low, high);
////        Quick_Sort(a, low, pivot - 1);
////        Quick_Sort(a, pivot + 1, high);
////    }
////} 
////int Partiton(int a[], int low, int high){////    int pivot  = a[low];
////    while(low < high){////        while(low < high && a[high] >= pivot){
////            --high;
////        }
////        a[low] = a[high];
////        while(low < high && a[low] <= pivot){
////            ++low;
////        }
////        a[high] = a[low];
////    }
////    a[low] = pivot;
////    return low;
////}

另一种写法可供参考

//#include<iostream>
//#include<string>
//#include<string.h>
//#include<algorithm>
//#include<cstring>
//using namespace std;
//
//int main(){
//    int t = 0;
//    scanf("%d",&t);
//    char s[10];
//    while(t--){//        scanf("%s",s);
//        int len = strlen(s);
//        if(len <= 8){//            sort(s,s+len);
//            do// 全排列 
//            {//                printf("%s\n",s);
//            }while(next_permutation(s,s+len));
//            printf("\n");
//        }        
//    }
//    return 0;
//}

正文完
 0