POJ 2418 二叉排序树

POJ 2418 二叉排序树

这一题题意就是给出很多字符串 要求出每个字符串出现的频率
STL写起来很简单 但是比较慢 这里看到一个比较好的思路就是根据字符串的排序建树 最后中序遍历计数 Node* createNode(Node *root,char cc[])
{
    if(root==NULL)
    {
        root=(Node *)malloc(sizeof(Node));
        root->lchild=NULL;
        root->rchild=NULL;
        root->times=1;
        strcpy(root->date,cc);
        return root;
    }
    if(strcmp(root->date,cc)==0)
    {
        root->times++;
    }
    else if(strcmp(root->date,cc)<0)
    {
        root->rchild=createNode(root->rchild,cc);
    }
    else if(strcmp(root->date,cc)>0)
    {
        root->lchild=createNode(root->lchild,cc);
    }

    return root;
}
再贴一个STL版本的 慢了一倍左右

#include<iostream>
#include<map>
#include<string>
using namespace std; string str;
map<string,int> mp;
map<string,int>::iterator iter,ed;
int main()
{
    int n=0;
    while(getline(cin,str))
    {
        mp[str]++;
        ++n;
    }
    iter = mp.begin();
    ed = mp.end();
   
    while(iter!=ed)
    {   
        cout<<iter->first;
        printf(" %.4lf\n",100.000000*(iter->second)/(double)n);
        ++iter;
    }
   
    return 0;
}

是卢亮写的 呵呵



你可能感兴趣的:(POJ 2418 二叉排序树)