其他分享
首页 > 其他分享> > E. Subsequences (easy version)(bfs贪心)

E. Subsequences (easy version)(bfs贪心)

作者:互联网

https://codeforces.com/problemset/problem/1183/E


思路:easy版本用贪心即可。一层层找。k总共只有100个

#include<iostream>
#include<vector>
#include<queue>
#include<cstring>
#include<cmath>
#include<map>
#include<set>
#include<cstdio>
#include<algorithm>
#define debug(a) cout<<#a<<"="<<a<<endl;
using namespace std;
const int maxn=200;
typedef long long LL;
typedef pair<string,LL>P;
inline LL read(){LL x=0,f=1;char ch=getchar();	while (!isdigit(ch)){if (ch=='-') f=-1;ch=getchar();}while (isdigit(ch)){x=x*10+ch-48;ch=getchar();}
return x*f;}
set<string>s;
queue<pair<string,LL>>que;
string str;
int main(void){
   cin.tie(0);std::ios::sync_with_stdio(false);
   LL n,k;cin>>n>>k;
   cin>>str;
   LL cnt=1;
   que.push({str,cnt});
   while(!que.empty()){
       P Now=que.front();que.pop();
       if(cnt>=k) break;
       string temp=Now.first;
       for(LL i=0;i<temp.size();i++){
           string s2=temp;
           s2.erase(i,1);
           if(!s.count(s2)){
               s.insert(s2);
               que.push({s2,++cnt});
               if(cnt>=k) break;
           }
       }
   }
   LL ans=0;
   for(auto i:s) ans+=(n-i.length());
   if(s.size()+1<k) ans=-1;
   cout<<ans<<"\n";
   return 0;
}

 

标签:ch,LL,cin,bfs,version,Subsequences,que,include,getchar
来源: https://blog.csdn.net/zstuyyyyccccbbbb/article/details/117194550