Codeforces Round#566(Div.2)1182C. Beautiful Lyrics

C. Beautiful Lyrics

time limit per test1 second
memory limit per test256 megabytes
inputstandard input
outputstandard output
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≤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();
            }
        }
    }
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章