Codeforces Round #566 (Div. 2) C

链接: https://codeforces.com/problemset/problem/1182/C
题意:
A lyric is beautiful if and only if it satisfies all conditions below.
The number of vowels in the first word of the first line is the same as the number of vowels in the first word of the second line.
The number of vowels in the second word of the first line is the same as the number of vowels in the second word of the second line.
The last vowel of the first line is the same as the last vowel of the second line. Note that there may be consonants after the vowel.

Also, letters “a”, “e”, “o”, “i”, and “u” are vowels. Note that “y” is never vowel.

For example of a beautiful lyric,
"hello hellooowww"
"whatsup yowowowow"
is a beautiful lyric because there are two vowels each in “hello” and “whatsup”, four vowels each in “hellooowww” and “yowowowow” (keep in mind that “y” is not a vowel), and the last vowel of each line is “o”.
How many beautiful lyrics can you write from given words?
Note that you cannot use a word more times than it is given to you. For example, if a word is given three times, you can use it at most three times.
Each word contains at least one vowel.

思路: 这道题其实很容易想到预处理出每个单词的元音个数与最后一个元音,接着先将元音个数与最后一个元音字母相同的进行配对成第二种,然后将剩下的元音不同,但个数相同的进行配对第一种,最后比较两种数量,需要注意的是第二种可以放到第一种,但第一种无法放到第二种
但在比赛的时候难就难在我想不出怎么去存这个预处理结果,弄得很复杂,最后死于代码能力弱,其实可以用map套map解决, m a p &lt; i n t , m a p &lt; c h a r , v e c t o r &lt; i n t &gt; &gt; &gt; map&lt;int,map&lt;char,vector&lt;int&gt; &gt; &gt; map<int,map<char,vector<int>>>
此题需要注意的是 当我们auto自动变量类型可以轻松很多,切记关闭同步之后不能用printf与puts

#include<bits/stdc++.h>
using namespace std;
const int N=1e5+5;
set<char>ch={'a','e','i','o','u'};
map<int,map<char,vector<int> > >mp;
string a[N];
int main()
{
	ios::sync_with_stdio(0); cin.tie(0);
	int n;
	cin>>n;
	for(int i=1;i<=n;i++)
	{
		string str;
		cin>>str;
		a[i]=str;
		int cnt=0;
		char last=0;
		for(int i=0;i<str.size();i++)
		{
			if(ch.count(str[i]))
			{
				cnt++;
				last=str[i];
			}
		}
		mp[cnt][last].push_back(i);
	}
	vector<pair<int,int>>c1,c2;
	for(auto &p: mp)
	{
		vector<int>tmp;
		for(auto &q: p.second)
		{
			auto &v=q.second;
			for(int i=0;i+1<v.size();i+=2)
			{
				c2.push_back({v[i],v[i+1]});
			}
			if(v.size()%2)
			{
				tmp.push_back(v.back());
			}
		}
		for(int i=0;i+1<tmp.size();i+=2)
		{
			c1.push_back({tmp[i],tmp[i+1]});
		}
	}
	while(c1.size()<c2.size())
	{
		c1.push_back(c2.back());
		c2.pop_back();
	}
	int tot=min(c1.size(),c2.size());
	cout<<tot<<endl;
	for(int i=0;i<tot;i++)
	{
		cout<<a[c1[i].first]<<' '<<a[c2[i].first]<<endl;
		cout<<a[c1[i].second]<<' '<<a[c2[i].second]<<endl;
	}
	return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值