zoukankan      html  css  js  c++  java
  • C. Beautiful Lyrics (模拟)构造

    C. Beautiful Lyrics

    time limit per test

    1 second

    memory limit per test

    256 megabytes

    input

    standard input

    output

    standard output

    You are given nn 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 nn (1≤n≤1051≤n≤105) — the number of words.

    The ii-th of the next nn lines contains string sisi consisting lowercase alphabet letters — the ii-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 mm — the number of maximum possible beautiful lyrics.

    In next 2m2m lines, print mm beautiful lyrics (two lines per lyric).

    If there are multiple answers, print any.

    Examples

    input

    Copy

    14
    wow
    this
    is
    the
    first
    mcdics
    codeforces
    round
    hooray
    i
    am
    proud
    about
    that
    

    output

    Copy

    3
    about proud
    hooray round
    wow first
    this is
    i that
    mcdics am
    

    input

    Copy

    7
    arsijo
    suggested
    the
    idea
    for
    this
    problem
    

    output

    Copy

    0
    

    input

    Copy

    4
    same
    same
    same
    differ
    

    output

    Copy

    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.

    题意较简单

    实现起来略微麻烦

    代码

    #include<bits/stdc++.h>
    using namespace std;
    struct node
    {
        string s;
        int num;
        char c;
        friend bool operator <(const node p,const node q)
        {
            if(p.num==q.num)
            {
                return p.c<q.c;
            }
            return p.num<q.num;
        }
    }a[100005];
    vector<pair<int,int> >ve1;
    vector<pair<int,int> >ve2;
    int vis[100005];
    int main()
    {
        memset(vis,0,sizeof(vis));
        int n;
        scanf("%d",&n);
        for(int i=1;i<=n;i++)
        {
            cin>>a[i].s;
        }
        for(int i=1;i<=n;i++)
        {
            a[i].num=0;
            for(int j=0;j<a[i].s.length();j++)
            {
                if(a[i].s[j]=='a'||a[i].s[j]=='e'||a[i].s[j]=='i'||a[i].s[j]=='o'||a[i].s[j]=='u')
                {
                    a[i].num++;
                    a[i].c=a[i].s[j];
                }
            }
        }
        sort(a+1,a+1+n);
        //cout<<endl;
        //for(int i=1;i<=n;i++) cout<<a[i].s<<endl;
        for(int i=1;i<n;i++)//pos1
        {
            if(a[i].num==a[i+1].num&&a[i].c==a[i+1].c)
            {
                vis[i]=1;
                vis[i+1]=1;
                ve1.push_back(make_pair(i,i+1));
                i++;
            }
        }
        int pos=0;
        a[0].num=0;
        for(int i=1;i<=n;i++)
        {
            if(vis[i]==0)
            {
                if(a[pos].num==a[i].num)
                {
                    ve2.push_back(make_pair(pos,i));
                    pos=0;
                }
                else pos=i;
            }
        }
        //cout<<ve1.size()<<" "<<ve2.size()<<endl;
        int k=min(ve1.size(),ve2.size());
        int m=k+(ve1.size()-k)/2;
        cout<<m<<endl;
        for(int i=0;i<k;i++)
        {
            //cout<<"2"<<endl;
            cout<<a[ve2[i].first].s<<" "<<a[ve1[i].first].s<<endl;
            cout<<a[ve2[i].second].s<<" "<<a[ve1[i].second].s<<endl;
        }
        //cout<<k<<" "<<ve1.size()<<endl;
        for(int i=k;i+1<ve1.size();i+=2)
        {
            cout<<a[ve1[i].first].s<<" "<<a[ve1[i+1].first].s<<endl;
            cout<<a[ve1[i].second].s<<" "<<a[ve1[i+1].second].s<<endl;
        }
    }
  • 相关阅读:
    MySQL数据库的完全备份与恢复
    MySQL数据库之索引、事务、存储引擎详细讲解
    LNMP架构介绍与部署
    Haproxy搭建Web集群
    LAMP环境之MySQL服务安装详细过程
    MySQL主从复制详解
    LAMP环境之编译安装httpd服务
    搭建yum软件仓库,让你维护轻松自如
    Shell脚本一键安装Samba服务
    Shell脚本之冒泡排序
  • 原文地址:https://www.cnblogs.com/caowenbo/p/11852250.html
Copyright © 2011-2022 走看看