Longest common substring: Difference between revisions

Restrict max substring length. Avoid in-out variable. Output set_intersection to vector directly. Use auto. Add some comments and space to improve read ability.
(Restrict max substring length. Avoid in-out variable. Output set_intersection to vector directly. Use auto. Add some comments and space to improve read ability.)
Line 184:
 
=={{header|C++}}==
<lang cpp>#include <string>
#include <string>
#include <algorithm>
#include <iostream>
Line 190 ⟶ 191:
#include <vector>
 
void findSubstringsauto collectSubStrings( const std::string & word s, std::set<std::string>int & substringsmaxSubLength ) {
{
int l = word.length( ) ;
for ( int startl = 0 s.length(); start < l ; start++ ) {
auto res = std::set<std::string> common ();
for ( int length = 1 ; length < l - start + 1 ; length++ ) {
substrings.insert ( word.substr for ( int start ,= length0; )start )< l; start++ )
}{
int m = std::min( maxSubLength, l - start + 1 );
}
for ( int length = 1 ; length < l - start + 1 m; length++ ) {
{
res.insert( s.substr( start, length ) );
}
}
return 0return res;
}
 
std::string lcs ( const std::string & first s0, const std::string & seconds1 ) {
{
std::set<std::string> firstSubstrings , secondSubstrings ;
// collect substring set
findSubstrings ( first , firstSubstrings ) ;
auto maxSubLength = std::min( s0.length(), s1.length() );
findSubstrings ( second , secondSubstrings ) ;
auto set0 = collectSubStrings( s0, maxSubLength );
std::set<std::string> common ;
auto set1 = collectSubStrings( s1, maxSubLength );
std::set_intersection ( firstSubstrings.begin( ) , firstSubstrings.end( ) ,
 
secondSubstrings.begin( ) , secondSubstrings.end( ) ,
// get commons into a vector
std::inserter ( common , common.begin( ) ) ) ;
auto common = std::vector<std::string> commonSubs ( common.begin( ) , common.end( ) ) ;
std::sort set_intersection( commonSubsset0.begin( ) , commonSubsset0.end(), set1.begin() , []set1.end( const std::string &s1 ),
const std::string &s2 ) { return s1.lengthback_inserter( ) > s2.length(common ) ; } ) ;
 
return *(commonSubs.begin( ) ) ;
// get the longest one
std::inserter nth_element( common .begin(), common.begin( ), common.end() ) ;,
[]( const std::string& s1, const std::string& s2 ) {
return s1.length() > s2.length();
} );
return *(commonSubscommon.begin( ) ) ;
}
 
int main( int argc, char* argv[] )
{
auto s1 = std::string s1 ( "thisisatest" ) ;
auto s2 = std::string s2 ( "testing123testing" ) ;
std::cout << "The longest common substring of " << s1 << " and " << s2 << " is:\n" ;
std::cout << lcs ( s1 , s2 ) << " !is:\n" ;
std::cout << "\"" << lcs( s1, s2 ) << "\" !\n";
return 0;
}
 
}</lang>
int main( ) {
std::string s1 ("thisisatest" ) ;
std::string s2 ( "testing123testing" ) ;
std::cout << "The longest common substring of " << s1 << " and " << s2 << " is:\n" ;
std::cout << lcs ( s1 , s2 ) << " !\n" ;
return 0 ;
}</lang>
{{out}}
test !</pre>
<pre>The longest common substring of thisisatest and testing123testing is:
test !</pre>
"test" !
</pre>
 
=={{header|C sharp|C#}}==
Anonymous user