Predict the message

Why even if my logic seems complete it’s unable to pass the test case for the below given problem “Predict The Message” from the recent contest "krack the code " ?

#include <bits/stdc++.h>
#define mcc unordered_map<char,char>
using namespace std;

void setIO() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
}

string base_change(const string& n,int from,int to) {
  int d = 0;
  int p = 1;
  for(int i = n.size() - 1;i >= 0;i--) {
    int di = isdigit(n[i]) ? (n[i] - '0') : (n[i] - 'A' + 10);
      d += di * p;
      p *= from;
  }
  string res = "";  
  while (d > 0) {
    int r = d % to;
    char digit = (r < 10) ? (r + '0') : (r - 10 + 'A');
    res = digit + res;
    d /= to;
  }
  return res;
}

int main() {
  setIO();
  int t;
  cin >> t;
  mcc p{{'0','A'},{'1','B'},{'2','C'},{'3','D'},{'4','E'},{'5','F'},{'6','G'},{'7','H'},{'8','I'},{'9','J'},{'A','K'},{'B','L'},{'C','M'},{'D','N'},{'E','O'},{'F','P'},{'G','Q'},{'H','R'},{'I','S'},{'J','T'},{'K','U'},{'L','V'},{'M','W'},{'N','X'}, {'O','Y'},{'P','Z'};
  while(t--) {
    int n,k,q;
    cin >> n >> k >> q;
    string s = base_change(to_string(q-1),10,k);
    string A(n-s.size(),'A');
    cout << A;
    for(char c : s) cout << p[c];
    cout << '\n';
  }
  return 0;
}

link to problem

@angelash2024
Plzz refer the following solution for better understanding .

#include <iostream>
#include<stdio.h>
using namespace std;

int main() {
	string arr[]={"A","B","C","D","E","F","G","H","I","J","K","L","M","N","O","P","Q","R","S","T","U","V","W","X","Y","Z"};
    int cases;
    scanf("%d",&cases);
    while(cases--){
        int n,k,i;
        scanf("%d %d",&n,&k);
        scanf("%d",&i);
        string s1="";
        while(i>0){
            n--;
            s1= arr[i%k] + s1;
            i=i/k;
        }
        while(n>0){
            s1= arr[0] + s1;
            n--;
        }
        cout<<s1<<endl;
    }
}
1 Like