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 stupidcccc at 2009-08-02 18:24:39 on Problem 1002
之前是先比较在排序,一直都是TLE
改成先排序后比较,就ac了,填上代码
#include <stdio.h>
#include <string.h>

int getinput(char **in, int n);
int count_sort(char **in,int *pre_seq,int *aft_seq,int len, int index);

char map[] = "22233344455566677778889999"; 

int main()
{
	int n, i,j,times,top;
	int *pre_seq,*aft_seq;
	char **in;
	bool dupFlag;
	scanf("%d",&n);
	in=new char*[n];
	top=0;
	for(i=0; i<n;i++)
	{
		in[i]=new char[10];
	}
	getinput(in,n);
	//sort the telephone num
	pre_seq=new int[n];
	aft_seq=new int[n];
	for(i=0;i<n;i++)
		pre_seq[i]=i;
	for(i=7;i>=0;i--)
	{
		if(i%2)
			count_sort(in,pre_seq,aft_seq,n,i);
		else
			count_sort(in,aft_seq,pre_seq,n,i);
	}
	
	dupFlag=false;
	for(i=0;i<n;)
	{
		times=1;
		for(j=i+1;j<n;j++)
		{
			if(strcmp(in[pre_seq[i]],in[pre_seq[j]])==0)
				times++;
			else
				break;
		}
		if(times>1)
		{
			dupFlag=true;
			printf("%s %d\n",in[pre_seq[i]],times);
		}
		i=j;
	}

	if(dupFlag==false)
		printf("No duplicates.\n"); 
	delete []in;
	delete []pre_seq;
	delete []aft_seq;
 	return 0;
}

int getinput(char **in, int n)
{
	char temp[50];
	int i,len,j,k;
	for(i=0;i<n;i++)
	{
		scanf("%s",temp);
		len=strlen(temp);
		for(j=0,k=0;j<len;j++)
			if(temp[j]!='-')
			{
				if(temp[j]>='A' && temp[j]<='Z')
					in[i][k]=map[temp[j]-'A'];
				else in[i][k]=temp[j];
				k++;
				if(k==3)
					in[i][k++]='-';
			}
		in[i][k]='\0';
	}
	return 0;
}

int count_sort(char **in,int *pre_seq,int *aft_seq,int len, int index)
{
	int i,temp;
	int c[10];
	if(len==0)
		return 0;
	if(index==3)
	{
		for(i=0;i<len;i++)
			aft_seq[i]=pre_seq[i];
		return 0;
	}
	for(i=0;i<10;i++)
		c[i]=0;
	for(i=0;i<len;i++)
	{
		temp=in[pre_seq[i]][index]-'0';
		c[temp]++;
	}
	for(i=1;i<10;i++)
		c[i]=c[i]+c[i-1];
	for(i=len-1;i>=0;i--)
	{
		temp=in[pre_seq[i]][index]-'0';
		aft_seq[c[temp]-1]=pre_seq[i];
		c[temp]--;
	}
	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