Skip to content
Navigation Menu
Sign in
Products
Platform
GitHub
Repository hosting, code review, and collaboration
Copilot
AI coding agent for tasks, projects and automated work
Build & Ship
Actions
CI/CD and automation
Code Quality
Quality analysis, fixes, and enforcement
Codespaces
Cloud development environments
Secure
Code Security
Vulnerability detection and remediation
Secret Protection
Secret scanning and leak prevention
All Features
Developers
Learn
Documentation
Training
Certifications
Build
Build with Copilot
Customize Copilot
Integrations
Explore
Trending repos
GitHub Sponsors
Developer articles
Changelog
Changelog
Your source for the latest features, improvements, and fixes across GitHub products, all in one place.
Businesses
By Stage
Enterprises
Team
Startups
Nonprofits
By Industry
Healthcare
Financial services
Manufacturing
Government
See all industries
Evaluate GitHub
Why GitHub
Customer Stories
Business insights
Trust Center
Add-ons
GitHub Advanced Security
Enterprise-grade code security
Copilot for business
Enterprise-grade AI features
Premium Support
Enterprise-grade 24/7 support
Resources
Learn & Connect
Documentation
Resource center
Blog
Events & webinars
Community Forum
Customer Stories
Integrations
Support
Customer support
Training
GitHub Status
Company
About
Partners
Changelog
GitHub Universe
GitHub Universe
The event for developers, by developers — talks, workshops, and what’s next for GitHub.
Pricing
Search
/
Sign in
Sign up
Appearance settings
You signed in with another tab or window.
Reload
to refresh your session.
You signed out in another tab or window.
Reload
to refresh your session.
You switched accounts on another tab or window.
Reload
to refresh your session.
Dismiss alert
{{ message }}
chenwenhang
AlgorithmPractice
Repository navigation
Code
Issues
Pull requests
Actions
Projects
Security and quality
Insights
More
items
Files
Expand file tree
master
Breadcrumbs
AlgorithmPractice
/
src
/
recursion_and_dynamic_programming
/
MaxWordSegmentation.java
Copy path
Blame
More file actions
Blame
More file actions
Latest commit
History
History
History
34 lines (32 loc) · 1.83 KB
master
Breadcrumbs
AlgorithmPractice
/
src
/
recursion_and_dynamic_programming
/
MaxWordSegmentation.java
Copy path
Top
File metadata and controls
Code
Blame
34 lines (32 loc) · 1.83 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
package recursion_and_dynamic_programming;
/**
* @Author: Wenhang Chen
* @Description:在自然语言处理中一个重要的问题是分词,例如句子“他说的确实在理”中“的确”“确实”“实在”“在理”都是常见的词汇,但是计算机必须为给定的句子准确判断出正确分词方法。一个简化的分词问题如下:给定一个长字符串y=y1y2…yn,分词是把y切分成若干连续部分,每部分都单独成为词汇。我们用函数quality(x)判断切分后的某词汇x=x1x2…xk的质量,函数值越高表示该词汇的正确性越高。分词的好坏用所有词汇的质量的和来表示。例如对句子“确实在理”分词,quality(确实)+quality(在理)>quality(确)+quality(实在)+quality(理)。请设计一个动态规划算法对字符串y分词,要求最大化所有词汇的质量和。(假定你可以调用quality(x)函数在一步内得到任何长度的词汇的质量)
* @Date: Created in 9:46 11/4/2019
* @Modified by:
*/
/*
* 记:dp[i][j]为i到j的最大词汇质量
* 状态转移方程:dp[i][j] = Math.max(dp[i][j], dp[i][k] + quality(k + 1, j));
*/
public class MaxWordSegmentation {
// 象征性定义一个quality
private int quality(int i, int j) {
// (数据类型)(最小值+Math.random()*(最大值-最小值+1))
return (int) (1 + Math.random() * (i - j + 1));
}
int getMaxValue(String[] word) {
int n = word.length;
int[][] dp = new int[n + 1][n + 1];
// 初始化dp[i][i]
for (int i = 1; i <= n; i++)
dp[i][i] = quality(i, i);
// 开始dp
for (int j = 1; j <= n; j++) {
for (int k = 1; k < j; k++) {
dp[1][j] = Math.max(dp[1][j], dp[1][k] + quality(k + 1, j));
}
}
return dp[1][n];
}
}
You can’t perform that action at this time.