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;
}

 

上一篇:Leetcode 1730. Shortest Path to Get Food [Python]


下一篇:刷题-力扣-690