当前位置:网站首页>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;
}
边栏推荐
- Simple greedy strategy
- Spread your wings and soar
- RT thread console device initialization
- Time display of the 12th Blue Bridge Cup
- C language learning log 1.2
- Section 2 - branch and loop statements
- Stepping on a horse (one stroke)
- Wang Dao Chapter II linear table exercises
- Force deduction 121 questions
- System file interface open
猜你喜欢

Configuration used by automatic teaching evaluation script

C language learning log 11.7

Case - random numbers without repetition (HashSet and TreeSet)

File descriptorfile description

Case - the ArrayList collection stores student objects and traverses them in three ways

Mind mapping series - Database

Section 2 - branch and loop statements

RTSP streaming using easydarwin+ffmpeg

Luogu p1036 number selection

Kaggle time series tutorial
随机推荐
OJ problem solution summary
Case - grade sorting - TreeSet set storage
OpenCV中的saturate操作(饱和操作)究竟是怎么回事
Modification and analysis of libcoap source code by Hongmeng device discovery module
17.6 unique_ Lock details
Explain the opencv function cv:: add() in detail, and attach sample code and running results of various cases
Case - random numbers without repetition (HashSet and TreeSet)
Force buckle 92 Reverse linked list II
C language exercise 1
Introduction to QT XML
Elliptic curve encryption
C language learning log 1.22
PostgreSQL Guide: Insider exploration (Chapter 7 heap tuples and index only scanning) - Notes
Mysql8.0.13 installation tutorial (with pictures)
BM1Z002FJ-EVK-001开机测评
Chapter 15 mechanism: Address Translation
【转载】C语言内存和字符操作函数大全
Violence enumeration~
String()和toString()方法得区别
Interpretation of QT keypressevent