Codeforces Round #566 (Div. 2) C. Beutiful Lyrics

题目链接:https://codeforc.es/contest/1182/problem/C
这道题是很久之前的一个坑,一开始没看到每个单词都有元音的这个条件,于是尝试了几次都没有补掉,今天下决心仔细看了一下,才看到这个条件。但还是理不清思路,不知道用什么STL来存储和处理。于是去看了tutorial的代码,不得不说代码风格真是太赞了,即使是用了我不熟悉的c++11特性,恰到好处的变量名也让我一看就懂。
思路:定义一个map<int, map<char, vector<string>>> mp,这样就可以通过mp[vowel_num][last_vowel]来获取相同元音数量和相同最后元音的单词,设两种二元组,一种是完美二元组,元音数量和最后元音都相同,另一种是半完美二元组,元音数量相等,但最后元音不同。在一个vowel_num里面先把所有的完美二元组提取出来用vector<pair<string, string>>存起来,剩下的就是半完美二元组,这时再用同样的方法提取出来。最后就没有什么难点,讨论处理就可以了。

#include<iostream>
#include<map>
#include<vector>
#include<string>
using namespace std;
inline int getVowelNum(string &s){
	int len = s.size();
	int cnt = 0;
	for(int i=0; i<len; ++i){
		if(s[i]=='a' || s[i]=='e' || s[i]=='i' || s[i]=='o' || s[i]=='u') cnt++;
	}
	return cnt;
}
inline char getLastVowel(string &s){
	for(int i=s.size()-1; i>=0; --i){
		if(s[i]=='a' || s[i]=='e' || s[i]=='i' || s[i]=='o' || s[i]=='u') return s[i];
	}throw("NULL");
}
int main()
{
	map<int, map<char, vector<string>>> string_by_vowels;
    int n;
    string s;
	cin >> n;
	//get input
	for(int i=1; i<=n; ++i){
		cin >> s;
		int vowel_num = getVowelNum(s);
		char last_vowel = getLastVowel(s);
		string_by_vowels[vowel_num][last_vowel].push_back(s);
	}
	
	vector<pair<string, string>> complete_duos, semicomplete_duos;
	for(auto &same_num : string_by_vowels){
		//get complete duos
		for(auto &same_last : same_num.second){
			while(same_last.second.size() >= 2){
				string s1 = same_last.second.back();
				same_last.second.pop_back();
				string s2 = same_last.second.back();
				same_last.second.pop_back();
				complete_duos.push_back( make_pair(s1, s2) );
			}
		}
		//get semicomplete duos
		vector<string> rest_words;
		for(auto &same_last : same_num.second){
			if(same_last.second.size() >= 1){
				rest_words.push_back(same_last.second.back());
				same_last.second.pop_back(); //!!!!
			}
		}
		while(rest_words.size() >= 2){
			string s1 = rest_words.back();
			rest_words.pop_back();
			string s2 = rest_words.back();
			rest_words.pop_back();
			semicomplete_duos.push_back(make_pair(s1, s2));
		}
	}
	
	//get beautiful lyrics
	int len_of_complete = complete_duos.size();
	int len_of_semicomplete = semicomplete_duos.size();
	if(len_of_complete <= len_of_semicomplete){
		cout << len_of_complete << endl;
		for(int i=0; i<len_of_complete; ++i){
			cout << semicomplete_duos[i].first << " " << complete_duos[i].first << endl;
			cout << semicomplete_duos[i].second << " " << complete_duos[i].second << endl;
		}
	}
	else{
		int len = (len_of_complete + len_of_semicomplete) / 2;
		cout << len << endl;
		for(int i=0; i<len_of_semicomplete; ++i){
			cout << semicomplete_duos[i].first << " " << complete_duos[i].first << endl;
			cout << semicomplete_duos[i].second << " " << complete_duos[i].second << endl;
		}
		for(int i=len_of_semicomplete; i<2*len-len_of_semicomplete; i=i+2){
			cout << complete_duos[i].first << " " << complete_duos[i+1].first << endl;
			cout << complete_duos[i].second << " " << complete_duos[i+1].second << endl;
		}
		
	}
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值