HDU1159 && POJ1458:Common Subsequence(LCS)
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.
programming contest
abcd mnp
2
0
题意:求出两个串的公共子序列的长度
思路:LCS的入门题,直接模板就可以了
#include <stdio.h>
#include <string.h>
#include <algorithm>
using namespace std; char s1[1000],s2[1000];
int dp[1000][1000];
int len1,len2; void LCS()
{
int i,j;
memset(dp,0,sizeof(dp));
for(i = 1; i<=len1; i++)
{
for(j = 1; j<=len2; j++)
{
if(s1[i-1] == s2[j-1])
dp[i][j] = dp[i-1][j-1]+1;
else
dp[i][j] = max(dp[i-1][j],dp[i][j-1]);
}
}
} int main()
{
while(~scanf("%s%s",s1,s2))
{
len1 = strlen(s1);
len2 = strlen(s2);
LCS();
printf("%d\n",dp[len1][len2]);
} return 0;
}
HDU1159 && POJ1458:Common Subsequence(LCS)的更多相关文章
- 动态规划求最长公共子序列(Longest Common Subsequence, LCS)
1. 问题描述 子串应该比较好理解,至于什么是子序列,这里给出一个例子:有两个母串 cnblogs belong 比如序列bo, bg, lg在母串cnblogs与belong中都出现过并且出现顺序与 ...
- HDU 1159:Common Subsequence(LCS模板)
Common Subsequence Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Other ...
- 【水:最长公共子序列】【HDU1159】【Common Subsequence】
Common Subsequence Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Other ...
- Common Subsequence LCS
题目链接:http://acm.hust.edu.cn/vjudge/contest/view.action?cid=87730#problem/F 题目: Description A subsequ ...
- Poj 1458 Common Subsequence(LCS)
一.Description A subsequence of a given sequence is the given sequence with some elements (possible n ...
- 算法:Common Subsequence(动态规划 Java 最长子序列)
Description A subsequence of a given sequence is the given sequence with some elements (possible non ...
- hdu 1159 Common Subsequence(LCS最长公共子序列)
Common Subsequence Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Other ...
- 最长公共字串算法, 文本比较算法, longest common subsequence(LCS) algorithm
''' merge two configure files, basic file is aFile insert the added content of bFile compare to aFil ...
- POJ 1458:Common Subsequence
Common Subsequence Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 41957 Accepted: 16 ...
随机推荐
- 全文索引--自定义chinese_lexer词典
全文索引它的数据字典本来就是自己加密过的数据格式,只有翻译过来了,才可以修改.这样修改后再生成它自己的数据格式文件,覆盖掉原来的,就会将新添加的关键词加入进去了!! 以下操作是在Oracle服务器安装 ...
- 用JAVA给JSON进行排版
之前听到朋友的面试题,是如何对JSON进行排版,于是就写了一个Demo,觉得挺有意思的,就贴出来了. 这个就是记录缩进来输出,大家也可以尝试一下其他更好算法来进行输出. 功能:可以把一行的JSON字符 ...
- 【cogs247】售票系统
[问题描述] 某次列车途经C个城市,城市编号依次为1到C,列车上共有S个座位,铁路局规定售出的车票只能是坐票, 即车上所有的旅客都有座.售票系统是由计算机执行的,每一个售票申请包含三个参数,分别用O. ...
- hibernate的get、load的方法的区别,IllegalArgument异常
关于hibernate中的load,get,以及延迟加载问题 今天在使用hibernate时,发现一异常: could not initialize proxy - no Session 查询资料之后 ...
- [HTML5 Canvas学习] 基础知识
HTML5 canvas元素通过脚本语言(通常是Javascript) 绘制图形, 它仅仅是一个绘图环境,需要通过getContext('2d')方法获得绘图环境对象,使用绘图环境对象在canvas元 ...
- phpstorm 配置
JetBrains PhpStorm 8注册码一枚 username :cf96PiPYt271u1TC License Key : 97807-12042010 00001GctOKh8f206hl ...
- 去掉所有的html标签
去掉所有的HTML标签:$text=preg_replace('/<[^>]+>/','',$text); 去掉<img>标签:$text=preg_replace('/ ...
- 看源码之Adapter和AdapterView之间的关系
总述 Android中"列表"的实现其实一个典型的MVC模式,其实中AdapterView相当于是View,负责视图的绘制以及视图的事件响应,Adapter相当于是Controll ...
- 使用飞信api接口实现短信发送(只能发送好友)
找了很久才找到一个能用的飞信API接口(http://quanapi.sinaapp.com/fetion.php?u=飞信登录手机号&p=飞信登录密码&to=接收飞信的手机号& ...
- TCP回射服务器程序:str_echo函数
str_echo函数执行处理每个客户的服务: 从客户读入数据,并把它们回射给客户 读入缓冲区并回射其中内容: read函数从套接字读入数据,writen函数把其中内容回射给客户 如果客户关闭连接,那么 ...