Note that there are some explanatory texts on larger screens.

plurals
  1. POLongest common substring constrained by pattern
    text
    copied!<h3>Problem:</h3> <p>I have 3 strings s1, s2, s3. Each contain garbage text on either side, with a defining pattern in its centre: <code>text1+number1</code>. <code>number1</code> increases by 2 in each string. I want to extract <code>text1+number1</code>.</p> <p>I have already written code to find <code>number1</code></p> <p><strong>How would I extend an LCS function to get text1?</strong></p> <pre><code>#include &lt;iostream&gt; const std::string longestCommonSubstring(int, std::string const&amp; s1, std::string const&amp; s2, std::string const&amp; s3); int main(void) { std::string s1="hello 5", s2="bolo 7", s3="lo 9sdf"; std::cout &lt;&lt; "Trying to get \"lo 5\", actual result: \"" &lt;&lt; longestCommonSubstring(5, s1, s2, s3) &lt;&lt; '\"'; } const std::string longestCommonSubstring(int must_include, std::string const&amp; s1, std::string const&amp; s2, std::string const&amp; s3) { std::string longest; for(size_t start=0, length=1; start + length &lt;= s1.size();) { std::string tmp = s1.substr(start, length); if (std::string::npos != s2.find(tmp) &amp;&amp; std::string::npos != s3.find(tmp)) { tmp.swap(longest); ++length; } else ++start; } return longest; } </code></pre> <hr> <h3>Example:</h3> <p>From <code>"hello 5"</code>, <code>"bolo 7"</code>, <code>"lo 9sdf"</code> I would like to get <code>"lo 5"</code></p> <h3>Code:</h3> <p>I have been able to write a simple LCS function(<a href="http://ideone.com/aD7tD" rel="nofollow">test-case</a>) but I am having trouble writing this modified one.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload