提交时间:2022-07-13 11:51:44
运行 ID: 51540
#include<bits/stdc++.h> using namespace std; template<typename T>void in(T &a) { T ans=0; bool f=0; char c=getchar(); for(;c<'0'||c>'9';c=getchar())if(c=='-')f=1; for(;c>='0'&&c<='9';c=getchar())ans=ans*10+c-'0'; a=(f?-ans:ans); } template<typename T,typename... Args>void in(T &a,Args&...args) { in(a),in(args...); } namespace solve{ int len=0; string ans="zzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzz"; void dfs(int d,string s,string now,string u){ if(d>=s.size()){ if(now.size()>len||(now.size()==len&&now<ans))ans=now,len=now.size(); return; } dfs(d+1,s,now,u); char f=s[d]; if((now.size()==0)||(u[f-96]=='0')||(u[f-96]=='1'&&now[now.size()-1]==f)){ string temp=now+f; u[f-96]='1'; dfs(d+1,s,temp,u); } } void solve1(int n,int k,int type,string s){ dfs(0,s,"","000000000000000"),cout<<len<<'\n'; if(type)cout<<ans<<'\n'; } } int n,k,type; string s; int main(){ in(n,k,type),cin>>s; solve::solve1(n,k,type,s); return 0; }