回 帖 发 新 帖 刷新版面

主题:总是超时的一段程序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;
}

回复列表 (共3个回复)

沙发


#include<iostream>
#include<vector>
#include<string>
using namespace std;
main()
{
 int phons;
 while(cin>>phons)
{
     string *p1=new string[phons];
     vector<int>tim(phons);
    int i=0;
    for(i=0;i<=phons-1;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>=0;j--)
        if(p1[i]==p1[j])
        {
            tim[i]=tim[j]+1;
            tim[j]=1;
            break;
        }
    }
    for(i=0;i<=phons-1;i++)
    {
         int q=i;
         for(int t=i+1;t<=phons-1;t++)
            if(p1[q]>=p1[t])
                 q=t;
        if(tim[q]!=1)
        {
            p1[q].insert(3,"-");
            cout<<p1[q]<<" "<<tim[q]<<endl;
        }
        string p2=p1[i];
        p1[i]=p1[q];
        p1[q]=p2;
        int p3=tim[i];
        tim[i]=tim[q];
        tim[q]=p3;
    }
}
 return 0;
}
以前写的一段

板凳

刚写的,已通过.
[code=c]
#include<stdio.h>
#include<algorithm>
using namespace std;

int num[]={ -1,-1,-1,   //-,.,/
            0,1,2,3,4,5,6,7,8,9,        //0,1,2,3,4,5,6,7,8,9
            -1, -1, -1, -1, -1, -1, -1, //:,;,<,=,>,?,@
            2,2,2, //A,B,C
            3,3,3, //D,E,F
            4,4,4, //G,H,I
            5,5,5, //J,K,L
            6,6,6, //M,N,O
            7,-1,7,7, //P,Q,R,S
            8,8,8, //T,U,V
            9,9,9, //W,X,Y
            -1     //Z
           };
int main(){
    char str[100];
    int size;
    scanf("%d",&size);
    int *phone =new int[size+1];
    for(int n=0;n<size;n++){
        scanf("%s",str);
        int count=0,index=0,tmp;
        phone[n] =0;
        while(count<7){
            tmp =num[str[index++]-'-'];
            if(tmp>=0){
                count ++;
                phone[n] =phone[n]*10+tmp;
            }
        }
    }
    sort(phone,phone+size);
    phone[size] =-1;
    int dup =0,pre =-1,all =0;
    char expr[9];
    for(int index=0;index<=size;index++){
        if(pre!=phone[index]){
            if(all>1){
                dup++;
                for(int k=0,i=7;k<7;k++){
                    expr[i--] =pre%10 +'0';
                    pre /= 10;
                    if(k==3) expr[i--] ='-';
                }
                expr[8] =0;
                printf("%s %d\n",expr,all);
            }
            pre =phone[index];
            all =1;
        }
        else all++;
    }
    delete[] phone;
    if(dup==0) printf("No duplicates.\n");
    return 0;
}[/code][em16]

3 楼


能不能指点一下我写的那段

我来回复

您尚未登录,请登录后再回复。点此登录或注册