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

但是为什么WA了呢

Posted by ACM_henry at 2008-11-03 17:17:06 on Problem 2004
In Reply To:枚举变化,排序单词内部字母,直接hash. Posted by:mixter at 2006-09-12 11:11:21
#include <iostream>
#include <algorithm>
using namespace std;
struct node
{
	char q[25];
	int index;
}p[10001];
char zifu[25];
int dp[10001][2];
const int prim=100001;
int sum=0;
struct h
{
	int key;
	int num;	
}hash[prim];
void insert(long v,int aim)
{
    long key=v; while(key<0) key+=prim;
    if(key>=prim) key=key%prim;
    while(hash[key].num!=-1)
	{
         if(hash[key].key==v){ return; }
         key=(key+1)%prim;            
    }     
    hash[key].num=aim; hash[key].key=v;
}

int search(long v)
{
    long key=v; while(key<0) key+=prim;
    if(key>=prim) key=key%prim;
    while(hash[key].num!=-1)
	{
         if(hash[key].key==v) return hash[key].num;
         key=(key+1)%prim;                      
    }
    return -1;
}

bool cmp(node x,node y)
{
	return strlen(x.q)<strlen(y.q);
}
void init()
{
	int i;
	for(i=0;i<prim;i++)
	{
		hash[i].num=-1;
	}
}
int main()
{
	init();
	int i,j,len;
	memset(dp,0,sizeof(dp));
	int sums=0;
	int maxs=0;
	while(cin>>p[sums].q)
	{
		if(strlen(p[sums].q)>maxs)
		{
			maxs=strlen(p[sums].q);
		}
		sums++;
	}
	for(i=0;i<sums;i++)
	{
		dp[i][1]=-1;
	}
	sort(p,p+sums,cmp);
	for(i=0;i<sums;i++)
	{
		sum=0;
		len=strlen(p[i].q);
		//cout<<p[i].q<<endl;
		p[i].index=i;
		if(len<maxs)
		{
			memcpy(zifu,p[i].q,len);
			sort(zifu,zifu+len);
			zifu[len]='\0';
			for(j=0;j<len;j++)
			{
				sum=zifu[j]-'a'+1+sum*26;
				if(sum>=prim)
				{
					sum%=prim;
				}
			}
			insert(sum,i);
		}
	}
	int k;
	for(i=sums-1;i>=0;i--)
	{
		len=strlen(p[i].q);
		memcpy(zifu,p[i].q,len);
		sort(zifu,zifu+len);
		zifu[len]='\0';
		for(j=0;j<len;j++)
		{
			sum=0;
			for(k=0;k<len;k++)
			{
				if(k==j)
				{
					continue;
				}
				sum=sum*26+zifu[k]-'a'+1;
				if(sum>=prim)
				{	
					sum=sum%prim;
				}
			}
			int v=search(sum);
			if(v!=-1)
			{
				if(dp[v][0]<=dp[i][0]+1)
				{
					dp[v][0]=dp[i][0]+1;
					dp[v][1]=i;
				}
			}
		}
	}
	maxs=0;
	int aim=0;
	for(i=0;i<sums;i++)
	{
		if(dp[i][0]>maxs)
		{
			maxs=dp[i][0];
			aim=i;
		}
	}
	while(aim!=-1)
	{
		cout<<p[aim].q<<endl;
		aim=dp[aim][1];
	}
return 0;
}

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