我可以使用自顶向下的动态编程方法打印LCS吗?(Can I print the LCS using top down approach of dynamic programming?)

我知道有一种使用自下而上方法的解决方案。 但我无法在任何地方使用自顶向下的方法找到任何解决方案。 以下是我用于查找LCS中字符数的代码:

#include <bits/stdc++.h>

using namespace std;

int memo[100][100];


int LCS(string a,string b,int x,int y){
    if(x==a.size() || y==b.size())
    { memo[x][y]=0; 

        return 0;

     }

    if(a[x]==b[y])
    {   


        if(memo[x][y]==-1){
            memo[x][y]=LCS(a,b,x+1,y+1);
        }

        return 1+ memo[x][y];

    }
    else
    {       
        if(memo[x][y]==-1)
            memo[x][y]=max(LCS(a,b,x+1,y),LCS(a,b,x,y+1));

           return memo[x][y];
    }
}

int main(int argc, char const *argv[])
{
    string a,b;
    cin>>a>>b;
    memset(memo,-1,sizeof(memo));
    cout<<LCS(a,b,0,0)<<endl;
    return 0;
}

I know there is a solution using the bottom-up approach. But I couldn't find any solution using top-down approach anywhere. Here is the code I'm using for finding the number of characters in LCS:

#include <bits/stdc++.h>

using namespace std;

int memo[100][100];


int LCS(string a,string b,int x,int y){
    if(x==a.size() || y==b.size())
    { memo[x][y]=0; 

        return 0;

     }

    if(a[x]==b[y])
    {   


        if(memo[x][y]==-1){
            memo[x][y]=LCS(a,b,x+1,y+1);
        }

        return 1+ memo[x][y];

    }
    else
    {       
        if(memo[x][y]==-1)
            memo[x][y]=max(LCS(a,b,x+1,y),LCS(a,b,x,y+1));

           return memo[x][y];
    }
}

int main(int argc, char const *argv[])
{
    string a,b;
    cin>>a>>b;
    memset(memo,-1,sizeof(memo));
    cout<<LCS(a,b,0,0)<<endl;
    return 0;
}

原文:https://stackoverflow.com/questions/47013760
2024-01-01 20:01

满意答案

到目前为止,有一个比上市更短的方法:

Reflections r = new Reflections(this.getClass().getPackage().getName());
Set<Field> fields = r.getFieldsAnnotatedWith(Id.class);

There's a shorter approach than listed so far:

Reflections r = new Reflections(this.getClass().getPackage().getName());
Set<Field> fields = r.getFieldsAnnotatedWith(Id.class);

相关问答

更多

在使用Spring 3 + Hibernate JPA时发现JPA注释类(Discover JPA annotated classes when using Spring 3+Hibernate JPA)

你可以把persistence.xml文件放在实体所在的jar文件中。 不需要指定任何东西,它可以自动工作。 You can put the persistence.xml file inside the jar where your entities live. Don't need to specify anything then, it works automagically.

Java EE Web应用程序测试JPA EJB3(Java EE web application Tests JPA EJB3)

例外的关键部分似乎是: java.lang.IllegalStateException: WEB9031: WebappClassLoader unable to load resource [org.postgresql.core.v3.QueryExecutorImpl$1], because it has not yet been started, or was already stopped at org.glassfish.web.loader.WebappClassLoader....

如果使用EJB3 JPA,我是否需要hibernate?(If using EJB3 JPA , do I need hibernate?)

如果容器支持JPA,它会为您提供API( EntityManager和其他),您不关心实现它的是什么。 所以不,你不必使用Hibernate。 一些容器将使用下面的Hibernate,其他EclipseLink等。但是从您的角度来看,您使用的API 正常工作 。 If the container supports JPA, it gives you an API (EntityManager and others), you don't care what implements it. So no...

如何在EJB3(JPA)和Hibernate中使用@Id注释字段?(How to get the field annotated with @Id in EJB3 (JPA) and Hibernate?)

到目前为止,有一个比上市更短的方法: Reflections r = new Reflections(this.getClass().getPackage().getName()); Set<Field> fields = r.getFieldsAnnotatedWith(Id.class); There's a shorter approach than listed so far: Reflections r = new Reflections(this.getClass().getPack...

JPA annotations = EJB3 annotations = Hibernate注释?(JPA annotations = EJB3 annotations = Hibernate annotations?)

JPA(Java Persistence API)注释声明了如何将Java类持久化到数据库。 Hibernate注释是JPA的一个实现,还有一些特定于Hibernate框架的额外注释。 EJB(Enterprise Java Beans)注释与JPA是分开的,用于描述EJB框架中业务逻辑的更一般方面(事务,并发,安全性等) JPA (Java Persistence API) annotations declare how Java classes should be persisted to a...

ejb3 toplink jpa 1.0查询和id序列策略(ejb3 toplink jpa 1.0 querying and id sequence strategy)

关于第二个问题:当您创建Clas实例,持久化并刷新时,ID将自动生成,并分配给Clas实例的ID字段。 由于在事务结束时自动进行刷新,因此调用此事务会话bean方法将返回带有ID的Clas实例: public Clas createClas() { Clas c = new Clas(); // call setters to populate the clas entityManager.persist(c); return c; } 来电代码: Clas c ...

到EJB3与否?(To EJB3 or not?)

我看不出如何允许JPA,但Hibernate不会。 鉴于Hibernate是一个JPA实现,这没有多大意义。 另一个JPA实现是否可以,例如OpenJPA? 您没有提到是否有现有服务器,或者是否会在尚未选择的服务器上安装您的应用程序。 这个决定已经做出了吗? 如果是这样,那可能决定了您的选择。 完整,最新的JavaEE服务器将随附EJB3和JPA实现。 如果服务器不是支持EJB3的服务器,那么EJB3就不那么引人注目了。 但是如果排除了诸如Spring和Hibernate之类的敏感技术,那么像iB...

JSF / EJB3避免了视图层中的延迟初始化异常(JSF / EJB3 avoiding lazy initialization exceptions in the view layer)

有两种处理惰性关联的方法。 第一种方法是使用以下方法初始化实体: Hibernate.initialize(proxy) 或者将获取类型设置为EAGER,它将在您加载时获取整个实体。 第二种也是更恰当的方式(在我看来)只要你保留实体就保持实体经理。 这可以使用像这样的@Stateful会话来完成: @Stateful public class UserService { @PersistenceContext(type=EXTENDED) private EntityManager ...

具有聚合属性的EJB3 / JPA实体(EJB3/JPA entity with an aggregated attribute)

我使用hibernate作为ORM / JPA提供程序,因此如果不存在JPA解决方案,则可以提供Hibernate解决方案。 使用@Formula可以实现可接受的解决方案(即获取最新B的Date )。 @Entity public class A { @Id private Long id; @OneToMany (mappedBy="parentA") private Collection<B> allBs; @Formula("(select max(...

如何在spring框架和EJB3之间做出选择[重复](how to make choice between spring framework and EJB3 [duplicate])

我想知道EJB和spring框架的优缺点 两者都很轻,基于POJO,你可以从类似的线程得到一个很好的图片。 您可能会发现EJB 2.x中存在的许多痛点已不复存在。 'EJB'可能会为您简化一些事情 如果您的应用程序需要远程客户端启动的分布式事务 如果您的应用程序是消息驱动的 现在我的问题是,我应该选择什么,带有JPA的EJB3或带有Hibernate的spring框架。 另外看看将CDI添加到堆栈(即EJB3.x + JPA + CDI)然后与(Spring + Hibernate)进行比较 An...

相关文章

更多

Aspect-Oriented Programming

Programming Methodology: Aspect-Oriented ...

Object Oriented Programming

Some might also contend that inheritance should be ...

[转]Top 20 Programming Lessons I've Learned in 20 Years

This post could be viewed as hard lessons learned f ...

10 Programming Languages You Should Learn Right Now

By Deborah Rothberg September 15, 2006 Knowing a ...

python top project of 2013

Hi Pythonistas! 测试和调试 Testing &amp; Debuggi ...

The Top 40 iPhone Apps of 2010

Editor’s note: This guest post is written by Alex A ...

《实用C语言编程(第三版)》(Practical C Programming, Third Edition)扫描版[PDF]

中文名: 实用C语言编程(第三版) 原名: Practical C Programming, T ...

Storm常见模式——求TOP N

Storm的另一种常见模式是对流式数据进行所谓“streaming top N”的计算,它的特点是持续 ...

《C++程序设计语言 特别版 十周年纪念版》(The C++ Programming Language Special Edition)扫描版[PDF]

中文名: C++程序设计语言 特别版 十周年纪念版 原名: The C++ Programmin ...

最新问答

更多

如何检索Ember.js模型的所有属性(How to retrieve all properties of an Ember.js model)

您可以简单地在模型上使用toJSON方法并从对象中获取密钥。 Ember.keys(model.toJSON()) 请注意,不会返回关键字。 You can simply use toJSON method on model and get the keys from object. Ember.keys(model.toJSON()) Note that will not return you keys for relations.

maven中snapshot快照库和release发布库的区别和作用

在使用maven过程中,我们在开发阶段经常性的会有很多公共库处于不稳定状态,随时需要修改并发布,可能一天就要发布一次,遇到bug时,甚至一天要发布N次。我们知道,maven的依赖管理是基于版本管理的,对于发布状态的artifact,如果版本号相同,即使我们内部的镜像服务器上的组件比本地新,maven也不会主动下载的。如果我们在开发阶段都是基于正式发布版本来做依赖管理,那么遇到这个问题,就需要升级组件的版本号,可这样就明显不符合要求和实际情况了。但是,如果是基于快照版本,那么问题就自热而然的解决了

arraylist中的搜索元素(Search element in arraylist)

您正在尝试搜索主题的arraylist,您需要编写一个小函数来将代码字符串与类的相应字符串进行比较。 您可以通过将其添加到主题类来完成此操作。 示例: @Override public boolean equals(String code) { return code.equals(this.); } 并将比较更改为需要匹配您匹配的代码的成员。 编辑:更简单的方法是将现有代码更改为: if (s.code.equals(codeNo[i])) //

从mysli_fetch_array中获取选定的值并输出(Get selected value from mysli_fetch_array and output)

这当然是一个重复的问题 。 编辑 : 循环执行后, $gg将指向列表中的最后一个值(在本例中为1002)。 我相信您正在尝试访问的用户选择的的值,可以通过以下方式完成: 在edit.php中 : "; while

Windows Phone上的可用共享扩展(Available Share Extensions on Windows Phone)

不可以。您只能作为图片的共享提供商。 对于其他所有内容,您可以为开发人员提供一个可以与您的应用关联的URI方案 ,因此如果他们决定与您分享内容,则可以稍后调用它。 但是,这不是系统范围的共享扩展。 No. You can only act as a share provider for pictures. For everything else, you can give developers an URI scheme that they can associate with your app

如何在命令提示符下将日期设置为文件名(How to set file name as date in command prompt)

首先,对于你的路径字符串,你应该将r前缀作为原始字符串,并且\不被视为转义字符。 其次,您可以使用datetime.datetime.now()来获取当前时间,然后使用strftime()来填充日期。 示例 - import datetime path = r'C:\soa11g\New\abc_%s.zip' % datetime.datetime.now().strftime('%d%m%Y') connect('weblogic','welcome','t3://localhost:700

如何在Laravel 5.2中使用paginate与关系?(How to use paginate with relationships in Laravel 5.2?)

请尝试以下方法: $messages = $user->contact_messages()->paginate(10); Try the following: $messages = $user->contact_messages()->paginate(10);

从iframe访问父页面的id元素(accessing id element of parent page from iframe)

您试图在父窗口上使用jQuery,尽管它没有在该上下文中定义。 它仅在子文档中定义。 而是更改您的子iframe脚本以在父窗口的元素上调用jQuery。 这是一个例子: 父文件 Parent click

linux的常用命令干什么用的

linux和win7不一样,win7都图形界面,都是用鼠标来操作打开,解压,或者关闭。而linux是没有图形界面的,就和命令提示符一样的一个文本框,操作linux里的文件可没有鼠标给你用,打开文件夹或者解压文件之类的操作都要通过linux常用命令。

Feign Client + Eureka POST请求正文(Feign Client + Eureka POST request body)

问题是Feign接口中的方法不能有多个“通用”参数。 您可以拥有任意数量的标头参数,但不能多于一个主体。 由于@RequestBody没有做任何事情,因此除了HttpServletRequest请求变量之外,它不被视为标题而是另一个变量。 所以我不得不改变我的业务逻辑只有一个参数。 The problem was that a method in Feign interface cannot have more than one 'general' argument. you can have