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

贴一个ac代码,数组开500就够了,动态规划分解问题

Posted by 20152430226 at 2017-03-07 22:31:43 on Problem 1458
#include <stdio.h>
#include <string.h>
#define N 500
void LCSLength(int m, int n, char x[], char y[],int c[][N],int b[][N])
{
    int i, j;
    for(i = 1; i <= m; i++) c[i][0] = 0;
    for(i = 0; i <= n; i++) c[0][i] = 0;

    for(i = 1; i <= m; i++)
        for(j = 1; j<=n; j++)
        {
            if(x[i-1]==y[j-1])
            {
                c[i][j] = c[i-1][j-1] + 1;
                b[i][j] = 1;
            }else if(c[i-1][j]>=c[i][j-1]){
                c[i][j] = c[i-1][j];
                b[i][j] = 2;
            }else{
                c[i][j] = c[i][j-1];
                b[i][j] = 3;
            }
        }
}

int main()
{
    char x[N];
    char y[N];
    int c[N][N], b[N][N];
    int m, n ;
    while(scanf("%s %s", x, y) !=EOF)
    {
        m = strlen(x);
        n = strlen(y);
        LCSLength(m,n, x, y, c, b);
        printf("%d\n", c[m][n]);

    }
    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