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

我可以使用自顶向下的动态编程方法打印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);

相关问答

更多
  • 你可以把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.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.loadC ...
  • 如果容器支持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, you ...
  • 到目前为止,有一个比上市更短的方法: Reflections r = new Reflections(this.getClass().getPackage().getName()); Set fields = r.getFieldsAnnotatedWith(Id.class); There's a shorter approach than listed so far: Reflections r = new Reflections(this.getClass().getPackage() ...
  • 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 data ...
  • 关于第二个问题:当您创建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 = myS ...
  • 我看不出如何允许JPA,但Hibernate不会。 鉴于Hibernate是一个JPA实现,这没有多大意义。 另一个JPA实现是否可以,例如OpenJPA? 您没有提到是否有现有服务器,或者是否会在尚未选择的服务器上安装您的应用程序。 这个决定已经做出了吗? 如果是这样,那可能决定了您的选择。 完整,最新的JavaEE服务器将随附EJB3和JPA实现。 如果服务器不是支持EJB3的服务器,那么EJB3就不那么引人注目了。 但是如果排除了诸如Spring和Hibernate之类的敏感技术,那么像iBatis这 ...
  • 有两种处理惰性关联的方法。 第一种方法是使用以下方法初始化实体: Hibernate.initialize(proxy) 或者将获取类型设置为EAGER,它将在您加载时获取整个实体。 第二种也是更恰当的方式(在我看来)只要你保留实体就保持实体经理。 这可以使用像这样的@Stateful会话来完成: @Stateful public class UserService { @PersistenceContext(type=EXTENDED) private EntityManager entit ...
  • 我使用hibernate作为ORM / JPA提供程序,因此如果不存在JPA解决方案,则可以提供Hibernate解决方案。 使用@Formula可以实现可接受的解决方案(即获取最新B的Date )。 @Entity public class A { @Id private Long id; @OneToMany (mappedBy="parentA") private Collection allBs; @Formula("(select max(b.som ...
  • 我想知道EJB和spring框架的优缺点 两者都很轻,基于POJO,你可以从类似的线程得到一个很好的图片。 您可能会发现EJB 2.x中存在的许多痛点已不复存在。 'EJB'可能会为您简化一些事情 如果您的应用程序需要远程客户端启动的分布式事务 如果您的应用程序是消息驱动的 现在我的问题是,我应该选择什么,带有JPA的EJB3或带有Hibernate的spring框架。 另外看看将CDI添加到堆栈(即EJB3.x + JPA + CDI)然后与(Spring + Hibernate)进行比较 And I w ...

相关文章

更多

最新问答

更多
  • Runnable上的NetworkOnMainThreadException(NetworkOnMainThreadException on Runnable)
  • C ++ 11 + SDL2 + Windows:多线程程序在任何输入事件后挂起(C++11 + SDL2 + Windows: Multithreaded program hangs after any input event)
  • AccessViolationException未处理[VB.Net] [Emgucv](AccessViolationException was unhandled [VB.Net] [Emgucv])
  • 计算时间和日期差异(Calculating Time and Date difference)
  • 以编程方式标签NSMutableAttributedString swift 4(Label NSMutableAttributedString programmatically swift 4)
  • C#对象和代码示例(C# objects and code examples)
  • 在python中是否有数学nCr函数?(Is there a math nCr function in python? [duplicate])
  • 检索R中列的最大值和第二个最大值的行名(Retrieve row names of maximum and second maximum values of a column in R)
  • 给定md5哈希时如何查找特定文件(How to find specific file when given md5 Hash)
  • Python字典因某些原因引发KeyError(Python Dictionary Throwing KeyError for Some Reason)
  • 如何让Joomla停止打开新标签中的每个链接?(How do I get Joomla to stop opening every link in a new tab?)
  • DNS服务器上的NS记录不匹配(Mismatched NS records at DNS server)
  • Python屏幕捕获错误(Python screen capture error)
  • 如何在帧集上放置div叠加?(How to put a div overlay over framesets?)
  • 页面刷新后是否可以保留表单(html)内容数据?(Is it possible to retain the form(html) content data after page refreshed?)
  • 使用iTeardownMyAppFrame和iStartMyAppInAFrame在OPA5测试中重新启动应用程序超时(Restart app within OPA5 test using iTeardownMyAppFrame and iStartMyAppInAFrame timed out)
  • 自动拆分文本内容到列(Automatically splitting text content into even columns)
  • 在r中的循环中将模型名称分配给gbm.step(assigning model names to gbm.step in loop in r)
  • 昆明哪里有电脑等级考试二级C培训?
  • C ++模板实例化,究竟是什么意思?(C++ template instantiation, what exactly does it mean?)
  • 帮助渲染来自fields_for的部分内容(Help to render a partial from fields_for)
  • 将url.action作为json对象返回mvc(return url.action as json object mvc)
  • 使用.BAT中的.application文件类型运行ac#Console App(Run a c# Console App with .application file type from a .BAT)
  • 将bindingRedirect添加到.Net标准库(Adding a bindingRedirect to a .Net Standard library)
  • Laravel版本升级会影响您的控制器吗?(Laravel version upgrade affects your controller?)
  • imaplib.error:命令SEARCH在状态AUTH中非法,只允许在SELECTED状态(imaplib.error: command SEARCH illegal in state AUTH, only allowed in states SELECTED)
  • 如何在eclipse debug impala前端
  • 如何通过Ajax API处理多个请求?(How to handle multiple requests through an Ajax API? [closed])
  • 使用Datetime索引来分析数据框数据(Using Datetime indexing to analyse dataframe data)
  • JS 实现一个菜单效果