题目链接:https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=16&page=show_problem&problem=1346

Dynamic programming

需要注意的是input里可能含有空行,一行空行也是一个string,所以如果用scanf("%s", string)是不能把空行存进string变量里的。需要用gets或者getline来处理input。

可惜的是我目前对于gets,getline的用法还没有很熟悉,之后会关于这方面的区别做解释。又或者有大神可以帮忙讲解一下,感恩

代码如下:

 #include <iostream>
#include <math.h>
#include <stdio.h>
#include <cstdio>
#include <algorithm>
#include <string.h>
#include <cstring>
#include <queue>
#include <vector>
#include <functional>
#include <cmath>
#define SCF(a) scanf("%d", &a)
#define IN(a) cin>>a
#define FOR(i, a, b) for(int i=a;i<b;i++)
typedef long long Int;
using namespace std; int main()
{
char str1[];
char str2[];
int len1, len2;
while (cin.getline(str1, ))
{
cin.getline(str2, );
for (len1 = ; str1[len1] != '\0'; len1++);
for (len2 = ; str2[len2] != '\0'; len2++);
int** match;
match = new int*[len1+];
FOR(i, , len1 + )
match[i] = new int[len2 + ];
FOR(i, , len1 + )
match[i][] = ;
FOR(i, , len2 + )
match[][i] = ; FOR(i, , len1 + )
{
FOR(j, , len2 + )
{
if (str1[i - ] == str2[j - ])
match[i][j] = match[i - ][j - ] + ;
else
{
match[i][j] = max(match[i - ][j], match[i][j - ]);
}
}
} printf("%d\n", match[len1][len2]);
FOR(i, , len1 + )
delete[] match[i];
delete[] match;
}
return ;
}

UVA 10405 Longest Common Subsequence的更多相关文章

  1. UVA 10405 Longest Common Subsequence (dp + LCS)

    Problem C: Longest Common Subsequence Sequence 1: Sequence 2: Given two sequences of characters, pri ...

  2. UVA 10405 Longest Common Subsequence --经典DP

    最长公共子序列,经典问题.算是我的DP开场题吧. dp[i][j]表示到s1的i位置,s2的j位置为止,前面最长公共子序列的长度. 状态转移: dp[i][j] = 0                 ...

  3. [UVa OJ] Longest Common Subsequence

    This is the classic LCS problem. Since it only requires you to print the maximum length, the code ca ...

  4. [Algorithms] Longest Common Subsequence

    The Longest Common Subsequence (LCS) problem is as follows: Given two sequences s and t, find the le ...

  5. 动态规划求最长公共子序列(Longest Common Subsequence, LCS)

    1. 问题描述 子串应该比较好理解,至于什么是子序列,这里给出一个例子:有两个母串 cnblogs belong 比如序列bo, bg, lg在母串cnblogs与belong中都出现过并且出现顺序与 ...

  6. LintCode Longest Common Subsequence

    原题链接在这里:http://www.lintcode.com/en/problem/longest-common-subsequence/ 题目: Given two strings, find t ...

  7. [UCSD白板题] Longest Common Subsequence of Three Sequences

    Problem Introduction In this problem, your goal is to compute the length of a longest common subsequ ...

  8. LCS(Longest Common Subsequence 最长公共子序列)

    最长公共子序列 英文缩写为LCS(Longest Common Subsequence).其定义是,一个序列 S ,如果分别是两个或多个已知序列的子序列,且是所有符合此条件序列中最长的,则 S 称为已 ...

  9. Longest Common Subsequence

    Given two strings, find the longest common subsequence (LCS). Your code should return the length of  ...

随机推荐

  1. 使用rsync 同步数据一些常用参数和示例

    rsync rsync是linux系统下的数据镜像备份工具.支持远程同步,本地复制,或者与其他SSH.rsync主机同步. 包括本地推到远程,远程拉到本地两种同步方式,也可以实现本地不同路径下文件的同 ...

  2. View可以设置tag携带数据

    View可以设置tag携带数据.       例子             初始化:ImageView  iv_brand2              设置:iv_brand2.setTag(strB ...

  3. js保留小数点后面几位的方法

    原文地址: http://www.jb51.net/article/45884.htm 四舍五入以下处理结果会四舍五入: ? 1 2 var num =2.446242342; num = num.t ...

  4. BufferedReader .BufferedWriter执行文本复制

    /** * 需求:演示 BufferedReader 和 BufferedWriter 的使用,复制一个 java 文件 */ package cn.itcast.others.iostream; i ...

  5. Python_变量命名

    Python的变量命名 变量的命名的原则一直都是我这种小白的头疼事,好几条,根本记不住...... 为了解决大家的头疼问题,今天想出来一个好办法,那就是:身边常备头疼片.......(哈哈哈,开玩笑的 ...

  6. random 模块 时间模块(time) sys模块 os模块

    random  模块 1.随机小数 random.random()  0-1内的随机小数 random.uniform(1,5)  1-5范围内的随机小数 2.随机整数 random.randint( ...

  7. httpclient和httpUrlConnect区别

    HttpURLConnection的用法 一.创建HttpURLConnection对象 URL url = new URL("http://localhost:8080/TestHttpU ...

  8. ConcurrentMap与CopyOnWrite容器

    ConcurrentMap接口下有两个重要的实现: ConcurrentHashMap ConcurrentSkipListMap(支持并发排序功能,弥补ConcurrentHashMap) Conc ...

  9. @RequestMapping使用须知

    ----------------------siwuxie095 @RequestMapping 使用须知 使用 @RequestMapping 注解映射请求路径 即 你可以使用 @RequestMa ...

  10. Linux 下 mysql的基本配置

    Linux 下 mysql的基本配置 2013年02月27日 ⁄ MySQL ⁄ 共 3000字 ⁄ 暂无评论 ⁄ 被围观 2,483 views+ 1. Linux mysql安装:    $ yu ...