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

大哥们帮帮忙呀!我要哭了,超时 10*n^2,怎么改进呢?

Posted by wangguanjin at 2005-07-30 00:28:15 on Problem 2511
#include<stdio.h>
#include<string.h>
#include<math.h>

int N,f[11][2501],a[11][2501];
bool yes[2501][2501];
struct book
{
	int E;
	char S[30];
}books[2501];

void init()
{
	int i,j;
	unsigned p1,p2;
	bool flag;
	for(i=1;i<=N;i++){
		yes[i][i]=false;
		for(j=i+1;j<=N;j++){
			flag=true;
			p1=0; p2=0; 
			while(p1 < strlen(books[i].S) && p2 < strlen(books[j].S)){
				while((p1 < strlen(books[i].S)) 
					&& !( (books[i].S[p1] >='A' && books[i].S[p1] <= 'Z') || (books[i].S[p1] >='a' && books[i].S[p1] <= 'z')) )
					p1++;
				while((p2 < strlen(books[j].S)) 
					&& !( (books[j].S[p2] >='A' && books[j].S[p2] <= 'Z') || (books[j].S[p2] >='a' && books[j].S[p2] <= 'z')) )
					p2++;
				if(books[i].S[p1] == books[j].S[p2] || abs(books[i].S[p1] - books[j].S[p2] == 32)) { yes[i][j]=yes[j][i]=false; flag=false; break; }
				p1++; p2++;
			}
			if(flag) yes[i][j]=yes[j][i]=true;
		}
	}
}

bool ok(int k,int j)
{
	char temp1,temp2;
	unsigned p1=0,p2=0;
	while((p1 < strlen(books[k].S)) 
					&& !( (books[k].S[p1] >='A' && books[k].S[p1] <= 'Z') || (books[k].S[p1] >='a' && books[k].S[p1] <= 'z')) )
			p1++;
	while((p2 < strlen(books[j].S)) 
					&& !( (books[j].S[p2] >='A' && books[j].S[p2] <= 'Z') || (books[j].S[p2] >='a' && books[j].S[p2] <= 'z')) )
			p2++;
	temp1=books[k].S[p1];temp2=books[j].S[p2];
	if(temp1>=97) temp1=temp1-32;
	if(temp2>=97) temp2=temp2-32;
	if(temp1<temp2) 
		return true;
	else 
		return false;
}
void work()
{	
	int b[11];
	int ta,x,y,t;
	int result,i,j,k,max,maxs;
	int temp;
	for(i=2;i<=10;i++){
		maxs=0;
		for(j=1;j<=N;j++){
			max=0;ta=0;
			for(k=1;k<=N;k++){
				if(yes[k][j] && ok(k,j)){
					if(max<f[i-1][k]+books[j].E && f[i-1][k] != 0) {
						max=f[i-1][k]+books[j].E;
						ta=k;
					}
				}
			}
			a[i][j]=ta;
			f[i][j]=max;
			if(maxs<f[i][j] && f[i][j]>0) {maxs=f[i][j];x=i;y=j;}
		}
		if(maxs!=0) temp=maxs;
		else  break;
	}
	result = i-1;
	b[result]=y;
	t=y;
	for(i=result;i>=2;i--){
		b[i-1] = a[i][t];
		t=a[i][t];
	}
	printf("%d\n%d\n",result,temp);
	for(i=1;i<=result;i++)
		printf("%s\n",books[ b[i] ].S);
}

int main()
{	
	int i;
	while(EOF!=scanf("%d",&N)){
		for(i=1;i<=N;i++){
			scanf("%d ",&books[i].E);
			f[1][i]=books[i].E; 
			
			gets(books[i].S);
			
		}
		init();
		work();
	}
	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