求两个字符串最长公共子串(动态规划)
来源:互联网 发布:office2016破解软件 编辑:程序博客网 时间:2024/05/01 09:32
code如下:
//Longest common sequence, dynamic programming methodvoid FindLCS(char *str1, char *str2){if(str1 == NULL || str2 == NULL)return;int length1 = strlen(str1)+1;int length2 = strlen(str2)+1;int **csLength,**direction;//two arrays to record the length and directionint i,j,maxLength=0,maxI=0,maxJ=0;csLength = (int **)new int[length2];for(i=0; i<length1; i++)csLength[i] = (int *)new int[length1];for(i=0;i<length2;i++)for(j=0;j<length1;j++)csLength[i][j] = 0;direction = (int **)new int[length2];for(i=0; i<length1; i++)direction[i] = (int *)new int[length1];for(i=0;i<length2;i++)for(j=0;j<length1;j++)direction[i][j] = 0;for(i=1;i<length2;i++)for(j=1;j<length1;j++){if(str2[i-1] == str1[j-1]){csLength[i][j] = csLength[i-1][j-1] + 1;direction[i][j] = 3;//3 means leftup}else if(csLength[i-1][j] > csLength[i][j-1]){csLength[i][j] = csLength[i-1][j];direction[i][j] = 1;//1 means left}else{csLength[i][j] = csLength[i][j-1];direction[i][j] = 2;//2 means up}if(maxLength < csLength[i][j]){maxLength = csLength[i][j];//record th max length and the corresponding indexmaxI = i;maxJ = j;}}i = maxI;j = maxJ;//the output is in reverse orderwhile(i!=0 && j!= 0){if( str2[i-1] == str1[j-1])cout<<str2[i-1]<<" ";if(direction[i][j] == 3){i--;j--;}else if(direction[i][j] == 1){i--;}else if(direction[i][j] == 2){j--;}}}
0 0
- 求两个字符串最长公共子串(动态规划)
- 求两个字符串的最长公共子序列的长度(动态规划)
- 求两个字符串的最长公共子序列的长度(动态规划)
- 动态规划之寻找两个字符串最长公共子串
- 【动态规划】求最长公共子串
- 动态规划求最长公共子串
- 求两个字符串最长公共子串
- 最长公共子字符串(动态规划)
- 动态规划法求两个字符串的最长公共子序列
- 动态规划求两个字符串的连续最长公共子序列
- 利用后缀数组求字符串的最长重复子串的算法 利用二维数组求两个字符串的最长公共字串(动态规划)
- 求两个字符串最长公共子串LCS(其它)
- 最长公共子串(动态规划)
- 最长公共子串(动态规划)
- 最长公共子串(动态规划)
- 动态规划求最长公共子序列
- 【动态规划】求最长公共子序列
- 动态规划:求最长公共子序列
- 大型互联网站解决高并发的常见策略
- Ubuntu下的配置文件
- 攻击工具SYN Flood的源代码
- Android 下载APK 安装APK 打开APK
- 时分多路复用(TDM)
- 求两个字符串最长公共子串(动态规划)
- ckermit 使用xcom 协议发送文件
- android取消开机时候的震动
- ubuntu安装nfs中的一些问题总结
- sscanf的基本用法
- 高并发量网站解决方案
- 关注C++细节——字面值初始化字符数组及字符串拷贝注意
- ListviewFilter 对A-Z排序 过滤 搜索功能(为github高手所做)
- Intellij IDEA 配置Tomact