FPJ Problem 2088 最長隊名

一,問題描述

二,問題分析

1.題目簡單分析就會有思路,即按照字典順序排序,並且拼接字符串,按照字典序的話可以直接排序算法,或者採用multiset

2.multiset可以保證插入的字符串按順序排列

3.考慮到輸入的隊員姓名會有重名的情況,所以不採用set而使用multiset

三,問題解答

#include<iostream>
#include<string>
#include<set>
using namespace std;

int main() {
	int T;			//測試數據的組數
	cin >> T;
	for (int i = 0; i < T; i++) {
		int n;			//隊員數
		cin >> n;
		multiset<string> team;
		string res;
		for (int i = 0; i < n; i++)			//輸入隊員名字
		{
			string name;
			cin >> name;
			team.insert(name);				//插入mulitset中
		}
		for (multiset<string>::iterator it = team.begin(); it != team.end(); it++) {		//利用迭代器遍歷
			res.append(*it);						//append()函數進行拼接
		}
		cout << res << endl;
		team.clear();							//及時清空集合
	}

	return 0;
}

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章