Pku ACM 1002

第一种方法是通过二叉查找树的方法来解决排序和计数问题,结果运行超时了,想改进这个算法:二叉查找树用平衡二叉树,但实现有点复杂,以后有时间我再考虑做),下面是我的代码:

#include <iostream>
#include <string>

using namespace std;

bool fla=false;

int biao[26]={2,2,2,3,3,3,4,4,4,5,5,5,6,6,6,7,7,7,7,8,8,8,9,9,9,9};
    
struct node
{
   string tel;
   int cou;
   struct node *lchild,*rchild;
};
void Inorder(node *T)
{//中序遍历二叉树
    if(T)
    {
        Inorder(T->lchild);
        if(T->cou>1)
        {
           cout<<T->tel<<" "<<T->cou<<endl;
           fla=true;
        }
        Inorder(T->rchild);
        
    }
}
 
void Insert(node *&T,string telph)
{//查找:如果存在,则数目加1,否则插入
    if(T)
    {
        node *p=T;
        node *f=NULL;
        while(p)
        {
            if(p->tel==telph) {
                p->cou++; return;
            }
            f=p;
            if(p->tel>telph) p=p->lchild;
            else p=p->rchild;
        }
        node *s=new node;
        s->tel=telph;
        s->cou=1;
        s->lchild=s->rchild=NULL;
        if(f->tel>telph) f->lchild=s;


        else f->rchild=s;
    }
    else
    {
        node *s=new node;
        s->tel=telph;
        s->cou=1;
        s->lchild=s->rchild=NULL;
        T=s;
    }
}  
int main()
{
    //ifstream cin("1.txt");
    int num;
    cin>>num;
    
    struct node *Tree=NULL;

    string telresult;
    string telphone;
    for(int i=0;i<num;i++)
    {
        cin>>telphone;
        int k=0;
        telresult.clear();
        for(int i=0;i<telphone.length();i++)
        {
            if(telphone[i]=='-') continue;
            if(telphone[i]>='0'&&telphone[i]<='9')
            {
                telresult.push_back(telphone[i]);
                k++;
                if(k==3) {telresult.push_back('-');k++;}
                
                continue;
            }
            telresult.push_back(biao[telphone[i]-'A']+'0');
            k++;  
            if(k==3) {telresult.push_back('-');k++;}
        }
        Insert(Tree,telresult);
    }
    Inorder(Tree);
    if(!fla) cout<<"No duplicates."<<endl;
    return 0;
}


第二种方法应用C++标准模板库的sort来实现排序,代码在北大的POJ上通过了,代码如下:

#include <iostream>
#include <string>
#include <algorithm>
#include <vector>

using namespace std;

bool fla=false;

int biao[26]={2,2,2,3,3,3,4,4,4,5,5,5,6,6,6,7,7,7,7,8,8,8,9,9,9,9};
    

int main()
{
    int num;
    
    vector<string> res;
    cin>>num;
        
    string telresult;
    string telphone;
    for(int i=0;i<num;i++)
    {
        cin>>telphone;
        int k=0;
        telresult.clear();
        for(int i=0;i<telphone.length();i++)
        {
            if(telphone[i]=='-') continue;
            if(telphone[i]>='0'&&telphone[i]<='9')
            {
                telresult.push_back(telphone[i]);
                k++;
                if(k==3) {telresult.push_back('-');k++;}
                
                continue;
            }
            telresult.push_back(biao[telphone[i]-'A']+'0');
            k++;  
            if(k==3) {telresult.push_back('-');k++;}
        }
        res.push_back(telresult);
        
    }
    
    sort(res.begin(),res.end());
    int i=1;
    int count=1;
    while(i<num)
    {
          while(i<num&&res[i]==res[i-1]) { count++; i++;}
          if(count>1) {
              cout<<res[i-1]<<" "<<count<<endl;
              fla=true;
          }
          i++;count=1;
    }    
    if(!fla) cout<<"No duplicates."<<endl;
 
    return 0;

}



补充:用STL的map和stdio.h来改进我目前的代码,运行时间提高了很多,STL还是很有用的。

#include <string>
#include <cstring>
#include <stdio.h>
#include <map>

using namespace std;

bool fla=false;

int biao[26]={2,2,2,3,3,3,4,4,4,5,5,5,6,6,6,7,7,7,7,8,8,8,9,9,9,9};

 
int main()
{
    freopen("2.txt","r",stdin);  //有了这条语句就可以从文件中读数据了
    freopen("r.txt","w",stdout);    //有了这条语句运行结果就直接输出到文件中了
    
    map<string,int> res;
    int num;
    scanf("%d",&num);
          
    string telresult;
    char telphone[50];
    for(int i=0;i<num;i++)
    {
        scanf("%s",telphone);
        int k=0;
        telresult.clear();
        int len=strlen(telphone);
        for(int i=0;i<len;i++)
        {
            if(telphone[i]=='-') continue;
            if(telphone[i]>='0'&&telphone[i]<='9')
            {
                telresult.push_back(telphone[i]);
                k++;
                if(k==3) {telresult.push_back('-');k++;}
                
                continue;
            }
            telresult.push_back(biao[telphone[i]-'A']+'0');
            k++;  
            if(k==3) {telresult.push_back('-');k++;}
        }
        map<string,int>::iterator  iter;
        iter = iter=res.find(telresult);
        if(iter==res.end()) res.insert(pair<string,int>(telresult,1));
        else iter->second++;
    }
    map<string,int>::iterator  iter=res.begin();
    while(iter!=res.end())
    {
          if(iter->second>1) {
              printf("%s %d\n",iter->first.c_str(),iter->second);
              fla=true;
          }
         iter++;
    }    
    if(!fla) printf("No duplicates.\n");
    
    return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值