..
include
include
include
include
using namespace std;
struct Node
{
int son[10];
bool end;
Node()
{
memset(son, -1, sizeof son);
end = false;
}
};
int main()
{
ios::sync_with_stdio(false);
cin.tie(0);
int T;
cin >> T;
while (T--)
{
vector
trie.emplace_back(Node()); //根节点
int n;
cin >> n;
bool flag = true; //true代表合法,没有前缀关系
for (int i = 0; i < n; i++)
{
string s;
cin >> s;
if (!flag) continue; //已经发现非法,跳过插入
int p = 0;
for (char ch : s)
{
int c = ch - '0';
//情况1:走到中间,遇到某个旧单词结束 →旧串是当前串前缀
if (trie[p].end == true)
{
flag = false;
break;
}
if (trie[p].son[c] == -1)
{
trie.emplace_back(Node());
trie[p].son[c] = trie.size() - 1;
}
p = trie[p].son[c];
}
if (!flag) continue;
//情况2:插入完本串,该节点还有子节点 →本串是某个旧串前缀
for (int k = 0; k < 10; k++)
{
if (trie[p].son[k] != -1)
{
flag = false;
break;
}
}
trie[p].end = true;
}
if (flag)
cout << "YES\n";
else
cout << "NO\n";
}
return 0;
}