题目说明:这个题主要是求最长递增子序列的长度
相关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 }