Skip to content

Instantly share code, notes, and snippets.

@mkfsn
Created June 29, 2014 14:00
Show Gist options
  • Select an option

  • Save mkfsn/20dfcce18b47346472e6 to your computer and use it in GitHub Desktop.

Select an option

Save mkfsn/20dfcce18b47346472e6 to your computer and use it in GitHub Desktop.
#include <cstdio>
#include <cstring>
#include <string>
#include <map>
using namespace std;
int main()
{
int t; // number of test cases
int treeNum; // sum of trees
string treeName; //tree name
char treeName_c[32];
map<string, int> tree;
map<string, int>::iterator it;
scanf("%d%*c", &t);
scanf("%*c");
while (t--) {
tree.clear();
treeNum = 0;
while (true) {
if (fgets(treeName_c, 32, stdin) == NULL || treeName_c[0] == '\n')
break;
if (strlen(treeName_c) > 0 && treeName_c[strlen(treeName_c) - 1] == '\n')
treeName_c[strlen(treeName_c) - 1] = '\0';
treeName = treeName_c;
treeNum++;
it = tree.find(treeName);
if (it == tree.end()) {
tree.insert(pair<string, int>(treeName, 1));
}
else {
it->second++;
}
}
it = tree.begin();
printf("%s %.4f\n", it->first.c_str(), (double)it->second * 100.0 / (double)treeNum);
it++;
for (; it != tree.end(); it++) {
printf("%s %.4f\n", it->first.c_str(), (double)it->second * 100.0 / (double)treeNum);
}
if (t != 0)
printf("\n");
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment