Tuesday, June 28, 2016

UVA 10062 - Tell me the frequencies!

#include<bits/stdc++.h>
using namespace std;
struct freq{
    int asci=0, fr=0;
};
bool comp(freq a , freq b)
{
    if(a.fr==b.fr) return a.asci > b.asci;
    return a.fr < b.fr;
}
int main()
{
    ios_base::sync_with_stdio(0);
    string s;
    int flag=0;
    while(getline(cin,s)){
        flag++;
        int a[256],j=0;
        freq cnt[256];
        memset(a,0,sizeof(a));
        for(int i=0; i<s.size(); i++){
            int x = s[i];
            a[x]++;
        }
        for(int i=0; i<256; i++){
            if(a[i]!=0){
                cnt[j].asci=i;
                cnt[j].fr=a[i];
                j++;
            }
        }
        if(flag!=1) cout<<'\n';
        sort(cnt , cnt+j,comp);
        for(int i=0; i<j; i++){
            cout<<cnt[i].asci<<' '<<cnt[i].fr;
                cout<<'\n';
        }
    }

    return 0;
}

No comments:

Post a Comment