1042. Character statistics (20) time limit MS Memory limit 65536 KB code length limit 8000 B procedure StandardAuthor Chen, Yue
Write a program to find the most frequent English letter in a given text.
Input format:
The input gives a string with a length of not more than 1000 in a row. The string consists of any visible characters and spaces in the ASCII code table, containing at least 1 English letters, ending with a carriage return (carriage return does not count).
Output format:
Outputs the most frequently occurring English letter in a row and its occurrences, separated by a space. If there is a tie, the letter with the smallest alphabetical order is output. Statistics are case insensitive and output lowercase letters.
Input Sample:
This was a simple TEST. There is numbers and other symbols 1&2&3 .....
Sample output:
E 7
Idea: Apply for a container, only the letters in uppercase to the container, and then to sort, and then calculate the efficiency is higher than traversing the container
1 //1042.cpp: Defines the entry point of the console application. 2 //3 4#include"stdafx.h"5#include <iostream>6#include <string>7#include <vector>8#include <algorithm>9#include <cctype>Ten One using namespacestd; A - intMain () - { the intMax =0, sum=0; - stringstr; - Charc,d; -vector<Char>v; + - getline (CIN, str); + A //only the letters are stored in the array, and uppercase is converted to lowercase at for(inti =0; Str[i]! =' /'; i++) - if(Isalpha (Str[i]))//Enter the container only for letters -V.push_back (ToLower (str[i));//convert uppercase to lowercase - -Sort (V.begin (), V.end ());//sort all the letters - invector<Char>::iterator I, end =v.end (); - to for(i = V.begin (), d = c = *i; I! = end; d=*i,i++) + { - //true if the current element differs from the previous element the if(d! = (*i)) * { $ if(Sum >max)Panax Notoginseng { -c =D; theMax =sum; + } A thesum =0; + } - $sum++; $ } - - //If all the letters have only one the if(d = = *(V.begin ())) -cout << * (V.begin ()) <<" "<<sum;Wuyi Else thecout << C <<" "<<Max; - Wu return 0; -}
PAT B 1042 Character statistics (c + + edition)