【Codeforces - 632C】The Smallest String Concatenation (对string巧妙排序)
題干:
You're given a list of?n?strings?a1,?a2,?...,?an. You'd like to concatenate them together in some order such that the resulting string would be lexicographically smallest.
Given the list of strings, output the lexicographically smallest concatenation.
Input
The first line contains integer?n?— the number of strings (1?≤?n?≤?5·104).
Each of the next?n?lines contains one string?ai?(1?≤?|ai|?≤?50) consisting of only lowercase English letters. The sum of string lengths will not exceed?5·104.
Output
Print the only string?a?— the lexicographically smallest string concatenation.
Examples
Input
4 abba abacaba bcd erOutput
abacabaabbabcderInput
5 x xx xxa xxaa xxaaaOutput
xxaaaxxaaxxaxxxInput
3 c cb cbaOutput
cbacbc?
解題報告:
類似于nyoj 1233這道題(這題需要處理大數)。本題直接排序就好了。這題如果不用string的話,需要用Node結構體存char [] ,然后對Node排序(用strcmp函數),不知道時間上會不會快一點?反正這個題用string類是300+ms左右。
AC代碼:
#include<cstring> #include<iostream> #include<algorithm> #include<cstdio> using namespace std; string s[50000 + 5]; int dp[1005][1005]; int len1,len2; bool cmp(string a,string b) {return a+b < b+a; } int main() {int n;cin>>n;for(int i = 1; i<=n; i++) {cin>>s[i];}sort(s+1,s+n+1,cmp); // for(int i = 1; i<=n; i++) { // cout << s[i]<<endl; // }for(int i = 1; i<=n; i++) {cout << s[i];}return 0 ; }總結
以上是生活随笔為你收集整理的【Codeforces - 632C】The Smallest String Concatenation (对string巧妙排序)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: nkvmon.exe - nkvmon是
- 下一篇: noads.exe - noads是什么