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;
}

 

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