PAT B 1042 Character statistics (c + + edition)

Source: Internet
Author: User

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)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.