竹園論壇

標題: 10062 - Tell me the frequencies! [打印本頁]

作者: jd3    時間: 2014-4-30 23:41
標題: 10062 - Tell me the frequencies!
本帖最後由 jd3 於 2014-4-30 23:52 編輯

題意:計算該行字串字符出現次數,並依照出現次數排序,若次數相同則優先輸出ASCII碼較大的

注意事項:1.每筆測資之間都有空行,首行不空、末行不空
                  2.輸入字串含有空格,如果只輸入換行也要輸出空白行作為間隔

/*
        AC
        15 ms
*/

#include<iostream>
#include<cstdio>
#include<algorithm>

using namespace std;


struct Freq
{
        int sign;
        int time;
};



inline bool operator < (const Freq &f1, const Freq &f2)
{
        if(f1.time != f2.time)
                return f1.time < f2.time;
        return f1.sign > f2.sign;
}


int main()
{
        char str[5000];
        Freq list[305];
               
        bool first = true;
        
        while(fgets(str,2000,stdin))
        {
                if(first)
                        first = false;
                else
                        putchar('\n');
                for(int i = 0 ; i < 300 ; i++)
                {
                        list[i].sign = i;
                        list[i].time = 0;
                }
               
               
                int len = 0;
                for(int i = 0 ; (str[i]!='\n' && str[i]!='\0') ; i++)
                {
                        len++;
                        list[str[i]].time++;
                }
               
                sort(list,list+300);
               
               
                for(int i = 0 ; i < 300 ; i++)
                        if(list[i].time > 0)
                                printf("%d %d\n", list[i].sign, list[i].time);
               
               
        }
        return 0;
}





歡迎光臨 竹園論壇 (http://forum.tfcis.org/) Powered by Discuz! X3.2