# Tell me the frequencies!(UVA10062) ## [程式繳交區](https://hackmd.io/@Renektonn/rkitY1TOke/edit) ## 題目 [點我](https://onlinejudge.org/external/100/10062.pdf) ## 解題網站 [UVA](https://onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=24&page=show_problem&problem=1003) [ZJ](https://zerojudge.tw/ShowProblem?problemid=c012) ## 解題步驟 #### 1. 了解題意 #### 2. 設計演算法 1. 掃過字串,用array記錄字母出現頻率,index為ASCII code,value為index的出現頻率 2. 將有出現的ASCII code{index , value}一個個推進vector中 3. 用題目的規則來排序vector 4. 按照順序印出來 #### 3. 實際做一次 #### 4. 確定測資範圍 題目說:The given lines will contain none of the first 32 or last 128 ASCII characters. 我以為不會出現空格(空格的ASCII code是32),結果有出現,因為ASCII code是從0開始 所以要用getline()或fgets()才能解決這題 #### 5. 檢查輸出格式 a1 a2'\n' b1 b2'\n' '\n' c1 c2'\n' d1 d2'\n' #### 6. 實作 ```cpp= #include <bits/stdc++.h> using namespace std; int cmp(pair <int , int> a , pair <int , int> b){ if(a.second == b.second){ return a.first > b.first; } return a.second < b.second; } void func(string str){ int table[129] = {}; for(int i = 0 ; i < str.length() ; i++){ table[str.at(i)]++; } vector <pair <int , int>> order; for(int i = 1 ; i <= 128 ; i++){ if(table[i]){ order.push_back(make_pair(i , table[i])); } } sort(order.begin() , order.end() , cmp); for(int i = 0 ; i < order.size() ; i++){ cout << order[i].first << " " << order[i].second << endl; } } int main() { string str; getline(cin , str); func(str); while(getline(cin , str)){ cout << endl; func(str); } return 0; } ```
×
Sign in
Email
Password
Forgot password
or
By clicking below, you agree to our
terms of service
.
Sign in via Facebook
Sign in via Twitter
Sign in via GitHub
Sign in via Dropbox
Sign in with Wallet
Wallet (
)
Connect another wallet
New to HackMD?
Sign up