您的位置:首页 > 其它

1038. Recover the Smallest Number

2015-10-28 15:11 429 查看


1038. Recover the Smallest Number (30)

时间限制

400 ms

内存限制

65536 kB

代码长度限制

16000 B

判题程序

Standard

作者

CHEN, Yue

Given a collection of number segments, you are supposed to recover the smallest number from them. For example, given {32, 321, 3214, 0229, 87}, we can recover many numbers such like 32-321-3214-0229-87 or 0229-32-87-321-3214 with respect to different orders
of combinations of these segments, and the smallest number is 0229-321-3214-32-87.

Input Specification:

Each input file contains one test case. Each case gives a positive integer N (<=10000) followed by N number segments. Each segment contains a non-negative integer of no more than 8 digits. All the numbers in a line are separated by a space.

Output Specification:

For each test case, print the smallest number in one line. Do not output leading zeros.
Sample Input:
5 32 321 3214 0229 87

Sample Output:

22932132143287

#include<iostream>
#include<string>
#include<vector>
#include<algorithm>
using namespace std;

vector<string> v;
bool cmp(string A, string B)
{
if(A.size() > B.size())
{
for(int i = 0; i < A.size(); i ++)
{
if(A[i] != B[i%B.size()])
return A[i] < B[i%B.size()];
}
}
else
{
for(int i = 0; i < B.size(); i ++)
{
if(A[i%A.size()] != B[i])
return A[i%A.size()] < B[i];
}
}
}

int main()
{
int n;
cin>>n;
string s;

for(int i = 0; i < n; i ++)
{
cin>>s;
v.push_back(s);
}

sort(v.begin(), v.end(), cmp);

string str;
for(int i = 0; i < v.size(); i ++)
str += v[i];
int index = 0;
while(str[index] == '0')
index ++;
str.erase(0, index);
if(str.size() == 0)
cout<<"0"<<endl;
else
cout<<str<<endl;

return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: