Why m I getting wrong answer in CHODE?

//
// CodeDecode.cpp
//
//
// Created by Gaurav Gulzar on 09/12/13.
//
//
// Question Link : http://www.codechef.com/problems/CHODE/

#include <iostream>
using namespace std;
#include <cstring>
#include <cstdio>
#include <fstream>
#include <cstdlib>

#define TLEN 150000

int countArr[128];

int compare(const void *a, const void *b){
    int tmp = countArr[(int)(*(char*)a)] - countArr[(int)(*(char*)b)];
    if(tmp == 0){
        return (*(char*)a) - (*(char*)b);
    }
    else
        return tmp;
}

int main(){
    int T;
    cin >> T;
    while(T > 0){
        char fseq[27], waste[50];
        cin >> fseq;
        gets(waste);
        //cin.getline(fseq,27,'\n');
        //string cipherS;
        char cipher[TLEN];
        //cin.getline(cipher,TLEN,'\n');
        gets(cipher);
        int len = strlen(cipher);
        
        for(int i=0; i<128; i++)
            countArr[i] = 0;
        
        for(int i=0; i<len; i++){
            if(isalpha(cipher[i]))
                countArr[cipher[i]]++;
        }
        char copy[len+1];
        strcpy(copy,cipher);
        qsort(cipher,len,sizeof(char),compare);
        
        //cout << "SC:" << cipher << "\n" << fseq << "\n";
        
        char map[128] = {'\0'};
        int j = 25;
        for(int i=len-1; i>=0; i--){
            if(map[cipher[i]] == '\0'){
                map[cipher[i]] = fseq[j];
                --j;
            }
        }
        
        char res[len+1];
        
        for(int i=0; i<len; i++){
            if(isalpha(copy[i])) {
                if(isupper(copy[i])) {
                    res[i] = toupper(map[copy[i]]);
                }
                else
                    res[i] = map[copy[i]];
            }
            else{
                res[i] = copy[i];
            }
        }
        res[len] = '\0';
        
        cout << res << "\n";
        --T;
    }
}

You seem to consider the frequencies of uppercase and lowercase alphabets separately…

  for(int i=0; i < len; i++){
        if(isalpha(cipher[i]))
            countArr[cipher[i]]++;
    }

But thats not what the question demands…You must consider them as one…At the time of printing the result if the alphabet at ith position in cipher text was uppercase you need to print the decoded alphabet as uppercase as well…otherwise in lowercase…
Consider the test case:

Input:
  qwrtyuipasdfgjkzxcvbnmheol
  DumMy!
Output:
  HelLo!
1 Like

Got it!!! Thanks a lot :slight_smile: