列表

详情


NC221635. WA

描述

Ben's fortune predict system is successfully developed!

The predict system is a distributed system consists of  computers. When it receives a predict request, each computer will generate one lower case letter as its output. The final fortune predict result is determined by the concentration of all  outputs.

Tired of getting good predictions like  or , Crystal decided to hack into the predict system and modify the predict result. He has already got the access permission of every computer in the predict system, so he could modify their output to any letter arbitrarily.

As Crystal is not going to take part in ICPC World Final 2077, he want unlucky predictions like . He found that the more substrings  occurs in the concentration of all  outputs, the unluckier he will get in the end. But as the contest is coming soon, he only has time to modify at most  outputs of the computers in the predict system.
As Crystal is busy hacking into the system, could you tell him how to get the most  substrings after his modification?

输入描述

The first line contains two integers  and  .

The second line contains a string of length , denotes the origin prediction. It is guaranteed that the string consists of lowercase English letters.


输出描述

Output two lines. The first line contains a single integer, denotes the maximum number of  substring Crystal can get, after his modification. The second line contains the final modified predict string, if there are multiple ways that results in the maximum number of  substring, print any.

Note that you don't have to minimize the number of  substring.

示例1

输入:

9 3
arakbacca

输出:

5
aaaaaacca

说明:


原站题解

上次编辑到这里,代码来自缓存 点击恢复默认模板

C++(clang++11) 解法, 执行用时: 39ms, 内存消耗: 7552K, 提交时间: 2021-05-08 20:59:25

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;

int i,j,k,n,m;
struct sb{
	int l,r,w;
	bool operator<(const sb x)const{return w>x.w;}
}s;

priority_queue<sb> q;

char c[500500];

int main(){
	ios::sync_with_stdio(0);
	cin>>n>>m>>c+1;
	for(i=1;i<=n;i++){
		if(c[i]=='a'){
			if(j){
				s.l=j+1;s.r=i-1;s.w=i-j-1;
				q.push(s);
			}
			j=i;
		}
	}
	while(!q.empty()){
		s=q.top();q.pop();
		if(s.w>m){break;}
		m-=s.w;
		for(i=s.l;i<=s.r;i++){
			c[i]='a';
		}
	}
	if(!j&&m){
		c[1]='a';m--;
	}
	for(i=1;i<=n;i++){
		if(m&&c[i]!='a'&&c[i-1]=='a'){
			c[i]='a';m--;
		}
	}
	for(i=1;i<=n;i++){
		if(c[i]=='a'&&c[i-1]=='a'){k++;}
	}
	cout<<k<<endl<<c+1;
}

上一题