6.3w+
社区成员
#include <iostream>
#include <string>
using namespace std;
char chMaps[] = {'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'};
class BTree
{
public:
BTree(const char* szVal = "")
{
Val = szVal;
nCnt = Val.empty() ? 0:1;
pLeft = pRight = NULL;
}
int Insert(const char* szVal)
{
int nCmpRet = strcmp(szVal, Val.c_str());
if (nCmpRet > 0)
{
if (pRight)
{
return pRight->Insert(szVal);
}
else
{
pRight = new BTree(szVal);
return 1;
}
}
else if (nCmpRet < 0)
{
if (pLeft)
{
return pLeft->Insert(szVal);
}
else
{
pLeft = new BTree(szVal);
return 1;
}
}
else
{
return ++nCnt;
}
}
void InOrder()
{
if (pLeft)
{
pLeft->InOrder();
}
if (nCnt > 1)
{
cout << Val << " " << nCnt << endl;
}
if (pRight)
{
pRight->InOrder();
}
}
private:
string Val;
int nCnt;
BTree *pLeft;
BTree *pRight;
};
int main()
{
int nLines = 0;
cin>>nLines;
if (nLines <= 0)
{
cout<<"No duplicates."<<endl;
return 0;
}
BTree *pTree = NULL;
int nCnt = 0;
bool bNoDuplicate = true;
do
{
char buf[128] = {0};
char tmp[10] = {0};
cin>>buf;
int nPos = 0;
for(int i=0; buf[i]!=0; i++)
{
if (buf[i] == '-')
{
continue;
}
if (buf[i] < 'A')
{
tmp[nPos++] = buf[i];
}
else
{
if (buf[i] == 'Q' || buf[i] == 'Z')
{
break;
}
tmp[nPos++] = chMaps[buf[i] - 'A'];
}
if (nPos == 3)
{
tmp[nPos++] = '-';
}
else if (nPos == 8)
{
if (pTree == NULL)
{
pTree = new BTree(tmp);
}
else
{
if (pTree->Insert(tmp) > 1)
{
bNoDuplicate = false;
}
}
break;
}
}
}while(--nLines > 0);
if (bNoDuplicate)
{
cout<<"No duplicates."<<endl;
}
else
{
pTree->InOrder();
}
return 0;
}