LCS源代码(C语言)
来源:互联网 发布:php会员权限 编辑:程序博客网 时间:2024/05/11 22:36
LCS算法原理参见:http://blog.csdn.net/v_july_v/article/details/6695482
#include <stdlib.h>#include <stdio.h>#include <string.h>#define LEFT 2#define UP 3#define LR4int LCS(char *str1, char *str2, int len1, int len2, char *b, char *c){if(NULL == str1 || NULL == str2 || NULL == b || NULL == c){return -1;}int i, j;for(i = 0; i <= len1; i++){*(b + len1 * i) = 0; //b[i][j] = *(*b + i * len1+ j)*(c + len1 * i) = 0;}for(j = 0; j <= len2; j++){*(b + j) = 0;*(c + j) = 0;}for(i = 1; i <= len1; i++){for(j = 1; j <= len2; j++){if(str1[i-1] == str2[j-1]){*(c + i * len1+ j) = *(c + (i - 1) * len1 + j - 1) + 1;*(b + i * len1+ j) = LR;}else{if(*(c + (i - 1) * len1 + j) >= *(c + i * len1 + j - 1)){*(c + i * len1 + j) = *(c + (i - 1) * len1 + j);*(b + i * len1 + j) = UP;}else{*(c + i * len1 + j) = *(c + i * len1 + j - 1);*(b + i * len1 + j) = LEFT;}}}}return 0;}int PrintLCS(char *b, char *str1, int len1, int len2){if(0 == len1 || 0 == len2 || NULL == b || NULL == str1){return -1; }if(*(b + len1 * 7 + len2) == LR){PrintLCS(b, str1, len1 - 1, len2 - 1);printf("%c ", str1[len1 - 1]);}else{ if(*(b + len1 * 7 + len2) == UP){PrintLCS(b, str1, len1 - 1, len2); }else if(*(b + len1 * 7 + len2) == LEFT){PrintLCS(b, str1, len1, len2 - 1); }}return 0;}int main(void){char *X = "ABCBDAB";char *Y = "BDCABA";int len1 = strlen(X);int len2 = strlen(Y);char b[len1+1][len2+1], c[len1+1][len2+1]; int i,j; LCS(X,Y,len1,len2,*b,*c); for(i = 0; i <= len1; i++){ for(j = 0; j <= len2; j++){ printf("%d ", c[i][j]); } printf("\n"); } printf("\n"); for(i = 0; i <= len1; i++){ for(j = 0; j <= len2; j++){ if(b[i][j] == LR) printf("* "); else if(b[i][j] == LEFT) printf("< "); else if(b[i][j] == UP) printf("^ "); else printf(" "); } printf("\n"); } printf("\n");PrintLCS(*b,X,len1,len2);return 0;}
0 0
- LCS源代码(C语言)
- LCS C语言源码
- C语言病毒源代码
- C语言库函数源代码
- C语言小游戏源代码
- Ping 源代码-C语言
- 俄罗斯方块源代码 C语言
- C语言通讯录源代码
- MD5之C语言源代码
- strcpy ( )【C语言库函数源代码】
- strlen ( )【C语言库函数源代码】
- strcat ( )【C语言库函数源代码】
- strncpy ( )【C语言库函数源代码】
- strncat ( )【C语言库函数源代码】
- strcmp ( )【C语言库函数源代码】
- strncmp ( )【C语言库函数源代码】
- stricmp ( )【C语言库函数源代码】
- strnicmp ( )【C语言库函数源代码】
- Canny 边缘检测(转)
- string CString wstring 之间的转化
- CONFIG_LOG_BUF_SHIFT的默认值
- Keil MDK : 标准IO函数重定向相关
- fds
- LCS源代码(C语言)
- liferay 调用ajax
- BocK的使用
- String.format函数使用方法介绍
- android源码分析(一) - 语言切换机制
- 【D3.JS数据可视化实战记录】绘制动态状态变化趋势图
- Chrome内核解析 -- 背景篇:布局
- sublime text 插件的删除方法
- leetcode之Min Stack