动态规划之最长公共子串(递归的备忘录写法)

#include <iostream>
#include <string>
#define Max 100
using namespace std;
//最长公共子字符串,递归的备忘录写法
int c[Max][Max];
string s1,s2;
int fun(int i,int j)
{
	if(c[i][j]>0)return c[i][j];
	if(i<0||j<0)return 0;
	if(s1[i]==s2[j]) { c[i][j]=fun(i-1,j-1)+1; cout<<s1[i];}
	else 
	{
		int a=fun(i-1,j);
		int b=fun(i,j-1);
		c[i][j]=a>b?a:b;
	}
	return c[i][j];
}
void main()
{
	s1="ASCDF";
	s2="QACFER";
	cout<<fun(s1.length()-1,s2.length()-1)<<endl;
}

    原文作者:动态规划
    原文地址: https://blog.csdn.net/noaboutfengyue/article/details/17382397
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞