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 zilongz at 2010-04-23 02:57:33 on Problem 1458
#include <stdio.h>
#include <string.h>

char    str1[501], str2[501];
int     len1, len2;
char    arr[501][501];

int     getmax(int, int);

int
main(int argc, char *argv[])
{
        int     i, j;

        while ( scanf("%s%s", str1, str2) == 2 )
        {
                len1 = strlen(str1);
                len2 = strlen(str2);

                for ( i = 0; i <= len1; ++i )
                        for ( j = 0; j <= len2; ++j )
                                arr[i][j] = -1;

                printf("%d\n", getmax(len1, len2));
        }
        return 0;
}

int
getmax(int i, int j)
{
        int     x, y;

        if ( arr[i][j] != -1 )
                return arr[i][j];


        if ( i == 0 || j == 0 )
                arr[i][j] = 0;
        else
        {
                if ( str1[i-1] == str2[j-1] )
                        arr[i][j] = getmax(i-1, j-1) + 1;
                else
                {
                        x = getmax(i-1, j);
                        y = getmax(i, j-1);
                        arr[i][j] = x > y ? x : y;
                }
        }

        return arr[i][j];
}


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