1198. Substring 8个串排出最小字典序(8!枚举)

/*
1198. Substring 8个串排出最小字典序(8!枚举)
题目大意:
          用N个字符串拼成一个新的字符串
          要求新字符串字典序最小
          如: a ab ac
          则答案为:aabac
          1<=N<=8,每个字符串长度不超过100
解题思路:
          枚举n!种情况,最多为8!=40320种情况;
          每枚举一种情况,与当前字典序最小字符串比较,如果字典序更小,则更新答案;
          递归求解。
          反例 ba b  bab bba
重点注意
          1.重点:学会使用sort函数 (#include <algorithm>)
          2.重点:了解next_permutation();(#incluide <algorithm>)
*/
#include <iostream>
#include <string>
#include<algorithm>

using namespace std;
//1.重点:学会使用sort函数 (#include <algorithm>) 
/*int cmp(string a,string b){
    if((a+b)>(b+a))
        return 0;
    else
        return 1;
}*/

bool cmp(string a , string b){
    return (a+b)<(b+a);
}

int main(){
    
    int T;
    int N;
    string a[8];
    cin >> T;
    
    for(int i=0;i<T;i++){
        cin >> N;
        if (N<=0) break;
        for(int j=0;j<N;j++){
            cin >> a[j]; 
        }
//1.重点:学会使用sort函数 (#include <algorithm>)         
        sort(a,a+N,cmp);
        
        for(int k=0;k<N;k++){
            cout<<a[k];
        }  
        cout << endl;
        //system("pause");                      
    }    
} 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值