Huawei trial-word count, Huawei word count

Source: Internet
Author: User

Huawei trial-word count, Huawei word count

Enter an article, and output the words with the highest frequency and the secondary frequency (all in lower case, separated by commas ). Only the space, comma, and period delimiters are displayed in the article.

Regardless of the frequency. Case Insensitive.


Input: I am a student. I come from XiDian, I love XiDian.

Output: I, xidian


#include<iostream>#include<string>#include<map>#include<cctype>using namespace std;int main(int argc, char *argv[]){    string s;    map<string,int>m;    while(getline(cin,s)!=NULL)    {        string t;        for(int i=0;i<s.size();++i)        {            if(s[i]==' '||s[i]==','||s[i]=='.')            {                m[t]+=1;                t="";            }            else                t+=s[i];        }    }    int first=0,second=0;    string s1,s2;    for(map<string,int>::iterator it=m.begin();it!=m.end();++it)    {        if(it->second>first)        {            s1=it->first;            first=it->second;        }        if(it->second>second&&it->second<first)        {            s2=it->first;            second=it->second;        }    }    for(int i=0;i<s1.size();++i)    {        cout<<(char)tolower(s1[i]);    }    cout<<",";    for(int i=0;i<s2.size();++i)    {        cout<<(char)tolower(s2[i]);    }    cout<<"\n";    return 0;}




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.