题目描述题目要求统计每行文本中每个字母大小写分别统计出现的频率并输出出现次数最多的字母按先大写后小写的字母顺序排列以及该频率。输入格式输入包含多行文本每行可能包含任意可打印字符。输入以文件结束符EOF\texttt{EOF}EOF终止。输出格式对于每行输入输出一行包含出现频率最高的字母按字母顺序先大写后小写以及该频率格式如e 6或al 7。样例输入When riding your bicycle backwards down a one-way street, if the wheel falls of a canoe, how many ball bearings does it take to fill up a water buffalo? Hello Howard.输出e 6 al 7 a 3 Hlo 2题目分析本题的核心是统计每行中字母的频率并找出最高频率对应的字母集合。算法步骤初始化长度为525252的计数数组索引0∼250 \sim 250∼25对应大写字母A∼Z\texttt{A} \sim \texttt{Z}A∼Z索引26∼5126 \sim 5126∼51对应小写字母a∼z\texttt{a} \sim \texttt{z}a∼z。遍历行中每个字符若为大写字母对应索引ch−’A’\textit{ch} - \texttt{A}ch−’A’加111。若为小写字母对应索引ch−’a’26\textit{ch} - \texttt{a} 26ch−’a’26加111。找出计数数组的最大值maxFreq\textit{maxFreq}maxFreq。遍历索引000到515151若计数等于maxFreq\textit{maxFreq}maxFreq则输出对应的字母。输出空格和maxFreq\textit{maxFreq}maxFreq。复杂度分析每行字符处理一次时间复杂度O(L)O(L)O(L)。代码实现// Whats The Frequency, Kenneth?// UVa ID: 499// Verdict: Accepted// Submission Date: 2016-07-13// UVa Run Time: 0.000s//// 版权所有C2016邱秋。metaphysis # yeah dot net#includebits/stdc.husingnamespacestd;intmain(intargc,char*argv[]){ios::sync_with_stdio(false);string line;while(getline(cin,line)){vectorintcounter(52);fill(counter.begin(),counter.end(),0);for(inti0;iline.length();i)if(islower(line[i]))counter[line[i]-a26];elseif(isupper(line[i]))counter[line[i]-A];intmax_frequency*max_element(counter.begin(),counter.end());for(inti0;icounter.size();i)if(counter[i]max_frequency)cout(char)((i26?A:a)(i26?i:(i-26)));cout max_frequencyendl;}return0;}
UVa 499 What‘s The Frequency Kenneth
发布时间:2026/6/15 11:17:42
题目描述题目要求统计每行文本中每个字母大小写分别统计出现的频率并输出出现次数最多的字母按先大写后小写的字母顺序排列以及该频率。输入格式输入包含多行文本每行可能包含任意可打印字符。输入以文件结束符EOF\texttt{EOF}EOF终止。输出格式对于每行输入输出一行包含出现频率最高的字母按字母顺序先大写后小写以及该频率格式如e 6或al 7。样例输入When riding your bicycle backwards down a one-way street, if the wheel falls of a canoe, how many ball bearings does it take to fill up a water buffalo? Hello Howard.输出e 6 al 7 a 3 Hlo 2题目分析本题的核心是统计每行中字母的频率并找出最高频率对应的字母集合。算法步骤初始化长度为525252的计数数组索引0∼250 \sim 250∼25对应大写字母A∼Z\texttt{A} \sim \texttt{Z}A∼Z索引26∼5126 \sim 5126∼51对应小写字母a∼z\texttt{a} \sim \texttt{z}a∼z。遍历行中每个字符若为大写字母对应索引ch−’A’\textit{ch} - \texttt{A}ch−’A’加111。若为小写字母对应索引ch−’a’26\textit{ch} - \texttt{a} 26ch−’a’26加111。找出计数数组的最大值maxFreq\textit{maxFreq}maxFreq。遍历索引000到515151若计数等于maxFreq\textit{maxFreq}maxFreq则输出对应的字母。输出空格和maxFreq\textit{maxFreq}maxFreq。复杂度分析每行字符处理一次时间复杂度O(L)O(L)O(L)。代码实现// Whats The Frequency, Kenneth?// UVa ID: 499// Verdict: Accepted// Submission Date: 2016-07-13// UVa Run Time: 0.000s//// 版权所有C2016邱秋。metaphysis # yeah dot net#includebits/stdc.husingnamespacestd;intmain(intargc,char*argv[]){ios::sync_with_stdio(false);string line;while(getline(cin,line)){vectorintcounter(52);fill(counter.begin(),counter.end(),0);for(inti0;iline.length();i)if(islower(line[i]))counter[line[i]-a26];elseif(isupper(line[i]))counter[line[i]-A];intmax_frequency*max_element(counter.begin(),counter.end());for(inti0;icounter.size();i)if(counter[i]max_frequency)cout(char)((i26?A:a)(i26?i:(i-26)));cout max_frequencyendl;}return0;}