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:求助! 用了快排和二分,但仍然TLE,代码如下

Posted by dongshanluo at 2006-08-08 13:12:07 on Problem 2872
In Reply To:求助! 用了快排和二分,但仍然TLE,代码如下 Posted by:zhj5825 at 2006-08-03 09:26:35
> 
> 是不是还有什么要注意的地方呢?请大家指点一下,非常感谢
> ——————————————————————————————————————————————
> #include<stdio.h>
> #include<stdlib.h>
> #include<string.h>
> char dic[100000][100],word[100],wrong[100000][100];
> int Partition(int,int);
> void QSort(int,int);
> main()
> {
>       int N,i,num_email,num,flag,num_wrong=0,mid,low,high;
>       scanf("%d",&N);
>       for (i=1;i<=N;i++)
>           scanf("%s",dic[i]);
>       scanf("%d",&num_email);
>       num=num_email;
>       QSort(1,N);
>       
>       while (num_email>0)
>       {
>           num_wrong=0;
>           while (1)
>           {    flag=0;
>                scanf("%s",word);
>                if (strcmp(word,"-1")==0)
>                    break;
> 
>                 low=1;high=N; 
>                 while (low<=high)
>                 {
>                     mid=(low+high)/2; 
>                     if (strcmp(word,dic[mid])==0)
>                         {flag=1;break;}
>                     else if (strcmp(word,dic[mid])<0)
>                         high=mid-1;
>                     else low=mid+1;          
>                  } 
>                if (flag==0)
>                    {    
>                         num_wrong++; 
>                         strcpy(wrong[num_wrong],word);
>                         
>                    }
>           }            
>           
>           if (num_wrong==0)
>                    printf("Email %d is spelled correctly.\n",num-num_email+1);
>           else
>           {
>                    printf("Email %d is not spelled correctly.\n",num-num_email+1);
>                    for (i=1;i<=num_wrong;i++)
>                        printf("%s\n",wrong[i]);    
>           }
>          num_email--;
>       } 
>       printf("End of Output.\n");
>       system("PAUSE"); 
> }
> int Partition(int low,int high)
> {
>     char cmp[30];
>     strcpy(cmp,dic[low]);
>     while (low<high)
>     {
>         while (low<high && strcmp(dic[high],cmp)>=0) --high;
>         strcpy(dic[low],dic[high]);
>         while (low<high && strcmp(dic[low],cmp)<=0) ++low;
>         strcpy(dic[high],dic[low]);     
>     }
>     strcpy(dic[low],cmp);
>     return low;
> }
> void QSort(int low, int high)
> {
>     int pivotloc;
>     if (low<high)
>     {
>         pivotloc=Partition(low,high);
>         QSort(low,pivotloc-1);
>         QSort(pivotloc+1,high);             
>     }     
> }
> 

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