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

我用字典树比较低效 C++

Posted by 3126016058 at 2014-03-11 09:42:27 on Problem 1002
In Reply To:感觉写得还行的代码,贴之: 916K 375MS G++ Posted by:Pcz at 2012-04-22 21:07:24
#include<cstdio>
#include<memory>
using namespace std;

struct NODE
{
	NODE*N[10];
	int count;
}M[100000*4],root1,root2;//外部为初始为 0;

NODE*NewNode()
{
	static int num=-1;
	return &M[++num];
}

bool NewNum(char S[],bool r);
void Print();

int main()
{
	memset(M,0,sizeof(M));
	int n=0;
	char S[100];
	scanf("%d",&n);
	gets(S);
	while(n--)
	{
		gets(S);
		if(NewNum(S,false))NewNum(S,true);//root2中恰好少一次
	}
	if(root2.count)
	{
		Print();
	}
	else puts("No duplicates.");
	return 0;
}

bool NewNum(char S[],bool r)
{
	static NODE* p;
	static int i,j,k;
	if(!r)p=&root1;
	else p=&root2;
	p->count++;
	r=false;
	i=-1;
	j=-1;
	while(S[++i])
	{
		if((S[i] >= '0' && S[i] <='9')||(S[i]>='A' && S[i]<'Z'&& S[i]!='Q'))
		{
			if(S[i] >= '0' && S[i] <='9')k=S[i]-'0';
			else k=((S[i]-'A'-(S[i]>'Q'))/3+2);
			if(!p->N[k])p->N[k]=NewNode();
			p=p->N[k];
			p->count++;
			if(++j == 6 && p->count > 1)r=true;
		}
	}
	return r;
}

void Print()
{
	struct WAY
	{
		NODE* p;
		int j;
		WAY()
		{
			p=NULL;
			j=-1;
		}
	}way[10];
	int i=0,j,k;
	NODE*p;
	way[i].p=&root2;
	way[i].j=-1;
	char S[10]="";
	while(true)
	{
		p=way[i].p;
		j=way[i].j;
		while(++j < 10)if(p->N[j])
		{
			way[i].j=j;
			way[++i].p=p->N[j];
			way[i].j=-1;
			break;
		}
		if(j == 10)
		{
			if(i==7)
			{
				k=-1;
				while(++k < 7)
				{
					S[k +(k>2)]=way[k].j+'0';
				}
				S[3]='-';
				printf("%s %d\n",S,p->count+1);
				i--;
			}
			else if(i==0)break;
			else i--;
		}
	}
}

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