BNUOJ 4215 最长公共连续子序列

最长公共连续子序列

Time Limit: 1000ms
Memory Limit: 65536KB
 
64-bit integer IO format:  %lld      Java class name:  Main
 

 给你两个序列S1和S2,长度分别是L1,L2 (1 <= L1 , L2 <= 180). 

 
写一个程序找出最长的连续公共子序列。
 
连续子序列定义为序列中连续的一个片段。例如序列"1 2 3"的子串有空串,"1","2","3","1 2","2 3","1 2 3"。
 

Input

第1行:两个整数L1,L2,以一个空格隔开。

第2行到第L1+1行:每行一个整数,第i+1行给出S1中的第i个数。
第L1+2行到第L1+L2+1行:每行一个整数,第L1+i+1行给出S1中的第i个数。
 

Output

 一个整数,给出S1和S2的最长连续子序列的长度

 

Sample Input

10 12
1
1
1
3
2
3
3
3
4
5
1
1
1
1
3
2
3
3
4
4
5
-8
 

Sample Output

7
 

Source

 
 1 #include <iostream>
 2 #include <cstdio>
 3 #include <cstring>
 4 #include <cstdlib>
 5 #include <vector>
 6 #include <climits>
 7 #include <algorithm>
 8 #include <cmath>
 9 #define LL long long
10 using namespace std;
11 int a[200],b[200],dp[200][200];
12 int main(){
13     int n,m,i,j,ans;
14     while(~scanf("%d %d",&n,&m)){
15        for(i = 1; i <= n; i++)
16             scanf("%d",a+i);
17         for(j = 1; j <= m; j++)
18             scanf("%d",b+j);
19         memset(dp,0,sizeof(dp));
20         for(ans = 0,i = 1; i <= n; i++){
21             for(j = 1; j <= m; j++){
22                 if(a[i] == b[j]){
23                     dp[i][j] = dp[i-1][j-1]+1;
24                 }
25                 if(dp[i][j] > ans) ans = dp[i][j];
26             }
27         }
28         printf("%d\n",ans);
29     }
30     return 0;
31 }
View Code

 

转载于:https://www.cnblogs.com/crackpotisback/p/3835730.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值