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

Re:Common Subsequence

Posted by whus200431610001 at 2007-06-28 15:49:55
In Reply To:Common Subsequence Posted by:whus200532550008 at 2006-12-23 16:07:16
#include <iostream>
#include <string>
using namespace std;
char sz1[1000];
char sz2[1000];
int anMaxLen[1000][1000];
int main()
{
 while( cin >> sz1 >> sz2 ) {
  int nLength1 = strlen( sz1);
  int nLength2 = strlen( sz2);
  //int nTmp;
  int i,j;
  for( i = 0;i <= nLength1; i ++ )
   anMaxLen[i][0] = 0;//初始化
  for( j = 0;j <= nLength2; j ++ )
   anMaxLen[0][j] = 0;//初始化
  for( i = 1;i <= nLength1;i ++ )   
  {
   for( j = 1; j <= nLength2; j ++ ) 
   {
    if( sz1[i-1] == sz2[j-1] )
     anMaxLen[i][j] = anMaxLen[i-1][j-1] + 1;
    else 
    {
     int nLen1 = anMaxLen[i][j-1];
     int nLen2 = anMaxLen[i-1][j];
     if( nLen1 > nLen2 )//取最大值;
      anMaxLen[i][j] = nLen1;
     else
      anMaxLen[i][j] = nLen2;
    }
   }
  }
  cout <<   anMaxLen[nLength1][nLength2] << endl;
 }
}

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