hdu5904LCIS+dp+最长公共子串

LCIS
Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 501 Accepted Submission(s): 231

Problem Description
Alex has two sequences a1,a2,…,an and b1,b2,…,bm. He wants find a longest common subsequence that consists of consecutive values in increasing order.

Input
There are multiple test cases. The first line of input contains an integer T, indicating the number of test cases. For each test case:

The first line contains two integers n and m (1≤n,m≤100000) – the length of two sequences. The second line contains n integers: a1,a2,…,an (1≤ai≤106). The third line contains n integers: b1,b2,…,bm (1≤bi≤106).

There are at most 1000 test cases and the sum of n and m does not exceed 2×106.

Output
For each test case, output the length of longest common subsequence that consists of consecutive values in increasing order.

Sample Input

3
3 3
1 2 3
3 2 1
10 5
1 23 2 32 4 3 4 5 6 1
1 2 3 4 5
1 1
2
1

Sample Output

1
5
0

Source
BestCoder Round #87

Recommend
wange2014 | We have carefully selected several similar problems for you: 5906 5905 5903 5902 5900

求两个串的最长公共子序列。并且这个串的数字是连续的。(如345,并不是下标要连续)
子串/子序列的区别:子串是连续相邻的,而子序列可以是不连续的
比如asdfgh和asfg:最长公共子串为:as。而最长公共子序列为:asfg
//orz。。读错题系列。。

dp[i]表示连续数字到i的长度。。

#include<bits/stdc++.h>
using namespace std;
#define LL long long
int a[100005],b[100005];
int dpA[100005],dpB[100005];
int main(){
    int t;
    scanf("%d",&t);
    while(t--){
        int n1,n2;
        scanf("%d %d",&n1,&n2);
        for(int i=1;i<=n1;i++) scanf("%d",&a[i]);
        for(int i=1;i<=n2;i++) scanf("%d",&b[i]);
        memset(dpA,0,sizeof(dpA));
        memset(dpB,0,sizeof(dpB));
        for(int i=1;i<=n1;i++) dpA[a[i]]=max(dpA[a[i]],dpA[a[i]-1]+1);
        for(int i=1;i<=n2;i++) dpB[b[i]]=max(dpB[b[i]],dpB[b[i]-1]+1);
        int ans=0;
        for(int i=1;i<=n1;i++){
            ans=max(ans,min(dpA[a[i]],dpB[a[i]]));
        }
        printf("%d\n",ans);
    }
    return 0;
}

顺便附上一个最长公共子序列(LCS)的 代码:至于最长公共子串,google到好像要用后缀数组才能nlogn求出。其他dp方法都是n^2的。。
//网上扣了两个代码:留着以后看吧。。。
最长公共子序列(LCS)

    //求取所有的最长公共子序列
    #include <iostream>
    using namespace std;

     const int X = 100, Y = 100;        //串的最大长度
     char result[X+1];                    //用于保存结果
     int count=0;                        //用于保存公共最长公共子串的个数

     /*功能:计算最优值
      *参数:
      *        x:字符串x
      *        y:字符串y
      *        b:标志数组
      *        xlen:字符串x的长度
      *        ylen:字符串y的长度
      *返回值:最长公共子序列的长度
      *
      */
     int Lcs_Length(string x, string y, int b[][Y+1],int xlen,int ylen)
     {
         int i = 0;
         int j = 0;

         int c[X+1][Y+1];
         for (i = 0; i<=xlen; i++)
         {
             c[i][0]=0;
         }
         for (i = 0; i <= ylen; i++ )
         {
             c[0][i]=0;
         }
         for (i = 1; i <= xlen; i++)
         {

             for (j = 1; j <= ylen; j++)
             {
                 if (x[i - 1] == y[j - 1])
                 {
                     c[i][j] = c[i-1][j-1]+1;
                     b[i][j] = 1;
                 }
                 else
                     if (c[i-1][j] > c[i][j-1])
                     {
                         c[i][j] = c[i-1][j];
                         b[i][j] = 2;
                     }
                     else
                         if(c[i-1][j] < c[i][j-1])
                         {
                             c[i][j] = c[i][j-1];
                             b[i][j] = 3;
                         }
                         else
                         {
                             c[i][j] = c[i][j-1]; //或者c[i][j]=c[i-1][j];
                             b[i][j] = 4;
                         }
             }
         }
         cout << "计算最优值效果图如下所示:" << endl;
         for(i = 1; i <= xlen; i++)
         {
             for(j = 1; j < ylen; j++)
             {
                 cout << c[i][j] << " ";
             }
             cout << endl;
         }
         return c[xlen][ylen];
     }

     /*功能:计算最长公共子序列
      *参数:
      *        xlen:字符串x的长度
      *        ylen:字符串y的长度
      *        x    :字符串x
      *        b:标志数组
      *        current_len:当前长度
      *        lcs_max_len:最长公共子序列长度
      *
      */
     void Display_Lcs(int i, int j, string x, int b[][Y+1],int current_len,int lcs_max_len)
     {
         if (i ==0 || j==0)
         {
             for(int s=0; s < lcs_max_len; s++)
             {
                 cout << result[s];
             }
             cout<<endl;
             count++;
             return;
         }
         if(b[i][j]== 1)
         {
             current_len--;
             result[current_len]=x[i- 1];
             Display_Lcs(i-1, j-1, x, b,current_len,lcs_max_len);
         }
         else
         {
             if(b[i][j] == 2)
             {
                 Display_Lcs(i-1, j, x, b,current_len,lcs_max_len);
             }
             else
             {
                 if(b[i][j]==3)
                 {
                     Display_Lcs(i, j-1, x, b,current_len,lcs_max_len);
                 }
                 else
                 {
                     Display_Lcs(i,j-1,x,b,current_len,lcs_max_len);
                     Display_Lcs(i-1,j,x,b,current_len,lcs_max_len);
                 }
             }
         }
     }

     int main(int argc, char* argv[])
     {
         string x = "ABCBDAB";
         string y = "BDCABA";
         int xlen = x.length();
         int ylen = y.length();

         int b[X + 1][Y + 1];

         int lcs_max_len = Lcs_Length( x, y, b, xlen,ylen );
         cout << lcs_max_len << endl;

         Display_Lcs( xlen, ylen, x, b, lcs_max_len, lcs_max_len );
         cout << "共有:" << count << "种";


         return 0;
     }

最长公共子串。

/**  
找出两个字符串的最长公共连续子串的长度 
** author :liuzhiwei   
** data   :2011-08-16 
**/   
#include "stdio.h"  
#include "string.h"  
#include "stdlib.h"  

int longest_common_substring(char *str1, char *str2)  
{  
    int i,j,k,len1,len2,max,x,y;  
    len1 = strlen(str1);  
    len2 = strlen(str2);  
    int **c = new int*[len1+1];  
    for(i = 0; i < len1+1; i++)  
        c[i] = new int[len2+1];  
    for(i = 0; i < len1+1; i++)  
        c[i][0]=0;        //第0列都初始化为0  
    for(j = 0; j < len2+1; j++)  
        c[0][j]=0;        //第0行都初始化为0   
    max = -1;  
    for(i = 1 ; i < len1+1 ; i++)  
    {  
        for(j = 1; j < len2+1; j++)  
        {  
            if(str1[i-1]==str2[j-1])     //只需要跟左上方的c[i-1][j-1]比较就可以了  
                c[i][j]=c[i-1][j-1]+1;  
            else                         //不连续的时候还要跟左边的c[i][j-1]、上边的c[i-1][j]值比较,这里不需要  
                c[i][j]=0;  
            if(c[i][j]>max)  
            {  
                max=c[i][j];  
                x=i;  
                y=j;  
            }  
        }  
    }  

    //输出公共子串  
    char s[1000];  
    k=max;  
    i=x-1,j=y-1;  
    s[k--]='\0';  
    while(i>=0 && j>=0)  
    {  
        if(str1[i]==str2[j])  
        {  
            s[k--]=str1[i];  
            i--;  
            j--;  
        }  
        else       //只要有一个不相等,就说明相等的公共字符断了,不连续了  
            break;  
    }  
    printf("最长公共子串为:");  
    puts(s);  
    for(i = 0; i < len1+1; i++)         //释放动态申请的二维数组  
        delete[] c[i];  
    delete[] c;  
    return max;  
}  
int main(void)  
{  
    char str1[1000],str2[1000];  
    printf("请输入第一个字符串:");  
    gets(str1);  
    printf("请输入第二个字符串:");  
    gets(str2);  
    int len = longest_common_substring(str1, str2);  
    printf("最长公共连续子串的长度为:%d\n",len);  
    system("pause");  
  return 0;  
}  
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值