Let x and y be two strings over some finite alphabet A. We would like to transform x into y allowing only operations given below:

  • Deletion: a letter in x is missing in y at a corresponding position.
  • Insertion: a letter in y is missing in x at a corresponding position.
  • Change: letters at corresponding positions are distinct

Certainly, we would like to minimize the number of all possible operations.

Illustration

A G T A A G T * A G G C

| | | | | | |

A G T * C * T G A C G C

Deletion: * in the bottom line
Insertion: * in the top line
Change: when the letters at the top and bottom are distinct

This tells us that to transform x = AGTCTGACGC into y = AGTAAGTAGGC we would be required to perform 5 operations (2 changes, 2 deletions and 1 insertion). If we want to minimize the number operations, we should do it like

A  G  T  A  A  G  T  A  G  G  C

| | | | | | |

A G T C T G * A C G C

and 4 moves would be required (3 changes and 1 deletion).

In this problem we would always consider strings x and y to be fixed, such that the number of letters in x is m and the number of letters in y is n where n ≥ m.

Assign 1 as the cost of an operation performed. Otherwise, assign 0 if there is no operation performed.

Write a program that would minimize the number of possible operations to transform any string x into a string y.

Input

The input consists of the strings x and y prefixed by their respective lengths, which are within 1000.

Output

An integer representing the minimum number of possible operations to transform any string x into a string y.

Sample Input

10 AGTCTGACGC
11 AGTAAGTAGGC

Sample Output

4

题意:
每组数据给出两个字符串x、y,通过删除或添加或替换三种操作把两个字符串变得相同。
求最少操作次数。 思路:
可以先定义一个dp[n+20][m+20]的二维数组,要明白题目求的是什么,根据需要去写题,而且要时刻记住题目求的是什么,自己定义的东西的具体含义到底是什么。
dp[i][j]代表,从长度为i的a数组到长度为j的b数组最少变换次数。
经过分析,可以得出来一个思想——同化思想,删除和添加可以看作是一种操作,比如a串可以通过添加'c'这个字符变得和b串一样,那也就说明b数组可以通过删除操作,删除'c'变得和a
串一样,所以可以说明通过添加操作和删除操作所需操作次数是确定的,
所以题目可以转换成,通过添加或者替换操作,最少把a、b串变的相同需要多少步。 注意:
a[i]!=b[i],是逻辑表达式,只有两个返回值,满足条件返回值为1,不满足返回为0;
if(1-1),是属于算数表达式,注意区分。 碰到题目一定要去好好思考,好好思考,好好思考。
 #include<string.h>
#include<stdio.h>
#include<algorithm>
using namespace std;
const int N=; char a[N],b[N];
int dp[N][N]; int main()
{
int n,m;
while(~scanf("%d%s%d%s",&n,a,&m,b))
{
memset(dp,,sizeof(dp));
for(int i=;i<=n;i++)
dp[i][]=i;
for(int i=;i<=m;i++)
dp[][i]=i;
for(int i=;i<=n;i++)
{
for(int j=;j<=m;j++)
{
dp[i][j]=min(dp[i-][j]+,dp[i][j-]+);
dp[i][j]=min(dp[i][j],dp[i-][j-]+(a[i-]!=b[j-]));
}
}
printf("%d\n",dp[n][m]); }
return ;
}

POJ3356-AGTC-dp+字符串处理的更多相关文章

  1. POJ3356 – AGTC(区间DP&&编辑距离)

    题目大意 给定字符串X和Y,可以对字符串进行一下三种操作: 1.删除一个字符 2.插入一个字符 3.替换一个字符 每个操作代价是1,问运用以上三种操作把X变为Y所需的最小步数是多少? 题解 定义dp[ ...

  2. POJ 3356 AGTC(DP求字符串编辑距离)

    给出两个长度小于1000的字符串,有三种操作,插入一个字符,删除一个字符,替换一个字符. 问A变成B所需的最少操作数(即编辑距离) 考虑DP,可以用反证法证明依次从头到尾对A,B进行匹配是不会影响答案 ...

  3. Codeforces 176B (线性DP+字符串)

    题目链接: http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=28214 题目大意:源串有如下变形:每次将串切为两半,位置颠倒形成 ...

  4. Codeforces Round #367 (Div. 2) A B C 暴力 二分 dp(字符串的反转)

    A. Beru-taxi time limit per test 1 second memory limit per test 256 megabytes input standard input o ...

  5. HDU 2089 数位dp/字符串处理 两种方法

    不要62 Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submis ...

  6. Codeforces 611d [DP][字符串]

    /* 题意:给一个长度不超过5000的字符串,每个字符都是0到9的数字. 要求将整个字符串划分成严格递增的几个数字,并且不允许前导零. 思路: 1.很开心得发现,当我在前i个区间以后再加一个区间的时候 ...

  7. poj3267--The Cow Lexicon(dp:字符串组合)

    The Cow Lexicon Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 8211   Accepted: 3864 D ...

  8. poj3356 AGTC

    Description Let x and y be two strings over some finite alphabet A. We would like to transform x int ...

  9. BZOJ3297: [USACO2011 Open]forgot DP+字符串

    Description 发生了这么多,贝茜已经忘记了她cowtube密码.然而,她记得一些有用的信息.首先,她记得她的密码(记为变 量P)长度为L(1 <= L<=1,000)字符串,并可 ...

  10. hdu 1159(DP+字符串最长公共序列)

    http://blog.csdn.net/a_eagle/article/details/7213236 公共序列可以用一个二维数组dp[i][j]保存每个点时的最大数字,本质就是一个双向比较. dp ...

随机推荐

  1. Rust <1>:数据类型、变量、可变性、常量、隐藏

    rust 是强类型语言,所有变量.常量都必须有明确的数据类型:很多情况下,省略类型声明,编译器可自动推导,但不是所有情况下都会成功. rust 有整型.浮点型.布尔型.字符型.数组.元组.枚举.结构体 ...

  2. Mybatis中#{}与${}的使用

    含义 #{}:为占位符 ${}:为拼接符 区别: 用法 #{}:为参数占位符?,即sql预编译         ${}为字符串替换, 即字符串拼接 执行流程 #{}:动态解析 --> 预编译 - ...

  3. Python之实现一个优先级队列

    问题 怎样实现一个按优先级排序的队列? 并且在这个队列上面每次 pop 操作总是返回优先级最高的那个元素 解决方案 下面的类利用 heapq 模块实现了一个简单的优先级队列: import heapq ...

  4. IDEA webapp文件夹不识别解决方案

    使用IDEA 创建moudule 用的是的是maven archertype-quickstart ,自动生成并么有webapp目录,于是我从别的项目中拷贝了一个,发现webapp文件夹图标和普通文件 ...

  5. 记录一下取ul中li的值

    //#id 事件 $('#onLineUser').on('click','li',function () { $(".list-group-item").removeClass( ...

  6. MVC+EF三层+抽象工厂

    MVC+EF三层+抽象工厂项目搭建   注意:项目经过两次搭建,所以截图中顶级命名空间有ZHH和ZHH2区别,但是架构的内容是一样的,可以将ZHH和ZHH2视为同一命名空间 一:权限管理 二:搜索 | ...

  7. codeforces 1182E Product Oriented Recurrence 矩阵快速幂

    题意:设f(n) = c ^ (2n - 6) * f(n - 1) * f(n - 2) * f(n - 3), 问第n项是多少? 思路:官方题解:我们先转化一下,令g(x) =  c ^ x * ...

  8. PyCharm Change Font Size

    file->settings->colors&fonts-> save as (save the current scheme as your own)-> font- ...

  9. openFrameworks Download

    { https://openframeworks.cc/zh_cn//download/ } 0.10.1 是最新发布的版本. 这个版本是修改了一些BUG的小版本,与版本 0.10.1100%兼容而且 ...

  10. JMeter4.0 IF Controller

    推荐使用 __jexl3 函数生成 if controller判断条件 举个栗子: 1. 假如条件为 "${demo}" == "test" 2. 在If Co ...