Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
Snowleopard_wu
Java
提交
a19d8b25
J
Java
项目概览
Snowleopard_wu
/
Java
与 Fork 源项目一致
从无法访问的项目Fork
通知
1
Star
0
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
J
Java
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
前往新版Gitcode,体验更适合开发者的 AI 搜索 >>
未验证
提交
a19d8b25
编写于
10月 14, 2020
作者:
R
Ray S
提交者:
GitHub
10月 14, 2020
浏览文件
操作
浏览文件
下载
差异文件
Merge pull request #1713 from matteomessmer/LongestPalindromicSubsequence
Fixes: #1709 Longest palindromic subsequence
上级
d7a5dbe3
60c0291e
变更
2
隐藏空白更改
内联
并排
Showing
2 changed file
with
90 addition
and
0 deletion
+90
-0
DynamicProgramming/LongestPalindromicSubsequence.java
DynamicProgramming/LongestPalindromicSubsequence.java
+57
-0
DynamicProgramming/LongestPalindromicSubsequenceTests.java
DynamicProgramming/LongestPalindromicSubsequenceTests.java
+33
-0
未找到文件。
DynamicProgramming/LongestPalindromicSubsequence.java
0 → 100644
浏览文件 @
a19d8b25
package
test
;
import
java.lang.*
;
import
java.io.*
;
import
java.util.*
;
/**
* Algorithm explanation https://www.educative.io/edpresso/longest-palindromic-subsequence-algorithm
*/
public
class
LongestPalindromicSubsequence
{
public
static
void
main
(
String
[]
args
)
{
String
a
=
"BBABCBCAB"
;
String
b
=
"BABCBAB"
;
String
aLPS
=
LPS
(
a
);
String
bLPS
=
LPS
(
b
);
System
.
out
.
println
(
a
+
" => "
+
aLPS
);
System
.
out
.
println
(
b
+
" => "
+
bLPS
);
}
public
static
String
LPS
(
String
original
)
throws
IllegalArgumentException
{
StringBuilder
reverse
=
new
StringBuilder
(
original
);
reverse
=
reverse
.
reverse
();
return
recursiveLPS
(
original
,
reverse
.
toString
());
}
private
static
String
recursiveLPS
(
String
original
,
String
reverse
)
{
String
bestResult
=
""
;
//no more chars, then return empty
if
(
original
.
length
()
==
0
||
reverse
.
length
()
==
0
)
{
bestResult
=
""
;
}
else
{
//if the last chars match, then remove it from both strings and recur
if
(
original
.
charAt
(
original
.
length
()
-
1
)
==
reverse
.
charAt
(
reverse
.
length
()
-
1
))
{
String
bestSubResult
=
recursiveLPS
(
original
.
substring
(
0
,
original
.
length
()
-
1
),
reverse
.
substring
(
0
,
reverse
.
length
()
-
1
));
bestResult
=
reverse
.
charAt
(
reverse
.
length
()
-
1
)
+
bestSubResult
;
}
else
{
//otherwise (1) ignore the last character of reverse, and recur on original and updated reverse again
//(2) ignore the last character of original and recur on the updated original and reverse again
//then select the best result from these two subproblems.
String
bestSubResult1
=
recursiveLPS
(
original
,
reverse
.
substring
(
0
,
reverse
.
length
()
-
1
));
String
bestSubResult2
=
recursiveLPS
(
original
.
substring
(
0
,
original
.
length
()
-
1
),
reverse
);
if
(
bestSubResult1
.
length
()>
bestSubResult2
.
length
())
{
bestResult
=
bestSubResult1
;
}
else
{
bestResult
=
bestSubResult2
;
}
}
}
return
bestResult
;
}
}
\ No newline at end of file
DynamicProgramming/LongestPalindromicSubsequenceTests.java
0 → 100644
浏览文件 @
a19d8b25
package
test
;
import
static
org
.
junit
.
jupiter
.
api
.
Assertions
.*;
import
org.junit.jupiter.api.Test
;
class
LongestPalindromicSubsequenceTests
{
@Test
void
test1
()
{
assertEquals
(
LongestPalindromicSubsequence
.
LPS
(
""
),
""
);
}
@Test
void
test2
()
{
assertEquals
(
LongestPalindromicSubsequence
.
LPS
(
"A"
),
"A"
);
}
@Test
void
test3
()
{
assertEquals
(
LongestPalindromicSubsequence
.
LPS
(
"BABCBAB"
),
"BABCBAB"
);
}
@Test
void
test4
()
{
assertEquals
(
LongestPalindromicSubsequence
.
LPS
(
"BBABCBCAB"
),
"BABCBAB"
);
}
@Test
void
test5
()
{
assertEquals
(
LongestPalindromicSubsequence
.
LPS
(
"AAAAAAAAAAAAAAAAAAAAAAAA"
),
"AAAAAAAAAAAAAAAAAAAAAAAA"
);
}
}
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录