Poj 1458 common subsequence

Source: Internet
Author: User

Question link: http://poj.org/problem? Id = 1458

 

Ideas:

Typical Longest Common subsequence problems are solved using dynamic planning.

Code:

 

#include <iostream>#include <string.h>using namespace std;const int MAX_N = 200 + 10;int dp[MAX_N][MAX_N];char X[MAX_N], Y[MAX_N];void Lcs( int XLen, int YLen ){    for ( int i = 1; i <= XLen; ++i )        for( int j = 1; j <= YLen; ++j )        {            if ( X[i-1] == Y[j-1] )                dp[i][j] = dp[i-1][j-1] + 1;            else            if ( dp[i-1][j] >= dp[i][j-1] )                dp[i][j] = dp[i-1][j];            else                dp[i][j] = dp[i][j-1];        }}int main(){    while ( scanf( "%s %s", X, Y ) != EOF )    {        int XLen, YLen;        memset( dp, 0, sizeof(dp) );        XLen = strlen( X );        YLen = strlen( Y );        Lcs( XLen, YLen );        printf( "%d\n", dp[XLen][YLen] );    }    return 0;}

 

Poj 1458 common subsequence

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.