It an application with LCS, I have 2 method for LCS (LCS1 and LCS2 in code)
My code can run in my computer but get "compiler ERROR" in online-judge,and I do not know WHY!!
Please get me some hints, thanks.
here are my code (c++):
Code: Select all
//Question http://acm.uva.es/p/v1/111.html
//CPU: , Memory:
//state:
#include <iostream>
#include <cmath>
using namespace std;
int LCS1(int *A, int i, int*B, int j)
{
if(i==0 | j==0)
return 0;
else if( A[i]==B[j] )
return LCS1(A, i-1, B, j-1)+1;
else
return max( LCS1(A, i, B, j-1), LCS1(A, i-1, B, j) );
}
int LCS2(int *A, int*B, int n)
{
int c[n+1][n+1];
for(int i=0; i<=n; i++)
c[i][0] = 0;
for(int j=0; j<=n; j++)
c[0][j] = 0;
for(int i=1; i<=n; i++)
{
for(int j=1; j<=n; j++)
{
if( A[i]==B[j] )
c[i][j] = c[i-1][j-1]+1;
else if(c[i-1][j] >= c[i][j-1])
c[i][j] = c[i-1][j];
else
c[i][j] = c[i][j-1];
}
}
return c[n][n];
}
int main()
{
int n;
cin >> n;
if(cin.fail() || n<2 || n>20)
return 0;
int *A = new int[n+1];
for(int i=1; i<=n; i++)
{
int temp;
cin >> temp;
A[temp] = i;
}
int *B = new int[n+1];
while(true){
for(int i=1; i<=n; i++)
{
int temp;
cin >> temp;
B[temp] = i;
}
cout << LCS1(A, n+1, B, n+1) << "\n";
cout << LCS2(A, B, n) << "\n";
if(cin.eof())
break;
}
delete A;
A=NULL;
delete B;
B=NULL;
//system("PAUSE");
return 0;
}