Language: C++

#include <iostream>
#include <math.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <queue>
#include <vector>
#include <algorithm>
#include <map>

std::vector<int > count(26, 0); 
int N, K;

bool enable_add(std::string ans, int j){
  std::string cand = ans;
  cand[cand.size()] = (char)('a'+j);
}

int main(){

  scanf("%d", &N);
  scanf("%d", &K);

  std::string S;
  std::cin >> S;
  for (int i=0; i<S.length(); i++){
    count_orig[int(S[i])-int('a')]++;
  }

  // search start
  std::string ans;
  for (int i=0; i<S.length(); i++){
    // i番目に何の文字を入れるか決める
    for (int j=0; j<26; j++){
      if (count_orig[j]>0){ //aからj番目のアルファベットが候補にあると判明した場合
        if (enable_add(ans, j)){ // j番目のアルファベットを追加しても問題ない場合
          ans[ans.length()] = (char)('a' + j);
          count[j]--;
        }
      }
    }
  }
  stsd::cout << ans << std::endl;
}
  int i= 0;
  int diffcount_first;
  int samecount_latter;
  while(i<S.length()){ // i番目まではsortされた文字を使い、i+1番目以降はソートされてないもので頑張る
    printf("i=%d\n", i);
    std::vector<int > count_check(count_orig); // 元の文字列
    std::vector<int > count_search(count_orig); // 候補となっているもの
    diffcount_first = 0;

    for (int j=0; j<=i; j++){
      count_check[int(S[j])-int('a')]--;
      count_search[int(sorted[j])-int('a')]--;
      if (S[j]!=sorted[j]){
        diffcount_first++;
      }
    }
    samecount_latter = 0;

    for (int j=0; j<26; j++){
      printf("%d: (%d, %d)\n", j, count_check[j], count_search[j]);
      samecount_latter += std::min(count_check[j], count_search[j]);
    }
    printf("samecount_latter=%d\n",samecount_latter);
    int diffcount_latter = (S.length()-i-1) - samecount_latter;
    printf("diffcount_first=%d\n", diffcount_first);
    printf("diffcount_latter=%d\n", diffcount_latter);
    if (diffcount_first + diffcount_latter > K){
      break;
    }
    i++;
  }

  int result = fmin(i, S.length()-1);
  printf("result=%d\n", result);
  
  // digit using sorted array fixed
  std::vector<int > count_check(count_orig); // 元の文字列
  std::vector<int > count_search(count_orig); // 候補となっているもの
  diffcount_first = 0;
  
  for (int j=0; j<=result; j++){
    count_check[int(S[j])-int('a')]--;
    count_search[int(sorted[j])-int('a')]--;
    if (S[j]!=sorted[j]){
      diffcount_first++;
    }
  }
  

  return 0;
}


Compiler output:
a.cc: In function 'bool enable_add(std::string, int)':
a.cc:17:1: warning: no return statement in function returning non-void [-Wreturn-type]
   17 | }
      | ^
a.cc: In function 'int main()':
a.cc:27:5: error: 'count_orig' was not declared in this scope
   27 |     count_orig[int(S[i])-int('a')]++;
      |     ^~~~~~~~~~
a.cc:35:11: error: 'count_orig' was not declared in this scope
   35 |       if (count_orig[j]>0){ //aからj番目のアルファベットが候補にあると判明した場合
      |           ^~~~~~~~~~
a.cc:43:3: error: 'stsd' has not been declared
   43 |   stsd::cout << ans << std::endl;
      |   ^~~~
a.cc: At global scope:
a.cc:48:3: error: expected unqualified-id before 'while'
   48 |   while(i<S.length()){ // i番目まではsortされた文字を使い、i+1番目以降はソートされてないもので頑張る
      |   ^~~~~
a.cc:77:24: error: 'S' was not declared in this scope
   77 |   int result = fmin(i, S.length()-1);
      |                        ^
a.cc:78:9: error: expected constructor, destructor, or type conversion before '(' token
   78 |   printf("result=%d\n", result);
      |         ^
a.cc:81:33: error: 'count_orig' was not declared in this scope
   81 |   std::vector<int > count_check(count_orig); // 元の文字列
      |                                 ^~~~~~~~~~
a.cc:82:34: error: 'count_orig' was not declared in this scope
   82 |   std::vector<int > count_search(count_orig); // 候補となっているもの
      |                                  ^~~~~~~~~~
a.cc:83:3: error: 'diffcount_first' does not name a type
   83 |   diffcount_first = 0;
      |   ^~~~~~~~~~~~~~~
a.cc:85:3: error: expected unqualified-id before 'for'
   85 |   for (int j=0; j<=result; j++){
      |   ^~~
a.cc:85:17: error: 'j' does not name a type
   85 |   for (int j=0; j<=result; j++){
      |                 ^
a.cc:85:28: error: 'j' does not name a type
   85 |   for (int j=0; j<=result; j++){
      |                            ^
a.cc:94:3: error: expected unqualified-id before 'return'
   94 |   return 0;
      |   ^~~~~~
a.cc:95:1: error: expected declaration before '}' token
   95 | }
      | ^