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

Re:Accepts了,但是花的时间很多,请问如何优化?

Posted by hmh0512 at 2009-08-02 19:10:19 on Problem 3438
In Reply To:Accepts了,但是花的时间很多,请问如何优化? Posted by:xiaohao2009 at 2009-04-28 18:09:23
你的一边循环处理一边打印当然慢了。可以牺牲空间来提高实间。就是说定义数组把你处理的结果存放起来,等全部处理完了,再一起打印。这样速度会快一点。我的算法和你的一样,花时204ms。
#include<stdio.h>
char s[1002],s1[1002];
int num[1002];
int main(int argc, char *argv[])
{
	int n,i,j,k,m;
	char x;
	scanf("%d ",&n);
	while(n--)
	{
		scanf("%s",s);
		k=0;
		for(i=0;s[i];i=m)
		{
			for(num[k]=1,j=i+1;s[j]==s[i];j++)	
				num[k]++;
			m=j;
			s1[k]=s[i];
			k++;
		}	
		for(i=0;i<k;i++)
			printf("%d%c",num[i],s1[i]);
		printf("\n");
	}
	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