Advertisement
Guest User

Untitled

a guest
Sep 23rd, 2019
96
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.17 KB | None | 0 0
  1. //https://www.spoj.com/problems/EDDIST/
  2. //https://github.com/marioyc/Online-Judge-Solutions/blob/master/SPOJ/Classical/7103%20-%20Edit%20Distance.cpp
  3. #include <bits/stdc++.h>
  4. using namespace std;
  5.  
  6. int main(){
  7.     int K,L;
  8.     string s;
  9.     while(true){
  10.         cin >> K;
  11.         if(K == -1) break;
  12.        
  13.         cin >> s;
  14.         L = s.size();
  15.        
  16.         int cont = 0;
  17.         for(int i = 0;i < L;++i)
  18.             if(s[i] != 'a') ++cont;
  19.        
  20.         if(cont <= K){
  21.             K -= cont;
  22.            
  23.             if(K >= L-cont) cout << endl;
  24.             else cout << string(L-cont-K,'a') << endl;
  25.         }else{
  26.             string ans = s;
  27.            
  28.             for(int ins = 0;ins <= K;++ins){
  29.                 string aux = s;
  30.                
  31.                 for(int i = 0,j = 0;i < L && j < K-ins;++i){
  32.                     if(aux[i] != 'a'){
  33.                         aux[i] = 'a';
  34.                         ++j;
  35.                     }
  36.                 }
  37.                
  38.                 if(ins > 0) aux = string(ins,'a') + aux;
  39.                 ans = min(ans,aux);
  40.             }
  41.            
  42.             cout << ans << endl;
  43.         }
  44.     }
  45. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement