UVa:10132 File Fragmentation

来源:互联网 发布:windows 10安装大小 编辑:程序博客网 时间:2024/06/02 21:19

其实想明白了还是道挺简单的题。

 

最大的碎片跟最小的组合中一定有原文件。次最大和次最小的碎片的组合中也一定有原文件。
很明显两个组合中相同的那个文件就是原文件。

因为没有完全相同的碎片,所以即使最大的和最小的碎片都有两个,这个思路也是完全可行的。

 

算是贪心吧,这是解决问题的一个很关键的点。

 

当然如果一共就两块碎片那就不用麻烦了,它俩的组合一定就是答案。

 

今天重做这道题居然一遍AC,想当时被卡了好久。。。
老师说过scanf、printf跟cin、cout混用会导致莫名的现象,因为这里用到了string,所以全换成cin、cout了。

 

#include <cstdio>#include <cstring>#include <iostream>#include <algorithm>using namespace std;bool cmp(string a,string b){    return a.size()<b.size();}int main(){    int T;    cin>>T;    cin.ignore();    cin.ignore();    while(T--)    {        string str[150];        int n=0;        while(getline(cin,str[n])&&!str[n].empty())n++;        if(n<=2)            cout<<str[0]+str[1]<<endl;        else        {        sort(str,str+n,cmp);        string a=str[0],b=str[1],c=str[n-2],d=str[n-1];        if(a+d==b+c||a+d==c+b) cout<<a+d<<endl;        else if(d+a==b+c||d+a==c+b) cout<<d+a<<endl;        else if(a+c==b+d||a+c==d+b) cout<<a+c<<endl;        else if(c+a==b+d||c+a==d+b) cout<<c+a<<endl;        }        if(T) cout<<endl;    }    return 0;}


 

原创粉丝点击