Skip to content

Commit

Permalink
add algorithm max common substring
Browse files Browse the repository at this point in the history
  • Loading branch information
Maiyatang2010 committed Sep 11, 2016
1 parent b191d2f commit 3a9e311
Showing 1 changed file with 32 additions and 0 deletions.
32 changes: 32 additions & 0 deletions algorithm/MaxCommonString.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
#include <iostream>
#inlcude <string>
#include <vector>
using namespace std;

int main(){
string s1,s2;
cin>>s1;
cin>>s2;

int l1 = s1.size(),l2 = s2.size();
vector<vector<int> > dp(l1,vector<int>(l2,0));

for(int i=1; i<=l1; i++){
for(int j=1; j<l2; j++){
if(s1[i-1] == s2[j-1])
dp[i][j] = dp[i-1][j-1]+1;
else
dp[i][j] = 0;
}
}

int maxlen = 0;

for(int i=1; i<=l1; i++)
for(int j=1; j<=l2; j++)
if(maxlen<dp[i][j]) maxlen=dp[i][j];

cout<<maxlen<<endl;

}

0 comments on commit 3a9e311

Please sign in to comment.