UVa 10062 Tell me the frequencies!

Code:
#include<stdio.h>
#include<stdlib.h>
#include<iostream>
#include<algorithm>
#include<math.h>
#include<map>
#include<vector>
using namespace std;
struct ic
{
    char ch;
    int n;
    ic(int a,char b):n(a),ch(b){};
    friend bool operator < (ic a,ic b);
};
bool operator < (ic a,ic b)
{
    if(a.n<b.n) return true;
    else if(a.n==b.n)
    {
        if(a.ch>=b.ch) return true;
    }
    return false;
}
int main()
{
    string str;
    bool first=0;
    while(getline(cin,str))
    {
        if(first) cout<<endl;
        map<char,int> m;
        vector<ic> v;
        int len=str.size();
        for(int i=0;i<len;i++)
        {
            m[str[i]]+=1;
        }
        for(auto it=m.begin();it!=m.end();it++)
        {
            v.push_back(ic(it->second,it->first));
        }
        sort(v.begin(),v.end());
        int vlen=v.size();
        for(int i=0;i<vlen;i++)
        {
             printf("%d %d\n",v[i].ch,v[i].n);
        }
        first=1;
    }
    return 0;

}

留言

這個網誌中的熱門文章

Things a Little Bird Told Me: Confessions of the Creative Mind

UVa 12970 Alcoholic Pilots

UVa 483 Word Scramble