UVA - 10132 File Fragmentation

题目链接:

File Fragmentation

思路:

首先将输入的字符串根据长度排序,最终的字符串长度=s[0].size()(最短的字符串长度)+s[ind-1].size()(最长的字符串的长度)。然后利用排序后的第一个字符串和第二个字符串还原原来的字符串。
坑点:
注意输入输出,最后一个case输出的时候不换行,其余输出都要换行
然后注意除了第一次读入字符串需要空行,剩下几个case读入都不用再空行…

#include <iostream>
#include<cstdio>
#include<string>
#include<algorithm>
using namespace std;
string s[200]; 
int vis[200];
bool cmp(const string &a,const string &b){
	return a.size()<b.size();
}
int main(int argc, char** argv) {
	int Case;
	scanf("%d",&Case);
	getchar();//换行
	getchar();//空行 
	while(Case--){
		string str;
		int ind=0;
		while(getline(cin,str)){//cin跳过空行 
			if(!str.length()) break;
			s[ind++]=str;
		}
		sort(s,s+ind,cmp);
		int L=s[ind-1].size()+s[0].size();
		int flag=0;
		for(int j=ind-1;s[j].size()==s[ind-1].size();j--){
			string s1=s[0]+s[j];
			string s2=s[j]+s[0];
			for(int k=ind-2;s[k].size()+s[1].size()==L;k--){
				string t1=s[1]+s[k];
				string t2=s[k]+s[1];
				if(t1==s1||t1==s2){
					cout<<t1<<endl;
					flag=1;
					break;
				}
				else if(t2==s1||t2==s2){
					cout<<t2<<endl;
					flag=1;
					break;
				}
			}
			if(flag)
				break;
		}
		if(Case!=0) cout<<endl;
	}
	return 0;
}

猜你喜欢

转载自blog.csdn.net/zhuixun_/article/details/84401639