mirror of
https://github.com/TheAlgorithms/C-Plus-Plus.git
synced 2026-03-21 12:23:13 +08:00
474 lines
40 KiB
HTML
474 lines
40 KiB
HTML
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "https://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
|
|
<html xmlns="http://www.w3.org/1999/xhtml">
|
|
<head>
|
|
<meta http-equiv="Content-Type" content="text/xhtml;charset=UTF-8"/>
|
|
<meta http-equiv="X-UA-Compatible" content="IE=9"/>
|
|
<meta name="generator" content="Doxygen 1.9.1"/>
|
|
<meta name="viewport" content="width=device-width, initial-scale=1"/>
|
|
<title>Algorithms_in_C++: dynamic_programming/word_break.cpp File Reference</title>
|
|
<link href="../../tabs.css" rel="stylesheet" type="text/css"/>
|
|
<script type="text/javascript" src="../../jquery.js"></script>
|
|
<script type="text/javascript" src="../../dynsections.js"></script>
|
|
<link href="../../navtree.css" rel="stylesheet" type="text/css"/>
|
|
<script type="text/javascript" src="../../resize.js"></script>
|
|
<script type="text/javascript" src="../../navtreedata.js"></script>
|
|
<script type="text/javascript" src="../../navtree.js"></script>
|
|
<link href="../../search/search.css" rel="stylesheet" type="text/css"/>
|
|
<script type="text/javascript" src="../../search/searchdata.js"></script>
|
|
<script type="text/javascript" src="../../search/search.js"></script>
|
|
<script type="text/x-mathjax-config">
|
|
MathJax.Hub.Config({
|
|
extensions: ["tex2jax.js", "TeX/AMSmath.js", "TeX/AMSsymbols.js"],
|
|
jax: ["input/TeX","output/HTML-CSS"],
|
|
});
|
|
</script>
|
|
<script type="text/javascript" async="async" src="https://cdnjs.cloudflare.com/ajax/libs/mathjax/2.7.7/MathJax.js?config=TeX-MML-AM_CHTML/MathJax.js"></script>
|
|
<link href="../../doxygen.css" rel="stylesheet" type="text/css" />
|
|
</head>
|
|
<body>
|
|
<div id="top"><!-- do not remove this div, it is closed by doxygen! -->
|
|
<div id="titlearea">
|
|
<table cellspacing="0" cellpadding="0">
|
|
<tbody>
|
|
<tr style="height: 56px;">
|
|
<td id="projectalign" style="padding-left: 0.5em;">
|
|
<div id="projectname">Algorithms_in_C++
|
|
 <span id="projectnumber">1.0.0</span>
|
|
</div>
|
|
<div id="projectbrief">Set of algorithms implemented in C++.</div>
|
|
</td>
|
|
</tr>
|
|
</tbody>
|
|
</table>
|
|
</div>
|
|
<!-- end header part -->
|
|
<!-- Generated by Doxygen 1.9.1 -->
|
|
<script type="text/javascript">
|
|
/* @license magnet:?xt=urn:btih:cf05388f2679ee054f2beb29a391d25f4e673ac3&dn=gpl-2.0.txt GPL-v2 */
|
|
var searchBox = new SearchBox("searchBox", "../../search",false,'Search','.html');
|
|
/* @license-end */
|
|
</script>
|
|
<script type="text/javascript" src="../../menudata.js"></script>
|
|
<script type="text/javascript" src="../../menu.js"></script>
|
|
<script type="text/javascript">
|
|
/* @license magnet:?xt=urn:btih:cf05388f2679ee054f2beb29a391d25f4e673ac3&dn=gpl-2.0.txt GPL-v2 */
|
|
$(function() {
|
|
initMenu('../../',true,false,'search.php','Search');
|
|
$(document).ready(function() { init_search(); });
|
|
});
|
|
/* @license-end */</script>
|
|
<div id="main-nav"></div>
|
|
</div><!-- top -->
|
|
<div id="side-nav" class="ui-resizable side-nav-resizable">
|
|
<div id="nav-tree">
|
|
<div id="nav-tree-contents">
|
|
<div id="nav-sync" class="sync"></div>
|
|
</div>
|
|
</div>
|
|
<div id="splitbar" style="-moz-user-select:none;"
|
|
class="ui-resizable-handle">
|
|
</div>
|
|
</div>
|
|
<script type="text/javascript">
|
|
/* @license magnet:?xt=urn:btih:cf05388f2679ee054f2beb29a391d25f4e673ac3&dn=gpl-2.0.txt GPL-v2 */
|
|
$(document).ready(function(){initNavTree('d3/d84/word__break_8cpp.html','../../'); initResizable(); });
|
|
/* @license-end */
|
|
</script>
|
|
<div id="doc-content">
|
|
<!-- window showing the filter options -->
|
|
<div id="MSearchSelectWindow"
|
|
onmouseover="return searchBox.OnSearchSelectShow()"
|
|
onmouseout="return searchBox.OnSearchSelectHide()"
|
|
onkeydown="return searchBox.OnSearchSelectKey(event)">
|
|
</div>
|
|
|
|
<!-- iframe showing the search results (closed by default) -->
|
|
<div id="MSearchResultsWindow">
|
|
<iframe src="javascript:void(0)" frameborder="0"
|
|
name="MSearchResults" id="MSearchResults">
|
|
</iframe>
|
|
</div>
|
|
|
|
<div class="header">
|
|
<div class="summary">
|
|
<a href="#namespaces">Namespaces</a> |
|
|
<a href="#func-members">Functions</a> </div>
|
|
<div class="headertitle">
|
|
<div class="title">word_break.cpp File Reference</div> </div>
|
|
</div><!--header-->
|
|
<div class="contents">
|
|
|
|
<p><a href="https://leetcode.com/problems/word-break/">Word Break Problem</a>
|
|
<a href="#details">More...</a></p>
|
|
<div class="textblock"><code>#include <cassert></code><br />
|
|
<code>#include <climits></code><br />
|
|
<code>#include <iostream></code><br />
|
|
<code>#include <string></code><br />
|
|
<code>#include <unordered_set></code><br />
|
|
<code>#include <vector></code><br />
|
|
</div><div class="textblock"><div class="dynheader">
|
|
Include dependency graph for word_break.cpp:</div>
|
|
<div class="dyncontent">
|
|
<div class="center"><iframe scrolling="no" frameborder="0" src="../../d2/d73/word__break_8cpp__incl.svg" width="548" height="127"><p><b>This browser is not able to show SVG: try Firefox, Chrome, Safari, or Opera instead.</b></p></iframe>
|
|
</div>
|
|
</div>
|
|
</div><table class="memberdecls">
|
|
<tr class="heading"><td colspan="2"><h2 class="groupheader"><a name="namespaces"></a>
|
|
Namespaces</h2></td></tr>
|
|
<tr class="memitem:dd/d24/namespacedynamic__programming"><td class="memItemLeft" align="right" valign="top">  </td><td class="memItemRight" valign="bottom"><a class="el" href="../../dd/d24/namespacedynamic__programming.html">dynamic_programming</a></td></tr>
|
|
<tr class="memdesc:dd/d24/namespacedynamic__programming"><td class="mdescLeft"> </td><td class="mdescRight">Dynamic Programming algorithms. <br /></td></tr>
|
|
<tr class="separator:"><td class="memSeparator" colspan="2"> </td></tr>
|
|
<tr class="memitem:d6/d53/namespaceword__break"><td class="memItemLeft" align="right" valign="top">  </td><td class="memItemRight" valign="bottom"><a class="el" href="../../d6/d53/namespaceword__break.html">word_break</a></td></tr>
|
|
<tr class="memdesc:d6/d53/namespaceword__break"><td class="mdescLeft"> </td><td class="mdescRight">Functions for <a href="https://leetcode.com/problems/word-break/">Word Break</a> problem. <br /></td></tr>
|
|
<tr class="separator:"><td class="memSeparator" colspan="2"> </td></tr>
|
|
</table><table class="memberdecls">
|
|
<tr class="heading"><td colspan="2"><h2 class="groupheader"><a name="func-members"></a>
|
|
Functions</h2></td></tr>
|
|
<tr class="memitem:a1cc9dd6e6190d10a010fdcdfe7a21a81"><td class="memItemLeft" align="right" valign="top">bool </td><td class="memItemRight" valign="bottom"><a class="el" href="../../d3/d84/word__break_8cpp.html#a1cc9dd6e6190d10a010fdcdfe7a21a81">dynamic_programming::word_break::exists</a> (const <a class="elRef" target="_blank" href="http://en.cppreference.com/w/cpp/string/basic_string.html">std::string</a> &str, const <a class="elRef" target="_blank" href="http://en.cppreference.com/w/cpp/container/unordered_set.html">std::unordered_set</a>< <a class="elRef" target="_blank" href="http://en.cppreference.com/w/cpp/string/basic_string.html">std::string</a> > &strSet)</td></tr>
|
|
<tr class="memdesc:a1cc9dd6e6190d10a010fdcdfe7a21a81"><td class="mdescLeft"> </td><td class="mdescRight">Function that checks if the string passed in param is present in the the unordered_set passed. <a href="../../d3/d84/word__break_8cpp.html#a1cc9dd6e6190d10a010fdcdfe7a21a81">More...</a><br /></td></tr>
|
|
<tr class="separator:a1cc9dd6e6190d10a010fdcdfe7a21a81"><td class="memSeparator" colspan="2"> </td></tr>
|
|
<tr class="memitem:a272b0f5cdb4e41fd6dee4538b808c06a"><td class="memItemLeft" align="right" valign="top">bool </td><td class="memItemRight" valign="bottom"><a class="el" href="../../d3/d84/word__break_8cpp.html#a272b0f5cdb4e41fd6dee4538b808c06a">dynamic_programming::word_break::check</a> (const <a class="elRef" target="_blank" href="http://en.cppreference.com/w/cpp/string/basic_string.html">std::string</a> &s, const <a class="elRef" target="_blank" href="http://en.cppreference.com/w/cpp/container/unordered_set.html">std::unordered_set</a>< <a class="elRef" target="_blank" href="http://en.cppreference.com/w/cpp/string/basic_string.html">std::string</a> > &strSet, int pos, <a class="elRef" target="_blank" href="http://en.cppreference.com/w/cpp/container/vector.html">std::vector</a>< int > *dp)</td></tr>
|
|
<tr class="memdesc:a272b0f5cdb4e41fd6dee4538b808c06a"><td class="mdescLeft"> </td><td class="mdescRight">Function that checks if the string passed in param can be segmented from position 'pos', and then correctly go on to segment the rest of the string correctly as well to reach a solution. <a href="../../d3/d84/word__break_8cpp.html#a272b0f5cdb4e41fd6dee4538b808c06a">More...</a><br /></td></tr>
|
|
<tr class="separator:a272b0f5cdb4e41fd6dee4538b808c06a"><td class="memSeparator" colspan="2"> </td></tr>
|
|
<tr class="memitem:afe4dcd6fd5282e535685361cba645d7c"><td class="memItemLeft" align="right" valign="top">bool </td><td class="memItemRight" valign="bottom"><a class="el" href="../../d3/d84/word__break_8cpp.html#afe4dcd6fd5282e535685361cba645d7c">dynamic_programming::word_break::wordBreak</a> (const <a class="elRef" target="_blank" href="http://en.cppreference.com/w/cpp/string/basic_string.html">std::string</a> &s, const <a class="elRef" target="_blank" href="http://en.cppreference.com/w/cpp/container/vector.html">std::vector</a>< <a class="elRef" target="_blank" href="http://en.cppreference.com/w/cpp/string/basic_string.html">std::string</a> > &wordDict)</td></tr>
|
|
<tr class="memdesc:afe4dcd6fd5282e535685361cba645d7c"><td class="mdescLeft"> </td><td class="mdescRight">Function that checks if the string passed in param can be segmented into the strings present in the vector. In others words, it checks if any permutation of strings in the vector can be concatenated to form the final string. <a href="../../d3/d84/word__break_8cpp.html#afe4dcd6fd5282e535685361cba645d7c">More...</a><br /></td></tr>
|
|
<tr class="separator:afe4dcd6fd5282e535685361cba645d7c"><td class="memSeparator" colspan="2"> </td></tr>
|
|
<tr class="memitem:aa8dca7b867074164d5f45b0f3851269d"><td class="memItemLeft" align="right" valign="top">static void </td><td class="memItemRight" valign="bottom"><a class="el" href="../../d3/d84/word__break_8cpp.html#aa8dca7b867074164d5f45b0f3851269d">test</a> ()</td></tr>
|
|
<tr class="memdesc:aa8dca7b867074164d5f45b0f3851269d"><td class="mdescLeft"> </td><td class="mdescRight">Test implementations. <a href="../../d3/d84/word__break_8cpp.html#aa8dca7b867074164d5f45b0f3851269d">More...</a><br /></td></tr>
|
|
<tr class="separator:aa8dca7b867074164d5f45b0f3851269d"><td class="memSeparator" colspan="2"> </td></tr>
|
|
<tr class="memitem:ae66f6b31b5ad750f1fe042a706a4e3d4"><td class="memItemLeft" align="right" valign="top">int </td><td class="memItemRight" valign="bottom"><a class="el" href="../../d3/d84/word__break_8cpp.html#ae66f6b31b5ad750f1fe042a706a4e3d4">main</a> ()</td></tr>
|
|
<tr class="memdesc:ae66f6b31b5ad750f1fe042a706a4e3d4"><td class="mdescLeft"> </td><td class="mdescRight">Main function. <a href="../../d3/d84/word__break_8cpp.html#ae66f6b31b5ad750f1fe042a706a4e3d4">More...</a><br /></td></tr>
|
|
<tr class="separator:ae66f6b31b5ad750f1fe042a706a4e3d4"><td class="memSeparator" colspan="2"> </td></tr>
|
|
</table>
|
|
<a name="details" id="details"></a><h2 class="groupheader">Detailed Description</h2>
|
|
<div class="textblock"><p><a href="https://leetcode.com/problems/word-break/">Word Break Problem</a> </p>
|
|
<p>Given a non-empty string s and a dictionary wordDict containing a list of non-empty words, determine if s can be segmented into a space-separated sequence of one or more dictionary words.</p>
|
|
<p>Note: The same word in the dictionary may be reused multiple times in the segmentation. You may assume the dictionary does not contain duplicate words.</p>
|
|
<p>Example 1: Input: s = "leetcode", wordDict = ["leet", "code"] Output: true Explanation: Return true because "leetcode" can be segmented as "leet code".</p>
|
|
<p>Example 2: Input: s = "applepenapple", wordDict = ["apple", "pen"] Output: true Explanation: Return true because "applepenapple" can be segmented as "apple
|
|
pen apple". Note that you are allowed to reuse a dictionary word.</p>
|
|
<p>Example 3: Input: s = "catsandog", wordDict = ["cats", "dog", "sand", "and", "cat"] Output: false</p>
|
|
<dl class="section author"><dt>Author</dt><dd><a href="https://github.com/axayjha">Akshay Anand</a> </dd></dl>
|
|
</div><h2 class="groupheader">Function Documentation</h2>
|
|
<a id="a272b0f5cdb4e41fd6dee4538b808c06a"></a>
|
|
<h2 class="memtitle"><span class="permalink"><a href="#a272b0f5cdb4e41fd6dee4538b808c06a">◆ </a></span>check()</h2>
|
|
|
|
<div class="memitem">
|
|
<div class="memproto">
|
|
<table class="memname">
|
|
<tr>
|
|
<td class="memname">bool dynamic_programming::word_break::check </td>
|
|
<td>(</td>
|
|
<td class="paramtype">const <a class="elRef" target="_blank" href="http://en.cppreference.com/w/cpp/string/basic_string.html">std::string</a> & </td>
|
|
<td class="paramname"><em>s</em>, </td>
|
|
</tr>
|
|
<tr>
|
|
<td class="paramkey"></td>
|
|
<td></td>
|
|
<td class="paramtype">const <a class="elRef" target="_blank" href="http://en.cppreference.com/w/cpp/container/unordered_set.html">std::unordered_set</a>< <a class="elRef" target="_blank" href="http://en.cppreference.com/w/cpp/string/basic_string.html">std::string</a> > & </td>
|
|
<td class="paramname"><em>strSet</em>, </td>
|
|
</tr>
|
|
<tr>
|
|
<td class="paramkey"></td>
|
|
<td></td>
|
|
<td class="paramtype">int </td>
|
|
<td class="paramname"><em>pos</em>, </td>
|
|
</tr>
|
|
<tr>
|
|
<td class="paramkey"></td>
|
|
<td></td>
|
|
<td class="paramtype"><a class="elRef" target="_blank" href="http://en.cppreference.com/w/cpp/container/vector.html">std::vector</a>< int > * </td>
|
|
<td class="paramname"><em>dp</em> </td>
|
|
</tr>
|
|
<tr>
|
|
<td></td>
|
|
<td>)</td>
|
|
<td></td><td></td>
|
|
</tr>
|
|
</table>
|
|
</div><div class="memdoc">
|
|
|
|
<p>Function that checks if the string passed in param can be segmented from position 'pos', and then correctly go on to segment the rest of the string correctly as well to reach a solution. </p>
|
|
<dl class="params"><dt>Parameters</dt><dd>
|
|
<table class="params">
|
|
<tr><td class="paramname">s</td><td>the complete string to be segmented </td></tr>
|
|
<tr><td class="paramname">strSet</td><td>unordered set of string, that is to be used as the reference dictionary </td></tr>
|
|
<tr><td class="paramname">pos</td><td>the index value at which we will segment string and test further if it is correctly segmented at pos </td></tr>
|
|
<tr><td class="paramname">dp</td><td>the vector to memoize solution for each position </td></tr>
|
|
</table>
|
|
</dd>
|
|
</dl>
|
|
<dl class="section return"><dt>Returns</dt><dd><code>true</code> if a valid solution/segmentation is possible by segmenting at index pos </dd>
|
|
<dd>
|
|
<code>false</code> otherwise </dd></dl>
|
|
<div class="fragment"><div class="line"><a name="l00081"></a><span class="lineno"> 81</span>  {</div>
|
|
<div class="line"><a name="l00082"></a><span class="lineno"> 82</span>  <span class="keywordflow">if</span> (pos == s.<a class="codeRef" target="_blank" href="http://en.cppreference.com/w/cpp/string/basic_string/size.html">length</a>()) {</div>
|
|
<div class="line"><a name="l00083"></a><span class="lineno"> 83</span>  <span class="comment">// if we have reached till the end of the string, means we have</span></div>
|
|
<div class="line"><a name="l00084"></a><span class="lineno"> 84</span>  <span class="comment">// segmented throughout correctly hence we have a solution, thus</span></div>
|
|
<div class="line"><a name="l00085"></a><span class="lineno"> 85</span>  <span class="comment">// returning true</span></div>
|
|
<div class="line"><a name="l00086"></a><span class="lineno"> 86</span>  <span class="keywordflow">return</span> <span class="keyword">true</span>;</div>
|
|
<div class="line"><a name="l00087"></a><span class="lineno"> 87</span>  }</div>
|
|
<div class="line"><a name="l00088"></a><span class="lineno"> 88</span>  </div>
|
|
<div class="line"><a name="l00089"></a><span class="lineno"> 89</span>  <span class="keywordflow">if</span> (dp->at(pos) != INT_MAX) {</div>
|
|
<div class="line"><a name="l00090"></a><span class="lineno"> 90</span>  <span class="comment">// if dp[pos] is not INT_MAX, means we must have saved a solution</span></div>
|
|
<div class="line"><a name="l00091"></a><span class="lineno"> 91</span>  <span class="comment">// for the position pos; then return if the solution at pos is true</span></div>
|
|
<div class="line"><a name="l00092"></a><span class="lineno"> 92</span>  <span class="comment">// or not</span></div>
|
|
<div class="line"><a name="l00093"></a><span class="lineno"> 93</span>  <span class="keywordflow">return</span> dp->at(pos) == 1;</div>
|
|
<div class="line"><a name="l00094"></a><span class="lineno"> 94</span>  }</div>
|
|
<div class="line"><a name="l00095"></a><span class="lineno"> 95</span>  </div>
|
|
<div class="line"><a name="l00096"></a><span class="lineno"> 96</span>  <a class="codeRef" target="_blank" href="http://en.cppreference.com/w/cpp/string/basic_string.html">std::string</a> wordTillNow =</div>
|
|
<div class="line"><a name="l00097"></a><span class="lineno"> 97</span>  <span class="stringliteral">""</span>; <span class="comment">// string to save the prefixes of word till different positons</span></div>
|
|
<div class="line"><a name="l00098"></a><span class="lineno"> 98</span>  </div>
|
|
<div class="line"><a name="l00099"></a><span class="lineno"> 99</span>  <span class="keywordflow">for</span> (<span class="keywordtype">int</span> i = pos; i < s.<a class="codeRef" target="_blank" href="http://en.cppreference.com/w/cpp/string/basic_string/size.html">length</a>(); i++) {</div>
|
|
<div class="line"><a name="l00100"></a><span class="lineno"> 100</span>  <span class="comment">// Loop starting from pos to end, to check valid set of</span></div>
|
|
<div class="line"><a name="l00101"></a><span class="lineno"> 101</span>  <span class="comment">// segmentations if any</span></div>
|
|
<div class="line"><a name="l00102"></a><span class="lineno"> 102</span>  wordTillNow +=</div>
|
|
<div class="line"><a name="l00103"></a><span class="lineno"> 103</span>  <a class="codeRef" target="_blank" href="http://en.cppreference.com/w/cpp/string/basic_string.html">std::string</a>(1, s[i]); <span class="comment">// storing the prefix till the position i</span></div>
|
|
<div class="line"><a name="l00104"></a><span class="lineno"> 104</span>  </div>
|
|
<div class="line"><a name="l00105"></a><span class="lineno"> 105</span>  <span class="comment">// if the prefix till current position is present in the dictionary</span></div>
|
|
<div class="line"><a name="l00106"></a><span class="lineno"> 106</span>  <span class="comment">// and the remaining substring can also be segmented legally, then</span></div>
|
|
<div class="line"><a name="l00107"></a><span class="lineno"> 107</span>  <span class="comment">// set solution at position pos in the memo, and return true</span></div>
|
|
<div class="line"><a name="l00108"></a><span class="lineno"> 108</span>  <span class="keywordflow">if</span> (<a class="code" href="../../d3/d84/word__break_8cpp.html#a1cc9dd6e6190d10a010fdcdfe7a21a81">exists</a>(wordTillNow, strSet) and <a class="code" href="../../d3/d84/word__break_8cpp.html#a272b0f5cdb4e41fd6dee4538b808c06a">check</a>(s, strSet, i + 1, dp)) {</div>
|
|
<div class="line"><a name="l00109"></a><span class="lineno"> 109</span>  dp->at(pos) = 1;</div>
|
|
<div class="line"><a name="l00110"></a><span class="lineno"> 110</span>  <span class="keywordflow">return</span> <span class="keyword">true</span>;</div>
|
|
<div class="line"><a name="l00111"></a><span class="lineno"> 111</span>  }</div>
|
|
<div class="line"><a name="l00112"></a><span class="lineno"> 112</span>  }</div>
|
|
<div class="line"><a name="l00113"></a><span class="lineno"> 113</span>  <span class="comment">// if function has still not returned, then there must be no legal</span></div>
|
|
<div class="line"><a name="l00114"></a><span class="lineno"> 114</span>  <span class="comment">// segmentation possible after segmenting at pos</span></div>
|
|
<div class="line"><a name="l00115"></a><span class="lineno"> 115</span>  dp->at(pos) = 0; <span class="comment">// so set solution at pos as false</span></div>
|
|
<div class="line"><a name="l00116"></a><span class="lineno"> 116</span>  <span class="keywordflow">return</span> <span class="keyword">false</span>; <span class="comment">// and return no solution at position pos</span></div>
|
|
<div class="line"><a name="l00117"></a><span class="lineno"> 117</span> }</div>
|
|
<div class="ttc" id="abasic_string_html"><div class="ttname"><a href="http://en.cppreference.com/w/cpp/string/basic_string.html">std::string</a></div></div>
|
|
<div class="ttc" id="asize_html"><div class="ttname"><a href="http://en.cppreference.com/w/cpp/string/basic_string/size.html">std::string::length</a></div><div class="ttdeci">T length(T... args)</div></div>
|
|
<div class="ttc" id="aword__break_8cpp_html_a1cc9dd6e6190d10a010fdcdfe7a21a81"><div class="ttname"><a href="../../d3/d84/word__break_8cpp.html#a1cc9dd6e6190d10a010fdcdfe7a21a81">dynamic_programming::word_break::exists</a></div><div class="ttdeci">bool exists(const std::string &str, const std::unordered_set< std::string > &strSet)</div><div class="ttdoc">Function that checks if the string passed in param is present in the the unordered_set passed.</div><div class="ttdef"><b>Definition:</b> word_break.cpp:60</div></div>
|
|
<div class="ttc" id="aword__break_8cpp_html_a272b0f5cdb4e41fd6dee4538b808c06a"><div class="ttname"><a href="../../d3/d84/word__break_8cpp.html#a272b0f5cdb4e41fd6dee4538b808c06a">dynamic_programming::word_break::check</a></div><div class="ttdeci">bool check(const std::string &s, const std::unordered_set< std::string > &strSet, int pos, std::vector< int > *dp)</div><div class="ttdoc">Function that checks if the string passed in param can be segmented from position 'pos',...</div><div class="ttdef"><b>Definition:</b> word_break.cpp:80</div></div>
|
|
</div><!-- fragment --><div class="dynheader">
|
|
Here is the call graph for this function:</div>
|
|
<div class="dyncontent">
|
|
<div class="center"><iframe scrolling="no" frameborder="0" src="../../d3/d84/word__break_8cpp_a272b0f5cdb4e41fd6dee4538b808c06a_cgraph.svg" width="572" height="128"><p><b>This browser is not able to show SVG: try Firefox, Chrome, Safari, or Opera instead.</b></p></iframe>
|
|
</div>
|
|
</div>
|
|
|
|
</div>
|
|
</div>
|
|
<a id="a1cc9dd6e6190d10a010fdcdfe7a21a81"></a>
|
|
<h2 class="memtitle"><span class="permalink"><a href="#a1cc9dd6e6190d10a010fdcdfe7a21a81">◆ </a></span>exists()</h2>
|
|
|
|
<div class="memitem">
|
|
<div class="memproto">
|
|
<table class="memname">
|
|
<tr>
|
|
<td class="memname">bool dynamic_programming::word_break::exists </td>
|
|
<td>(</td>
|
|
<td class="paramtype">const <a class="elRef" target="_blank" href="http://en.cppreference.com/w/cpp/string/basic_string.html">std::string</a> & </td>
|
|
<td class="paramname"><em>str</em>, </td>
|
|
</tr>
|
|
<tr>
|
|
<td class="paramkey"></td>
|
|
<td></td>
|
|
<td class="paramtype">const <a class="elRef" target="_blank" href="http://en.cppreference.com/w/cpp/container/unordered_set.html">std::unordered_set</a>< <a class="elRef" target="_blank" href="http://en.cppreference.com/w/cpp/string/basic_string.html">std::string</a> > & </td>
|
|
<td class="paramname"><em>strSet</em> </td>
|
|
</tr>
|
|
<tr>
|
|
<td></td>
|
|
<td>)</td>
|
|
<td></td><td></td>
|
|
</tr>
|
|
</table>
|
|
</div><div class="memdoc">
|
|
|
|
<p>Function that checks if the string passed in param is present in the the unordered_set passed. </p>
|
|
<dl class="params"><dt>Parameters</dt><dd>
|
|
<table class="params">
|
|
<tr><td class="paramname">str</td><td>the string to be searched </td></tr>
|
|
<tr><td class="paramname">strSet</td><td>unordered set of string, that is to be looked into </td></tr>
|
|
</table>
|
|
</dd>
|
|
</dl>
|
|
<dl class="section return"><dt>Returns</dt><dd><code>true</code> if str is present in strSet </dd>
|
|
<dd>
|
|
<code>false</code> if str is not present in strSet </dd></dl>
|
|
<div class="fragment"><div class="line"><a name="l00061"></a><span class="lineno"> 61</span>  {</div>
|
|
<div class="line"><a name="l00062"></a><span class="lineno"> 62</span>  <span class="keywordflow">return</span> strSet.<a class="codeRef" target="_blank" href="http://en.cppreference.com/w/cpp/container/unordered_set/find.html">find</a>(str) != strSet.<a class="codeRef" target="_blank" href="http://en.cppreference.com/w/cpp/container/unordered_set/end.html">end</a>();</div>
|
|
<div class="line"><a name="l00063"></a><span class="lineno"> 63</span> }</div>
|
|
<div class="ttc" id="aend_html"><div class="ttname"><a href="http://en.cppreference.com/w/cpp/container/unordered_set/end.html">std::unordered_set::end</a></div><div class="ttdeci">T end(T... args)</div></div>
|
|
<div class="ttc" id="afind_html"><div class="ttname"><a href="http://en.cppreference.com/w/cpp/container/unordered_set/find.html">std::unordered_set::find</a></div><div class="ttdeci">T find(T... args)</div></div>
|
|
</div><!-- fragment --><div class="dynheader">
|
|
Here is the call graph for this function:</div>
|
|
<div class="dyncontent">
|
|
<div class="center"><iframe scrolling="no" frameborder="0" src="../../d3/d84/word__break_8cpp_a1cc9dd6e6190d10a010fdcdfe7a21a81_cgraph.svg" width="371" height="106"><p><b>This browser is not able to show SVG: try Firefox, Chrome, Safari, or Opera instead.</b></p></iframe>
|
|
</div>
|
|
</div>
|
|
|
|
</div>
|
|
</div>
|
|
<a id="ae66f6b31b5ad750f1fe042a706a4e3d4"></a>
|
|
<h2 class="memtitle"><span class="permalink"><a href="#ae66f6b31b5ad750f1fe042a706a4e3d4">◆ </a></span>main()</h2>
|
|
|
|
<div class="memitem">
|
|
<div class="memproto">
|
|
<table class="memname">
|
|
<tr>
|
|
<td class="memname">int main </td>
|
|
<td>(</td>
|
|
<td class="paramtype">void </td>
|
|
<td class="paramname"></td><td>)</td>
|
|
<td></td>
|
|
</tr>
|
|
</table>
|
|
</div><div class="memdoc">
|
|
|
|
<p>Main function. </p>
|
|
<dl class="section return"><dt>Returns</dt><dd>0 on exit </dd></dl>
|
|
<div class="fragment"><div class="line"><a name="l00174"></a><span class="lineno"> 174</span>  {</div>
|
|
<div class="line"><a name="l00175"></a><span class="lineno"> 175</span>  <a class="code" href="../../d3/d84/word__break_8cpp.html#aa8dca7b867074164d5f45b0f3851269d">test</a>(); <span class="comment">// call the test function :)</span></div>
|
|
<div class="line"><a name="l00176"></a><span class="lineno"> 176</span>  </div>
|
|
<div class="line"><a name="l00177"></a><span class="lineno"> 177</span>  <span class="comment">// the complete string</span></div>
|
|
<div class="line"><a name="l00178"></a><span class="lineno"> 178</span>  <span class="keyword">const</span> <a class="codeRef" target="_blank" href="http://en.cppreference.com/w/cpp/string/basic_string.html">std::string</a> s = <span class="stringliteral">"applepenapple"</span>;</div>
|
|
<div class="line"><a name="l00179"></a><span class="lineno"> 179</span>  <span class="comment">// the dictionary to be used</span></div>
|
|
<div class="line"><a name="l00180"></a><span class="lineno"> 180</span>  <span class="keyword">const</span> <a class="codeRef" target="_blank" href="http://en.cppreference.com/w/cpp/container/vector.html">std::vector<std::string></a> wordDict = {<span class="stringliteral">"apple"</span>, <span class="stringliteral">"pen"</span>};</div>
|
|
<div class="line"><a name="l00181"></a><span class="lineno"> 181</span>  </div>
|
|
<div class="line"><a name="l00182"></a><span class="lineno"> 182</span>  <span class="comment">// should return true, as applepenapple can be segmented as apple + pen +</span></div>
|
|
<div class="line"><a name="l00183"></a><span class="lineno"> 183</span>  <span class="comment">// apple</span></div>
|
|
<div class="line"><a name="l00184"></a><span class="lineno"> 184</span>  <a class="codeRef" target="_blank" href="http://en.cppreference.com/w/cpp/io/basic_ostream.html">std::cout</a> << <a class="code" href="../../d3/d84/word__break_8cpp.html#afe4dcd6fd5282e535685361cba645d7c">dynamic_programming::word_break::wordBreak</a>(s, wordDict)</div>
|
|
<div class="line"><a name="l00185"></a><span class="lineno"> 185</span>  << <a class="codeRef" target="_blank" href="http://en.cppreference.com/w/cpp/io/manip/endl.html">std::endl</a>;</div>
|
|
<div class="line"><a name="l00186"></a><span class="lineno"> 186</span> }</div>
|
|
<div class="ttc" id="abasic_ostream_html"><div class="ttname"><a href="http://en.cppreference.com/w/cpp/io/basic_ostream.html">std::cout</a></div></div>
|
|
<div class="ttc" id="aendl_html"><div class="ttname"><a href="http://en.cppreference.com/w/cpp/io/manip/endl.html">std::endl</a></div><div class="ttdeci">T endl(T... args)</div></div>
|
|
<div class="ttc" id="avector_html"><div class="ttname"><a href="http://en.cppreference.com/w/cpp/container/vector.html">std::vector</a></div></div>
|
|
<div class="ttc" id="aword__break_8cpp_html_aa8dca7b867074164d5f45b0f3851269d"><div class="ttname"><a href="../../d3/d84/word__break_8cpp.html#aa8dca7b867074164d5f45b0f3851269d">test</a></div><div class="ttdeci">static void test()</div><div class="ttdoc">Test implementations.</div><div class="ttdef"><b>Definition:</b> word_break.cpp:156</div></div>
|
|
<div class="ttc" id="aword__break_8cpp_html_afe4dcd6fd5282e535685361cba645d7c"><div class="ttname"><a href="../../d3/d84/word__break_8cpp.html#afe4dcd6fd5282e535685361cba645d7c">dynamic_programming::word_break::wordBreak</a></div><div class="ttdeci">bool wordBreak(const std::string &s, const std::vector< std::string > &wordDict)</div><div class="ttdoc">Function that checks if the string passed in param can be segmented into the strings present in the v...</div><div class="ttdef"><b>Definition:</b> word_break.cpp:131</div></div>
|
|
</div><!-- fragment --><div class="dynheader">
|
|
Here is the call graph for this function:</div>
|
|
<div class="dyncontent">
|
|
<div class="center"><iframe scrolling="no" frameborder="0" src="../../d3/d84/word__break_8cpp_ae66f6b31b5ad750f1fe042a706a4e3d4_cgraph.svg" width="274" height="63"><p><b>This browser is not able to show SVG: try Firefox, Chrome, Safari, or Opera instead.</b></p></iframe>
|
|
</div>
|
|
</div>
|
|
|
|
</div>
|
|
</div>
|
|
<a id="aa8dca7b867074164d5f45b0f3851269d"></a>
|
|
<h2 class="memtitle"><span class="permalink"><a href="#aa8dca7b867074164d5f45b0f3851269d">◆ </a></span>test()</h2>
|
|
|
|
<div class="memitem">
|
|
<div class="memproto">
|
|
<table class="mlabels">
|
|
<tr>
|
|
<td class="mlabels-left">
|
|
<table class="memname">
|
|
<tr>
|
|
<td class="memname">static void test </td>
|
|
<td>(</td>
|
|
<td class="paramname"></td><td>)</td>
|
|
<td></td>
|
|
</tr>
|
|
</table>
|
|
</td>
|
|
<td class="mlabels-right">
|
|
<span class="mlabels"><span class="mlabel">static</span></span> </td>
|
|
</tr>
|
|
</table>
|
|
</div><div class="memdoc">
|
|
|
|
<p>Test implementations. </p>
|
|
<dl class="section return"><dt>Returns</dt><dd>void </dd></dl>
|
|
<div class="fragment"><div class="line"><a name="l00156"></a><span class="lineno"> 156</span>  {</div>
|
|
<div class="line"><a name="l00157"></a><span class="lineno"> 157</span>  <span class="comment">// the complete string</span></div>
|
|
<div class="line"><a name="l00158"></a><span class="lineno"> 158</span>  <span class="keyword">const</span> <a class="codeRef" target="_blank" href="http://en.cppreference.com/w/cpp/string/basic_string.html">std::string</a> s = <span class="stringliteral">"applepenapple"</span>;</div>
|
|
<div class="line"><a name="l00159"></a><span class="lineno"> 159</span>  <span class="comment">// the dictionary to be used</span></div>
|
|
<div class="line"><a name="l00160"></a><span class="lineno"> 160</span>  <span class="keyword">const</span> <a class="codeRef" target="_blank" href="http://en.cppreference.com/w/cpp/container/vector.html">std::vector<std::string></a> wordDict = {<span class="stringliteral">"apple"</span>, <span class="stringliteral">"pen"</span>};</div>
|
|
<div class="line"><a name="l00161"></a><span class="lineno"> 161</span>  </div>
|
|
<div class="line"><a name="l00162"></a><span class="lineno"> 162</span>  assert(dynamic_programming::word_break::wordBreak(s, wordDict));</div>
|
|
<div class="line"><a name="l00163"></a><span class="lineno"> 163</span>  </div>
|
|
<div class="line"><a name="l00164"></a><span class="lineno"> 164</span>  <span class="comment">// should return true, as applepenapple can be segmented as apple + pen +</span></div>
|
|
<div class="line"><a name="l00165"></a><span class="lineno"> 165</span>  <span class="comment">// apple</span></div>
|
|
<div class="line"><a name="l00166"></a><span class="lineno"> 166</span>  <a class="codeRef" target="_blank" href="http://en.cppreference.com/w/cpp/io/basic_ostream.html">std::cout</a> << <a class="code" href="../../d3/d84/word__break_8cpp.html#afe4dcd6fd5282e535685361cba645d7c">dynamic_programming::word_break::wordBreak</a>(s, wordDict)</div>
|
|
<div class="line"><a name="l00167"></a><span class="lineno"> 167</span>  << <a class="codeRef" target="_blank" href="http://en.cppreference.com/w/cpp/io/manip/endl.html">std::endl</a>;</div>
|
|
<div class="line"><a name="l00168"></a><span class="lineno"> 168</span>  <a class="codeRef" target="_blank" href="http://en.cppreference.com/w/cpp/io/basic_ostream.html">std::cout</a> << <span class="stringliteral">"Test implementation passed!\n"</span>;</div>
|
|
<div class="line"><a name="l00169"></a><span class="lineno"> 169</span> }</div>
|
|
</div><!-- fragment --><div class="dynheader">
|
|
Here is the call graph for this function:</div>
|
|
<div class="dyncontent">
|
|
<div class="center"><iframe scrolling="no" frameborder="0" src="../../d3/d84/word__break_8cpp_aa8dca7b867074164d5f45b0f3851269d_cgraph.svg" width="175" height="38"><p><b>This browser is not able to show SVG: try Firefox, Chrome, Safari, or Opera instead.</b></p></iframe>
|
|
</div>
|
|
</div>
|
|
|
|
</div>
|
|
</div>
|
|
<a id="afe4dcd6fd5282e535685361cba645d7c"></a>
|
|
<h2 class="memtitle"><span class="permalink"><a href="#afe4dcd6fd5282e535685361cba645d7c">◆ </a></span>wordBreak()</h2>
|
|
|
|
<div class="memitem">
|
|
<div class="memproto">
|
|
<table class="memname">
|
|
<tr>
|
|
<td class="memname">bool dynamic_programming::word_break::wordBreak </td>
|
|
<td>(</td>
|
|
<td class="paramtype">const <a class="elRef" target="_blank" href="http://en.cppreference.com/w/cpp/string/basic_string.html">std::string</a> & </td>
|
|
<td class="paramname"><em>s</em>, </td>
|
|
</tr>
|
|
<tr>
|
|
<td class="paramkey"></td>
|
|
<td></td>
|
|
<td class="paramtype">const <a class="elRef" target="_blank" href="http://en.cppreference.com/w/cpp/container/vector.html">std::vector</a>< <a class="elRef" target="_blank" href="http://en.cppreference.com/w/cpp/string/basic_string.html">std::string</a> > & </td>
|
|
<td class="paramname"><em>wordDict</em> </td>
|
|
</tr>
|
|
<tr>
|
|
<td></td>
|
|
<td>)</td>
|
|
<td></td><td></td>
|
|
</tr>
|
|
</table>
|
|
</div><div class="memdoc">
|
|
|
|
<p>Function that checks if the string passed in param can be segmented into the strings present in the vector. In others words, it checks if any permutation of strings in the vector can be concatenated to form the final string. </p>
|
|
<dl class="params"><dt>Parameters</dt><dd>
|
|
<table class="params">
|
|
<tr><td class="paramname">s</td><td>the complete string to be segmented </td></tr>
|
|
<tr><td class="paramname">wordDict</td><td>a vector of words to be used as dictionary to look into </td></tr>
|
|
</table>
|
|
</dd>
|
|
</dl>
|
|
<dl class="section return"><dt>Returns</dt><dd><code>true</code> if s can be formed by a combination of strings present in wordDict </dd>
|
|
<dd>
|
|
<code>false</code> otherwise </dd></dl>
|
|
<div class="fragment"><div class="line"><a name="l00131"></a><span class="lineno"> 131</span>  {</div>
|
|
<div class="line"><a name="l00132"></a><span class="lineno"> 132</span>  <span class="comment">// unordered set to store words in the dictionary for constant time</span></div>
|
|
<div class="line"><a name="l00133"></a><span class="lineno"> 133</span>  <span class="comment">// search</span></div>
|
|
<div class="line"><a name="l00134"></a><span class="lineno"> 134</span>  <a class="codeRef" target="_blank" href="http://en.cppreference.com/w/cpp/container/unordered_set.html">std::unordered_set<std::string></a> strSet;</div>
|
|
<div class="line"><a name="l00135"></a><span class="lineno"> 135</span>  <span class="keywordflow">for</span> (<span class="keyword">const</span> <span class="keyword">auto</span> &s : wordDict) {</div>
|
|
<div class="line"><a name="l00136"></a><span class="lineno"> 136</span>  strSet.<a class="codeRef" target="_blank" href="http://en.cppreference.com/w/cpp/container/unordered_set/insert.html">insert</a>(s);</div>
|
|
<div class="line"><a name="l00137"></a><span class="lineno"> 137</span>  }</div>
|
|
<div class="line"><a name="l00138"></a><span class="lineno"> 138</span>  <span class="comment">// a vector to be used for memoization, whose value at index i will</span></div>
|
|
<div class="line"><a name="l00139"></a><span class="lineno"> 139</span>  <span class="comment">// tell if the string s can be segmented (correctly) at position i.</span></div>
|
|
<div class="line"><a name="l00140"></a><span class="lineno"> 140</span>  <span class="comment">// initializing it with INT_MAX (which will denote no solution)</span></div>
|
|
<div class="line"><a name="l00141"></a><span class="lineno"> 141</span>  <a class="codeRef" target="_blank" href="http://en.cppreference.com/w/cpp/container/vector.html">std::vector<int></a> dp(s.<a class="codeRef" target="_blank" href="http://en.cppreference.com/w/cpp/string/basic_string/size.html">length</a>(), INT_MAX);</div>
|
|
<div class="line"><a name="l00142"></a><span class="lineno"> 142</span>  </div>
|
|
<div class="line"><a name="l00143"></a><span class="lineno"> 143</span>  <span class="comment">// calling check method with position = 0, to check from left</span></div>
|
|
<div class="line"><a name="l00144"></a><span class="lineno"> 144</span>  <span class="comment">// from where can be start segmenting the complete string in correct</span></div>
|
|
<div class="line"><a name="l00145"></a><span class="lineno"> 145</span>  <span class="comment">// manner</span></div>
|
|
<div class="line"><a name="l00146"></a><span class="lineno"> 146</span>  <span class="keywordflow">return</span> <a class="code" href="../../d3/d84/word__break_8cpp.html#a272b0f5cdb4e41fd6dee4538b808c06a">check</a>(s, strSet, 0, &dp);</div>
|
|
<div class="line"><a name="l00147"></a><span class="lineno"> 147</span> }</div>
|
|
<div class="ttc" id="ainsert_html"><div class="ttname"><a href="http://en.cppreference.com/w/cpp/container/unordered_set/insert.html">std::unordered_set::insert</a></div><div class="ttdeci">T insert(T... args)</div></div>
|
|
<div class="ttc" id="aunordered_set_html"><div class="ttname"><a href="http://en.cppreference.com/w/cpp/container/unordered_set.html">std::unordered_set</a></div></div>
|
|
</div><!-- fragment --><div class="dynheader">
|
|
Here is the call graph for this function:</div>
|
|
<div class="dyncontent">
|
|
<div class="center"><div class="zoom"><iframe scrolling="no" frameborder="0" src="../../d3/d84/word__break_8cpp_afe4dcd6fd5282e535685361cba645d7c_cgraph.svg" width="100%" height="445"><p><b>This browser is not able to show SVG: try Firefox, Chrome, Safari, or Opera instead.</b></p></iframe></div>
|
|
</div>
|
|
</div>
|
|
|
|
</div>
|
|
</div>
|
|
</div><!-- contents -->
|
|
</div><!-- doc-content -->
|
|
<!-- start footer part -->
|
|
<div id="nav-path" class="navpath"><!-- id is needed for treeview function! -->
|
|
<ul>
|
|
<li class="navelem"><a class="el" href="../../dir_8a20dd5bfd5341a725342bf72b6b686f.html">dynamic_programming</a></li><li class="navelem"><a class="el" href="../../d3/d84/word__break_8cpp.html">word_break.cpp</a></li>
|
|
<li class="footer">Generated by <a href="https://www.doxygen.org/index.html"><img class="footer" src="../../doxygen.svg" width="104" height="31" alt="doxygen"/></a> 1.9.1 </li>
|
|
</ul>
|
|
</div>
|
|
</body>
|
|
</html>
|