# 題目 * 密碼翻譯(cryptanalysis)是指把某個人寫的密文(cryptographic writing)加以分解。這個程序通常會對密文訊息做統計分析。你的任務就是寫一個程式來對密文作簡單的分析。 # 思路 1. *先讀取int 到n*,然後直接 *while loop* 做字串讀取。 3.1 將 *map* 的內容轉換到 *vector* 這可以簡化流程;調用 *std::sort()* 針對 *vector* 進行排序 。 3.2 排序規則依照題目所求即可。 3.3 直接把 *vector* 輸出就好了。 # 注意事項 >很多地方使用了標準庫的函式,如果不喜歡就自己改掉吧! # Code ```C++=1 #include <bits/stdc++.h> using sp=std::pair<char,int>; int main() { int n; std::cin>>n; std::cin.ignore(); std::string s; std::map<char, int> mp; while(getline(std::cin,s)){ std::stringstream sin(s); char c; while(sin>>c) if(std::isalpha(c)) mp[std::toupper(c)]++; } std::vector<sp> vec(mp.begin(),mp.end()); std::sort(vec.begin(),vec.end(), [](const sp&a, const sp&b){ if(a.second!=b.second) return a.second>b.second; return b.first>a.first; }); for(auto i:vec) std::cout<<i.first<<" " <<i.second<<"\n"; } ```
×
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