Online JudgeProblem SetAuthorsOnline ContestsUser
Web Board
Home Page
F.A.Qs
Statistical Charts
Problems
Submit Problem
Online Status
Prob.ID:
Register
Update your info
Authors ranklist
Current Contest
Past Contests
Scheduled Contests
Award Contest
User ID:
Password:
  Register

经典的动态规划题目,动态转移方程比较简单。

Posted by jourkliu at 2009-03-09 21:37:40 on Problem 1458
动态转移方程为:
f[i][j]=max{f[i-1,j],f[i,j-1],f[i-1,j-1]+1|s1[i]==s2[j]}
f[0][0]=0,f[0][i]=0,f[i][0]=0
程序仅供参考:
#include <iostream>
#include <string>
using namespace std;
const int maxn=500;
int max(int x,int y)
{
	return x>y?x:y;
}
int main()
{
	string s1,s2;
	while(cin>>s1>>s2){
		int f[maxn][maxn]={0},len1=s1.length(),len2=s2.length();
		for(int i=1;i<=len1;i++)
	 		for(int j=1;j<=len2;j++){
	 			f[i][j]=max(f[i-1][j],f[i][j-1]);
	 			if(s1[i-1]==s2[j-1]) f[i][j]=max(f[i][j],f[i-1][j-1]+1);
	 		}
		cout<<f[len1][len2]<<endl;
	}
	return 0;
}

Followed by:

Post your reply here:
User ID:
Password:
Title:

Content:

Home Page   Go Back  To top


All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator