ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

tiktokOA 1048. Longest String Chain

2021-10-03 13:33:53  阅读:228  来源: 互联网

标签:map return Chain int 1048 maxn Longest words word


自己写的,超时

class Solution {
public:
    int longestStrChain(vector<string>& words) {
        int maxn = 1;
        for(int i = 0; i < words.size(); i++){
            maxn = max(maxn, dfs(words[i],words));
        }
        return maxn;
    }
    int dfs(string a, vector<string>& words){
        int maxn = 1;
        for(int i = 0; i < words.size(); i++){
            if(isPre(a, words[i])){
                maxn = max(maxn, 1+dfs(words[i],words));
            }
        }
        return maxn;
    }
    bool isPre(string a, string b){
        if(a.length()+1 != b.length()){
            return false;
        }
        for(int i = 0; i < a.length(); i++){
            if(a[i] != b[i]){
                for(int j = 0; j < 26; j++){
                    char c = 'a' + j;
                    a = a.substr(0,i) + c + a.substr(i);
                    if(a == b)
                        return true;
                    else
                        a = a.substr(0,i) + a.substr(i+1);
                }
                return false;
            }
        }
        return true;
    }
};

剪枝以后依旧超时,但是快了一些

class Solution {
public:
    int longestStrChain(vector<string>& words) {
        unordered_map<int,int> map;
        int maxn = 1;
        for(int i = 0; i < words.size(); i++){
            if(!map[i]){
                map[i] = dfs(i,words,map);
            }
            maxn = max(maxn, map[i]);
        }
        return maxn;
    }
    int dfs(int index, vector<string>& words,unordered_map<int,int> &map){
        int maxn = 1;
        for(int i = 0; i < words.size(); i++){
            if(isPre(words[index], words[i])){
                if(!map[i]){
                    map[i] = dfs(i,words,map);
                }
                maxn = max(maxn, 1+map[i]);
            }
        }
        return maxn;
    }
    bool isPre(string a, string b){
        if(a.length()+1 != b.length()){
            return false;
        }
        for(int i = 0; i < a.length(); i++){
            if(a[i] != b[i]){
                for(int j = 0; j < 26; j++){
                    char c = 'a' + j;
                    a = a.substr(0,i) + c + a.substr(i);
                    if(a == b)
                        return true;
                    else
                        a = a.substr(0,i) + a.substr(i+1);
                }
                return false;
            }
        }
        return true;
    }
};

答案自底向上

class Solution {

public :

    int longestStrChain(vector<string> &words) {
        unordered_map<string, int> dp;

        // Sorting the list in terms of the word length.
        std::sort(words.begin(), words.end(), [](const std::string &word1, const std::string &word2) {
            return word1.size() < word2.size();
        });

        int longestWordSequenceLength = 1;

        for (const string &word : words) {
            int presentLength = 1;
            // Find all possible predecessors for the current word by removing one letter at a time.
            for (int i = 0; i < word.length(); i++) {
                string predecessor = word.substr(0, i) + word.substr(i + 1, word.length() + 1);
                if (dp.find(predecessor) != dp.end()) {
                    int previousLength = dp[predecessor];
                    presentLength = max(presentLength, previousLength + 1);
                }
            }
            dp[word] = presentLength;
            longestWordSequenceLength = max(longestWordSequenceLength, presentLength);
        }
        return longestWordSequenceLength;
    }
};

答案自顶向下


class Solution {
    
private:

    int dfs(unordered_set<string> &words, unordered_map<string, int> &memo, string currentWord) {
        // If the word is encountered previously we just return its value present in the map (memoization).
        if (memo.find(currentWord) != memo.end()) {
            return memo[currentWord];
        }
        // This stores the maximum length of word sequence possible with the 'currentWord' as the
        int maxLength = 1;

        // creating all possible strings taking out one character at a time from the `currentWord`
        for (int i = 0; i < currentWord.length(); i++) {
            string newWord = currentWord.substr(0, i) + currentWord.substr(i + 1);
            // If the new word formed is present in the list, we do a dfs search with this newWord.
            if (words.find(newWord) != words.end()) {
                int currentLength = 1 + dfs(words, memo, newWord);
                maxLength = max(maxLength, currentLength);
            }
        }
        memo[currentWord] = maxLength;

        return maxLength;
    }

public :
    int longestStrChain(vector<string> &words) {
        unordered_map<string, int> memo;
        unordered_set<string> wordsPresent;
        for (const string &word : words) {
            wordsPresent.insert(word);
        }
        int ans = 0;
        for (const string &word : words) {
            ans = max(ans, dfs(wordsPresent, memo, word));
        }
        return ans;
    }
};

答案是改成哈希表了,这样查找快。

修改后自己的代码

class Solution {
public:
    int longestStrChain(vector<string>& words) {
        unordered_map<string,int> map;
        unordered_set<string> wordsPresent;
        for (const string &word : words) {
            wordsPresent.insert(word);
        }
        int maxn = 0;
        for(int i = 0; i < words.size(); i++){
            if(!map[words[i]]){
                map[words[i]] = dfs(words[i],wordsPresent,map);
            }
            maxn = max(maxn, map[words[i]]);
        }
        return maxn;
    }
    int dfs(string s, unordered_set<string>& words,unordered_map<string,int> &map){
        int maxn = 1;
        for(int i = 0; i < s.length(); i++){
            string temp = s.substr(0,i)+s.substr(i+1);
            if(words.find(temp) != words.end()){
                if(!map[temp]){
                    map[temp] = dfs(temp, words, map);
                }
                maxn = max(maxn, 1+map[temp]);
            }
        }
        return maxn;
    }
};

标签:map,return,Chain,int,1048,maxn,Longest,words,word
来源: https://blog.csdn.net/HuiFeiDeTuoNiaoGZ/article/details/120593600

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有