当前位置:网站首页>Dynamic programming - longest common substring
Dynamic programming - longest common substring
2022-06-13 05:09:00 【laven_ li】
# include <stdio.h>
# include <windows.h>
# define MAX(a, b) ((a) > (b) ? (a) : (b))
int LCSubstring1(char * str1, char * str2);
int LCSubstring2(char * str1, char * str2);
int LCSubstring3(char * str1, char * str2);
int main(void){
char * str1 = "bcda";
char * str2 = "abcde";
printf("%d\n", LCSubstring1(str1, str2));
printf("%d\n", LCSubstring2(str1, str2));
printf("%d\n", LCSubstring3(str1, str2));
system("pause");
return 0;
}
int LCSubstring1(char * str1, char * str2){
if(NULL == str1 || NULL == str2)
return 0;
int n = strlen(str1);
int m = strlen(str2);
int ** dp = (int**)calloc((n+1), sizeof(int*));
for(int i = 0; i <= n; i++){
dp[i] = (int*)calloc((m+1), sizeof(int));
}
//dp[j][k] For each j And the k The length of the longest common substring of two strings ending in characters
int max = 0;
for(int j = 1; j <= n; j++){
for(int k = 1; k <= m; k++){
if(str1[j-1] == str2[k-1]){
dp[j][k] = dp[j-1][k-1] + 1;
}
max = MAX(max, dp[j][k]);
}
}
return max;
}
int LCSubstring2(char * str1, char * str2){
if(NULL == str1 || NULL == str2)
return 0;
int n = strlen(str1);
int m = strlen(str2);
int * dp = (int*)calloc((m+1), sizeof(int));
//dp[k] For the first and the second respectively k The length of the longest common substring of two strings ending in characters
int max = 0;
for(int j = 1; j <= n; j++){
int temp = 0;
for(int k = 1; k <= m; k++){
int Topleft = temp;
temp = dp[k];
if(str1[j-1] == str2[k-1]){
dp[k] = Topleft + 1;
}
max = MAX(max, dp[k]);
}
}
return max;
}
int LCSubstring3(char * str1, char * str2){
if(NULL == str1 || NULL == str2)
return 0;
char * rows = str1;
char * cols = str2;
if(strlen(str1) < strlen(str2)){
rows = str2;
cols = str1;
}
int row = strlen(rows);
int col = strlen(cols);
int * dp = (int*)calloc((col+1), sizeof(int));
int max = 0;
for(int j = 1; j <= row; j++){
int temp = 0;
for(int k = 1; k <= col; k++){
int topleft = temp;
temp = dp[k];
if(rows[j-1] == cols[k-1]){
dp[k] = topleft + 1;
}
max = MAX(max, dp[k]);
}
}
return max;
}
边栏推荐
- QT brushes and brushes
- Listiterator list iterator
- The problem of flex layout adaptive failure
- String()和toString()方法得区别
- 语音信号分帧的理解
- Clause 33: decltype is used for auto & type formal parameters, with std:: forward
- Section 7 - structures
- System file interface open
- Hidden implementation and decoupling, knowing Pimpl mode
- QT signal is automatically associated with the slot
猜你喜欢
随机推荐
LeetCode第297场周赛(20220612)
UNO
String()和toString()方法得区别
Clause 47: please use traits classes to represent type information
QT direction key to move focus
Flex布局自适应失效的问题
Hidden implementation and decoupling, knowing Pimpl mode
Metaltc4.0 stable release
Common skills in embedded programming
Chapter 13 abstraction: address space
Force buckle 25 A group of K flipped linked lists
Understanding inode
Enhanced for loop
Regular expressions in QT
Draw a hammer
PostgreSQL Guide: inside exploration (Chapter 10 basic backup and point in time recovery) - Notes
Section 3 - functions
C language learning log 10.11
System file interface open
[thread / multithread] execution sequence of threads









