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 ...
随机推荐
- Codeforces Round #321 (Div. 2) E Kefa and Watch (线段树维护Hash)
E. Kefa and Watch time limit per test 1 second memory limit per test 256 megabytes input standard in ...
- 2012-2013 ACM-ICPC, NEERC, Central Subregional Contest J Computer Network1 (缩点+最远点对)
题意:在连通图中,求一条边使得加入这条边以后的消除的桥尽量多. 在同一个边双连通分量内加边肯定不会消除桥的, 求边双连通分量以后缩点,把桥当成边,实际上是要选一条最长的链. 缩点以后会形成一颗树,一定 ...
- 【转】iOS-生成Bundle包-引入bundle-使用bundle
在我们使用第三方框架时,常常看到XXX.bundle的文件. 我们找到该文件,显示包内容,大致看到很多资源文件:图片.配置文本.XIB文件…… 什么是Bundle文件? 简单理解,就是资源文件包. ...
- 使用Process组件访问本地进程
实现效果; 知识运用: Process组件的StartInfo属性 //获取或设置要传递给Process的Start方法的属性 public ProcessStartInfo StartInfo {g ...
- AutoWidthInput
import React from 'react'; import PropTypes from 'prop-types'; class AutoWidthInput extends React.Co ...
- mysql如何让自增id从1开始设置方法
有两种方式 第一种: 如果表中数据没有用.如果直接删除数据,自动增长ID还是不会从1开始的,可以利用“清空数据表”.这样自动增长ID也将会从1开始. 清空表的sql如下 truncate table ...
- java基础—注解annotation
一.认识注解 注解(Annotation)很重要,未来的开发模式都是基于注解的,JPA是基于注解的,Spring2.5以上都是基于注解的,Hibernate3.x以后也是基于注解的,现在的Struts ...
- 虚拟机设置NAT
需要开启虚拟机网络相关服务, 安装虚拟网卡, 还有必须安装 VMware Tools VMware虚拟机下实现NAT方式上网1. 把你的虚拟网卡VMnet8设置为自动获得IP.自动获得DNS服务器,启 ...
- UpdatePanel中点击按钮Session过期跳转页面相关问题:Sys.WebForms.PageRequestManagerParserErrorException:无法分析从服务器收到的消息
使用 Response.Write("<script language=javascript>window.location.href='Login.aspx';</scr ...
- Angular-constructor和ngOnInit区别
参考文档:https://blog.csdn.net/u010730126/article/details/64486997 总结:constructor做依赖注入,避免业务操作: ngOninit做 ...