算法见:http://www.cnblogs.com/grenet/archive/2010/06/03/1750454.html
求最长公共子串(不需要连续)
#include <stdio.h>
#include <string>
#define N 100 int max(int a, int b, int c){
return (a>b?a:b)>c?(a>b?a:b):c;
} int needleman(char s1[], char s2[]){
int len1 = strlen(s1);
int len2 = strlen(s2);
int i,j;
int count[N][N];
for(i=;i<len1+;i++){
count[i][] = ;
}
for(i=;i<len2+;i++){
count[][i] = ;
}
for(i=;i<len1+;i++){
for(j=;j<len2+;j++){
if(s1[i] == s2[j]){
count[i][j] = count[i-][j-]+;
} else {
count[i][j] = max(count[i-][j-],count[i][j-],count[i-][j]);
}
}
}
return count[len1][len2];
} int main(){
char s1[N];
char s2[N];
while(scanf("%s%s",s1,s2)!=EOF){
int result = needleman(s1,s2);
printf("%d\n",result);
}
return ;
}