当前位置:网站首页>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;
}
边栏推荐
- Advanced C - Section 2 - pointers
- BM1Z002FJ-EVK-001开机测评
- Clause 32: moving objects into closures using initialization capture objects
- C language learning log 1.24
- Luogu p3654 fisrt step
- Pycharm错误解决:Process finished with exit code -1073741819 (0xC0000005)
- Clause 33: decltype is used for auto & type formal parameters, with std:: forward
- Simple-SR:Best-Buddy GANs for Highly Detailed Image Super-Resolution論文淺析
- Std:: map insert details
- 关于匿名内部类
猜你喜欢

Explain the opencv function cv:: add() in detail, and attach sample code and running results of various cases

Section 6 - pointers

Case - the list set stores student objects and traverses them in three ways

Sampo Lock

RMQ、LCA

Logical point

Section 7 - structures

Metartc4.0 integrated ffmpeg compilation

Article 29: assuming that the mobile operation does not exist, is expensive, and is not used

metaRTC4.0稳定版发布
随机推荐
Small project - household income and expenditure software (1)
C language learning log 12.5
Chapter 2 process management
Article 29: assuming that the mobile operation does not exist, is expensive, and is not used
[reprint] complete collection of C language memory and character operation functions
Logical point
Ruoyi cloud startup tutorial (hand-held graphics)
Simple SR: best buddy Gans for highly detailed image super resolution
Std:: map insert details
All blog navigation
LeetCode第297场周赛(20220612)
RMQ、LCA
OpenCV中的saturate操作(饱和操作)究竟是怎么回事
17.6 unique_lock详解
Luogu p1036 number selection
C language learning log 10.4
COAP protocol libcoap API
【多线程编程】Future接口获取线程执行结果数据
Enhanced for loop
Section 5 - Operator details