# UVA 10062 Tell me the frequencies! ## 題目連結 [UVA 10062](https://vjudge.net/problem/UVA-10062) ### 題目內容 Given a line of text you will have to find out the frequencies of the ASCII characters present in it. The given lines will contain none of the first 32 or last 128 ASCII characters. Of course lines may end with \n and \r but always keep those out of consideration. ### 輸入限制 Several lines of text are given as input. Each line of text is considered as a single input. Maximum length of each line is 1000. ### 輸出限制 Print the ASCII value of the ASCII characters which are present and their frequency according to the given format below. A blank line should separate each set of output. Print the ASCII characters in the ascending order of their frequencies. If two characters are present the same time print the information of the ASCII character with higher ASCII value first. Input is terminated by end of file. ### 解題思路 1.這題要計算每個字元的頻率,利用map來統計 2.為了按照題目要求排序,設一個vector來存放map裡的元素 3.vector裡可以放成對的元素(pair<char,int>),變成很像map的可變動陣列 ### 程式碼 ```cpp= #include<bits/stdc++.h> using namespace std; bool cmp(pair<char,int> a,pair<char,int> b){ if(a.second==b.second){ return a.first>b.first; } return a.second<b.second; } int main(){ string s; int t=0; while(getline(cin,s)){ map<char,int> mp; if(t){ cout<<"\n"; } t=1; for(int i=0;i<s.length();i++){ mp[s[i]]++; } vector<pair<char,int> > mpp; for(auto it:mp){ mpp.push_back({it.first,it.second}); } sort(mpp.begin(),mpp.end(),cmp); for(auto it:mpp){ cout<<(int)it.first<<" "<<it.second<<endl; } } } ``` ## 測資 ### Sample input AAABBC 122333 ### Sample output 67 1 66 2 65 3 49 1 50 2 51 3 ## 中文題目連結 [zerojudge c012](https://zerojudge.tw/ShowProblem?problemid=c012)