Anonymous user
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 <algorithm>
#include <iostream>
Line 190 ⟶ 191:
#include <vector>
{
for ( int length = 1 ; length < l - start + 1 ; length++ ) {▼
int m = std::min( maxSubLength, l - start + 1 );
}▼
{
res.insert( s.substr( start, length ) );
}
▲ }
}
std::string lcs
{
// collect substring set
auto maxSubLength = std::min( s0.length(), s1.length() );
auto set0 = collectSubStrings( s0, maxSubLength );
▲ std::set<std::string> common ;
auto set1 = collectSubStrings( s1, maxSubLength );
// get commons into a vector
std::inserter ( common , common.begin( ) ) ) ;▼
auto common = std::vector<std::string>
std::
return *(commonSubs.begin( ) ) ;▼
// get the longest one
[]( const std::string& s1, const std::string& s2 ) {
return s1.length() > s2.length();
} );
}
int main( int argc, char* argv[] )
{
std::cout << "\"" << lcs( s1, s2 ) << "\" !\n";
return 0;
}
▲ 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>
"test" !
</pre>
=={{header|C sharp|C#}}==
|