Documentation for c26eea874d

This commit is contained in:
realstealthninja
2025-05-19 11:38:33 +00:00
parent c7a9a42397
commit 14583a2f33
2829 changed files with 30266 additions and 21669 deletions

View File

@@ -4,7 +4,7 @@
<head>
<meta http-equiv="Content-Type" content="text/xhtml;charset=UTF-8"/>
<meta http-equiv="X-UA-Compatible" content="IE=11"/>
<meta name="generator" content="Doxygen 1.12.0"/>
<meta name="generator" content="Doxygen 1.13.2"/>
<meta name="viewport" content="width=device-width, initial-scale=1"/>
<title>TheAlgorithms/C++: dynamic_programming/word_break.cpp Source File</title>
<link rel="icon" href="../../favicon.svg" type="image/x-icon" />
@@ -60,7 +60,7 @@ window.MathJax = {
</table>
</div>
<!-- end header part -->
<!-- Generated by Doxygen 1.12.0 -->
<!-- Generated by Doxygen 1.13.2 -->
<script type="text/javascript">
/* @license magnet:?xt=urn:btih:d3d9a9a6595521f9666a5e94cc830dab83b65699&amp;dn=expat.txt MIT */
var searchBox = new SearchBox("searchBox", "../../search/",'.html');
@@ -124,27 +124,28 @@ $(function(){initNavTree('d3/d84/word__break_8cpp_source.html','../../'); initRe
<div class="headertitle"><div class="title">word_break.cpp</div></div>
</div><!--header-->
<div class="contents">
<a href="../../d3/d84/word__break_8cpp.html">Go to the documentation of this file.</a><div class="fragment"><div class="line"><a id="l00001" name="l00001"></a><span class="lineno"> 1</span> </div>
<a href="../../d3/d84/word__break_8cpp.html">Go to the documentation of this file.</a><div class="fragment"><div class="line"><a id="l00001" name="l00001"></a><span class="lineno"> 1</span></div>
<div class="line"><a id="l00030" name="l00030"></a><span class="lineno"> 30</span> </div>
<div class="line"><a id="l00031" name="l00031"></a><span class="lineno"> 31</span><span class="preprocessor">#include &lt;cassert&gt;</span></div>
<div class="line"><a id="l00032" name="l00032"></a><span class="lineno"> 32</span><span class="preprocessor">#include &lt;climits&gt;</span></div>
<div class="line"><a id="l00033" name="l00033"></a><span class="lineno"> 33</span><span class="preprocessor">#include &lt;iostream&gt;</span></div>
<div class="line"><a id="l00034" name="l00034"></a><span class="lineno"> 34</span><span class="preprocessor">#include &lt;string&gt;</span></div>
<div class="line"><a id="l00035" name="l00035"></a><span class="lineno"> 35</span><span class="preprocessor">#include &lt;unordered_set&gt;</span></div>
<div class="line"><a id="l00036" name="l00036"></a><span class="lineno"> 36</span><span class="preprocessor">#include &lt;vector&gt;</span></div>
<div class="line"><a id="l00037" name="l00037"></a><span class="lineno"> 37</span> </div>
<div class="line"><a id="l00037" name="l00037"></a><span class="lineno"> 37</span></div>
<div class="line"><a id="l00042" name="l00042"></a><span class="lineno"> 42</span><span class="keyword">namespace </span><a class="code hl_namespace" href="../../dd/d24/namespacedynamic__programming.html">dynamic_programming</a> {</div>
<div class="line"><a id="l00043" name="l00043"></a><span class="lineno"> 43</span> </div>
<div class="line"><a id="l00043" name="l00043"></a><span class="lineno"> 43</span></div>
<div class="line"><a id="l00049" name="l00049"></a><span class="lineno"> 49</span><span class="keyword">namespace </span><a class="code hl_namespace" href="../../d6/d53/namespaceword__break.html">word_break</a> {</div>
<div class="line"><a id="l00050" name="l00050"></a><span class="lineno"> 50</span> </div>
<div class="line"><a id="l00050" name="l00050"></a><span class="lineno"> 50</span></div>
<div class="foldopen" id="foldopen00060" data-start="{" data-end="}">
<div class="line"><a id="l00060" name="l00060"></a><span class="lineno"><a class="line" href="../../d3/d84/word__break_8cpp.html#a1cc9dd6e6190d10a010fdcdfe7a21a81"> 60</a></span><span class="keywordtype">bool</span> <a class="code hl_function" href="../../d3/d84/word__break_8cpp.html#a1cc9dd6e6190d10a010fdcdfe7a21a81">exists</a>(<span class="keyword">const</span> std::string &amp;str,</div>
<div class="line"><a id="l00061" name="l00061"></a><span class="lineno"> 61</span> <span class="keyword">const</span> std::unordered_set&lt;std::string&gt; &amp;strSet) {</div>
<div class="line"><a id="l00062" name="l00062"></a><span class="lineno"> 62</span> <span class="keywordflow">return</span> strSet.find(str) != strSet.end();</div>
<div class="line"><a id="l00063" name="l00063"></a><span class="lineno"> 63</span>}</div>
</div>
<div class="line"><a id="l00064" name="l00064"></a><span class="lineno"> 64</span> </div>
<div class="line"><a id="l00064" name="l00064"></a><span class="lineno"> 64</span></div>
<div class="foldopen" id="foldopen00080" data-start="{" data-end="}">
<div class="line"><a id="l00080" name="l00080"></a><span class="lineno"><a class="line" href="../../d3/d84/word__break_8cpp.html#a272b0f5cdb4e41fd6dee4538b808c06a"> 80</a></span><span class="keywordtype">bool</span> check(<span class="keyword">const</span> std::string &amp;s, <span class="keyword">const</span> std::unordered_set&lt;std::string&gt; &amp;strSet,</div>
<div class="line"><a id="l00080" name="l00080"></a><span class="lineno"><a class="line" href="../../d3/d84/word__break_8cpp.html#a272b0f5cdb4e41fd6dee4538b808c06a"> 80</a></span><span class="keywordtype">bool</span> <a class="code hl_function" href="../../d3/d84/word__break_8cpp.html#a272b0f5cdb4e41fd6dee4538b808c06a">check</a>(<span class="keyword">const</span> std::string &amp;s, <span class="keyword">const</span> std::unordered_set&lt;std::string&gt; &amp;strSet,</div>
<div class="line"><a id="l00081" name="l00081"></a><span class="lineno"> 81</span> <span class="keywordtype">int</span> pos, std::vector&lt;int&gt; *<a class="code hl_namespace" href="../../df/d88/namespacedp.html">dp</a>) {</div>
<div class="line"><a id="l00082" name="l00082"></a><span class="lineno"> 82</span> <span class="keywordflow">if</span> (pos == s.length()) {</div>
<div class="line"><a id="l00083" 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>
@@ -172,7 +173,7 @@ $(function(){initNavTree('d3/d84/word__break_8cpp_source.html','../../'); initRe
<div class="line"><a id="l00105" 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 id="l00106" 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 id="l00107" 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 id="l00108" name="l00108"></a><span class="lineno"> 108</span> <span class="keywordflow">if</span> (<a class="code hl_function" href="../../d3/d84/word__break_8cpp.html#a1cc9dd6e6190d10a010fdcdfe7a21a81">exists</a>(wordTillNow, strSet) &amp;&amp; check(s, strSet, i + 1, <a class="code hl_namespace" href="../../df/d88/namespacedp.html">dp</a>)) {</div>
<div class="line"><a id="l00108" name="l00108"></a><span class="lineno"> 108</span> <span class="keywordflow">if</span> (<a class="code hl_function" href="../../d3/d84/word__break_8cpp.html#a1cc9dd6e6190d10a010fdcdfe7a21a81">exists</a>(wordTillNow, strSet) &amp;&amp; <a class="code hl_function" href="../../d3/d84/word__break_8cpp.html#a272b0f5cdb4e41fd6dee4538b808c06a">check</a>(s, strSet, i + 1, <a class="code hl_namespace" href="../../df/d88/namespacedp.html">dp</a>)) {</div>
<div class="line"><a id="l00109" name="l00109"></a><span class="lineno"> 109</span> <a class="code hl_namespace" href="../../df/d88/namespacedp.html">dp</a>-&gt;at(pos) = 1;</div>
<div class="line"><a id="l00110" name="l00110"></a><span class="lineno"> 110</span> <span class="keywordflow">return</span> <span class="keyword">true</span>;</div>
<div class="line"><a id="l00111" name="l00111"></a><span class="lineno"> 111</span> }</div>
@@ -183,7 +184,7 @@ $(function(){initNavTree('d3/d84/word__break_8cpp_source.html','../../'); initRe
<div class="line"><a id="l00116" 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 id="l00117" name="l00117"></a><span class="lineno"> 117</span>}</div>
</div>
<div class="line"><a id="l00118" name="l00118"></a><span class="lineno"> 118</span> </div>
<div class="line"><a id="l00118" name="l00118"></a><span class="lineno"> 118</span></div>
<div class="foldopen" id="foldopen00131" data-start="{" data-end="}">
<div class="line"><a id="l00131" name="l00131"></a><span class="lineno"><a class="line" href="../../d3/d84/word__break_8cpp.html#afe4dcd6fd5282e535685361cba645d7c"> 131</a></span><span class="keywordtype">bool</span> <a class="code hl_function" href="../../d3/d84/word__break_8cpp.html#afe4dcd6fd5282e535685361cba645d7c">wordBreak</a>(<span class="keyword">const</span> std::string &amp;s, <span class="keyword">const</span> std::vector&lt;std::string&gt; &amp;wordDict) {</div>
<div class="line"><a id="l00132" name="l00132"></a><span class="lineno"> 132</span> <span class="comment">// unordered set to store words in the dictionary for constant time</span></div>
@@ -200,13 +201,13 @@ $(function(){initNavTree('d3/d84/word__break_8cpp_source.html','../../'); initRe
<div class="line"><a id="l00143" 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 id="l00144" 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 id="l00145" name="l00145"></a><span class="lineno"> 145</span> <span class="comment">// manner</span></div>
<div class="line"><a id="l00146" name="l00146"></a><span class="lineno"> 146</span> <span class="keywordflow">return</span> check(s, strSet, 0, &amp;<a class="code hl_namespace" href="../../df/d88/namespacedp.html">dp</a>);</div>
<div class="line"><a id="l00146" name="l00146"></a><span class="lineno"> 146</span> <span class="keywordflow">return</span> <a class="code hl_function" href="../../d3/d84/word__break_8cpp.html#a272b0f5cdb4e41fd6dee4538b808c06a">check</a>(s, strSet, 0, &amp;<a class="code hl_namespace" href="../../df/d88/namespacedp.html">dp</a>);</div>
<div class="line"><a id="l00147" name="l00147"></a><span class="lineno"> 147</span>}</div>
</div>
<div class="line"><a id="l00148" name="l00148"></a><span class="lineno"> 148</span> </div>
<div class="line"><a id="l00149" name="l00149"></a><span class="lineno"> 149</span>} <span class="comment">// namespace word_break</span></div>
<div class="line"><a id="l00150" name="l00150"></a><span class="lineno"> 150</span>} <span class="comment">// namespace dynamic_programming</span></div>
<div class="line"><a id="l00151" name="l00151"></a><span class="lineno"> 151</span> </div>
<div class="line"><a id="l00151" name="l00151"></a><span class="lineno"> 151</span></div>
<div class="foldopen" id="foldopen00156" data-start="{" data-end="}">
<div class="line"><a id="l00156" name="l00156"></a><span class="lineno"><a class="line" href="../../d3/d84/word__break_8cpp.html#aa8dca7b867074164d5f45b0f3851269d"> 156</a></span><span class="keyword">static</span> <span class="keywordtype">void</span> <a class="code hl_function" href="../../d3/d84/word__break_8cpp.html#aa8dca7b867074164d5f45b0f3851269d">test</a>() {</div>
<div class="line"><a id="l00157" name="l00157"></a><span class="lineno"> 157</span> <span class="comment">// the complete string</span></div>
@@ -214,15 +215,16 @@ $(function(){initNavTree('d3/d84/word__break_8cpp_source.html','../../'); initRe
<div class="line"><a id="l00159" name="l00159"></a><span class="lineno"> 159</span> <span class="comment">// the dictionary to be used</span></div>
<div class="line"><a id="l00160" name="l00160"></a><span class="lineno"> 160</span> <span class="keyword">const</span> std::vector&lt;std::string&gt; wordDict = {<span class="stringliteral">&quot;apple&quot;</span>, <span class="stringliteral">&quot;pen&quot;</span>};</div>
<div class="line"><a id="l00161" name="l00161"></a><span class="lineno"> 161</span> </div>
<div class="line"><a id="l00162" name="l00162"></a><span class="lineno"> 162</span> assert(dynamic_programming::word_break::wordBreak(s, wordDict));</div>
<div class="line"><a id="l00162" name="l00162"></a><span class="lineno"> 162</span> assert(<a class="code hl_function" href="../../d3/d84/word__break_8cpp.html#afe4dcd6fd5282e535685361cba645d7c">dynamic_programming::word_break::wordBreak</a>(s, wordDict));</div>
<div class="line"><a id="l00163" name="l00163"></a><span class="lineno"> 163</span> </div>
<div class="line"><a id="l00164" 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 id="l00165" name="l00165"></a><span class="lineno"> 165</span> <span class="comment">// apple</span></div>
<div class="line"><a id="l00166" name="l00166"></a><span class="lineno"> 166</span> std::cout &lt;&lt; dynamic_programming::word_break::wordBreak(s, wordDict)</div>
<div class="line"><a id="l00166" name="l00166"></a><span class="lineno"> 166</span> std::cout &lt;&lt; <a class="code hl_function" href="../../d3/d84/word__break_8cpp.html#afe4dcd6fd5282e535685361cba645d7c">dynamic_programming::word_break::wordBreak</a>(s, wordDict)</div>
<div class="line"><a id="l00167" name="l00167"></a><span class="lineno"> 167</span> &lt;&lt; std::endl;</div>
<div class="line"><a id="l00168" name="l00168"></a><span class="lineno"> 168</span> std::cout &lt;&lt; <span class="stringliteral">&quot;Test implementation passed!\n&quot;</span>;</div>
<div class="line"><a id="l00169" name="l00169"></a><span class="lineno"> 169</span>}</div>
</div>
<div class="line"><a id="l00170" name="l00170"></a><span class="lineno"> 170</span></div>
<div class="foldopen" id="foldopen00174" data-start="{" data-end="}">
<div class="line"><a id="l00174" name="l00174"></a><span class="lineno"><a class="line" href="../../d3/d84/word__break_8cpp.html#ae66f6b31b5ad750f1fe042a706a4e3d4"> 174</a></span><span class="keywordtype">int</span> <a class="code hl_function" href="../../d3/d84/word__break_8cpp.html#ae66f6b31b5ad750f1fe042a706a4e3d4">main</a>() {</div>
<div class="line"><a id="l00175" name="l00175"></a><span class="lineno"> 175</span> <a class="code hl_function" href="../../d3/d84/word__break_8cpp.html#aa8dca7b867074164d5f45b0f3851269d">test</a>(); <span class="comment">// call the test function :)</span></div>
@@ -234,7 +236,7 @@ $(function(){initNavTree('d3/d84/word__break_8cpp_source.html','../../'); initRe
<div class="line"><a id="l00181" name="l00181"></a><span class="lineno"> 181</span> </div>
<div class="line"><a id="l00182" 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 id="l00183" name="l00183"></a><span class="lineno"> 183</span> <span class="comment">// apple</span></div>
<div class="line"><a id="l00184" name="l00184"></a><span class="lineno"> 184</span> std::cout &lt;&lt; dynamic_programming::word_break::wordBreak(s, wordDict)</div>
<div class="line"><a id="l00184" name="l00184"></a><span class="lineno"> 184</span> std::cout &lt;&lt; <a class="code hl_function" href="../../d3/d84/word__break_8cpp.html#afe4dcd6fd5282e535685361cba645d7c">dynamic_programming::word_break::wordBreak</a>(s, wordDict)</div>
<div class="line"><a id="l00185" name="l00185"></a><span class="lineno"> 185</span> &lt;&lt; std::endl;</div>
<div class="line"><a id="l00186" name="l00186"></a><span class="lineno"> 186</span>}</div>
</div>
@@ -242,6 +244,7 @@ $(function(){initNavTree('d3/d84/word__break_8cpp_source.html','../../'); initRe
<div class="ttc" id="anamespacedynamic__programming_html"><div class="ttname"><a href="../../dd/d24/namespacedynamic__programming.html">dynamic_programming</a></div><div class="ttdoc">Dynamic Programming algorithms.</div></div>
<div class="ttc" id="anamespaceword__break_html"><div class="ttname"><a href="../../d6/d53/namespaceword__break.html">word_break</a></div><div class="ttdoc">Functions for Word Break problem.</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 &amp;str, const std::unordered_set&lt; std::string &gt; &amp;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> <a href="#l00060">word_break.cpp:60</a></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 &amp;s, const std::unordered_set&lt; std::string &gt; &amp;strSet, int pos, std::vector&lt; int &gt; *dp)</div><div class="ttdoc">Function that checks if the string passed in param can be segmented from position &#39;pos&#39;,...</div><div class="ttdef"><b>Definition</b> <a href="#l00080">word_break.cpp:80</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> <a href="#l00156">word_break.cpp:156</a></div></div>
<div class="ttc" id="aword__break_8cpp_html_ae66f6b31b5ad750f1fe042a706a4e3d4"><div class="ttname"><a href="../../d3/d84/word__break_8cpp.html#ae66f6b31b5ad750f1fe042a706a4e3d4">main</a></div><div class="ttdeci">int main()</div><div class="ttdoc">Main function.</div><div class="ttdef"><b>Definition</b> <a href="#l00174">word_break.cpp:174</a></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 &amp;s, const std::vector&lt; std::string &gt; &amp;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> <a href="#l00131">word_break.cpp:131</a></div></div>
@@ -251,7 +254,7 @@ $(function(){initNavTree('d3/d84/word__break_8cpp_source.html','../../'); initRe
<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.12.0 </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.13.2 </li>
</ul>
</div>
</body>