You are given ?n words, each of which consists of lowercase alphabet letters. Each word contains at least one vowel. You are going to choose some of the given words and make as many beautiful lyrics as possible.
Each lyric consists of two lines. Each line consists of two words separated by whitespace.
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".
For example of a not beautiful lyric,
"hey man"
"iam mcdic"
is not a beautiful lyric because "hey" and "iam" don't have same number of vowels and the last vowels of two lines are different ("a" in the first and "i" in the second).
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.
Input
The first line contains single integer ?n (1≤?≤1051≤n≤105) — the number of words.
The ?i-th of the next ?n lines contains string ??si consisting lowercase alphabet letters — the ?i-th word. It is guaranteed that the sum of the total word length is equal or less than 106106. Each word contains at least one vowel.
Output
In the first line, print ?m — the number of maximum possible beautiful lyrics.
In next 2?2m lines, print ?m beautiful lyrics (two lines per lyric).
If there are multiple answers, print any.
思路:开一个结构体预处理所有字串的 元音字母个数及其最后一个元音字母。根据元音字母个数进行排序。开两个队列q1,q2。分别装第二位单词对和第一位单词。按照贪心思想第二位字串尽量放在后面,前面补上第一位字串。若进行完上述操作后第二位还有剩余,将多的补到第一位去
#include<iostream>
#include<cstdio>
#include<cmath>
#include<cstring>
#include<string>
#include<algorithm>
#include<queue>
#define ll long long
using namespace std;
queue<ll>q1,q2;
struct node{
int i;
ll num;
char ch;
};
struct Node{
string s1;
string s2;
}ans[1008611];
bool cmp(node a,node b){
if(a.num==b.num){
return a.ch<b.ch;
}
return a.num<b.num;
}
string str[100000+10];
int vis[1008611];
node q[1008611];
int main(){
ll n;
cin>>n;
for(int i=0;i<n;i++){
cin>>str[i];
q[i].num=0;
q[i].i=i;
for(int j=0;j<str[i].length();j++){
char s=str[i][j];
if(s=='a'||s=='e'||s=='i'||s=='o'|| s=='u'){
q[i].num++;
q[i].ch=s;
}
}
}
sort(q,q+n,cmp);
ll cnt=0;
for(int i=0;i<n-1;i++){
if(q[i].num==q[i+1].num&&q[i].ch==q[i+1].ch){
cnt++;
q1.push(i);
q1.push(i+1);
i++;
}
else{
q2.push(i);
if(i==(n-2))q2.push(n-1);
}
}
if(cnt<1){
cout<<0<<endl;
}
else{
ll num1,num2,nn=0;
while(q2.size()>=2&&q1.size()>=2){
num1=q2.front();
q2.pop();
num2=q2.front();
if(q[num1].num==q[num2].num){
q2.pop();
ans[nn].s1=str[q[num1].i];
ans[nn++].s2=str[q[q1.front()].i];
q1.pop();
ans[nn].s1=str[q[num2].i];
ans[nn++].s2=str[q[q1.front()].i];
q1.pop();
}
}
while(q1.size()>=4){
ans[nn].s1=str[q[q1.front()].i];
q1.pop();
ans[nn+1].s1=str[q[q1.front()].i];
q1.pop();
ans[nn].s2=str[q[q1.front()].i];
q1.pop();
ans[nn+1].s2=str[q[q1.front()].i];
q1.pop();
nn+=2;
}
cout<<nn/2<<endl;
for(int i=0;i<nn;i++){
cout<<ans[i].s1<<' '<<ans[i].s2<<endl;
}
}
return 0;
}