首页 > 其他分享 >Common Subsequence

Common Subsequence

时间:2023-04-20 18:03:08浏览次数:39  
标签:... sequence int Subsequence subsequence Common input 1010


Common Subsequence


Time Limit: 1000MS

 

Memory Limit: 10000K

Total Submissions: 43130

 

Accepted: 17470


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, x ij = 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


#include<stdio.h>
#include<string.h>
#include<math.h>
#define max(a,b) (a>b?a:b)
int p[1010][1010];
char s1[1010],s2[1010];
int main(){ 
 int i,j; 
 while(scanf("%s%s",s1,s2)!=EOF)
 {
  memset(p,0,sizeof(p));
  int l1=strlen(s1);
  int l2=strlen(s2);
  for(i=1;i<=l1;i++)
  {
   for(j=1;j<=l2;j++)
   {
    if(s1[i-1]==s2[j-1])
     p[i][j]=p[i-1][j-1]+1;
    else
     p[i][j]=max(p[i-1][j],p[i][j-1]);
   }
  }
  printf("%d\n",p[l1][l2]);
 }
 return 0;
}

标签:...,sequence,int,Subsequence,subsequence,Common,input,1010
From: https://blog.51cto.com/u_16079508/6210103

相关文章

  • Git Tip: git push ‘No refs in common and none specified’
    评:@seehttp://www.thebuzzmedia.com/git-tip-git-push-no-refs-in-common-and-none-specified/Gitisasource-controltoolusedbysoftwaredevelopers.IrecentlyswitchedfromSubversiontoGitandwhilethingshavebeenmostlysmooth,therehavebeena......
  • Docker: common container Dockerfile
     mavenFROM--platform=amd64maven:3-openjdk-18-slimRUNcd/etc/apt&&>sources.list&&cat>sources.list<<EOFdebhttps://mirrors.ustc.edu.cn/debianbullseyemaincontribnon-freedeb-srchttps://mirrors.ustc.edu.cn/deb......
  • Common code
    Commoncode代表段库Csharp查询1.简单查询varquery=newQueryExpression("account");varcondition=newConditionExpression("name",ConditionOperator.Equal,"Contoso");query.Criteria.AddCondition(condition);varorder=newOrderE......
  • ava: 程序包com.alibaba.nacos.api.common不存在_RuoYi-Cloud-Plus-master_jar包不存
    来看看原因吧,jar包是存在的,但是就是在idea中引用不到,来看看怎么回事: 原来就是这个包找不到,但是从下面看是有的: 但是注意,这里的com.alibaba.nacos.api...原来可不是这样的,这个是我后来修改过的,原来是只有com.alibaba.nacos.common,而引用的是com.alibaba.nacos.api.commo......
  • 前端科普系列(3):CommonJS 不是前端却革命了前端
    作者:Morrain一、前言上一篇《前端科普系列(2):Node.js换个角度看世界》,我们聊了Node.js相关的东西,Node.js能在诞生后火到如此一塌糊涂,离不开它成熟的模块化实现,Node.js的模块化是在CommonJS规范的基础上实现的。那CommonJS又是什么呢?先来看下,它在维基百科上的定义:CommonJS是......
  • base64 之坑----Base64后出现换行符 org.apache.commons.net.util.Base64()
    坑记录问题:base64后的结果会出现\r\n换行符, 复现:publicstaticvoidmain(String[]args){Stringkey="ssjsi21djsiej284858jweejrh34981dwde32323243232423423412121";Strings=java.util.Base64.getEncoder().encodeToString(key.getBytes(St......
  • CommonJS和ES6中的导入导出使用
    1.CommonJS使用exportsconstname='张三'functionFn(){return1}//导出单个数据exports.name=nameexports.Fn=Fn//导入数据//可使用解构cons......
  • 注入了一个记录日志的接口,启动微服务工程的时候报错了:No qualifying bean of type 'co
    【问题描述】Causedby:org.springframework.beans.factory.BeanCreationException:Errorcreatingbeanwithname'monitorAreaServiceImpl':Injectionofresource......
  • Commons-Collections1反序列化
    JDK版本为jdk8u65commons-collections版本为3.2.1InvokerTransformerCC1的漏洞点在InvokerTransformer,InvokerTransformer下有一个transform方法:publicObjecttransf......
  • Java 执行命令 Apache Common-Exec
    command="cmd.exe/c"+command;ByteArrayOutputStreamstdout=newByteArrayOutputStream();PumpStreamHandlerpsh=newPumpStreamHandler(stdout);CommandLinecl......