首页
\
问答
\
我可以使用自顶向下的动态编程方法打印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);
相关问答
更多-
在使用Spring 3 + Hibernate JPA时发现JPA注释类(Discover JPA annotated classes when using Spring 3+Hibernate JPA)[2023-07-08]
你可以把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 ...
-
如何在EJB3(JPA)和Hibernate中使用@Id注释字段?(How to get the field annotated with @Id in EJB3 (JPA) and Hibernate?)[2021-08-21]
到目前为止,有一个比上市更短的方法: Reflections r = new Reflections(this.getClass().getPackage().getName()); Setfields = 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 ...
-
到EJB3与否?(To EJB3 or not?)[2023-12-07]
我看不出如何允许JPA,但Hibernate不会。 鉴于Hibernate是一个JPA实现,这没有多大意义。 另一个JPA实现是否可以,例如OpenJPA? 您没有提到是否有现有服务器,或者是否会在尚未选择的服务器上安装您的应用程序。 这个决定已经做出了吗? 如果是这样,那可能决定了您的选择。 完整,最新的JavaEE服务器将随附EJB3和JPA实现。 如果服务器不是支持EJB3的服务器,那么EJB3就不那么引人注目了。 但是如果排除了诸如Spring和Hibernate之类的敏感技术,那么像iBatis这 ... -
JSF / EJB3避免了视图层中的延迟初始化异常(JSF / EJB3 avoiding lazy initialization exceptions in the view layer)[2022-08-05]
有两种处理惰性关联的方法。 第一种方法是使用以下方法初始化实体: 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 ...
-
如何在spring框架和EJB3之间做出选择[重复](how to make choice between spring framework and EJB3 [duplicate])[2022-03-07]
我想知道EJB和spring框架的优缺点 两者都很轻,基于POJO,你可以从类似的线程得到一个很好的图片。 您可能会发现EJB 2.x中存在的许多痛点已不复存在。 'EJB'可能会为您简化一些事情 如果您的应用程序需要远程客户端启动的分布式事务 如果您的应用程序是消息驱动的 现在我的问题是,我应该选择什么,带有JPA的EJB3或带有Hibernate的spring框架。 另外看看将CDI添加到堆栈(即EJB3.x + JPA + CDI)然后与(Spring + Hibernate)进行比较 And I w ...