您的位置:首页 > 产品设计 > UI/UE

C - Common Subsequence E - 编辑距离 D - 最长公共子序列Lcs(dp经典!!)

2017-08-09 20:03 477 查看
A subsequence of a given sequence is the given sequence with some elements (possible none) left out. Given a sequence X = <x1, x2, ..., xm> another sequence Z = <z1, z2, ..., zk> is a subsequence of X if there exists a strictly increasing sequence <i1, i2, ..., ik> of indices of X such that for all j = 1,2,...,k, xij = zj. For example, Z = <a, b, f, c> is a subsequence of X = <a, b, c, f, b, c> with index sequence <1, 2, 4, 6>. Given two sequences X and Y the problem is to find the length of the maximum-length common subsequence of X and Y.
The program input is from a text file. Each data set in the file contains two strings representing the given sequences. The sequences are separated by any number of white spaces. The input data are correct. For each set of data the program prints on the standard output the length of the maximum-length common subsequence from the beginning of a separate line.


Input

abcfbc abfcab
programming contest
abcd mnp


Output

4
2
0


(那么长意思就是求两个序列的最长公共子序列咯,套套模板,复制!!是下面两题的基础,可以说三题层层递进!这题出的好啊!!)

二!!!

编辑距离,又称Levenshtein距离(也叫做Edit Distance),是指两个字串之间,由一个转成另一个所需的最少编辑操作次数。许可的编辑操作包括将一个字符替换成另一个字符,插入一个字符,删除一个字符。

例如将kitten一字转成sitting:

sitten (k->s)

sittin (e->i)

sitting (->g)

所以kitten和sitting的编辑距离是3。俄罗斯科学家Vladimir Levenshtein在1965年提出这个概念。

给出两个字符串a,b,求a和b的编辑距离。

Input

第1行:字符串a(a的长度 <= 1000)。

第2行:字符串b(b的长度 <= 1000)。

Output

输出a和b的编辑距离

Sample Input

kitten
sitting


Sample Output

3


代码:

#include<stdio.h>
#include<string.h>
#include<iostream>
#include<algorithm>
using namespace std;
int dp[1010][1010];
char a[1010],b[1010];
int main()
{
int lena,lenb,length,same;
scanf("%s",a);
scanf("%s",b);
lena=strlen(a);
lenb=strlen(b);
for(int i = 0;i <= lena;i++)
dp[i][0] = i;//初始化为相应的数字,和求最大公共子序列的长度有点不同!!!
for(int i = 0;i <= lenb;i++)
dp[0][i] = i;//初始化为相应的数字,和求最大公共子序列的长度有点不同!!!
for(int i= 1;i <= lena;i++){
for (int j = 1;j <= lenb;j++){
dp[i][j]=min(dp[i-1][j],dp[i][j-1])+1;
if (a[i-1] == b[j-1]){
dp[i][j] = min(dp[i-1][j-1],dp[i][j]);
}
else{
dp[i][j] = min(dp[i-1][j-1]+1,dp[i][j]);
}
}
}
printf("%d\n",dp[lena][lenb]);
}

/*for(int i=0;i<=n;i++){
for(int j=0;j<=m;j++){
printf("%d ",dp[i][j]);
}
}
printf("\n");*/用来输出对应矩阵,可用来检测。


三!!!!!(两题类似)

给出两个字符串A B,求A与B的最长公共子序列(子序列不要求是连续的)。

比如两个串为:

abcicba
abdkscab

ab是两个串的子序列,abc也是,abca也是,其中abca是这两个字符串最长的子序列。


Input

第1行:字符串A

第2行:字符串B

(A,B的长度 <= 1000)

Output

输出最长的子序列,如果有多个,随意输出1个。

Sample Input

abcicba
abdkscab


Sample Output

abca


这道题其实最难得是输出这个序列,下面说下做法,我们根据之前的出来的dp值来一步一步得出这个序列。从两个字符串最后开始,为什么呢,因为这样才不会漏掉某一个解,然后还是根据是否相等来判断,当相等的时候取这个字符,如果不相等的时候,我们要移动下标,那么根据什么来移动呢,还是根据dp值来移动,如果dp[i-1][j]大于dp[i][j-1]那么明显是i–,否则j–,很容易理解的,最后倒叙输出就行了

#include<stdio.h>
#include<string.h>
#include<iostream>
#include<algorithm>
using namespace std;
int dp[1010][1010];
char a[1010],b[1010],c[1010];
int main()
{
int lena,lenb,maxn;
scanf("%s",a);
scanf("%s",b);
lena=strlen(a);
lenb=strlen(b);
for(int i = 0;i <= lena;i++)
dp[i][0] = 0;//(与上一题此处有差别!!)
for(int i = 0;i <= lenb;i++)
dp[0][i] = 0;//(与上一题此处有差别!!)
for(int i= 1;i <= lena;i++){
for (int j = 1;j <= lenb;j++){
if (a[i-1] == b[j-1]){
dp[i][j] = dp[i-1][j-1] + 1;
}
else{
dp[i][j] = max(dp[i-1][j],dp[i][j-1]);
}
}
}
/*for(int i=0;i<=lena;i++)
{
for(int j=0;j<=lenb;j++)
{
printf("%d ",dp[i][j]);
}
printf("\n");
}*/**输出矩阵,检测用!!**
int k=0;
for(int i=lena;i>=0;){
for(int j=lenb;j>=0;){
if(a[i-1]==b[j-1]){
c[k]=a[i-1];
i--;
j--;
k++;
}
else
{
if(dp[i-1][j]>dp[i][j-1])
i--;
else
j--;

}
if(i<1||j<1){
i=-1;
j=-1;
break;
}
}
}
for(int i=k-1;i>=0;i--){
printf("%c",c[i]);
}
printf("\n");
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐