PAT 1038. Recover the Smallest Number
来源:互联网 发布:二分搜索完整算法 编辑:程序博客网 时间:2024/06/08 20:15
给定N(N<10000)段“数字片段”,把它们连成一个数,要求连成的数尽量小。
当时有个朦胧的想法是“我要把小的片段尽量排前面”,但是怎么样算是“小的片段”呢?
然后是想到“前面的不会影响到后面的”,我不知道具体该怎么表达,反正想到“前面的不会影响到后面的”以后,就感觉这个问题是贪心着来,不用动规着来,也就是说,只用一遍排序,然后就可以连起来输出了。
然后思考的就是,一遍排序,那么只需要对“数字片段”进行两两地判大小就好。
所以重点就在怎么比较了。显然,片段a和片段b如果一样长,比较方法就不用说了。如果其中有一个比较长,比如说b比a长,那么,先比较b前面那部分,如果b前面还没超出a长度的那部分就能比出大小,那也没问题了。如果b前面还没有超出a的部分,每一位都跟a的对应位相等,怎么办?我的方法是比较a+b和b+a。(还是说不出理由来,各种凭直觉呃)
就这样,过掉了。
#include<cstdio>#include<string>#include<algorithm>#include<iostream>using namespace std;const int N=10003;string s[N];int n;bool comp(string a,string b){string c;int i;for(i=0;i<a.length()&&i<b.length();i++)if(a[i]!=b[i])return a[i]<b[i];c=a;a=a+b;b=b+c;return a<b;}int main(){scanf("%d",&n);int i;for(i=0;i<n;i++)cin>>s[i];sort(s,s+n,comp);string ans="";for(i=0;i<n;i++)ans+=s[i];while(ans.size()>1&&ans[0]=='0')ans.erase(ans.begin());cout<<ans<<endl;return 0;}
0 0
- 1038. Recover the Smallest Number (30)-PAT
- PAT 1038. Recover the Smallest Number
- PAT 1038. Recover the Smallest Number
- PAT 1038. Recover the Smallest Number
- PAT 1038. Recover the Smallest Number (30)
- 【PAT】1038. Recover the Smallest Number (30)
- [pat]1038. Recover the Smallest Number (30)
- pat 1038. Recover the Smallest Number (30)
- PAT 1038. Recover the Smallest Number
- 【PAT】1038. Recover the Smallest Number
- PAT 1038. Recover the Smallest Number (30)
- PAT--1038. Recover the Smallest Number
- 浙大PAT 1038题 1038. Recover the Smallest Number
- PAT A 1038. Recover the Smallest Number (30)
- PAT (Advanced) 1038. Recover the Smallest Number (30)
- PAT(A) - 1038. Recover the Smallest Number (30)
- 【PAT甲级】1038. Recover the Smallest Number (30)
- 1038. Recover the Smallest Number (30) PAT甲级
- vlc在windows下的开发使用
- msdn之断言篇
- 【最短路径】hdu 3790
- eclipse 添加dtd
- java内部类
- PAT 1038. Recover the Smallest Number
- 理解矩阵
- LeetCode | Validate Binary Search Tree(有效的二叉搜索树)
- NoClassDefFoundError与ClassNotFoundException的区别
- 增量式修改检验和(IP, TCP, UDP)算法的研究和实现
- c# combobox绑定数据源
- 在RAC中,当私有网线拔了后,会怎么样?
- 使用solr搭建你的全文检索
- 创建一个Fraction类(分数)实现分数的加减乘除,比较大小、约分等方法。 要求:为类添加属性