动态规划和递归求lcs

代码如下

#include<iostream>
#include<string>
#include<algorithm>
using namespace std;
int data[2000][2000];
string a = "3741a169n084+932a968l840g035o126r160i539t644h812m737+375i919s815+210a465n302y234+360w485e024l174l698d312e454f025i006n494e376d677+651c373o160m418p173u900t034a542t144i134o107n610a628l715+277p841r630o097c046e000d407u018r082e439+439t714h726a096t284+739t123a143k178e039s852+378s958o919m728e300+478v048a355l260u637e480s348+252a562s569+581i767n340p107u404t588+428a174n336d647+257p541r832o112d830u085c018e645s292+782s463o834m765e312+358v268a149l808u058e434s632+134a726s023+317o512u829t311p602u526t829627007156527547003539349050502050257951314758377499746144882015949272638241892135962009891222967125042957069569378242774277373090915734258858283879475246592101461548221554971247933819361828948710580856476315401704723321566669522450924564304973345084336917392325501820042541829777629147567118327787446394227354953910660960656115450044675729818927389787909637922348732998482661583751721545046254185729742865271803332549512146999297678448574667930165762510685508149576313377040655566420091275256717113704157460510603081909420648675368792652070";
string b = "7033a825n442+580a140l659g853o200r879i610t403h938m362+054i997s569+165a354n009y190+733w699e162l095l606d927e110f262i145n339e295d327+714c538o936m930p658u099t346a808t401i238o952n810a973l035+039p915r253o507c888e136d064u955r363e209+661t070h489a601t629+855t441a594k460e369s045+872s862o687m130e480+677v536a356l240u347e799s394+425a229s234+242i778n153p334u901t846+150a308n134d237+781p077r980o514d895u771c528e088s891+366s067o288m878e340+486v518a875l598u023e219s644+227a141s199+330o867u339t537p109u343t37029194510158042071986019868543397297983536434100740125846172549146130546568196906639882909058749780829482948179779024459813702517981321228505397454757711562214873601157160013067814068867508873473464428682607428944350635707409339805005719085804575583396938537782993229887397987202511943370359074262187894441638585134919055878274369639484510531876823560908923262320499393985379237493430407883777369733930995864053932745957510185495997259268485458";
//递归求lcs太耗时
int LCS(string &a, string &b,string &res,int i,int j)
{
	if (i == a.length() || j == b.length())
		return 0;
	if (a[i] == b[j])
	{
		res.push_back(a[i]);
		return LCS(a,b,res,i+1,j+1)+1;
	}
	else
	{
		return max(LCS(a, b, res, i, j + 1), LCS(a, b, res, i + 1, j));
	}

}
int main()
{
	
	string res;
	int i = 0;
	int j = 0;
	//int r=LCS(a,b,res,i,j);
	//cout << res.size() << endl;
	//cout << r;
	//动态规划求lcs
	for (int i = 0; i < b.size(); i++)
		data[0][i] = 0;
	for (int i = 0; i < a.size(); i++)
		data[i][0] = 0;
	for (int i = 0; i < a.size(); i++)
	{
		for (int j = 0; j < b.size(); j++)
		{
			if (a[i] == b[j])
			{
				data[i + 1][j + 1] = data[i][j] + 1;
				res.push_back(a[i]);

			}
			else if (data[i][j + 1]>data[i + 1][j])
				data[i + 1][j + 1] = data[i][j + 1];
			else
				data[i + 1][j + 1] = data[i + 1][j];

		}
	}
	cout << data[a.size()][b.size()] << endl;
	
	cout << endl;
	system("pause");
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值