用BST存储单词
#include<iostream>
#include<string>
#include<cctype>
#include<vector>
#include<algorithm>
#define SIZE 1048576
using namespace std;
struct pattern{
string str;
int fre;
};
struct bst{
struct pattern p;
struct bst *left, *right;
};
struct bst* Insert(string str, struct bst *t){
if (!t){
t = new struct bst;
t->left = t->right = NULL;
t->p.str = str;
t->p.fre = 1;
}
else if (t->p.str > str)
t->left = Insert(str, t->left);
else if (t->p.str < str)
t->right = Insert(str, t->right);
else if (t->p.str == str)
t->p.fre++;
return t;
}
bool cmp(const struct pattern &a, const struct pattern &b){
if (a.fre == b.fre)
return a.str < b.str;
else return a.fre > b.fre;
}
struct pattern large = { "", 0 };
void Search(struct bst*t){
if (!t)
return;
Search(t->left);
if (cmp(t->p, large))
large = t->p;
Search(t->right);
}
int main(){
freopen("1.in", "r", stdin);
string str,s;
getline(cin, str);
int i = 0,j,k;
struct bst *t = NULL;
while (i<str.length()){
s.clear();
while (isalnum(str[i])){
s.push_back(tolower(str[i++]));
}
//s.push_back('\0');
i++;
if (s.size()){
t = Insert(s, t);
}
}
Search(t);
if (t)
cout << large.str <<" "<<large.fre<<endl;
return 0;
}