C. Beautiful Lyrics
memory limit per test256 megabytes
inputstandard input
outputstandard output
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≤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 106. Each word contains at least one vowel.
Output
In the first line, print m — the number of maximum possible beautiful lyrics.
In next 2m lines, print m beautiful lyrics (two lines per lyric).
If there are multiple answers, print any.
Examples
input
14
wow
this
is
the
first
mcdics
codeforces
round
hooray
i
am
proud
about
that
output
3
about proud
hooray round
wow first
this is
i that
mcdics am
input
7
arsijo
suggested
the
idea
for
this
problem
output
0
input
4
same
same
same
differ
output
1
same differ
same same
Note
In the first example, those beautiful lyrics are one of the possible answers. Let’s look at the first lyric on the sample output of the first example. “about proud hooray round” forms a beautiful lyric because “about” and “hooray” have same number of vowels, “proud” and “round” have same number of vowels, and both lines have same last vowel. On the other hand, you cannot form any beautiful lyric with the word “codeforces”.
In the second example, you cannot form any beautiful lyric from given words.
In the third example, you can use the word “same” up to three times.
思路:
這一題主要是排序,排序排得好就可以過,排兩次序,
第一次
排出最後一個元音相同且元音數量相同的單詞,兩兩記錄.
第二次
在排除第一次選出去的單詞後, 排出元音數量相同的單詞,兩兩記錄.
最後
第一種情況
第一次排序記錄單詞數量 大於 第一次排序記錄單詞數量, 那麼 可以排出的 好歌詞 的 對數 是 第二次排序記錄單詞數量 除以 2 加上 第一次排序記錄單詞數量 減去 第二次排序記錄單詞數量的 差 除以 4.
原因:
第一次排序記錄單詞數量 和 第二次排序記錄單詞數量 先 可以湊出 第二次排序記錄單詞數量 除以 2 對,那麼 第一次排序記錄單詞數量 還有剩餘,那麼看剩餘的 第一次排序記錄單詞數量 還能不能排出 好的歌詞,也就是 第一次排序記錄單詞數量 減去 第二次排序記錄單詞數量的 差 除以 4.
第二種情況
第一次排序記錄單詞數量 小於等於 第一次排序記錄單詞數量,這個就直接看 第一次排序記錄單詞數量 能湊成幾對就可以了,不用管第二次排序記錄單詞數量 .
原因:
第一次排序記錄單詞數量 一定右足夠的 第二次排序記錄單詞數量,可以與之配對
代碼:
#include<cstdio>
#include<cmath>
#include<cstring>
#include<ctime>
#include<iostream>
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<iomanip>
#include<cstring>
#include<string>
#include<cmath>
#include<stack>
#include<queue>
#include<vector>
#include<set>
#include<map>
#define ll long long
#define mes(x,y) memset(x,y,sizeof(x))
using namespace std;
ll gar(ll a,ll b){//最大公約數
return b==0?a:gar(b,a%b);
}
const int maxn=2e5+9;
const int mod=1e9+7;
stack<string>sum1,sum2;
struct node{
char end;
string v;
ll sum;
bool flag;
}lv[maxn];
bool yuan(char c){
if(c=='a'||c=='i'||c=='o'||c=='u'||c=='e')return 1;
else return 0;
}
bool cmp1(node x,node y){
if(x.end==y.end) return x.sum<y.sum;
else return x.end<y.end;
}
bool cmp2(node n1,node n2){
if(n1.flag)return false;
if(n2.flag)return true;
if(n1.sum==n2.sum)return n1.end<n2.end;
else return n1.sum<n2.sum;
}
ll n,i,j,m=0,len,sum;string s;char c;
int main(){
while(cin>>n) {
m = 0;
while (!sum1.empty())sum1.pop();
while (!sum2.empty())sum2.pop();
while (n--) {
cin >> s;
len = s.length();
sum = 0;
for (i = 0; i < len; i++) {
if (yuan(s[i])) {
sum++;
c = s[i];
}
}
if (sum) {
lv[m].end = c;
lv[m].flag = 0;
lv[m].v = s;
lv[m].sum = sum;
m++;
}
}
sort(lv, lv + m, cmp1);
cout << endl;
for (i = 1; i < m; i++) {
if (lv[i].end == lv[i - 1].end && lv[i].sum == lv[i - 1].sum && lv[i].flag == 0 && lv[i - 1].flag == 0) {
sum1.push(lv[i].v);
sum1.push(lv[i - 1].v);
lv[i].flag = 1;
lv[i - 1].flag = 1;
}
}
sort(lv, lv + m, cmp2);
for (i = 1; i < m; i++) {
if (lv[i].sum == lv[i - 1].sum && lv[i].flag == 0 && lv[i - 1].flag == 0) {
sum2.push(lv[i].v);
sum2.push(lv[i - 1].v);
lv[i].flag = 1;
lv[i - 1].flag = 1;
}
}
bool next = false;
if (sum1.size() > sum2.size()) {
next = true;
sum = sum2.size() / 2 + (sum1.size() - sum2.size()) / 4;
} else {
sum = sum1.size() / 2;
}
cout << sum << endl;
if (next) {
len = sum2.size() / 2;
sum -= len;
for (i = 0; i < len; i++) {
cout << sum2.top() << " " << sum1.top() << endl;
sum1.pop();
sum2.pop();
cout << sum2.top() << " " << sum1.top() << endl;
sum1.pop();
sum2.pop();
}
for (i = 0; i < sum; i++) {
string s1 = sum1.top();
sum1.pop();
string s3 = sum1.top();
sum1.pop();
string s2 = sum1.top();
sum1.pop();
string s4 = sum1.top();
sum1.pop();
cout << s1 << " " << s2 << endl;
cout << s3 << " " << s4 << endl;
}
} else {
for (i = 0; i < sum; i++) {
cout << sum2.top() << " " << sum1.top() << endl;
sum1.pop();
sum2.pop();
cout << sum2.top() << " " << sum1.top() << endl;
sum1.pop();
sum2.pop();
}
}
}
}