C. Beautiful Lyrics 贪心

传送

题意:按规则拼词

思路:先按元音结尾排序取第二种词,再按数目取第一种词,贪心的先用第一种词,如果不够,就第二种词可以当第一种用.

#include<bits/stdc++.h>
#define ll long long
using namespace std;
const int MAXN=1e5+10;
struct node
{
    string word;
    int num;
    char last;
}words[MAXN];
int cmp(node aa,node bb)
{
    if(aa.num==bb.num)
        return aa.last<bb.last;
    return aa.num<bb.num;
}
vector<pair<int, int> > fi, se;
int main()
{
    int n;
    cin>>n;
    for(int i=1;i<=n;i++)
    {
        string s;
        cin>>s;
        words[i].word=s;
        for(int j=0;j<s.size();j++)
        {
            if (s[j] == 'a' || s[j] == 'e' || s[j] == 'i' || s[j] == 'o' || s[j] == 'u')
            {
                words[i].num++;
                words[i].last = s[j];
            }
        }
    }
    sort(words+1,words+1+n,cmp);
    int pos=1;
    int cnt=0;
    int w=-1;
    while(pos<=n)
    {
        if (words[pos].num == words[pos+1].num && words[pos].last == words[pos+1].last)
        {
            se.push_back({pos, pos+1});
            pos += 2;
            continue;
        }
        if(w==-1)// 如果不符合 那么和前面的位置进行比较
        {
            w=pos;
            cnt=words[pos].num;
            pos++;
            continue;
        }
        if(words[pos].num==cnt)// 和前位置元音数量一样 放入数量一样的容器里
        {
            fi.push_back({w, pos});
            w = -1;
            pos++;
        }
        else// 否则更新位置 因为按照元音数量排序的
        {
            w = pos;
            cnt = words[pos].num;
            pos++;
        }
    }
    int s1=fi.size(),s2=se.size();
    int res=0;
    res+=min(s1,s2)+max(0,(s2-s1)/2);
    cout<<res<<endl;
    int i;
    for(i=0;i<min(s1,s2);i++)
    {
        cout << words[fi[i].first].word << ' ' << words[se[i].first].word << endl;
        cout << words[fi[i].second].word << ' ' << words[se[i].second].word << endl;
    }
    for(;i+1<se.size();i+=2)
    {
        cout << words[se[i].first].word << ' ' << words[se[i+1].first].word << endl;
        cout << words[se[i].second].word << ' ' << words[se[i+1].second].word << endl;
    }
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值