主题:总是超时的一段程序acm-pku-1002谁来讲解一原因或给出不超时的c++编写
#include<iostream>
#include<vector>
#include<string>
using namespace std;
main()
{
int phons;
while(cin>>phons)
{
string *p1=new string[phons+1];
vector<int>tim(phons+1);
int i=0;
for(i=1;i<=phons;i++)
{
cin>>p1[i];
for(int k=0;k<=p1[i].length()-1;k++)
{
if(p1[i][k]=='-')
{
p1[i].erase(k,1);
k=k-1;
continue;
}
else
{
int sc;
sc=p1[i][k]-'A';
if(sc<=17&&sc>=0)
p1[i][k]=sc/3+50;
if(sc>=18&&sc<='Y'-'A')
p1[i][k]=(sc-1)/3+50;
}
}
tim[i]=1;
for(int j=i-1;j>=1;j--)
if(p1[i]==p1[j])
{
tim[i]=tim[j]+1;
tim[j]=1;
break;
}
}
int d=phons/2;
while(d>1)
{
d=d/2;
for(i=1+d;i<=phons;i++)
if(p1[i]<p1[i-d])
{
p1[0]=p1[i];
tim[0]=tim[i];
for(int j=i-d;j>0&&(p1[0]<p1[j]);j-=d)
{
p1[j+d]=p1[j];
tim[j+d]=tim[j];
}
p1[j+d]=p1[0];
tim[j+d]=tim[0];
}
}
for(i=1;i<=phons;i++)
if(tim[i]!=0&&tim[i]!=1)
{
p1[i].insert(3,"-");
cout<<p1[i]<<" "<<tim[i]<<endl;
}
}
return 0;
}
#include<vector>
#include<string>
using namespace std;
main()
{
int phons;
while(cin>>phons)
{
string *p1=new string[phons+1];
vector<int>tim(phons+1);
int i=0;
for(i=1;i<=phons;i++)
{
cin>>p1[i];
for(int k=0;k<=p1[i].length()-1;k++)
{
if(p1[i][k]=='-')
{
p1[i].erase(k,1);
k=k-1;
continue;
}
else
{
int sc;
sc=p1[i][k]-'A';
if(sc<=17&&sc>=0)
p1[i][k]=sc/3+50;
if(sc>=18&&sc<='Y'-'A')
p1[i][k]=(sc-1)/3+50;
}
}
tim[i]=1;
for(int j=i-1;j>=1;j--)
if(p1[i]==p1[j])
{
tim[i]=tim[j]+1;
tim[j]=1;
break;
}
}
int d=phons/2;
while(d>1)
{
d=d/2;
for(i=1+d;i<=phons;i++)
if(p1[i]<p1[i-d])
{
p1[0]=p1[i];
tim[0]=tim[i];
for(int j=i-d;j>0&&(p1[0]<p1[j]);j-=d)
{
p1[j+d]=p1[j];
tim[j+d]=tim[j];
}
p1[j+d]=p1[0];
tim[j+d]=tim[0];
}
}
for(i=1;i<=phons;i++)
if(tim[i]!=0&&tim[i]!=1)
{
p1[i].insert(3,"-");
cout<<p1[i]<<" "<<tim[i]<<endl;
}
}
return 0;
}