码迷,mamicode.com
首页 > 其他好文 > 详细

POJ 1458 Common Subsequence

时间:2014-10-18 21:01:51      阅读:203      评论:0      收藏:0      [点我收藏+]

标签:blog   io   os   for   sp   2014   on   log   bs   

题意:求LCS

Sol:经典的 LCS。

if ( i==0  || j==0 )  dp [ i , j ] = 0 ;

else if ( X[ i ] == Y [  j ] ) dp [ i-1 , j-1 ] + 1;

else dp [ i, j ] = max  (  dp[ i - 1 , j ] , dp [ i ,  j-1 ] ) 


#include <cstdio>
#include <iostream>
#include <cstring>
#include <cmath>
#include <string>

using namespace std;

const int maxn = 500 + 10;

int dp[maxn][maxn];

int main()
{
	memset(dp,0,sizeof(dp));
	int len1,len2;
	string str1,str2;
	while(cin>>str1>>str2)
	{
		len1=str1.length();
		len2=str2.length();
		for(int i=1;i<=len1;i++)
		{
			for(int j=1;j<=len2;j++)
			{
				if(str1[i-1]==str2[j-1])
					dp[i][j]=dp[i-1][j-1]+1;
				else
					dp[i][j]=max(dp[i-1][j],dp[i][j-1]);
			}
		}
		printf("%d\n",dp[len1][len2]);
	}
	return 0;
}


POJ 1458 Common Subsequence

标签:blog   io   os   for   sp   2014   on   log   bs   

原文地址:http://blog.csdn.net/imutzcy/article/details/40215337

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!