1048. 最长字符串链

转载请声明地址四元君

1048. 最长字符串链

题目难度 Medium

给出一个单词列表,其中每个单词都由小写英文字母组成。

如果我们可以在 word1 的任何地方添加一个字母使其变成 word2,那么我们认为 word1 是 word2 的前身。例如,“abc” 是 “abac” 的前身。

词链是单词 [word_1, word_2, …, word_k] 组成的序列,k >= 1,其中 word_1 是 word_2 的前身,word_2 是 word_3 的前身,依此类推。

从给定单词列表 words 中选择单词组成词链,返回词链的最长可能长度。

示例:

输入:["a","b","ba","bca","bda","bdca"]
输出:4
解释:最长单词链之一为 "a","ba","bda","bdca"。

提示:

1 <= words.length <= 1000
1 <= words[i].length <= 16
words[i] 仅由小写英文字母组成。
思路

之前做过的一道题是找到words里面可以凑成的最大字符串。那个做法可以是哈希表或者是前缀树。最好是前缀树。

这个题一时间没想到前缀树具体该怎么做,用了稍微暴力的方法。遍历每一个字符串,看从这个字符串出发得到的最大值是多少,如果满足条件,就深度搜索。使用一个全局的map记录该位置的字符串可有的最大词链长度。如果走过了就直接返回。最后记录这个最大值返回。

代码
class Solution {
public:
    map<string,int> dp;
    bool isMade(string a,string b){
        int flag = 0;
        for(int i=0,j=0;i<a.size()&&j<b.size();){
            if(a[i]!=b[j]){
                if(flag==0){
                    flag = 1;
                    i++;
                }
                else return false;
            }else{
                i++,j++;
            }
        }
        return true;
    }

    int calLen(string &s,vector<string>& words){
        if(dp.count(s))return dp[s];
        int ans = 1;
        for(int i=0;i<words.size();i++){
            if(words[i].size()-1==s.size()&&isMade(words[i],s)){
                ans = max(ans,calLen(words[i],words)+1);
            }
        }
        return dp[s]=ans;
    }

    int longestStrChain(vector<string>& words) {
        int ans = 1;
        for(int i=0;i<words.size();i++){
            ans = max(ans,calLen(words[i],words));

        }
        return ans;
    }
};
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,我可以帮你写一个函数来实现这个功能。首先,我们需要定义一个结构体来表示表中的每个节点,包括一个指向下一个节点的指针和一个字符串成员来存储字符串。代码如下: ```c typedef struct Node { char* str; struct Node* next; } Node; ``` 接下来,我们可以编写一个函数来创建这个表,读取用户输入并将输入的字符串插入到表中: ```c Node* createList() { Node* head = NULL; Node* tail = NULL; char input[100]; while (1) { printf("请输入一个字符串(输入exit结束):\n"); scanf("%s", input); if (strcmp(input, "exit") == 0) { break; } Node* node = (Node*) malloc(sizeof(Node)); node->str = (char*) malloc(strlen(input) + 1); strcpy(node->str, input); node->next = NULL; if (head == NULL) { head = node; tail = node; } else { tail->next = node; tail = node; } } return head; } ``` 这个函数会一直读取用户输入,直到用户输入“exit”为止。每次读取到一个字符串,就会创建一个新的节点,并将节点插入到表的尾部。 接下来,我们可以编写一个函数来统计给定字符串表中出现的次数: ```c int countOccurrences(Node* head, char* str) { int count = 0; Node* current = head; while (current != NULL) { if (strcmp(current->str, str) == 0) { count++; } current = current->next; } return count; } ``` 这个函数会遍历整个表,每当遇到与给定字符串相同时,计数器就会加1。最后,函数将计数器的值作为结果返回。 最后,我们可以编写一个主函数来调用这些函数并输出结果: ```c int main() { Node* head = createList(); char input[100]; while (1) { printf("请输入要查询的字符串(输入exit结束):\n"); scanf("%s", input); if (strcmp(input, "exit") == 0) { break; } int count = countOccurrences(head, input); printf("'%s' 在表中出现了 %d 次\n", input, count); } return 0; } ``` 这个主函数会先调用 createList 函数来创建表,然后不断读取用户输入的字符串,并调用 countOccurrences 函数来统计字符串表中出现的次数,最后输出结果。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值