joj2035

一开始超时,怎么改都超时,后来果断把stl换成纯数组,竟然AC了,莫非stl的操作很费时间吗,不解:

之前的超时stl版:

#include<iostream>
#include<stack>
#include<queue>
using namespace std;
int mark[10005];
int main()
{
    int n;
   
    while(scanf("%d",&n),n>0)
    {
      if(n==0)
        continue;
      memset(mark,0,sizeof(mark));
      vector<int> s1;
      queue<int> s2;
      int i,a;
      for(i=0;i<n;i++)
      {
         scanf("%d",&a);
         s1.push_back(a);
      }
      for(i=0;i<n;i++)
      {
         scanf("%d",&a);
         s2.push(a);
      }
      int t=s2.front();
      printf("%d\n",t);
      int count=0;
      while(s1[count]!=s2.front())
      {
           mark[s1[count]]=-1;
           count++;
      }
      s2.pop();
      while(s2.size()!=0)
      {  
          int m=s2.front();
          if(mark[m]==-1)
          {
             s2.pop();
          }
          else
          {
             int i;
             printf("%d\n",m);
             for(i=count-1;s1[i]!=m;i++)
             {
                 mark[s1[i]]=-1;
             }
             s2.pop();
          }
      }
    }
}

改良版:

#include<iostream>
#include<stack>
#include<queue>
using namespace std;
int mark[10005];
int s1[10005];
int s2[10005];
int main()
{
    int n;
   
    while(scanf("%d",&n),n>0)
    {
      if(n==0)
        continue;
      memset(mark,0,sizeof(mark));
     
      int i,a;
      for(i=0;i<n;i++)
         scanf("%d",&s1[i]);
      for(i=0;i<n;i++)
         scanf("%d",&s2[i]);
      int t=s2[0];
      printf("%d\n",t);
      int count=0;
      while(s1[count]!=t)
      {
           mark[s1[count]]=-1;
           count++;
      }
      int ii;
      for(ii=1;ii<n;ii++)  
      {
          int m=s2[ii];
          if(mark[m]==0)
          {
             int i;
             printf("%d\n",m);
             for(i=count-1;s1[i]!=m;i++)
             {
                 mark[s1[i]]=-1;
             }
          }
      }
    }
}

 

posted @ 2011-10-30 10:36  DChipNau  阅读(143)  评论(0编辑  收藏  举报