#include <set>#include <algorithm>
#include <cstring>
#include <cstdlib>
#include <cstdio>
using namespace std;
struct node_comp;
struct node_data_t;
struct node_t
{
char _ch;
node_data_t* _data;
};
struct node_comp // 给STL的set容器的排序算子。
{
bool operator()(const node_t& left, const node_t& right)const
{
return left._ch < right._ch;
}
}; http://ykyi.net zausiu's blog struct node_data_t
{
set<node_t, node_comp> _nodes;
bool _terminal; // 标记是不是一个电话号码的终点字符了。
};
typedef set<node_t, node_comp> nodes_set;
nodes_set top_nodes;
// 如果已经可以判断电话号码不满足要求就返回false.
// 递归调用该函数。
bool odyssey(nodes_set& nodes, const char* num)
{
if ( 0 == *num )
{
return true;
}
//
node_t node;
node._ch = *num;
node._data = new node_data_t;
node._data->_terminal = !num[1];
pair<nodes_set::iterator, bool> retval = nodes.insert(node);
if ( retval.second ) // 还没有这个数字的结点.新插入的.
{
bool b;
b = odyssey(retval.first->_data->_nodes, num+1);
return b;
}
else // 已经存在
{
delete node._data;
// 并且已经是之前一个数字串的最后一个数字字符.或者现在这个字符是最后一个.
if ( retval.first->_data->_terminal || !num[1] )
{
return false;
}
else if ( !num[1] )
{
retval.first->_data->_terminal = true;
return true;
}
else
{
retval.first->_data->_terminal = false;
bool b;
b = odyssey(retval.first->_data->_nodes, num+1); // 递归
return b;
}
}
}
// 释放内存.
void free_mem(nodes_set& nodes)
{
for ( nodes_set::const_iterator ite = nodes.begin();
ite != nodes.end();
++ite )
{
free_mem(ite->_data->_nodes);
delete ite->_data;
}
nodes.clear();
}
int main(int argc, char* argv[])
{
int case_count; // 有多少个case
int phone_num_count; // 多少个电话霖巴
char phone_num_str[11]; // 存电话霖巴
bool successed; // 一组数字字符串是否符合要求
scanf("%d", &case_count);
for ( int i = 0; i < case_count; i++ )
{
scanf("%d", &phone_num_count);
successed = true;
for ( int j = 0; j < phone_num_count; j++ )
{
scanf("%s", phone_num_str);
if ( successed )
{
successed = odyssey(top_nodes, phone_num_str);
}
}
if ( successed )
{
printf(“YES\n”);
}
else
{
printf(“NO\n”);
}
free_mem(top_nodes);
}
return 0;
}