poj 3356 文本编辑距离
来源:互联网 发布:php 高级算法 编辑:程序博客网 时间:2024/05/17 02:17
Problem Description:
题目大意就是给出了两个字符串x,y.并且规定可以通过delete, insert, change 三种操作修改字符串,问最少经过多少步操作可以将串x 修改成y.
设dp[i][j]表示x串前i位和y串前j位需要的最少操作数。
分三种情况:
situation 1: dp[i][j]=dp[i-1][j-1]+(x[i]==y[j]?0:1) 若是最后两个串的最后一个元素相等,则不需操作,否则使用一步change;
situation 2: dp[i][j]=dp[i-1][j]+1. 若是x串的前(i-1)位和y串的前j位相等,则使用一步delete, 删除y[j];
situation 3: dp[i][j]=dp[i][j-1]+1.若是x串的前i位和y串的前(j-1)位相等,则使用一步insert, 删除x[i];
最终dp[i][j] = min(situation 1,situation 2,situation 3 ).
Code:
#include<iostream>
#include<cstdio>
using namespace std;
#define MAXN 1005
char x[MAXN];
char y[MAXN];
int dp[MAXN][MAXN];
int min(int a, int b,int c)
{
int temp = a<b?a:b;
return c<temp?c:temp;
}
int main()
{
int lengthx,lengthy;
while(cin>>lengthy>>y)
{
cin>>lengthx>>x;
dp[0][0] = 0;
for(int i=1;i<=lengthx;i++) dp[i][0]=i;
for(int j=1;j<=lengthy;j++) dp[0][j]=j;
for(int i=1;i<=lengthx;i++)
for(int j=1;j<=lengthy;j++) dp[i][j]=min(dp[i-1][j]+1,dp[i][j-1]+1,dp[i-1][j-1]+(x[i-1]==y[j-1]?0:1));
cout<<dp[lengthx][lengthy]<<endl;
}
return 0;
}
- poj 3356 文本编辑距离
- 文本编辑距离
- 文本编辑距离的计算
- scala文本编辑距离算法实现
- 文本编辑距离计算,简单清晰
- 文本对比。文本编辑距离算法
- scala文本编辑距离算法实现
- poj 3356 求最短编辑距离
- POJ 3356(编辑距离)
- poj 3356 AGTC 【编辑距离】
- POJ 3356 【字符串编辑距离】
- POJ-3356 AGTC(编辑距离)
- 文本编辑
- POJ-3356(编辑距离)(AGTC)
- [算法] poj 3356 字符串的距离 AGTC
- poj 3356 编辑距离(空间优化)
- poj 3356 AGTC(dp,最小编辑距离)
- POJ 3356 AGTC (编辑距离 DP)
- Rotate List
- lamp架构源码安装
- 手机第三方支付产品安全性
- 【Leetcode】Find Minimum in Rotated Sorted Array II
- Remove Nth Node From End of List
- poj 3356 文本编辑距离
- Maximum Product Subarray
- [Leetcode] Search in Rotated Sorted Array
- PostgreSQL的特性
- USACO Picture 解题报告
- 2014年的双11背后会隐藏什么猫腻?
- 双11背后隐藏的猫腻:商品先提价再做降价促销
- 2014年的双11背后会隐藏什么猫腻?
- 饭店的顶顶顶顶顶顶顶顶顶顶顶顶顶顶顶顶顶顶顶顶顶顶顶顶顶顶顶顶顶顶顶顶顶顶顶