poj3630 Phone List

Phone List

题目背景:

poj3630

分析:trie树的裸题,直接先全部读入,然后判定是否有前缀存在即可。

Source:

#include
     
     
      
      
#include
      
      
       
       
#include
       
       
        
        
#include
        
        
         
         
#include
         
         
           #include 
          
            #include 
           
             using namespace std; int tot, t, n; string str; struct node { int cnt; node *next[10]; node() { cnt = 0; memset(next, 0, sizeof(next)); } }; node *root, *tree[10010]; inline void R(int &v) { char c = 0; bool p = true; v = 0; while (!isdigit(c)) { if(c == '-') p = false; c = getchar(); } while (isdigit(c)) { v = (v << 3) + (v << 1) + (c ^ '0'); c = getchar(); } if (!p) v = -v; } void getin(string &s) { int len = s.size(); node *p = root; for (int i = 0; i < len; ++i) { if (!p->next[s[i] - '0']) p->next[s[i] - '0'] = new node(); p = p->next[s[i] - '0']; p->cnt++; } tree[tot++] = p; } bool find() { for (int i = 0; i < tot; ++i) if (tree[i]->cnt > 1) return false; return true; } void remove(node *x) { if (!x) return ; for (int i = 0; i < 10; ++i) remove(x->next[i]); delete x; } void work() { cin >> t; while (t--) { root = new node(); tot = 0; cin >> n; for (int i = 1; i <= n; ++i) { cin >> str; getin(str); } if (find()) cout << "YES" << '\n'; else cout << "NO" << '\n'; remove(root); } } int main() { ios::sync_with_stdio(false); cin.tie(NULL); work(); return 0; } 
            
           
         
        
        
       
       
      
      
     
     

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值