Find longest word in dictionary that is a subsequence of a given string

贪心算法:

1)将D按字符串的长度,从长到短排序。

2)从D[0]开始,尝试找到这个字符串(这里叫word)是S的子序列。

3)从word[0]开始,遍历S,尝试在S中找到这个字符;一旦找到,接着从当前位置的后面找word[1]是否在S中,以此类推。

// pch.cpp: 与预编译标头对应的源文件;编译成功所必需的

#include "pch.h"// 一般情况下,忽略此文件,但如果你使用的是预编译标头,请保留它。
#include<stdio.h>
#include<stdlib.h>
#include<unordered_map>
#include<iostream>
#include<string>
#include<assert.h>
#include<set>using namespace std;
struct length_more {bool operator()(const string& s1, const string& s2) {if (s1.length() == s2.length())return true;return s1.length() > s2.length();}
};/* 错误写法:
string find(string MainWord, set<string, length_more> WordPool) {set<string>::iterator it;string res="";//bool notBreak = true;  //判断循环是否继续的标志for (it = WordPool.begin(); it != WordPool.end(); ++it) {int i = 0;string word = *it;char w;for (int j = 0; j < word.length(); ++j) {for (;i < MainWord.length(); ++i ) {if (word[j] == MainWord[i]) {res += word[j];w = word[j];break;}}if (word[j] != w)break;elsei = 0;}if (WordPool.count(res)) {break;}else {res.clear();}}return res;
}*///正确:
string find(string MainWord, set<string, length_more> WordPool) {set<string>::iterator it;string res = "";for (it = WordPool.begin(); it != WordPool.end(); ++it) {int i = 0;string word = *it;bool flag = false;for (int j = 0; j < word.length(); ++j) {flag = false;for (;i < MainWord.length(); ++i) {if (word[j] == MainWord[i]) {res += word[j];flag = true;break;}}if (!flag)break;}if (flag)return res;elseres.clear();}if(res.length()==0) return "";
}int main() {string MainWord = "abppplee";set<string, length_more> WordPool;WordPool.insert("able");WordPool.insert("ale");WordPool.insert("apple");WordPool.insert("bale");WordPool.insert("kangaroo");cout << find(MainWord, WordPool) << endl;//set<string>::iterator it;//for (it = WordPool.begin(); it != WordPool.end(); it++)//    cout << (*it) << endl;return 0;
}

改进的贪心算法:将MainWord 插入到map中,记录每个元素对应得位置。感觉改进的这个更麻烦了,需要用到multimap来记录一个键值(字符)对应的多个位置,然后再用equal_range() 把这些位置给找到,循环遍历一遍:若找到比前一个字符索引大的,就看下一个字符,否则还要试这个字符的下一个索引位置。

// pch.cpp: 与预编译标头对应的源文件;编译成功所必需的

#include "pch.h"// 一般情况下,忽略此文件,但如果你使用的是预编译标头,请保留它。
#include<stdio.h>
#include<stdlib.h>
#include<map>
#include<iostream>
#include<string>
#include<assert.h>
#include<set>using namespace std;struct more{bool operator()(const string& s1, const string& s2) {if (s1.length() == s2.length())return true;elsereturn s1.length() > s2.length();}
};string find(string MainWord, set<string, more> WordPool) {multimap<char, int> mp;int i = 0;string res="";for (char a : MainWord) {mp.insert(make_pair(a, i));++i;}for (auto a : WordPool) {int k = -1;pair<multimap<char, int>::iterator, multimap<char, int>::iterator> it;bool flag;for (auto b : a) {flag = false;it = mp.equal_range(b);multimap<char, int>::iterator i = it.first;for ( ;i != it.second; ) {if (i->second > k) {k = i->second;res += i->first;flag = true;break;}else++i;}}if (!flag) res.clear();else return res;}if(res.length()==0) return "";}int main() {string MainWord = "abppplee";set<string, more> WordPool;WordPool.insert("able");WordPool.insert("ale");WordPool.insert("apple");WordPool.insert("bale");WordPool.insert("kangaroo");cout << find(MainWord, WordPool) << endl;//set<string>::iterator it;//for (it = WordPool.begin(); it != WordPool.end(); it++)//    cout << (*it) << endl;return 0;
}

multimap:

http://www.cplusplus.com/reference/map/multimap/equal_range/

转载于:https://www.cnblogs.com/Bella2017/p/11153231.html

(Greedy approach)Find longest word in dictionary that is a subsequence of a given string相关推荐

  1. 数据结构与算法(C++)– 贪婪算法(Greedy algorithm)

    贪婪算法(Greedy algorithm) 1.基础 定义:贪婪算法分阶段地工作,在每一阶段,选择在当前最好的决策,不考虑将来的后果.所以一般只能得到局部最优而不是全局最优. 贪婪算法: Dijks ...

  2. 贪心搜索(greedy search)、集束搜索(beam search)、随机采样(random sample)

    当我们训练完成一个自然语言生成模型后,需要使用这个模型生成新的语言(句子),如何生成这些句子,使用如下的方法:贪心搜索,集束搜索,随机搜索. 贪心搜索(greedy search)/采样(Sampli ...

  3. 集束搜索(beam search)和贪心搜索(greedy search)

    最近读论文的时候看到文中经常用到集束搜索(beam search),可能很多人不懂这到底是个什么算法,其实很简单,顺便把贪心搜索(greedy search)也介绍一下. 贪心搜索(greedy se ...

  4. CASA(Carnegie-Ames-Stanford Approach)模型、MAXENT模型

    植被作为陆地生态系统的重要组成部分对于生态环境功能的维持具有关键作用.植被净初级生产力(Net Primary Productivity, NPP)是指单位面积上绿色植被在单位时间内由光合作用生产的有 ...

  5. 贪心(Greedy Algorithm)

    贪心(Greedy Algorithm) 贪心 44.通配符匹配 45.跳跃游戏 II 55.跳跃游戏 122.买卖股票的最佳时机II 134.加油站 135.分发糖果 179.最大数 277.搜寻名 ...

  6. 贪婪算法(Greedy Algorithms)

    文章目录 一,什么是贪婪算法 二,最短路径 三,使用贪婪解题策略的演算法 3.1 活动选择问题 3.2 贪婪选择(Greedy options) 3.3 将动态规划解转化为贪婪解 四,高效的贪婪算法 ...

  7. C#LeetCode刷题之#720-词典中最长的单词(Longest Word in Dictionary)

    问题 该文章的最新版本已迁移至个人博客[比特飞],单击链接 https://www.byteflying.com/archives/4120 访问. 给出一个字符串数组words组成的一本英语词典.从 ...

  8. 贝叶斯方法(Bayesian approach) —— 一种概率解释(probabilistic interpretation)

    1. Bayesian approach 对于多项式拟合问题,我们可通过最小二乘(least squares)的方式计算得到模型的参数,最小二乘法又可视为最大似然(maximum likelihood ...

  9. 贪心算法(Greedy Algorithm)之霍夫曼编码

    文章目录 1. 贪心算法 2. 应用 2.1 找零钱 2.2 区间覆盖 2.3 霍夫曼编码 霍夫曼编码完整代码 1. 贪心算法 我们希望在一定的限制条件下,获得一个最优解 每次都在当前的标准下做出当下 ...

最新文章

  1. ubuntu安装mysql 密码忘了怎么办_在ubuntu上面安装mysql都密码忘记及一些常用命令...
  2. Linux 2.6.32-279.el6.x86_64 ANDROID SDK碰到”LIBC.SO.6: VERSION `GLIBC_2.14′ NOT FOUND”的解决方法(1)
  3. python中文件描述符_Python中的描述符
  4. 防火墙透明模式下虚拟系统配置实例
  5. PHP 可变变量的使用
  6. 全国地级市坐标、名称、编码获取 / 全球城市坐标位置
  7. 打印纸张尺寸换算_纸张的尺寸规格对照
  8. Lecture 008-Heuristic algorithms
  9. 大数据时代下对马克思主义的一些探讨
  10. 如何选择适合你的兴趣爱好(一),拉丁舞
  11. 论学习大数据什么语言比较合适,不容错过!
  12. 韦根w34是多少位_Levi's裤子尺码中的W34和L34各是多少厘米?
  13. IBM DB2 Alphablox 8.3 build 149 安装过程 部署 WebSphere Application Server 6.0.1上
  14. 微服务商城系统(十) Spring Security Oauth2 + JWT 用户认证
  15. 【三维目标检测】Second 模型 (二)
  16. gsoap参数老是记不住,岁月不饶人
  17. 外媒评出全球32家AI独角兽公司:中国占10家
  18. C语言 malloc calloc realloc
  19. 【基本操作】RouterOS-安装和使用RouterOS(想要搭简易DHCP服务器和PPPoE服务器的看过来)
  20. 东邪西毒 - 台词全本

热门文章

  1. JavaScript-输入输出语法
  2. LIRE的使用:搜索相似的图片
  3. map mybatis 的字段返回0_mybatis返回map类型数据空值字段不显示(三种解决方法)
  4. 使用python批量验证邮箱密码_python(Django 网页登陆账号、密码、邮箱验证)
  5. Kafka设计解析(一)- Kafka背景及架构介绍
  6. TLS certificate verification has been disabled
  7. 网页中引用两个css冲突怎么办
  8. Eclipse下Java项目转web项目
  9. SSM项目使用junit单元测试时Mybatis通配符加载Mapper不能正常加载
  10. webworker应用场景_初始WebWorker