Longest Common Subsequence

Witam.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
#include<cstdio>
int n,m,a=0,b,c=1,i,j,C[1001][1001],I[1001],J[1001];
char A[1001],B[1001],D[1001];
 
int main(){ 
scanf("%d",&b);
 
while(c<=b){
   scanf("%d ",&n);
   for(i=1; i<=n; i++)
   A[i]=getchar();
   scanf("%d ",&m);
   for(i=1; i<=m; i++)
   B[i]=getchar();
 
   for(i=0; i<=n; i++)
        C[i][0]=0;
   for(j=1; j<=m; j++)
        C[0][j]=0;
 
   for(i=1; i<=n; i++)
        for(j=1; j<=m; 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];
 
    for(i=n,j=m;i>0 && j>0;){
      if(C[i-1][j]==C[i][j])
          i--;
      else if(C[i][j-1]==C[i][j])
          j--;
      else{
          D[a]=A[i];
          I[a]=i;
          J[a++]=j;
          i--; j--;
      };
    };
    if(C[n][m]<2) printf("case %d N",c);
    else{ 
      printf("case %d Y\n%d",c,C[n][m]);
      for(i=C[n][m]-1; i>=0; i--)
            printf("\n%c %d %d",D[i],I[i],J[i]);
      };
    printf("\n");      
    a=0;
    c++;
};
return 0;
};


I don't how tranform this code to get execution time under 4s?
Use a better algorithm? :D
but what?
Topic archived. No new replies allowed.