HDU1671 Phone List (Dictionary tree)

Source: Internet
Author: User

HDU1671 Phone List (Dictionary tree)

Question:

If you enter multiple string numbers, you must determine whether some numeric strings are prefix of other strings. If NO output exists, otherwise YES is output.

Solution:

Create a dictionary tree with trie, mark 1 at the end of each digit string, and then judge each input string. Whether there is a flag mark.

 

# Include
 
  
# Include
  
   
# Include
   
    
# Include
    
     
Using namespace std; const int MAX_LEN = 11; typedef struct trie {trie * next [10]; int num ;}t; T * tree; bool flag; void trieDel (T * p) {for (int I = 0; I <10; I ++) {if (p-> next [I]! = NULL) trieDel (p-> next [I]);} free (p);} void trieInsert (char str []) {T * p = tree, * q; int len = strlen (str); for (int I = 0; I
     
      
Next [id] = NULL) {q = new T; for (int j = 0; j <10; j ++) q-> next [j] = NULL; q-> num = 0; p-> next [id] = q; p = p-> next [id];} else p = p-> next [id]; if (p-> num = 1) flag = false;} p-> num = 1; for (int I = 0; I <10; I ++) {if (p-> next [I]! = NULL) {flag = false; break ;}} void init () {tree = new T; for (int I = 0; I <10; I ++) {tree-> next [I] = NULL ;}int main () {int cas; scanf ("% d", & cas); while (cas --) {flag = true; int n; scanf ("% d", & n); getchar (); init (); for (int I = 0; I
      
        The following code does not use the dictionary tree:
       

 

 

#include
        
         #include#include
         #include
          
           #include
           
            #include
            
             #include
             
              #include
              
               #include
               
                #include
                
                 #include
                 
                  #include
                  
                   typedef unsigned long long ull;typedef long long ll;using namespace std;string inp[10010];bool solve(const string &a, const string & b){ if(a.length() > b.length()) return false; for(unsigned i = 0; i < a.length(); ++i) if(a[i] != b[i]) return false; return true;}int main(){ int t; cin >>t; while(t--){ int n; bool bol = true; cin >> n; for(int i = 0; i < n; ++i) cin >> inp[i]; sort(inp, inp + n); for(int i = 0; i < n; ++i) if(solve(inp[i], inp[i+1])) bol = false; cout <<(bol?"YES":"NO") <
                   
                    

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.