| ||||||||||
| Online Judge | Problem Set | Authors | Online Contests | User | ||||||
|---|---|---|---|---|---|---|---|---|---|---|
| Web Board Home Page F.A.Qs Statistical Charts | Current Contest Past Contests Scheduled Contests Award Contest | |||||||||
求两字符串的最长公共子序列//dp问题主要是分析最小子问题,以及状态变化
#include<iostream>
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<math.h>
#include<ctype.h>
#include<algorithm>
using namespace std;
#define N 1010
#define INF 0x3f3f3f3f
int dp[N][N];
int main()
{
char s1[N], s2[N];
int len1, len2;
while(scanf("%s%s", s1, s2)!=EOF)
{
len1=strlen(s1);
len2=strlen(s2);
/*for(int i=0; i<=len1; i++)
dp[i][0]=0;
for(int j=0; j<=len2; j++)
dp[0][j]=0;*/
memset(dp, 0, sizeof(dp));
for(int i=0; i<len1; i++)
for(int j=0; j<len2; j++)
{
if(s1[i]==s2[j])
dp[i+1][j+1]=dp[i][j]+1;
else
dp[i+1][j+1]=max(dp[i][j+1], dp[i+1][j]);
}
printf("%d\n", dp[len1][len2]);
}
return 0;
}
Followed by: Post your reply here: |
All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator