其他分享
首页 > 其他分享> > 2204 字符串连接

2204 字符串连接

作者:互联网

2204 字符串连接

注意

下面这种做法是错误的,因为

/*
4
ba
b
bc
bb
这种样例应该输出babbbbc
而程序缺输出了bbabbbc
这是因为set集合的确是按字典序来排的,但按字典序来排并不是这道题的正解。
*/


#include<bits/stdc++.h>
using namespace std;
set<string> s; 
int main(void){
    int n;
    cin >> n;
    for(int i = 1; i <= n; i++){
        string t;
        cin >> t;
        s.insert(t);
    }
    set<string> :: iterator it;
    for(it = s.begin(); it != s.end(); it++){
        cout<<*it;
    }
    cout<<endl;
    return 0;
} 

正确做法

#include<bits/stdc++.h>
using namespace std;
const int maxn = 1e3+10;
string s[maxn];
bool cmp(string s,string t){
    return s+t < t+s;
}
int main(void){
    int n;
    cin >> n;
    for(int i = 0; i < n; i++) cin >> s[i]; 
    sort(s,s+n,cmp);
    for(int i = 0; i < n; i++) cout<<s[i];
    cout<<endl;
    return 0;
} 

标签:set,2204,string,int,cin,++,字符串,连接,cout
来源: https://www.cnblogs.com/AC-AC/p/12448144.html