NC220443. AC
描述
输入描述
The first line contains two integers and .
The second line contains a string of length , denoting 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 Bob 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.
示例1
输入:
9 2 arakbacca
输出:
3 acacbacca
C++ 解法, 执行用时: 165ms, 内存消耗: 15752K, 提交时间: 2022-02-25 23:50:00
#include<bits/stdc++.h> #define pii pair<int,int> using namespace std; const int M=5e5+9; int n,k,ans; int pre[M],nex[M],a[M],l[M],r[M]; char s[M]; bool vis[M]; priority_queue<pii>q; int main(){ scanf("%d%d%s",&n,&k,s+1); for(int i=1;i<=n;++i)pre[i]=i-1,nex[i]=i+1,l[i]=r[i]=i; for(int i=1;i<n;++i){ a[i]=(s[i]!='a')+(s[i+1]!='c'); q.push({-a[i],i}); } a[0]=a[n]=1e8; nex[0]=1;pre[n+1]=n; while(!q.empty()&&-q.top().first<=k){ int val=q.top().first,id=q.top().second; q.pop(); if(vis[id])continue; k+=val; ans++; r[id]=r[nex[id]]; l[id]=l[pre[id]]; a[id]=a[pre[id]]+a[nex[id]]+val; vis[pre[id]]=vis[nex[id]]=1; pre[id]=pre[pre[id]]; nex[id]=nex[nex[id]]; pre[nex[id]]=nex[pre[id]]=id; q.push({-a[id],id}); } printf("%d\n",ans); for(int id=1;id<=n;++id){ if(vis[id])continue; for(int i=l[id]+1;i<r[id];i+=2)s[i]='a',s[i+1]='c'; } printf("%s\n",s+1); return 0; }