优先队列

#include<bits/stdc++.h>
#define MAX 150005
#define MOD 1000000007
typedef long long LL;
//const int INF = 1e9+7;
#define VM 1000010
using namespace std;
#define inf 0.00000001

char str[1005],ans[1005];
int p;
int has[1005],op[1005];
struct node{
    char c;
    int num;
    bool operator < (const node &a)const
    {
        if(num!=a.num)
            return num < a.num;
        return c>a.c;
    }
}nd[1005];

int main()
{
    int i,j;
    scanf("%s",str);
    getchar();
    scanf("%d",&p);
   //printf("%s %d\n",str,p);
    if(p==0)printf("%s\n",str);
    else {
         int k=0;
         priority_queue<node> q;
         int len=strlen(str);
         for(i=0;i<len;i++){
            int tmp=str[i]-'a';
            if(has[tmp]==0){
               nd[k].c=str[i];
               nd[k].num=1;
               op[tmp]=k;
               k++;
            }
            has[tmp]++;
            if(has[tmp]>1){
                nd[op[tmp]].num++;
            }
         }
         for(i=0;i<k;i++){
            q.push(nd[i]);
            cout<<nd[i].c<<" "<<nd[i].num<<endl;
         }
         int cnt=0;
         int flag=0;
         while(!q.empty()){
            node td=q.top();
            q.pop();
            cout<<td.c<<" "<<td.num<<endl;
            i=cnt;
            ans[i]=td.c;
            td.num--;
            while(true){
                    if(td.num==0)break;
               ans[i+p+1]=td.c;
               td.num--;

               if(i+p+1>=len){flag=1;break;}
               i=i+p+1;
               if(i+p+1>=len){break;}
            }
            if(flag)break;
            cnt++;
         }
         if(strlen(ans)<len)flag=1;
         if(flag)printf("-1\n");
         else printf("%s\n",ans);
    }
	return 0;
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值