Leetcode.820. Word compression coding

topic

Given a list of words, this list will be encoded as a string S index and an index list A.

For example, if the list is [ "time", "me", "bell"], we can be expressed as S = "time # bell #" and indexes = [0, 2, 5].

For each index, we can start by reading the string index from the string S in position until the "#" end, to restore our previous list of words.

Then the minimum length of the string to the success of a given word list for encoding is how much?

Example:

Input: words = [ "time", "me", "bell"]
Output: 10
Description: S = "time # bell # ", indexes = [0, 2, 5].
 

prompt:

. 1 <= words.length <= 2000
. 1 <= words [I] .length <=. 7
each word lowercase.

Thinking

As long as a suffix of a word can be another word, the code length can be reduced its size in the string. Each time a character string with the remaining string taken for comparison to see whether the rest of the string can be a suffix, time complexity is O (n ^ 2 * c), c is the maximum length of a string, a predetermined title 7, barely pass all test cases.

class Solution {
public:
    int minimumLengthEncoding(vector<string>& words) {
        int p=0, n=0, s=words.size();
        vector<int> merged(words.size(), 0);
        for(int i=0; i<words.size(); i++){
            p+=words[i].size();
            for(int j=0; j<words.size(); j++){
                if(j==i || merged[j])    continue;
                if(func(words[i], words[j])){
                    n-=words[i].size();
                    s--;
                    merged[i]=1;
                    break;
                }
            }
        }
        return p+n+s;
    }
    bool func(string& str1, string& str2){//比对 str1 是否能成为 str2 的后缀
        if(str1.size()>str2.size())
            return false;
        int p1=str1.size();
        int p2=str2.size();
        while(p1--){
            if(str1[p1]!=str2[--p2])
                return false;
        }
        return true;
    }
};

In the comments section to see the god of sweet aunt Sweetiee Tree with the word solution to a problem , just the overall complexity of O (n * c), that is to traverse through all strings. Handling about the Great God of the code for future review and consolidate themselves:

class Solution {
    public int minimumLengthEncoding(String[] words) {
        int len = 0;
        Trie trie = new Trie();
        // 先对单词列表根据单词长度由长到短排序
        Arrays.sort(words, (s1, s2) -> s2.length() - s1.length());
        // 单词插入trie,返回该单词增加的编码长度
        for (String word: words) {
            len += trie.insert(word);
        }
        return len;
    }
}

// 定义tire
class Trie {
    
    TrieNode root;
    
    public Trie() {
        root = new TrieNode();
    }

    public int insert(String word) {
        TrieNode cur = root;
        boolean isNew = false;
        // 倒着插入单词
        for (int i = word.length() - 1; i >= 0; i--) {
            int c = word.charAt(i) - 'a';
            if (cur.children[c] == null) {
                isNew = true; // 是新单词
                cur.children[c] = new TrieNode();
            }
            cur = cur.children[c];
        }
        // 如果是新单词的话编码长度增加新单词的长度+1,否则不变。
        return isNew? word.length() + 1: 0;
    }
}

class TrieNode {
    char val;
    TrieNode[] children = new TrieNode[26];

    public TrieNode() {}
}

 

Published 18 original articles · won praise 0 · Views 773

Guess you like

Origin blog.csdn.net/afiguresomething/article/details/105174406