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 154115081034 at 2016-04-16 10:21:18 on Problem 1458
//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:
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