LIS O (n ^ 2) Algorithm:
1 for(int i=1;i<=n;i++){ 2 for(int j=1;j<i;i++){ 3 if(arr[i]>arr[j]) 4 d[i]=max(d[i],d[j]+1); 5 ] 6 ]
LIS O (nlogn) algorithm:
1 #include<iostream> 2 #include<cstdio> 3 using namespace std; 4 const int maxn=1e5+5; 5 int a[maxn]; 6 int n; 7 int lis[maxn]; 8 int len=1; 9 int find(int x){ 10 int l=1,r=len,m; 11 while(l<r){ 12 m=l+(r-l)/2; 13 if(LIS [m]> A = [X]) { // Here, if the equal sign is the non-removed strictly increasing sequence of 14 R & lt = m; 15 } 16 the else { . 17 L = m + . 1 ; 18 is } . 19 } 20 is return L; 21 is } 22 is int main ( void ) { 23 is Scanf ( " % D " , & n-); 24 for ( int I = . 1 ; I <= n-; I ++) Scanf ( " % D " , & A [I]); 25 lis[1]=a[1]; 26 for(int i=2;i<=n;i++){ 27 if(a[i]>lis[len]){ 28 lis[++len]=a[i]; 29 } 30 else{ 31 int pos=find(i); 32 lis[pos]=a[i]; 33 } 34 } 35 printf("%d",len); 36 return 0; 37 }
LCS O (n ^ 2) Algorithm:
1 for(int i=1;i<=n;i++){ 2 for(int j=1;j<=n;j++){ 3 if(a[i]==b[i]) 4 d[i][j]=max(d[i]j],d[i-1][j-1]); 5 else 6 d[i][j]=max(d[i-1][j],d[i][j-1]); 7 } 8 }
LCS O (nlogn) algorithm:
Essence nlogn longest common subsequence algorithm is converted to the problem by the longest sequence (LIS), as can nlogn LIS implemented, so LCS seeking to reduce the time complexity nlogn.
Suppose there are two sequences s1 [1 ~ 6] = {a, b, c, a, d, c}, s2 [1 ~ 7] = {c, a, b, e, d, a, b}.
S1 recording position of each element appearing in s2, then the position in descending order, the above example can be expressed as:
loc( a)= { 6, 2 }, loc( b ) = { 7, 3 }, loc( c ) = { 1 }, loc( d ) = { 5 }。
S1 to the position of each element in the order s1 elements are arranged in a sequence s3 = {6, 2, 7, 3, 1, 6, 2, 5, 1}.
In seeking to s3 LIS resulting value is the LCS seeking answers.
(Nlogn and the character codes from https://www.cnblogs.com/KYSpring/p/9021909.html )
1 #include<iostream> 2 #include<cstdio> 3 #include<cstdlib> 4 using namespace std; 5 const int maxn=1e6+5; 6 int n,len=0; 7 int lis[maxn]; 8 int a[maxn]; 9 int b[maxn]; 10 int loc[maxn]; 11 int find(int x){ 12 int l=1,r=len,m; 13 while(l<R & lt) { 14 m = L + (rl is an) / 2 ; 15 // IF (LIS [m]> = B [X]) { // retarded error, looking for so long. . 16 IF (LIS [m]> = X) { . 17 R & lt = m; 18 is } . 19 the else L = m + . 1 ; 20 is } 21 is return L; 22 is } 23 is int main ( void ) { 24 Scanf ( " % D " , & n-); 25 for ( int I = . 1;i<=n;i++)scanf("%d",&a[i]); 26 for(int i=1;i<=n;i++){ 27 scanf("%d",&b[i]); 28 loc[b[i]]=i; 29 } 30 for(int i=1;i<=n;i++){ 31 b[i]=loc[a[i]]; 32 } 33 // for(int i=1;i<=n;i++)printf("%d",b[i]) ;// 34 // printf("\n"); 35 if(n!=0)lis[++len]=b[1]; 36 for(int i=2;i<=n;i++){ 37 if(b[i]>lis[len]){ 38 lis[++len]=b[i]; 39 } 40 else{ 41 int pos=find(b[i]); 42 lis[pos]=b[i]; 43 } 44 } 45 printf("%d",len); 46 return 0; 47 }
save route:
1 #include<stdio.h> 2 #include<string.h> 3 #include<stack> 4 #include<algorithm> 5 using namespace std; 6 #define N 1010 7 int dp[N][N]; 8 char c; 9 int main() 10 { 11 char a[N]; 12 char b[N]; 13 scanf("%s%s",a,b); 14 int la=strlen(a); 15 int lb=strlen(b); 16 memset(dp,0,sizeof(dp)); 17 for(int i=1; i<=la; i++) 18 { 19 for(int j=1; j<=lb; j++) 20 { 21 if(a[i-1]==b[j-1]) 22 dp[i][j]=dp[i-1][j-1]+1; 23 else 24 dp[i][j]=max(dp[i-1] [J], DP [I] [J- . 1 ]); 25 } 26 is } 27 int I = La, J = LB; 28 Stack < char > S; 29 the while (DP [I] [J]) 30 { 31 is IF (DP [I] [J] == DP [I- . 1 ] [J]) /// from leftward 32 { 33 is i-- ; 34 is } 35 the else IF (DP [I] [J] = DP = [I] [J- . 1 ]) /// from a direction 36 { 37 J, ; 38 is } 39 the else IF (DP [I] [J]> DP [I- . 1 ] [J- . 1 ]) /// from the upper left 40 { 41 is i-- ; 42 is J, ; 43 is s.push (A [I]); 44 is } 45 } 46 is the while (! s.empty ()) 47 { 48 C = s.top (); 49 the printf ( " % C " , C); 50 s.pop (); 51 } 52 return 0; 53 }
Public longest string:
1) Violence:
1 int longestSubstring(string x, string y) { 2 int max = 0; 3 string str = ""; 4 for (int i = 0; i < x.size(); i++) { 5 int len = 0; 6 int j = i; 7 while (j < x.size()) { 8 str += x[j]; 9 if (y.find(str) == y.npos) break; 10 len++; 11 j++; 12 if (len > max) max = len; 13 } 14 str = ""; 15 } 16 return max; 17 }
2) Dynamic Programming:
1 int longestSubstring(string x, string y) { 2 vector<vector<int> > f(x.size() + 1, vector<int>(y.size() + 1, 0)); 3 int max = -1; 4 for (int i = 1; i <= x.size(); i++) { 5 for (int j = 1; j <= y.size(); j++) { 6 if (x[i - 1] != y[j - 1]) f[i][j] = 0; 7 else if (x[i - 1] == y[j - 1]) f[i][j] = f[i - 1][j - 1] + 1; 8 if (max < f[i][j]) { 9 max = f[i][j]; 10 } 11 } 12 } 13 return max; 14 }