poj 1002 很好的水题

思路简单,但要过可是非常艰难(测试数据非常bt,ac率就可以看出来)

 

刚开始直接对字符串进行排序,超时了。

之后采用索引的方法排序,仍旧超时。

最后化为整数,并用sort函数进行排序,终于ac(用自己编的快排仍旧超时)

 

五点总结:

1)对整数操作比对字符串操作效率高

2)用stlsort函数比自己写的快排效率高(要好好学下stl了)

3)读取一行字符用gets函数比scanf效率稍高一点

         但要注意之前的scanf要读入回车符,否则回车会被当前gets读取

                   scanf("%d/n", &num);    

4printf中可以设定输出整数的场宽,并且可以设定填充0

         详细介绍:http://blog.sina.com.cn/s/blog_5ac88b350100auna.html

     

5)对字符串初始化(new)很耗时!!

         所以尽量不要在循环内初始化字符串,而是在外面初始化一次。这一题就因为这一点差别差了700MS!!!!详见注释

 

         总之这一题让我学习了如何争分夺秒

代码:
#include

#include

using namespace std;

int carray[100001];

//int digitlength[100001];

int letter[25] = {2,2,2,3,3,3,4,4,4,5,5,5,6,6,6,7,0,7,7,8,8,8,9,9,9};

char *temp=new char[50];

int main()

{

         int num;

         scanf("%d/n", &num);              //要读入回车符

         int i;

         //char temp;

         for(i=0;i

         {

                   //char*temp=new char[50]              将这一步放在main外面,时间减了700MS!!!!

                   gets(temp);

                   int pt=0,j=0;

                   char ch;

                   ch=temp[pt++];

                   //bool nonzerofind=0;

                   while(ch!='/0')

                   {

                            if(ch!='-') 

                            {

                                     if(ch>='A'&&ch<='Z')

                                     {       

                                               carray[i] = carray[i]*10+letter[ch-'A'];

                                              

                                     }       

                                     else

                                     {

                                               carray[i]=carray[i]*10+ch-'0';

                                     }

                            }

                            ch=temp[pt++];

                   }       

         }

         sort(carray,carray+num);

         int count=1;

         bool dup=0;

         for(i=1;i

         {

                   if(carray[i]==carray[i-1])  count++;

                   else

                   {

                            if(count>1)

                            {

 

                                     printf("%03d-%04d %d/n", carray[i-1]/10000, carray[i-1]%10000, count); 

                                     dup=1;

                            }

                            count=1;

                   }       

         }

         if(count>1)       

         {

 

                   printf("%03d-%04d %d/n", carray[i-1]/10000, carray[i-1]%10000, count); 

                   dup=1;

        

         }

         if(dup==0)         cout<<"No duplicates."<

         return 0;

}

 

你可能感兴趣的:(bt,测试)