Timus: 1002. Phone Numbers

Source: Internet
Author: User

Convert the phone number into a combination of words that can be remembered in the dictionary to find the shortest combination.


Knowledge points applied to my question:

1 Trie data structure

2. map applications

3. Knowledge of dynamic programming for Word Break

4. Recursive pruning


Ideas:

1. Create a Trie dictionary tree to facilitate searching. However, instead of using characters, the dictionary tree converts characters into numbers and creates a digital dictionary tree. Then, the leaf node sets a container vector. The original word.

2. Create a table dynamically and record the string prefix substrings that can be found in the dictionary tree.

3. If the entire string is found in the dictionary tree, the word can be directly returned. If it cannot be directly found, you need to find a prefix substring In the table, and then find it in the dictionary tree to see if the word containing this substring is found, the length of the word to be found must be the shortest. -The pruning method can be used to improve efficiency.

Http://acm.timus.ru/problem.aspx? Space = 1 & num = 1002


Jing Xin-http://blog.csdn.net/kenden23


# Include
 
  
# Include
  
   
# Include
   
    
# Include
    
     
# Include
     
      
Using namespace std; class PhoneNumber1002_2 {static const int SIZE = 10; struct Node {vector
      
        Words; Node * children [SIZE]; explicit Node (): words () {for (int I = 0; I <SIZE; I ++) {children [I] = NULL ;}}; struct Trie {Node * emRoot; int count; explicit Trie (int c = 0): count (c) {emRoot = new Node ;}~ Trie () {deleteTrie (emRoot);} void deleteTrie (Node * root) {if (root) {for (int I = 0; I <SIZE; I ++) {deleteTrie (root-> children [I]);} delete root; root = NULL ;}}; void insert (Trie * trie, string & keys, string & keyWords) {int len = (int) keys. size (); Node * pCrawl = trie-> emRoot; trie-> count ++; for (int I = 0; I <len; I ++) {int k = keys [I]-'0'; if (! PCrawl-> children [k]) {pCrawl-> children [k] = new Node;} pCrawl = pCrawl-> children [k];} pCrawl-> words. push_back (keyWords);} Node * search (Node * root, string & keys) {int len = (int) keys. size (); Node * pCrawl = root; for (int I = 0; I <len; I ++) {int k = keys [I]-'0 '; if (! PCrawl-> children [k]) {return NULL; // not finishing all keys} pCrawl = pCrawl-> children [k];} return pCrawl ;} void searchLeft (Node * leaf, Node * r, int len, int & prun) {if (len> = prun) return; if (leaf-> words. size () {r = leaf; prun = len; return ;}for (int I = 0; I <SIZE; I ++) {searchLeft (leaf-> children [I], r, len + 1, prun) ;}} void wordsToKey (string & keys, string & keyWords, unordered_map
       
         & UmCC) {for (int I = 0; I <(int) keyWords. size (); I ++) {keys. push_back (umCC [keyWords [I]) ;}} void charsToMap (const string phdig [], unordered_map
        
          & UmCC) {for (int I = 0; I <10; I ++) {for (int k = 0; k <(int) phdig [I]. size (); k ++) {umCC [phdig [I] [k] = I + '0' ;}} string searchComb (Trie * trie, string & num) {vector
         
           Tbl (num. size (); for (int I = 0; I <(int) num. size (); I ++) {string s = num. substr (0, I + 1); Node * n = search (trie-> emRoot, s); if (n & n-> words. size () {tbl [I]. append (n-> words [0]); continue; // here the error code is break !!} For (int j = 1; j <= I; j ++) {if (tbl [J-1]. size () {s = num. substr (j, I-j + 1); n = search (trie-> emRoot, s); if (n & n-> words. size () {tbl [I]. append (tbl [J-1]); tbl [I]. append (""); tbl [I]. append (n-> words [0]); break ;}}} if (tbl. back (). size () {return tbl. back ();} string ans; for (int I = 0; I <(int) tbl. size ()-1; I ++) {if (tbl [I]. size () {string tmp = tbl [I]; string keys = num. substr (I + 1); Node * n = search (trie-> e MRoot, keys); if (! N) continue; Node * r = NULL; int prun = INT_MAX; searchLeft (n, r, 0, prun); tmp + = r-> words [0]; if (ans. empty () | tmp. size () <ans. size () {ans = tmp ;}} return ans. empty ()? "No solution. ": ans;} // test the function. Do not use the void printTrie (Node * n) {if (n) {for (int I = 0; I <SIZE; I ++) {printTrie (n-> children [I]); for (int j = 0; j <(int) n-> words. size (); j ++) {cout <
          
            Words [j] <
           
             UmCC; charsToMap (phdig, umCC); int N; string num, keys, keyWords; while (cin> num) & "-1 "! = Num) {cin> N; Trie trie; while (N --) {cin> keyWords; wordsToKey (keys, keyWords, umCC); insert (& trie, keys, keyWords); keys. clear (); // do not forget to clear} cout <
            
             


Related Article

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.