lab3

题目说明:这个题主要是求最长递增子序列的长度

相关AC代码如下(0.01sAC):

 1 // Problem#: 17991
 2 // Submission#: 4642839
 3 // The source code is licensed under Creative Commons Attribution-NonCommercial-ShareAlike 3.0 Unported License
 4 // URI: http://creativecommons.org/licenses/by-nc-sa/3.0/
 5 // All Copyright reserved by Informatic Lab of Sun Yat-sen University
 6 #include<cmath>
 7 #include<cstdio>
 8 #include<cstring>
 9 #include<iostream>
10 #include<algorithm>
11 
12 using namespace std;
13 typedef long long ll;
14 
15 const int N = 50005;
16 ll a[N],dp[N];
17 
18 int BinSearch(int x,int len)
19 {
20     int i=1,j=len;
21     while(i<=j)
22     {
23         int mid=(i+j)>>1;
24         if(dp[mid]<x) i=mid+1;
25         else j=mid-1;
26     }
27     return i;
28 }
29 int main()
30 {
31     int n;
32 
33     while(cin>>n)
34     {
35         for(int i=1;i<=n;++i)
36             cin>>a[i];
37         int len=1;
38 
39         int pos;
40         dp[1]=a[1];
41         for(int i=2;i<=n;++i)
42         {
43             if(a[i]<dp[1])
44                 dp[1]=a[i];
45             else if(a[i]>dp[len])
46                 dp[++len]=a[i];
47             else{
48                 pos=BinSearch(a[i],len);
49                 dp[pos]=a[i];
50             }
51         }
52         cout<<len<<' '<<dp[len]<<endl;
53     }
54     return 0;
55 }                                 

题目说明:本题是求两个字符序列的最大公共子序列

 1 // Problem#: 17992
 2 // Submission#: 4642902
 3 // The source code is licensed under Creative Commons Attribution-NonCommercial-ShareAlike 3.0 Unported License
 4 // URI: http://creativecommons.org/licenses/by-nc-sa/3.0/
 5 // All Copyright reserved by Informatic Lab of Sun Yat-sen University
 6 #include <iostream>
 7 #include <string>
 8 #include <vector>
 9 
10 using namespace std;
11 
12 int main()
13 {
14     string s1, s2;
15     vector<int> len;
16     int i, j, b, tmp;
17 
18     while ( cin >> s1 >> s2 ) {
19         len.clear();
20         for ( j = 0; j < s2.length(); j++ ) {
21             len.push_back( 0 );
22         }
23         for ( i = 0; i < s1.length(); i++ ) {
24             b = 0;
25             for ( j = 0; j < s2.length(); j++ ) {
26                 if ( s1[i] == s2[j] ) {
27                     tmp = len[j];
28                     len[j] = b + 1;
29                     b = tmp;
30                 }
31                 else {
32                     tmp = len[j];
33                     if ( j - 1 >= 0 && len[j] < len[j - 1] ) {
34                             len[j] = len[j - 1];
35                     }
36                     b = tmp;
37                 }
38             }
39         }
40 
41         cout << len[j - 1] << endl;
42     }
43 
44     return 0;
45 }                                 

 

转载于:https://www.cnblogs.com/caizhk/articles/5388374.html

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值