fjnu 1658 Common Subsequence

本文介绍了一种解决最长递增子序列问题的算法实现,通过动态规划的方法找到两个字符串之间的最长公共子序列,并提供了完整的C语言源代码。讨论了算法的时间复杂度及常见问题。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Description

Sample Input

abcfbc         abfcab
programming    contest 
abcd           mnp

Sample Output

4
2
0

 

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;
}




 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值