HDUOJ--1159Common Subsequence
Common Subsequence
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 19595 Accepted Submission(s): 8326
Problem Description
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.
Sample Input
abcfbc abfcab
programming contest
abcd mnp
Sample Output
4
2
0
Source
简单的lcs
lcs 模板:
int lcs(const char *a,const char *b)
{
int i,j;
int m=strlen(a),n=strlen(b);
mar[0][0]=0;
for(i=1;i<=m;i++)
mar[i][0]=0;
for(i=1;i<=n;i++)
mar[0][i]=0;
for(i=1;i<=m;i++)
{
for(j=1;j<=n;j++)
{
if(a[i-1]==b[j-1])
mar[i][j]=mar[i-1][j-1]+1;
else
mar[i][j]=mar[i-1][j]>mar[i][j-1]?mar[i-1][j]:mar[i][j-1];
}
}
return mar[m][n];
}
此题的代码:
1 #include<stdio.h>
2 #include<string.h>
3 #define maxn 1000
4 int mar[maxn][maxn];
5 char x[maxn],y[maxn];
6
7 int lcs(const char *a,const char *b)
8 {
9 int i,j;
10 int m=strlen(a),n=strlen(b);
11 mar[0][0]=0;
12 for(i=1;i<=m;i++)
13 mar[i][0]=0;
14 for(i=1;i<=n;i++)
15 mar[0][i]=0;
16 for(i=1;i<=m;i++)
17 {
18 for(j=1;j<=n;j++)
19 {
20 if(a[i-1]==b[j-1])
21 mar[i][j]=mar[i-1][j-1]+1;
22 else
23 mar[i][j]=mar[i-1][j]>mar[i][j-1]?mar[i-1][j]:mar[i][j-1];
24 }
25 }
26 return mar[m][n];
27 }
28 int main()
29 {
30 while(scanf("%s%s",&x,&y)!=EOF)
31 printf("%dn",lcs(x,y));
32 return 0;
33 }
- 如何将Kerberos环境下CDH集群JAVA升级至JDK8
- 干货|如何做准确率达98%的交通标志识别系统?
- 用57行代码搞定花8000万美元采购车牌识别项目
- Cloudera Manager Server服务在RedHat7状态显示异常分析
- 开源 | 基于Python的人脸识别:识别准确率高达99.38%!
- 转录组数据的基因表达变化情况探索
- 如何配置Kerberos服务的高可用
- 利用深度学习生成梵高风格画像
- 使用Python-Requests实现ODL对OVS的流表下发
- Keras入门必看教程
- bedtools 用法大全(一文就够吧)
- 区块链资产安全攻略
- 如何在Kudu1.5中使用Sentry授权
- 深度学习入门实战
- JavaScript 教程
- JavaScript 编辑工具
- JavaScript 与HTML
- JavaScript 与Java
- JavaScript 数据结构
- JavaScript 基本数据类型
- JavaScript 特殊数据类型
- JavaScript 运算符
- JavaScript typeof 运算符
- JavaScript 表达式
- JavaScript 类型转换
- JavaScript 基本语法
- JavaScript 注释
- Javascript 基本处理流程
- Javascript 选择结构
- Javascript if 语句
- Javascript if 语句的嵌套
- Javascript switch 语句
- Javascript 循环结构
- Javascript 循环结构实例
- Javascript 跳转语句
- Javascript 控制语句总结
- Javascript 函数介绍
- Javascript 函数的定义
- Javascript 函数调用
- Javascript 几种特殊的函数
- JavaScript 内置函数简介
- Javascript eval() 函数
- Javascript isFinite() 函数
- Javascript isNaN() 函数
- parseInt() 与 parseFloat()
- escape() 与 unescape()
- Javascript 字符串介绍
- Javascript length属性
- javascript 字符串函数
- Javascript 日期对象简介
- Javascript 日期对象用途
- Date 对象属性和方法
- Javascript 数组是什么
- Javascript 创建数组
- Javascript 数组赋值与取值
- Javascript 数组属性和方法
- kubernetes监控-Heapster+InfluxDB+Grafana(十五)
- centos7 安装mysql5.7
- ceph-简介及安装(luminous)版
- Activiti7 查询用户任务列表
- ansible自动化部署工具
- IDEA Activiti 画图中文乱码
- k8s集群部署高可用完整版
- kubenetes-rancher多集群管理(二十二)
- kubernetes-helm程序包管理器(二十)
- Activiti7 组任务
- kubeadm快速部署kubernetes(十九)
- prometheus-简介及安装
- 微服务治理istio
- 基于Kubernetes构建Jenkins微服务发布平台
- gitlab