fjnu 1658 Common Subsequence

来源:互联网 发布:逆战异地登录数据异常 编辑:程序博客网 时间:2024/04/27 06:58

Description

Sample Input

abcfbc         abfcabprogramming    contest abcd           mnp

Sample Output

420

 

KEY:这题就求最长递增子序列的问题,不是什么难题,可是老是TLE,居然是因为空间给小了……无语~

 

Source:

#include
<stdio.h>
#include
<string.h>

int max(int a,int b)
{
    
if(a>=b) return a;
    
else return b;
}


void GetList(char str1[],char str2[])
{
    
int la=strlen(str1)-1;
    
int lb=strlen(str2)-1;
    
int i,j;
    
int f[1000][1000]={0};
    
for(i=1;i<=la;i++)
        
for(j=1;j<=lb;j++)
        
{
            
if(str1[i]==str2[j])
            
{
                f[i][j]
=f[i-1][j-1]+1;
            }

            
else
            
{
                f[i][j]
=max(f[i-1][j],f[i][j-1]);
            }

        }

    printf(
"%d ",f[la][lb]);
}


int main()
{
/*    freopen("fjnu_1658.in","r",stdin);*/
    
char str1[1000],str2[1000],t1[1000],t2[1000];
    strcpy(str1,
"#");
    strcpy(str2,
"#");
    
while(scanf("%s%s",t1,t2)!=EOF)
    
{
        strcat(str1,t1);
        strcat(str2,t2);
        GetList(str1,str2);
        strcpy(str1,
"#");
        strcpy(str2,
"#");
    }

    
return 0;
}




 
原创粉丝点击