#include <iostream>
using namespace std;
#include <memory.h>
#include <string.h>
typedef struct node{
char pbuf[20];
int pcount;
struct node* left;
struct node* right;
}node;
void insertnode(node* ppar, node* pnew)
{
if(strcmp(ppar->pbuf, pnew->pbuf)<0)
{
if(ppar->right!=NULL)
insertnode(ppar->right, pnew);
else
ppar->right=pnew;
}
else if(strcmp(ppar->pbuf, pnew->pbuf)>0)
{
if(ppar->left!=NULL)
insertnode(ppar->left, pnew);
else
ppar->left=pnew;
}
else
ppar->pcount++;
}
void printout(node* p, int& flag)
{
if(p->left!=NULL)
printout(p->left, flag);
if(p->pcount>1)
{
printf("%.3s-%s %d\n", p->pbuf, p->pbuf+3, p->pcount);
flag = 1;
}
if(p->right!=NULL)
printout(p->right, flag);
}
int main()
{
char mapp[128];
memset(mapp, 0, 128);
int i =0;
for(i='0'; i<='9'; i++)
mapp[i] = i;
memset(mapp+'A', '2', 3);
memset(mapp+'D', '3', 3);
memset(mapp+'G', '4', 3);
memset(mapp+'J', '5', 3);
memset(mapp+'M', '6', 3);
memset(mapp+'P', '7', 1);
memset(mapp+'R', '7', 2);
memset(mapp+'T', '8', 3);
memset(mapp+'W', '9', 3);
node* phead = new node;
memset((void*)phead, 0, sizeof(node));
int n;
cin>>n;
char inbuf[20];
for(i=0; i<n; i++)
{
memset(inbuf, 0, 20);
cin>>inbuf;
char* pin = inbuf;
node* pnew = new node;
memset((void*)pnew, 0, sizeof(node));
pnew->pcount = 1;
char* pout =pnew->pbuf;
while(*pin)
{
if(mapp[*pin]!='\0'){
*pout++ = mapp[*pin];
}
pin++;
}
insertnode(phead, pnew);
}
int flag =0;
printout(phead, flag);
if(flag==0)
cout<<"No duplicates. ";
return 0;
}
刚开始报Output Limit Exceeded,网上百度了许多,一个网页说说用二叉树,给过了
成绩很惨(查询网址http://poj.org/problemstatus?problem_id=1002&start=5000&orderby=time&language=-1)
2 10611620(318) zjufan 4468K 32MS C++ 4305B 2012-08-05 17:13:38
...
16151 11010059 dragoo1 5736K 1391MS C++ 1697B 2012-11-12 22:09:22