Online JudgeProblem SetAuthorsOnline ContestsUser
Web Board
Home Page
F.A.Qs
Statistical Charts
Problems
Submit Problem
Online Status
Prob.ID:
Register
Update your info
Authors ranklist
Current Contest
Past Contests
Scheduled Contests
Award Contest
User ID:
Password:
  Register

请问下我怎么错了(内附代码)

Posted by bra at 2008-11-17 19:49:25 on Problem 3283
/*
trie树
*/
#include<iostream>
using namespace std;

int ans,n,num;
char card[100000][3];

class trie 
{ 
public: 
    trie* next[15][5];
    trie() 
    {   
        for(int i=1;i<14;i++)
			for(int j=1;j<5;j++)
				next[i][j]=NULL; 
    } 
}*root;
int getValueId(char c)
{
	if(c>='2'&&c<='9')
		return c-'0';
	else if(c=='A')
		return 1;
	else if(c=='J')
		return 11;
	else if(c=='Q')
		return 12;
	else   // if(c=='K')
		return 13;
}
int getSuitId(char c)
{
	if(c=='C')
		return 1;
	else if(c=='D')
		return 2;
	else if(c=='H')
		return 3;
	else return 4;
}
void insert()
{
	trie *now=root;
	int x,y;

	for(int k=num-1;k>=0;k--)
	{
		if(strlen(card[k])==3)
			x=10;
		else x=getValueId(card[k][0]);

		y=getSuitId(card[k][strlen(card[k])-1]);

		if(now->next[x][y]==NULL) 
		{
			ans++;
			now->next[x][y]=new trie;
		}
		now=now->next[x][y];
	}
}
void del(trie * now)
{
	for(int i=1;i<14;i++)
		for(int j=1;j<5;j++)
			if(now->next[i][j]!=NULL)
				del(now->next[i][j]);
	delete(now);
}
int main()
{
	while(scanf("%d",&n),n)
	{
		root=new trie();
		ans=0;
		for(int i=0;i<n;i++)
		{
			scanf("%d",&num);
			for(int j=0;j<num;j++)
				scanf("%s",&card[j]);
			insert();
		}
		printf("%d\n",ans);
		del(root);
	}
}

Followed by:

Post your reply here:
User ID:
Password:
Title:

Content:

Home Page   Go Back  To top


All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator