鱼C论坛

 找回密码
 立即注册
查看: 1274|回复: 0

[技术交流] C++刷leetcode(472. 连接词)【字典树】

[复制链接]
发表于 2020-4-12 15:26:24 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
本帖最后由 糖逗 于 2020-4-12 15:41 编辑

题目描述:
  1. 给定一个不含重复单词的列表,编写一个程序,返回给定单词列表中所有的连接词。

  2. 连接词的定义为:一个字符串完全是由至少两个给定数组中的单词组成的。

  3. 示例:

  4. 输入: ["cat","cats","catsdogcats","dog","dogcatsdog","hippopotamuses","rat","ratcatdogcat"]

  5. 输出: ["catsdogcats","dogcatsdog","ratcatdogcat"]

  6. 解释: "catsdogcats"由"cats", "dog" 和 "cats"组成;
  7.      "dogcatsdog"由"dog", "cats"和"dog"组成;
  8.      "ratcatdogcat"由"rat", "cat", "dog"和"cat"组成。
  9. 说明:

  10. 给定数组的元素总数不超过 10000。
  11. 给定数组中元素的长度总和不超过 600000。
  12. 所有输入字符串只包含小写字母。
  13. 不需要考虑答案输出的顺序。

  14. 来源:力扣(LeetCode)
  15. 链接:https://leetcode-cn.com/problems/concatenated-words
  16. 著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。
复制代码



  1. #include <iostream>
  2. #include <string>
  3. #include <vector>
  4. #include <map>

  5. using namespace std;


  6. struct TrieNode{
  7.         bool flag;
  8.         map<char, TrieNode*> next;
  9.         TrieNode():flag(false){
  10.                
  11.         }
  12. };

  13. TrieNode* root = new TrieNode();
  14. void init(vector<string> & input){
  15.         for(auto word : input){
  16.                 TrieNode* temp = root;
  17.                 for(auto cha:word){
  18.                         if(temp -> next.count(cha) == 0){
  19.                                 temp-> next[cha] = new TrieNode();
  20.                         }
  21.                         temp = temp ->next[cha];
  22.                        
  23.                 }
  24.                 temp -> flag = true;
  25.         }
  26.        
  27. }



  28. bool valid(string input){
  29.        
  30.         int len = input.size();
  31.         vector<vector<int> > dp(len + 1);
  32.         dp[0] = vector<int>(1, -1);
  33.         for(int i = 0; i < len; i++){
  34.                 if(dp[i].empty() || root-> next.count(input[i]) == 0) continue;
  35.                 TrieNode* temp = root;
  36.                 int j = i;
  37.                 while(j < len && temp -> next.count(input[j])){
  38.                         temp = temp -> next[input[j++]];
  39.                         if(temp -> flag){
  40.                                 if(i != 0 && j == len) return true;
  41.                                 dp[j].push_back(i);
  42.                         }
  43.                 }
  44.         }
  45.         return false;
  46.        
  47. }

  48. vector<string> solution(vector<string>&input){
  49.         vector<string> res;
  50.         init(input);
  51.         for(auto word : input){
  52.                 if(valid(word)){
  53.                         res.push_back(word);
  54.                 }
  55.         }
  56.         return res;
  57. }




  58. int main(void){
  59.         vector<string> input;
  60.         string words;
  61.         while(cin >> words){
  62.                 input.push_back(words);
  63.         }
  64.        
  65.         vector<string> res = solution(input);
  66.         for(int i = 0; i < res.size(); i++){
  67.                 cout << res[i] << " ";
  68.         }
  69.         cout << endl;
  70.         return 0;
  71. }
复制代码



注意事项:
1.参考链接:https://leetcode-cn.com/problems ... -hua-by-da-li-wang/
2.dp是作为动态规划的中间存储,先构建字典树。
3.i != 0的判断是保证长字符串中至少有两个子串。
4.字典树的参考链接:https://blog.csdn.net/bqw18744018044/article/details/82502435

本帖被以下淘专辑推荐:

小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2025-7-12 19:05

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表