pojAGTC(LCS,DP)
题目链接:
啊哈哈,点我点我
题意:给两个字符串,找出经过多少个操作能够使得两个串相等。。
思路:找出两个串的最长公共子序列,然后用最大的串的长度减去最长公共子序列的长度得到的就是须要的操作数。。
题目:
AGTC
Time Limit: 1000MS
Memory Limit: 65536K
Total Submissions: 10015
Accepted: 3849
Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 10015 | Accepted: 3849 |
Description
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 CDeletion: * 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
Source
代码为:
#include<cstring>
#include<cstdio>
#include<algorithm>
#include<iostream>
using namespace std;
const int maxn=1000+10;
int dp[maxn][maxn];
char str1[maxn],str2[maxn];
int LCS(int len1,int len2)
{
memset(dp,0,sizeof(dp));
for(int i=1;i<=len1;i++)
for(int j=1;j<=len2;j++)
{
if(str1[i-1]==str2[j-1])
dp[i][j]=dp[i-1][j-1]+1;
else
dp[i][j]=max(dp[i-1][j],dp[i][j-1]);
}
return dp[len1][len2];
}
int main()
{
int n,m;
while(~scanf("%d%s",&n,str1))
{
scanf("%d%s",&m,str2);
int len1=strlen(str1);
int len2=strlen(str2);
int ans=LCS(len1,len2);
int max_ans=max(n,m);
printf("%d\n",max_ans-ans);
}
return 0;
}
#include<cstdio>
#include<algorithm>
#include<iostream>
using namespace std;
const int maxn=1000+10;
int dp[maxn][maxn];
char str1[maxn],str2[maxn]; int LCS(int len1,int len2)
{
memset(dp,0,sizeof(dp));
for(int i=1;i<=len1;i++)
for(int j=1;j<=len2;j++)
{
if(str1[i-1]==str2[j-1])
dp[i][j]=dp[i-1][j-1]+1;
else
dp[i][j]=max(dp[i-1][j],dp[i][j-1]);
}
return dp[len1][len2];
} int main()
{
int n,m;
while(~scanf("%d%s",&n,str1))
{
scanf("%d%s",&m,str2);
int len1=strlen(str1);
int len2=strlen(str2);
int ans=LCS(len1,len2);
int max_ans=max(n,m);
printf("%d\n",max_ans-ans);
}
return 0;
}
pojAGTC(LCS,DP)的更多相关文章
- 51Nod 1092 回文字符串(LCS + dp)
51Nod 1092 数据结构暑假作业上出现的一题,学习了一下相关算法之后,找到了oj测试能AC. 1.回文串是一种中心对称的结构,这道题可以转变为求最长回文子序列长度的题目.(子序列:可以不连续) ...
- 51 Nod 1006 最长公共子序列(LCS & DP)
原题链接:https://www.51nod.com/onlineJudge/questionCode.html#!problemId=1006 题目分析: 首先先知道LCS问题,这有两种: Long ...
- DP基础(线性DP)总结
DP基础(线性DP)总结 前言:虽然确实有点基础......但凡事得脚踏实地地做,基础不牢,地动山摇,,,嗯! LIS(最长上升子序列) dp方程:dp[i]=max{dp[j]+1,a[j]< ...
- ACdreamOJ 1154 Lowbit Sum (数字dp)
ACdreamOJ 1154 Lowbit Sum (数位dp) ACM 题目地址:pid=1154" target="_blank" style="color ...
- 「SDOI2016」储能表(数位dp)
「SDOI2016」储能表(数位dp) 神仙数位 \(dp\) 系列 可能我做题做得少 \(QAQ\) \(f[i][0/1][0/1][0/1]\) 表示第 \(i\) 位 \(n\) 是否到达上界 ...
- 【HDU1693】Eat the Trees(插头dp)
[HDU1693]Eat the Trees(插头dp) 题面 HDU Vjudge 大概就是网格图上有些点不能走,现在要找到若干条不相交的哈密顿回路使得所有格子都恰好被走过一遍. 题解 这题的弱化版 ...
- 【BZOJ1814】Ural 1519 Formula 1 (插头dp)
[BZOJ1814]Ural 1519 Formula 1 (插头dp) 题面 BZOJ Vjudge 题解 戳这里 上面那个链接里面写的非常好啦. 然后说几个点吧. 首先是关于为什么只需要考虑三进制 ...
- 【BZOJ4712】洪水(动态dp)
[BZOJ4712]洪水(动态dp) 题面 BZOJ 然而是权限题QwQ,所以粘过来算了. Description 小A走到一个山脚下,准备给自己造一个小屋.这时候,小A的朋友(op,又叫管理员)打开 ...
- 【HDU 5647】DZY Loves Connecting(树DP)
pid=5647">[HDU 5647]DZY Loves Connecting(树DP) DZY Loves Connecting Time Limit: 4000/2000 MS ...
随机推荐
- avalon.js 多级下拉框实现
学习avalon.js的时候,有一个多级下拉框的例子,地址 戳这里 代码实现了联动, 但是逻辑上面理解有点难度,获取选择的值 和 页面初始化 功能存在问题. 在写地图编辑的时候,也用到了多级下拉框,特 ...
- MapReduce ---- TD-IDF
1.TF-IDF TF-IDF(term frequency/inverse document frequency) 的概念被公认为信息检索中最重要的发明.描述单个term与特定document的相关 ...
- 7.使用ProcessBuilder执行本地命令(转)
import java.io.BufferedReader; import java.io.BufferedWriter; import java.io.File; import java.io.IO ...
- openNebula images
- 基于Hadoop的大数据平台实施记——整体架构设计
大数据的热度在持续的升温,继云计算之后大数据成为又一大众所追捧的新星.我们暂不去讨论大数据到底是否适用于您的组织,至少在互联网上已经被吹嘘成无所不能的超级战舰.好像一夜之间我们就从互联网时代跳跃进了大 ...
- ASP.NET实现列表页连接查询 拼接sql语句 绑定grivdView
ASP.NET实现列表页连接查询 拼接sql语句 如图效果: 基本需求:1.当页面第一次加载的时候默认查询一个月时间(或者说是登陆者所属权限的所有数据)的数据绑定到gridView 2.添加查询条件时 ...
- org.openqa.selenium.SessionNotCreatedException: A new session could not be created.
解决方案 1. 重新插拔手机. 2. 检查appium端口是否被占用,如是,杀掉占用了改端口的进程,然后重启appium. 3.
- JavaScript之arguements对象学习
简介:在JavaScript中,有一个特殊的对象-Arguements对象,它是当前函数的一个内置属性,它类似与Array对象(数组形式),但不是Array的一个实例.下面通过代码来论证: <s ...
- GWT RPC机制
GWT RPC GWT RPCRemote Procedure Calls GWT: Google Web Toolkit的缩写,有了 GWT可以使用 Java 编程语言编写 AJAX 前端,然后 G ...
- Building Workspace速度慢的原因
今天把ext3.0部署到web project很慢很慢,查了一下,这个当笔记. 转自http://blog.163.com/jong_cai/blog/static/87028045201311178 ...