hdoj 1159最长公共子序列
/*Common Subsequence
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.
Input
The program input is from the std input. Each data set in the input contains two
strings representing the given sequences. The sequences are separated by any number of
white spaces. The input data are correct.
Output
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.
Sample Input
abcfbc abfcab
programming contest
abcd mnp
Sample Output
4
2
0*/
<span style="font-size:18px;">#include <stdio.h>
#include <string.h>
#define maxn 1000
char str1[maxn], str2[maxn];
int dp[maxn][maxn];
int max(int a, int b)
{
return a > b ? a : b;
}
int gg()
{
int m= 0;
for(int i = 1; str1[i]; ++i){
for(int j = 1; str2[j]; ++j){
if(str1[i] == str2[j]){
dp[i][j] = dp[i-1][j-1] + 1;
}else dp[i][j] = max(dp[i - 1][j], dp[i][j - 1]);
if(dp[i][j]>m) m= dp[i][j];
}
}
return m;
} int main()
{
while(scanf("%s%s", str1 + 1, str2 + 1)== 2){
printf("%d\n", gg());
}
return 0;
}
</span>
hdoj 1159最长公共子序列的更多相关文章
- HDU 1159 最长公共子序列(n*m)
Common Subsequence Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Other ...
- HDU 1159 最长公共子序列
Common Subsequence Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Other ...
- POJ 1159 Palindrome-最长公共子序列问题+滚动数组(dp数组的重复利用)(结合奇偶性)
Description A palindrome is a symmetrical string, that is, a string read identically from left to ri ...
- HDU 1159 Common Subsequence:LCS(最长公共子序列)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1159 题意: 求最长公共子序列. 题解: (LCS模板题) 表示状态: dp[i][j] = max ...
- POJ 1159 Palindrome(最长公共子序列)
http://poj.org/problem?id=1159 题意: 给出一个字符串,计算最少要插入多少个字符可以使得该串变为回文串. 思路: 计算出最长公共子序列,再用原长-LCS=所要添加的字符数 ...
- hdu 1159 Common Subsequence(最长公共子序列)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1159 Common Subsequence Time Limit: 2000/1000 MS (Jav ...
- hdu 1159 Common Subsequence(最长公共子序列 DP)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1159 Common Subsequence Time Limit: 2000/1000 MS (Jav ...
- HDU 1159 Common Subsequence 最长公共子序列
HDU 1159 Common Subsequence 最长公共子序列 题意 给你两个字符串,求出这两个字符串的最长公共子序列,这里的子序列不一定是连续的,只要满足前后关系就可以. 解题思路 这个当然 ...
- HDU 1159 Common Subsequence --- DP入门之最长公共子序列
题目链接 基础的最长公共子序列 #include <bits/stdc++.h> using namespace std; ; char c[maxn],d[maxn]; int dp[m ...
随机推荐
- 虚机中访问外网;NAT中的POSTROUTING是怎么搞的?
看下docker中是怎么配置的网络 在虚机中访问外网:设定了qemu,在主机上添加路由:sudo iptables -t nat -I POSTROUTING -s 192.168.1.110 -j ...
- java.net.SocketException: recvfrom failed: EBADF (Bad file descriptor)
1. 问题说明: 与服务器之间进行socket通信的时候,客户端关闭socket之后,会抛出一个IOException,异常信息如下: java.net.SocketException: recvfr ...
- 【bzoj4974】字符串大师 逆模拟KMP
题目描述 一个串T是S的循环节,当且仅当存在正整数k,使得S是$T^k$(即T重复k次)的前缀,比如abcd是abcdabcdab的循环节.给定一个长度为n的仅由小写字符构成的字符串S,请对于每个k( ...
- finally代码块不被执行的情况总结
以前跟别人讨论finally关键字,我总是简单的说:“fianly代码块中的代码一定会执行,一般用来清除IO资源等非内存资源(内存资源由GC机制回收)”. 今天翻书发现了几种不会执行的情况,现在总结下 ...
- vue-cli安装sass
npm install node-sass --save npm install sass-loader --save 也可以使用淘宝镜像 npm install -g cnpm --registry ...
- php中json_encode和json_decode的用法
1.json_encode基本用法:数组转字符串 <?php $arr = array ('a'=>1,'b'=>2,'c'=>3,'d'=>4,'e'=>5); ...
- oracle怎么查看一个表或一个索引占用多少空间
很多时候我们想知道一个表或一个索引占用多少M的空间,以下脚本就是满足这个要求的,记住替换其中的内容. SELECT owner, segment_name, SUM(bytes)/1024/1024 ...
- 前端模块加载规范AMD与CMD小记
AMD代表:requirejs: CMD代表:seajs: AMD CMD 代表 requirejs seajs 执行 提前加载,不管是否调用模块,先解析所以模块 提前加载,在真正需要使 ...
- roadhogrc.mock.js配置
1.roadhogrc.mock.js const fs=require('fs'); const path=require('path'); const mockPath=path.join(__d ...
- 【Tomcat】如何注册Tomcat到Window Service服务
对于Web项目来说,经常用的服务器就是Tomcat.但是麻烦的事是,每次都得启动一下Tomcat服务.但是,如果把Tomcat的服务注册为Windows Service服务,就可以设置为开机自动启动, ...