当前位置:网站首页>A series of problems of dp+ backtracking segmentation palindrome string
A series of problems of dp+ backtracking segmentation palindrome string
2022-07-24 03:18:00 【Curry won't throw three points】
class Solution { public int minCut(String s) { int n=s.length(); // For length is n String , use [1,n] Express int []dp=new int[n+1];//dp[i] From s[0...i] Minimum number of divisions for (int i = 1; i <=n; i++) { if(isHuiwen(s.substring(0,i))){ // If it's a palindrome string , Then this paragraph does not need to be divided dp[i]=0; }else { dp[i]=i-1;// First set a maximum number of divisions (i Maximum segmentation of characters i-1 Time ) for (int j = 1; j <=i ; j++) { if(isHuiwen(s.substring(j-1,i))){ // String segmentation is left closed and right open [j,i-1) dp[i]=Math.min(dp[i],dp[j-1]+1); } } } } return dp[n]; } private boolean isHuiwen(String substring) { int start=0; int end=substring.length()-1; while (start<end){ if (substring.charAt(start)!=substring.charAt(end)){ return false; } end--; start++; } return true; } }Optimize : In fact, dynamic programming can also be used to judge palindrome strings
- base case The length is 1 It must be a string
- String is 2, The same two characters are also palindromes
class Solution { public int minCut(String s) { int n=s.length(); int dp[]=new int[n+1]; boolean[][]g=new boolean[n+1][n+1];//g[l][r] Express s[l...r] Is it a palindrome string It also stipulates that the subscript of the first character is 1 for (int r =1; r <=n; r++) { for (int l =r ; l>=1 ; l--) { if(l==r){ g[l][r]=true; // One character is palindrome string }else { if (s.charAt(l-1)==s.charAt(r-1)){ if(r-l==1||g[l+1][r-1]){ g[l][r]=true; // If there are only two characters , The description is palindrome string // perhaps a b b a a==a when ,,bb It's also a palindrome string that abba Palindrome string } } } } } for (int i = 1; i <=n; i++) { if(g[1][i]){ dp[i]=0; // If [1,i] It's a palindrome string , Then there is no need to split }else { dp[i]=i-1; for (int l = 1; l <=n ; l++) { if (g[l][i]){ dp[i]=Math.min(dp[i],dp[l-1]+1); } } } } return dp[n]; } }
边栏推荐
- MariaDB related instructions
- JVM初始
- Binary tree traversal
- B. Eastern Exhibition- Codeforces Round #703 (Div. 2)
- Open source quantum development framework cirq
- The first edition of Niuke brush question series (automorphic number, return the number of prime numbers less than N, and the first character only appears once)
- FTP service and configuration
- JS 數組 isAarray() typeof
- Cannot resolve symbol 'override' of idea clone‘
- JS 数组 isAarray() typeof
猜你喜欢

Data Lake: comparative analysis of open source data Lake schemes deltalake, Hudi and iceberg

FTP服務與配置

Qt自定义类使用自定义含参信号与槽

OSPF comprehensive experimental configuration

Services et configurations FTP

C user defined type details

LCD1602 - binge 51

JIRA automation experience sharing for 2 years

Tweenmax+svg Pikachu transformation ball

在openEuler社区开源的Embedded SIG,来聊聊它的多 OS 混合部署框架
随机推荐
JS array isaarray() typeof
The function of SIP account - tell you what is SIP line
Error code 0x80004005
关于Aries框架增删改查-查Demo
JIRA automation experience sharing for 2 years
Ugui source code analysis - iclippable
How does the small program mall refine the operation of members?
Go IO operation - file read
String.split()最详细源码解读及注意事项
Lumberyard game engine of o3de
Unity 消息推送
String.split() the most detailed source code interpretation and precautions
SkyWalking分布式系统应用程序性能监控工具-上
A simple and perfect WPF management system framework source code
Connected graph (day 72)
轮播图van-swipe的报错:cannot read a properties of null(reading width)
kettle
Basic use of Pinia
Summernote font displays Chinese
SSM based blog system [with background management]


