2418

/*
好做 ,也可以用SPlay,BST,SizeBalancedTree,二叉排序树
采用trie tree做的
*/

// include file
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <cctype>
#include <ctime>

#include <iostream>
#include <sstream>
#include <fstream>
#include <iomanip>
#include <bitset>

#include <algorithm>
#include <string>
#include <vector>
#include <queue>
#include <set>
#include <list>
#include <functional>

using namespace std;

// typedef
typedef long long LL;
typedef unsigned long long ULL;

// 
#define read freopen("in.txt","r",stdin)
#define write freopen("out.txt","w",stdout)
#define FORi(a,b,c) for(int i=(a);i<(b);i+=c)
#define FORj(a,b,c) for(int j=(a);j<(b);j+=c)
#define FORk(a,b,c) for(int k=(a);k<(b);k+=c)
#define FORp(a,b,c) for(int p=(a);p<(b);p+=c)
#define FORii(a,b,c) for(int ii=(a);ii<(b);ii+=c)
#define FORjj(a,b,c) for(int jj=(a);jj<(b);jj+=c)
#define FORkk(a,b,c) for(int kk=(a);kk<(b);kk+=c)

#define FF(i,a)    for(int i=0;i<(a);i++)
#define FFD(i,a)   for(int i=(a)-1;i>=0;i--)

#define Z(a) (a<<1)
#define Y(a) (a>>1)

const double eps = 1e-6;
const double INFf = 1e10;
const int INFi = 1000000000;
const double Pi = acos(-1.0);

template<class T> inline T sqr(T a){return a*a;}
template<class T> inline T TMAX(T x,T y)
{
	if(x>y) return x;
	return y;
}
template<class T> inline T TMIN(T x,T y)
{
	if(x<y) return x;
	return y;
}
template<class T> inline void SWAP(T &x,T &y)
{
	T t = x;
	x = y;
	y = t;
}
template<class T> inline T MMAX(T x,T y,T z)
{
	return TMAX(TMAX(x,y),z);
}


// code begin
struct trie_node
{
	trie_node* next[128];
	char name[32];
	int cnt;
};

trie_node mem[1000100];
int dx;
trie_node *root;
bool used[1000100];

int stk[10010],top;
char in[32],tmp[32];
int sum;

trie_node* CreateNode()
{
	trie_node* p = &mem[dx++];
	FORi(0,53,1)
	{
		p->next[i] = NULL;
	}
	p->cnt = 0;
	return p;
}

int getId(char c)
{
	return (int)c;
}

void InsertNode(char *tar)
{
	int id;
	trie_node* p = root;
	strcpy(tmp,tar);
	while(*tar)
	{
		id = getId(*tar);
		if(p->next[id]==NULL)
		{
			p->next[id] = CreateNode();
		}
		p = p->next[id];
		tar++;
	}
	if(p->cnt==0)
		strcpy(p->name,tmp);
	p->cnt++;
	if(!used[dx-1])
	{
		stk[top++] = dx-1;
		used[dx-1] = true;
	}
}

bool cmp(int a,int b)
{
	return strcmp(mem[a].name,mem[b].name)<0?1:0;
}

int main()
{
	read;
	write;
	dx = 0;
	top  =0;
	root = CreateNode();
	memset(used,0,sizeof(used));
	sum = 0;
	while(gets(in)!=NULL)
	{
		sum ++;
		InsertNode(in);
	}
	
	sort(stk,stk+top,cmp);

	FORi(0,top,1)
	{
		printf("%s %.4f\n",mem[stk[i]].name,(mem[stk[i]].cnt*100.0)/sum);
	}
	return 0;
}

posted @ 2011-04-07 12:43  AC2012  阅读(208)  评论(0编辑  收藏  举报