ACM程序设计选修课——1018: Common Subsequence(DP)
问题 L: Common Subsequence
时间限制: 1 Sec 内存限制: 32 MB
提交: 70 解决: 40
[提交][状态][讨论版]
题目描述
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.
输入
输出
样例输入
- abcfbc abfcab
- programming contest
- abcd mnp
样例输出
- 4
- 2
- 0
..在大神的指点下,似乎明白了点啥。智商需要充值。假设这俩字符串为a与bC[i][j]来表示当前移动到a的第i个位置,b的第j个位置时所产生的最大公共子字符个数。
C[i][j]要么保持上两个状态中的一个,要么出现了公共的字符进行+1操作。
list[i+1][j+1]=(a[i]==b[j] ? list[i][j]+1 : max( list[i+1][j] , list[i][j+1]) );
代码:
- #include<iostream>
- #include<string>
- #include<algorithm>
- #include<map>
- #include<set>
- #include<cstring>
- #include<cmath>
- using namespace std;
- int list[1010][1010];
- int main(void)
- {
- int t,i,j,ans;
- string a,b;
- while (cin>>a>>b)
- {
- memset(list,0,sizeof(list));
- int la=(int)a.size();
- int lb=(int)b.size();
- for (i=0; i<la; i++)
- {
- for (j=0; j<lb; j++)
- {
- list[i+1][j+1]=(a[i]==b[j]?list[i][j]+1:max(list[i+1][j],list[i][j+1]));//状态转移方程
- }
- }
- printf("%d\n",list[la][lb]);
- }
- return 0;
- }
ACM程序设计选修课——1018: Common Subsequence(DP)的更多相关文章
- Common Subsequence(dp)
Common Subsequence Time Limit: 2 Sec Memory Limit: 64 MBSubmit: 951 Solved: 374 Description A subs ...
- UVA 10405 Longest Common Subsequence (dp + LCS)
Problem C: Longest Common Subsequence Sequence 1: Sequence 2: Given two sequences of characters, pri ...
- POJ1458 Common Subsequence —— DP 最长公共子序列(LCS)
题目链接:http://poj.org/problem?id=1458 Common Subsequence Time Limit: 1000MS Memory Limit: 10000K Tot ...
- POJ - 1458 Common Subsequence DP最长公共子序列(LCS)
Common Subsequence A subsequence of a given sequence is the given sequence with some elements (possi ...
- Longest Common Subsequence (DP)
Given two strings, find the longest common subsequence (LCS). Your code should return the length of ...
- HDU 1159 Common Subsequence --- DP入门之最长公共子序列
题目链接 基础的最长公共子序列 #include <bits/stdc++.h> using namespace std; ; char c[maxn],d[maxn]; int dp[m ...
- CF 346B. Lucky Common Subsequence(DP+KMP)
这题确实很棒..又是无想法..其实是AC自动机+DP的感觉,但是只有一个串,用kmp就行了. dp[i][j][k],k代表前缀为virus[k]的状态,len表示其他所有状态串,处理出Ac[len] ...
- ACM程序设计选修课——1043: Radical loves integer sequences(YY)
1043: Radical loves integer sequences Time Limit: 1 Sec Memory Limit: 128 MB Submit: 36 Solved: 4 ...
- ACM程序设计选修课——1036: Hungar的菜鸟赛季(YY)
1036: Hungar的菜鸟赛季 Time Limit: 1 Sec Memory Limit: 64 MB Submit: 20 Solved: 14 [Submit][Status][Web ...
随机推荐
- redis的一些问题总结,转载自infoq
Redis是时下比较流行的Nosql技术.在优酷我们使用Redis Cluster构建了一套内存存储系统,项目代号蓝鲸.到目前为止集群有700+节点,即将达到作者推荐的最大集群规模1000节点.集群从 ...
- UVA 11732 strcmp() Anyone (Trie+链表)
先两两比较,比较次数是两者相同的最长前缀长度*2+1,比较特殊的情况是两者完全相同时候比较次数是单词长度*2+2, 两个单词'末尾\0'和'\0'比较一次,s尾部'\0'和循环内'\0'比较一次. 因 ...
- CodeForces 48C D - The Race (Fraction,数学)
每个加油的站可以确定一个alpha的上下界,比如,第i次加油站a[i],前面加了i次油,a[i]*10≤ alpha*i <(a[i]+1)*10. 取最大的下界,取最小的上界,看看两者之间的满 ...
- UVA 1611 Crane 起重机 (子问题)
题意:给一个1~n排列,1<=n<=10000,每次操作选取一个长度为偶数的连续区间.交换前一半和后一半,使它变成升序. 题解:每次只要把最小的移动到最左边,那么问题规模就缩小了.假设当前 ...
- ansible 通过堡垒机/跳板机 访问目标机器需求实战(ssh agent forward)
一. 需求背景: 在我们使用ansible的过程中经常会遇到这样的情况,我们要管理的机器都在内网中,这些内网机器的登录都是通过跳板机或者堡垒机登录.我们的ansible机器不能直接管理到这些后端的机器 ...
- java基础—流
一.JAVA流式输入/输出原理
- 小试牛刀,建立jsp网页与导出war包
一.建立jsp网页 首先创建一个动态项目(我们学习的是动态网) 二.检查编码utf-8有没错误. 如有错误就是没有设置eclipse,请按照eclipse设置 编写一段代码,进行了解 三.导出一个wa ...
- cocos2dx for lua 摄像机移动
在cocos2dx中,我们想通过移动摄像机来做一些特殊处理,比如将摄像机聚焦在某个物体上,或者摄像机颤抖,摄像机原理观察sprite回收状况等等, 都需要通过相机移动来使用. cocos2dx中的摄像 ...
- 简单的Datable转List方法
public static class DataTableUtils<T> where T : new() { public static List<T> ConvertToM ...
- pycharm安装 suds模块报错:AttributeError: module 'pip' has no attribute 'main'
需求:安装suds模块 遇到的问题: 一.报错信息:[file][Default Settint]---Project Interpreter 点击 搜索suds安装模块报错 解决:依据上图提示找到C ...