您的位置:首页 > 其它

POJ 2974 487-3279

2010-10-25 23:12 309 查看
//第一次我居然用链表做 结果超时 最后还是参考别人做的才AC
//不过从中知道了qsort 和 bsearch 这两个函数 还是收获手挺大的
#include <iostream>
#include <stdlib.h>
#include <string>
using namespace std;
#define MAX 100000
char a[MAX][9];
void decode(char *b)
{
int j=0;
static int count=0;
while (*b)
{
if(*b=='-')
{
b++;
continue;
}
if(j==3)
j++;
else
{
if (*b>='0' && *b<='9')
a[count][j++]=*b;
else if(*b>='A' && *b<='O')
a[count][j++]=(*b-'A')/3+'2';
else if(*b=='P'||*b=='R' ||*b=='S')
a[count][j++]='7';
else if(*b=='T'||*b=='U'||*b=='V')
a[count][j++]='8';
else if(*b=='W'||*b=='X'||*b=='Y')
a[count][j++]='9';
b++;
}
}
a[count][3]='-';
a[count][8]=0;
count++;
}
int compare(const void *a,const void *b)
{
return strcmp((char*)a,(char*)b);
}
int main()
{
bool flag=true;
char b[20];
memset(a,0,MAX*9*sizeof(char));
int n,i,j;
cin>>n;
for (i=0;i<n;i++)
{
cin>>b;
decode(b);
}
qsort(a,n,sizeof(char)*9,compare);
i=j=0;
while(i<n)
{
j=i;
i++;
while(i<n && strcmp(a[i],a[j])==0)
i++;
if(i-j>1)
{
cout<<a[j]<<" "<<i-j<<endl;
flag=false;
}
}
if(flag)
cout<<"No duplicates."<<endl;
return 0;
}


 

 

下面是用链表做的 估计应该没错 就是时间次了点

#include <iostream>
using namespace std;
typedef struct Node
{
char tel[8];
int cnt;
Node *next;
}Node,*pNode;
void ToTelnum(char *a,char *b);
bool Find(pNode head,char *b,pNode &ph);
void InsertList(pNode head,pNode p);
void Print(pNode head);
void Destroy(pNode head);
int main()
{
pNode head=new Node;
pNode ph;
head->next=NULL;
int n,i;
char a[20],b[8];
cin>>n;
for (i=0;i<n;i++)
{
cin>>a;
ToTelnum(a,b);
if(Find(head,b,ph))
ph->cnt=ph->cnt+1;
else
{
pNode p=new Node;
p->cnt=1;
p->next=NULL;
strcpy(p->tel,b);
InsertList(head,p);
}
}
cout<<endl;
Print(head);
Destroy(head);
return 0;
}
void ToTelnum(char *a,char *b)//a to b
{
while (*a)
{
if (*a>='0' && *a<='9')
*b++=*a;
else if(*a>='A' && *a<='O')
*b++=(*a-'A')/3+'2';
else if(*a=='P'||*a=='R' ||*a=='S')
*b++='7';
else if(*a=='T'||*a=='U'||*a=='V')
*b++='8';
else if(*a=='W'||*a=='X'||*a=='Y')
*b++='9';
a++;
}
*b=0;
}
bool Find(pNode head,char *b,pNode &ph)
{
ph=head->next;
while (ph!=NULL && strcmp(b,ph->tel))
ph=ph->next;
if(!ph)
return false;
else
return true;
}
void InsertList(pNode head,pNode p)
{
pNode post=head->next,pre=head;
while(post!=NULL && strcmp(p->tel,post->tel)>0)
{
post=post->next;pre=pre->next;
}
if(!post)
{
pre->next=p;
}
else
{
p->next=pre->next;pre->next=p;
}
}
void Destroy(pNode head)
{
pNode p;
while (!head)
{
p=head;
delete head;
head=p->next;
}
}
void Print(pNode head)
{
pNode p=head;
int i=0;
while (p!=NULL)
{
if(p->cnt>1)
{
for(i=0;i<3;i++)
cout<<p->tel[i];
cout<<"-";
for(i=3;i<7;i++)
cout<<p->tel[i];
cout<<" "<<p->cnt<<endl;
}
p=p->next;
}
}*/
                                                                                               

 还有高人用了数组映射的方法 太妙了

 

#include<stdio.h>
#include <stdlib.h>
#include<string.h>
int main()
{
int n,i,j,k;
char in[20];
char out[100000][9];
char map[]="22233344455566677778889999";
scanf("%d",n);
for(i=0;i<n;i++)
{
scanf("%s",in);
for(j=0,k=0;in[j]!=' ';j++)
{
if(in[j]=='-')continue;
if(k==3)
out[i][k++]='-';
if(in[j]<='Z'&&in[j]>='A')
out[i][k++]=map[in[j]-'A'];
else
out[i][k++]=in[j];
}
out[i][k]=' ';
}
qsort(out,n,9,strcmp);
for(i=0,j=0,k=0;i<n;i++)
{
if(!strcmp(out[i],out[j]));
else
{
if(i-j>1)
{
k++;
printf("%s %d/n",out[j],i-j);
}
j=i;

}
if(i==n-1&&i-j>0)
{
k++;
printf("%s %d/n",out[j],i-j+1);
}
}
if(k==0)printf("No duplicates./n");

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  ini null struct