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

牛人们看看我的程序,只一次排序,为什么还TLE?!

Posted by xiao1590 at 2006-04-30 20:55:47 on Problem 2408
#include<stdio.h>
#include<string.h>
#include<stdlib.h>

int compare(const void *a,const void *b)
{
	return(strcmp((char *)a,(char *)b));
}

int MIN(int a[])//找出现数的最小值,返回下标
{
	int min=a[0],i=0;
	if (a[1]<=min)
	{
		min=a[1];
		i=1;
	}
	if (a[2]<=min)
	{
		min=a[2];
		i=2;
	}
	if (a[3]<=min)
	{
		min=a[3];
		i=3;
	}
	if (a[4]<=min)
	{
		min=a[4];
		i=4;
	}
	return(i);
}

int MAX(int a[])//找出现数的最大值,返回下标
{
	int max=a[0],i=0;
	if (a[1]>max)
	{
		max=a[1];
		i=1;
	}
	if (a[2]>max)
	{
		max=a[2];
		i=2;
	}
	if (a[3]>max)
	{
		max=a[3];
		i=3;
	}
	if (a[4]>max)
	{
		max=a[4];
		i=4;
	}
	return(i);
}

int cmp(char stra[],char strb[])//比较两个字符串是不是由相同的字母组成
{
	int a[26]={0},b[26]={0},i,len,flag=1;
	len=strlen(stra);
	for(i=0;i<len;i++)
	{
		a[stra[i]-'a']++;
		b[strb[i]-'a']++;
	}
	for(i=0;i<26;i++)
	{
		if (a[i]!=b[i])
		{
			flag=0;
			break;
		}
	}
	return(flag);
}

void main()
{
	char in[30000][25],out[5][1000],temp[1000];
	int i,j,k=0,len=0,max=0,left=5,c,tt;
	int count[5]={0};
	int mark[30000]={0};
	while(scanf("%s",in[len])!=EOF)
	{
		len++;
	}
	qsort((void*)in,len,sizeof(in[0]),compare);//按字典序排列
	for(i=0;i<len;i++)
	{
		if (mark[i]==-1)//检查过的字符标记为-1,不再检查
			continue;
		mark[i]=-1;
		temp[0]='\0';//temp保存要输出的整个字符串
		strcat(temp,in[i]);
		strcat(temp," ");
		c=1;//C来保存本次搜索出现的符合条件的单词数
		for(j=i+1;j<len;j++)
		{
			if (strlen(in[i])==strlen(in[j]))
			{
				if (cmp(in[i],in[j]))
				{
					mark[j]=-1;
					strcat(temp,in[j]);
					strcat(temp," ");
					c++;
				}
			}
		}
		if (left)
		{
			count[5-left]=c;
			strcpy(out[5-left],temp);
			left--;
		}
		else
		{
			tt=MIN(count);
			if (c>count[tt])
			{
				count[tt]=c;
				strcpy(out[tt],temp);
			}
		}
	}
	for(i=0;i<5;i++)//输出,从大到小
	{
		tt=MAX(count);
		if (count[tt]>0)
		{
			printf("Group of size %d: %s.\n",count[tt],out[tt]);
			count[tt]=-1;
		}
		else
			break;
	}
}

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