标签:
Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 18202 Accepted Submission(s): 6115
#include <iostream> #include <cstring> #include <string> #include <algorithm> using namespace std; struct Node{ Node* pt[10]; bool isEnd; }; Node* root; int n; string s[10005]; Node memory[100000]; int allo; void trie_init() { memset(memory, 0, sizeof(memory)); allo = 0; root = &memory[allo++]; } bool trie_insert(string& str) { Node *p = root, *q; for(int i = 0; str[i] != ‘\0‘; ++i){ int id = str[i]-‘0‘; if(p->pt[id] == NULL){ q = &memory[allo++]; p->pt[id] = q; } else{ if(p->pt[id]->isEnd) return false; } p = p->pt[id]; } p->isEnd = true; return true; } void solve() { trie_init(); bool ok = true; for(int i = 0; i < n; ++i){ ok = trie_insert(s[i]); if(!ok){ cout<<"NO"<<endl; return; } } cout<<"YES"<<endl; } bool cmp(const string& a, const string& b) { return a.length() < b.length(); } int main() { int t; cin>>t; while(t--){ cin>>n; for(int i = 0; i < n; ++i) cin>>s[i]; sort(s, s+n, cmp); solve(); } return 0; }
标签:
原文地址:http://www.cnblogs.com/inmoonlight/p/5906704.html