POJ1458(Common Subsequence)

题目链接

经典DP问题。由于最初if(c[i][j]>=0)   return c[i][j];一句中少写一个等于号造成TLE,查了好久啊……

View Code
 1 #include <stdio.h>
2 #include <string.h>
3 #define MAX(a,b) ((a)>(b)?(a):(b))
4 #define N 300
5 char a[N],b[N];
6 int c[N][N];
7 int f(int i,int j)
8 {
9 if(i<0 || j<0) return 0;
10 if(c[i][j]>=0) return c[i][j];
11 if(a[i]==b[j]) return c[i][j]=f(i-1,j-1)+1;
12 return c[i][j]=MAX(f(i-1,j),f(i,j-1));
13 }
14 int main()
15 {
16 int i,j;
17 while(~scanf("%s%s",a,b))
18 {
19 memset(c,-1,sizeof(c));
20 i=strlen(a),j=strlen(b);
21 printf("%d\n",f(i-1,j-1));
22 }
23 return 0;
24 }



原文地址:https://www.cnblogs.com/algorithms/p/2435324.html