lundi 31 juillet 2017

UVA 10062 Wrong Answer

http://ift.tt/2uclKM9

#include<bits/stdc++.h>
using namespace std;
int compare(pair<int, int> a, pair<int, int> b) {
    if(a.first == b.first) return (a.second> b.second);
    if (a.first < b.first) return 1;
    if (a.first > b.first) return 0;
}

int main(){

    string x;
    while(true){
        pair<int , int> freq[150];
        int free[150]={0};
        getline(cin,x);
        if(x=="")break;
        int f =1;
        for(int i = 0 ; i < x.length();i++){
            if(free[int(x[i])])
            {
                freq[free[int(x[i])]].first++;
            }
            else{
                free[int(x[i])]= f;
                freq[f++]= make_pair(1,int(x[i]));
            }
        }
        sort(freq,freq+f,compare);
        for(int i = 1 ; i < f ; i++){
            cout<<freq[i].second<<" "<<freq[i].first<<endl;
        }
        cout<<endl;
    }
}

As simple this problem could be, I don't know why I get the wrong answer. All I did is as follows:

  1. Made two arrays, one to store the frequency along with the value of the char and the other to check what char already existed within the first array to increase the frequency

  2. Sorted using the compare function I created above

  3. Output the array with an extra \n in the end

Aucun commentaire:

Enregistrer un commentaire