洛谷 UVA10226 Hardwood Species

洛谷 UVA10226 Hardwood Species

Luo Gu evaluation portal

Title Description

PDF

img

Input Format

img

Output Format

img

Sample input and output

Input # 1 copy

Output # 1 copy

Title Translation:

Given a number string of the output format is :( lexicographical ordering) + given proportion among the total number of times a given character string in this string appears.

Note: multiple sets of data.

answer:

Describe three things:

The first - ignore whitespace string input mode only consider the carriage return.

The second - \ (C ++ \, \, STL \, \, the Map \) container.

The third - trie.

The first:

Ignore whitespace string input mode only consider the carriage return.

code show as below:

getline(cin,s);

(Use of specific ways you can go online to see, but talking about are too complicated, jargon various streams simply do not understand what the hell is (konjac may be too weak)), I think we just need to remember this stuff format can be.

S herein refers to the \ (C ++ STL \) of \ (String \) container. If you have this container is not very understanding of small partners, the venue of this Tacca this blog:

On the C ++ STL string container

The second:

map container.

If you have a map of container is not very familiar with the junior partner please refer to the konjac of this blog:

Detailed C ++ STL map container

Third:

Trie.

If you have the dictionary tree is not very understanding of small partners please refer to the konjac of this blog:

Detailed trie


Well, now, we thought:

Let us count the number of times the title string appears, then we can easily think \ (map \) this mapping relationship building, the establishment of a \ (map \) is constructed from \ (string \) to \ (int \) one mapping can easily statistical information.

The most pleasant is, \ (the Map \) comes first keyword ranking just support \ (string \) dictionary sequential operation. Just do not get too comfortable!

So we come to an AC short codes:

Note: You need a lot of attention to detail, multiple sets of data very sick, multi card wrap wrap card, be sure to carefully cf. own code and following a program in the end is not the same.

#include<cstdio>
#include<map>
#include<iostream>
#include<string>
using namespace std;
int t,tot;
string s;
map<string,int> mp;
map<string,int>::iterator it;
int main()
{
    scanf("%d\n",&t);
    while(t--)
    {
        tot=0;
        mp.clear();
        while(1)
        {
            getline(cin,s);
            if(s[0]=='\0')
                break;
            tot++;
            mp[s]++;
        }
        for(it=mp.begin();it!=mp.end();it++)
        {
            double ans=(it->second)*100.0/tot;
            printf("%s %.4lf\n",it->first.c_str(),ans);
        }
        if(t)
            puts("");
    }
    return 0;
}

Guess you like

Origin www.cnblogs.com/fusiwei/p/11973229.html