“最长公共字符串子序列”问题的动态规划法算法
2008-01-03 09:43
387 查看
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif)
/**//*
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
标题:<<系统设计师>>应试编程实例-[动态规划算法程序设计]
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
作者:成晓旭
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
时间:2002年09月15日(18:20:00-21:25:00)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
实现“最长公共字符串子序列”问题的动态规划算法实现函数
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
时间:2002年09月15日(21:31:00-22:00:00)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
实现“最长公共字符串子序列”问题的动态规划算法实现函数
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif)
*/
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
#include "stdio.h"
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
#include "stdlib.h"
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
#include "string.h"
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
#define MAXN 64 //全局最大值常量
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
//:=========================“最长公共字符串子序列”问题的动态规划法算法=========================
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
//计算两个字符串序列的最长公共子序列的长度函数
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
int First_Born_SubStr_Len(char *a,char *b,int substrlen[][MAXN])
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
int i,j,m = strlen(a),n = strlen(b);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
for(i = 0;i <= m;i++)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
substrlen[i][0] = 0;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
for(j = 1;j <= m;j++)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
substrlen[0][j] = 0;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
for(i = 1;i <= m;i++)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
for(j = 1;j <= n;j++)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
if(a[i - 1] == b[j - 1])
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
substrlen[i][j] = substrlen[i - 1][j - 1] + 1;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
else if(substrlen[i - 1][j] >= substrlen[i][j - 1])
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
substrlen[i][j] = substrlen[i - 1][j];
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
else
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
substrlen[i][j] = substrlen[i][j - 1];
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
return(substrlen[m]
);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
//构造最长公共子序列函数
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
char *Build_First_Born_SubStr(char *a,char *b,char str[])
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
int k,i = strlen(a),j = strlen(b),array[MAXN][MAXN];
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
k = First_Born_SubStr_Len(a,b,array);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
str[k] = '';
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
while(k > 0)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
if(array[i][j] == array[i - 1][j])
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
i --;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
else if(array[i][j] == array[i][j - 1])
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
j --;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
else
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
str[--k] = a[i - 1];
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
i --;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
j --;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
return(str);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
//测试“最长公共字符串子序列”问题的动态规划法函数
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
void Run_SubString()
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
char str0[MAXN],str1[MAXN],str2[MAXN];
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
printf("输入第一个字符串(长度 < %d): ",MAXN);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
scanf("%s",&str1);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
printf("输入第二个字符串(长度 < %d): ",MAXN);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
scanf("%s",&str2);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
printf("其“最长公共字符串子序列” = %s ",Build_First_Born_SubStr(str1,str2,str0));
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
//:=========================“最长公共字符串子序列”问题的动态规划法算法=========================
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
int main(int argc, char* argv[])
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
//Run_SubString();
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
Run_Ikebana();
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
printf(" 应用程序运行结束! ");
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
return 0;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
Trackback: http://tb.blog.csdn.net/TrackBack.aspx?PostId=935869
相关文章推荐
- “最长公共字符串子序列”问题的动态规划法算法
- “最长公共字符串子序列”问题的动态规划法算法
- “最长公共字符串子序列”问题的动态规划法算法
- “最长公共字符串子序列”问题的动态规划法算法
- “最长公共字符串子序列”问题的动态规划法算法
- “最长公共字符串子序列”问题的动态规划法算法
- “最长公共字符串子序列”问题的动态规划法算法
- “最长公共字符串子序列”问题的动态规划法算法
- “最长公共字符串子序列”问题的动态规划法算法
- “最长公共字符串子序列”问题的动态规划法算法
- “最长公共字符串子序列”问题的动态规划法算法
- “最长公共字符串子序列”问题的动态规划法算法
- “最长公共字符串子序列”问题的动态规划法算法
- “最长公共字符串子序列”问题的动态规划法算法
- 一个看似简单却复杂的问题:求两个字符串的 左向右匹配 所有的 最长连续的 公共子字符串( 在每个字符串中先后次序相同的) 序列
- 最长公共子串(子序列)、最长递增子序列、最长回文子串等问题
- HDU1257 最长递增公共子序列问题
- 最长公共子串、最长公共子序列、最长回文子串、模式匹配、最大子序列--字符串问题整理
- 最长公共子序列,最长公共子串,最长递增子序列的问题~
- 最大连续子序列和,乘积,最长递增子串,最长公共子串,子序列等问题(动态规划等)