Common Subsequence Gym - 102307C(最长公共子序列 n比较大 )

Manuel thinks that Diego is his long lost brother. But Diego thinks Manuel is wrong, and to prove it, he got DNA samples from himself and Manuel. Now Diego has given you the DNA samples and it is your task to say whether they are brothers or not.

The DNA samples of Diego and Manuel are strings A and B, both have length n (1≤n≤105) and consist of only the characters ‘A’, ‘T’, ‘G’ and ‘C’. If there is a common subsequence of A and B that has length greater than or equal to 0.99×n, then Diego and Manuel are brothers, in other case, they are not.

Input
The input consists of two lines with strings A and B, respectively.

Output
You should output a single line with “Long lost brothers D:” (without quotes) if Diego and Manuel are brothers, and “Not brothers 😦” (without quotes) if they are not.

Examples
Input
GAATTGCGTACAATGC
GAATTGCGTACAATGC
Output
Long lost brothers D:
Input
CCATAGAGAA
CGATAGAGAA
Output
Not brothers 😦
Note
A subsequence of a string X is any string that you can get by removing any number of characters from X.

A common subsequence of strings X and Y is a string that is a subsequence of both X and Y.

题目意思就是要求最长公共子序列,但是n比较大,如果用一般的求法,会T

#include <algorithm>
#include <cstring>
#include <iostream>
#include<cstdio>
#include<queue>
#include<vector>
#include<set>
#include<map>
using namespace std;
typedef long long ll;
const int inf=0x3f3f3f3f;
const long long inff=0x3f3f3f3f3f3f3f3f;
const int maxn = 500050;
typedef long long ll;

string s1,s2;
int dp[1005][1005];

int main()
{
    cin>>s1>>s2;
    int t=s1.size();  //这里的s1,s2的长度相同,所以就一个t就行
    int n=min(t,1000);  //取小的
    int m=min(t,1000);
    int ans=0;
    for(int i=0; i<=n; i++)
    {
        for(int j=0; j<=m; j++)
        {
            while(i+dp[i][j]<t&&j+dp[i][j]<t&&s1[i+dp[i][j]]==s2[j+dp[i][j]])dp[i][j]++;
            dp[i+1][j]=max(dp[i][j],dp[i+1][j]);
            dp[i][j+1]=max(dp[i][j],dp[i][j+1]);
            ans=max(dp[i][j],ans);
        }
    }
    double q=ans*1.0/t;   //ans就是最长的公共子序列
    if(q>=0.99)
        printf("Long lost brothers D:\n");
    else printf("Not brothers :(\n");
    return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值