Count the number of words [map simple application]

Count the number of words

Time limit: 1Sec Memory limit: 128MB Submit: 464 Resolution: 188

Title description

Count the number of occurrences of different words (words are case-sensitive, but case is ignored in the statistics) in the input paragraphs of English articles. The total number of words in the input paragraph does not exceed 100, and the length of the longest word does not exceed 20 letters. 

enter

A paragraph containing several sentences. Each sentence consists of several English words. Except for spaces, commas and periods, these input sentences do not contain other non-letter characters, and the commas and periods immediately follow the English words in front of it , There is no space in between. The last character in the paragraph is a carriage return, which means the end of input. 

Output

If there are a total of M different English words in the paragraph, output M lines in the order of their appearance in the paragraph. The format of each line is: All letters in the word are output in uppercase (the longest word is output in the top box, and there is no before it. Extra spaces; other words are aligned to the right) + colon + N * signs + number of occurrences of the word in the paragraph N 

Sample input

This  is  a  test.  This  test  is  easy.  This  is  a  test.  This  test  is  easy. 

Sample output

THIS:****4
  IS:****4
   A:**2
TEST:****4
EASY:**2

Problem solution: It feels like a very simple problem, let yourself write a mess, ensure the order of the words and judge the number of words.

Here I set an f to ensure that there are no spaces (it feels very silly). Then use map to save it. I have been thinking about the wrong question before, but I changed it to unordered.

#include <iostream>
#include<bits/stdc++.h>

using namespace std;

char s[50000];
string ss[5000];
int main()
{
    gets(s);
//    printf("%s",s);
    unordered_map<string,int>mp;
    mp.clear();
    int len = strlen(s);
    string st = "";
    int maxlen = 0;
    int f = 0;
    int top = 0;
    for(int i = 0; i < len; i ++)
    {
        if(s[i] >='a' && s[i]<='z')
        {
            s[i] = s[i] -'a' + 'A';
        }
        if(s[i] >= 'A' && s[i] <= 'Z' && f == 0)
        {
            st += s[i];
            f = 1;
        }
        else if(s[i] >= 'A' && s[i] <= 'Z' && f == 1)
        {
            st += s[i];
        }
        else if(f == 1)
        {
            mp[st] ++;
//            cout << st <<" ---"<<mp[st] <<endl;
            int tlen = st.length();
            f = 0;
            maxlen = max(tlen,maxlen);
            if(mp[st] == 1) ss[top++]= st;
            st = "";

        }
    }
    unordered_map<string,int> :: iterator it;
    for (int i = 0; i < top; i ++)
    {

        int tlen = ss[i].length();
        int x = maxlen - tlen;
        while(x--)
        {
            printf(" ");
        }

        cout << ss[i] << ":";
        int ii = 0;
        while(ii < mp[ss[i]])
        {
            printf("*");
            ii ++;
        }
        cout << mp[ss[i]] <<endl;
    }
    return 0;
}

 

Guess you like

Origin blog.csdn.net/Mercury_Lc/article/details/106479954
Recommended