Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <bits/stdc++.h>
- using namespace std;
- #define forsn(i, s, n) for(int i=s;i<int(n);i++)
- #define forn(i, n) forsn(i, 0, n)
- #define all(v) v.begin(), v.end()
- #define rall(v) v.rbegin(), v.rend()
- #define NACHO ios_base::sync_with_stdio(0);cin.tie(NULL);
- typedef long long tint;
- struct Trie{
- map<char, Trie> arbol;
- bool finPalabra = false;
- };
- bool encontrado = 1;
- string s;
- int tamPalabra;
- void insertar(Trie &t, int pos) {
- if(pos < tamPalabra){
- if(pos == tamPalabra-1) t.finPalabra = true;
- insertar(t.arbol[s[pos]], pos+1);
- }
- }
- bool consistent = 1;
- void recorrer(Trie &t, const string &palabra){
- if(t.finPalabra && t.arbol.size() > 1){ consistent = 0;}
- for(auto &i:t.arbol) {
- recorrer(i.second, palabra+i.first);
- }
- }
- int main(){
- NACHO;
- int tc; cin >> tc;
- while(tc--){
- consistent = 1;
- int n; cin >> n;
- Trie t;
- forn(i, n){
- cin >> s;
- tamPalabra = int(s.size())+1;
- insertar(t, 0);
- }
- recorrer(t, "");
- if(consistent) cout << "YES" << "\n";
- else cout << "NO" << "\n";
- }
- }
- //
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement