提交时间:2022-07-13 11:48:46
运行 ID: 51507
#include <bits/stdc++.h> using namespace std; int n,k,type,d,num,cntn[5100],ans=0; string a; signed main() { // freopen ("seq.in","r",stdin); // freopen ("seq.out","w",stdout); scanf("%d%d%d",&n,&k,&type); cin>>a; d=a[n-1],num=n-1; for(int i=n-2; i>=0; --i) { if(a[i]==d) cntn[i]=-1,cntn[num]++; else d=a[i],num=i; } for(int i=0; i<n; ++i) { for(int j=0; j<=n-1; ++j) if(a[j]==a[i] && cntn[j]!=-1 && cntn[i]!=-1 &&i!=j) { if(cntn[i]>cntn[j]) cntn[j]=-1; else if(cntn[i]<cntn[j]) cntn[i]=-1; else { if(a[i]<a[i+1]) cntn[j]=-1; else cntn[i]=-1; } } } for(int i=1; i<=n; ++i) if (cntn[i-1]!=-1) ans+=cntn[i-1]+1; printf("%d\n",ans); if(type!=0) for(int i=1; i<=n; ++i) if(cntn[i-1]!=-1) for(int j=1; j<=cntn[i-1]+1; j++) cout<<a[i-1]; return 0; } // 10 26 2 abbccbaabc