电话单POJ3630
思路
题干在这:POJ3630
依次把电话号码插入字典树。如果一个号码插入过程中到了一个结束节点,或者插完这个号码并没有建立新节点,说明是no;否则是yes。
ac代码
#include<cstdio>
#include<iostream>
#include<algorithm>
#include<cstdlib>
#include<queue>
#include<cstring>
#include<vector>
#define ll long long
#define N 200010
using namespace std;
int tot = 1;
int trie[N][10];
int endi[N];
vector<int> resort;
int main() {
int t;
cin >> t;
for (int i = 0; i < t; i++) {
int sign = 0;
tot = 1;
memset(trie, 0, sizeof(trie));
memset(endi, 0, sizeof(endi));
int n;
cin >> n;
for (int j = 0; j < n; j++) {
char s[20] = { 0 };
scanf_s("%s", s, 20);
if (sign)
continue;
int sq = 0;
int mark = 0;
for (int k = 0; k < strlen(s); k++) {
if (trie[sq][s[k] - '0'] == 0) {
tot++;
trie[sq][s[k] - '0'] = tot;
sq = tot;
mark = 1;
}
else {
sq = trie[sq][s[k] - '0'];
}
if (endi[sq] == 1) {
resort.push_back(0);
sign = 1;
break;
}
if (k == strlen(s) - 1) {
if (mark == 0) {
resort.push_back(0);
sign = 1;
break;
}
else
endi[sq] = 1;
}
}
}
if (sign)
continue;
resort.push_back(1);
}
for (int i = 0; i < t; i++) {
if (resort[i])
cout << "YES" << endl;
else
cout << "NO" << endl;
}
}