链接: 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
<
i
n
t
,
m
a
p
<
c
h
a
r
,
v
e
c
t
o
r
<
i
n
t
>
>
>
map<int,map<char,vector<int> > >
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;
}