#include <iostream>
#include <string>
#include <list>
using namespace std;
struct Node
{
int count;
Node *child[10];
Node()
{
count = 0;
int i;
for (i = 0; i < 10; i++)
child[i] = 0;
}
};
Node* current;
Node* newNode;
void Insert(Node* root, string phoneNum)
{
current = root;
for (int i = 0; i < phoneNum.length(); i++)
{
int num = phoneNum[i] - '0';
if (current->child[num] != NULL)
{
current = current->child[num];
++(current->count);
}
else
{
newNode = new Node;
++(newNode->count);
current->child[num] = newNode;
current = newNode;
}
}
}
int Search(Node* root, string phoneNum)
{
current = root;
for (int i = 0; i < phoneNum.length(); i++)
{
int num = phoneNum[i] - '0';
if (current->child[num] == NULL)
{
return 0;
}
current = current->child[num];
}
return current->count;
}
int main()
{
int num = 0;
string phoneNum;
list<string> vStr;
while (cin >> num)
{
if (num == 0)
{
break;
}
bool flag = false;
Node* root = new Node;
for (int i = 0; i < num; i++)
{
cin >> phoneNum;
vStr.push_back(phoneNum);
Insert(root, phoneNum);
}
list<string>::iterator it = vStr.begin();
for (; it != vStr.end(); it++)
{
if (Search(root, *it) - 1)
{
flag = true;
break;
}
}
if (flag)
cout << "NO" << endl;
else
cout << "YES" << endl;
vStr.clear();
}
return 0;
}
oj phone number
最新推荐文章于 2024-09-10 19:16:08 发布
这段代码实现了一个电话号码的存储和查找算法。通过一个结构体`Node`表示电话号码树节点,`Insert`函数用于插入电话号码,`Search`函数用于查找电话号码出现的次数。在主函数中,读取一组电话号码并存储到树中,然后检查是否存在重复的电话号码,如果存在则输出"NO",否则输出"YES"。
摘要由CSDN通过智能技术生成