当前位置:网站首页>2021-04-27: if the adjacent position of a character does not have the same character
2021-04-27: if the adjacent position of a character does not have the same character
2022-06-24 15:55:00 【Fuda scaffold constructor's daily question】
2021-04-27: If there is no same character in the adjacent position of a character , Then the character in this position cannot be eliminated . such as :"ab", among a and b Can not be eliminated . If a character has the same character adjacent to it , Can be eliminated together . such as :“abbbc”, The middle string b Can be eliminated , After elimination, there are still “ac”. If some characters are eliminated , The rest of the characters are thought to lean back together . Given a string , You can decide the order of elimination of each step , The goal is to eliminate as many characters as possible , Returns the minimum number of remaining characters . such as :"aacca", If you eliminate the leftmost first "aa", Then there will be "cca", And then put "cc" Eliminate , The rest "a" Will no longer be able to eliminate , return 1. But if you eliminate the middle first "cc", Then there will be "aaa", Finally, all of them are eliminated, and there is no character left , return 0, This is the best solution . Another example :"baaccabb", If you eliminate the leftmost two first a, be left over "bccabb", If you eliminate the two leftmost c, be left over "babb", Finally, eliminate the two on the far right b, be left over "ba" Can no longer eliminate , return 2. And the best strategy is : First eliminate the two in the middle c, be left over "baaabb", Eliminate the middle three a, be left over "bbb", Finally, eliminate three b, Leave no characters , return 0, This is the best solution .
Fuda answer 2021-04-27:
Dynamic programming .
The code to use golang To write . The code is as follows :
package main
import (
"fmt"
"math"
)
func main() {
s := "aabbac"
ret := restMin(s)
fmt.Println(ret)
}
// A dynamic programming version of a good attempt
func restMin(s string) int {
if len(s) < 2 {
return len(s)
}
N := len(s)
dp := make([][][]int, N)
for i := 0; i < N; i++ {
dp[i] = make([][]int, N)
for j := 0; j < N; j++ {
dp[i][j] = make([]int, 2)
}
}
for i := 0; i < N; i++ {
for j := 0; j < N; j++ {
for k := 0; k < 2; k++ {
dp[i][j][k] = -1
}
}
}
return dpProcess(s, 0, N-1, false, dp)
}
func twoSelectOne(c bool, a int, b int) int {
if c {
return a
} else {
return b
}
}
func getMin(a int, b int) int {
if a < b {
return a
} else {
return b
}
}
func dpProcess(str string, L int, R int, has bool, dp [][][]int) int {
if L > R {
return 0
}
K := twoSelectOne(has, 1, 0)
if dp[L][R][K] != -1 {
return dp[L][R][K]
}
ans := 0
if L == R {
ans = twoSelectOne(K == 0, 1, 0)
} else {
index := L
all := K
for index <= R && str[index] == str[L] {
all++
index++
}
way1 := twoSelectOne(all > 1, 0, 1) + dpProcess(str, index, R, false, dp)
way2 := math.MaxInt64
for split := index; split <= R; split++ {
if str[split] == str[L] && str[split] != str[split-1] {
if dpProcess(str, index, split-1, false, dp) == 0 {
way2 = getMin(way2, dpProcess(str, split, R, all > 0, dp))
}
}
}
ans = getMin(way1, way2)
}
dp[L][R][K] = ans
return ans
}The results are as follows :
边栏推荐
- Using alicloud RDS for SQL Server Performance insight to optimize database load - first understanding of performance insight
- 60 个神级 VS Code 插件!!
- 60 divine vs Code plug-ins!!
- Very exciting! 12000 words summarized the theory of network technology, reviewing the old and learning the new
- leetcode 139. Word Break 单词拆分(中等)
- From practical teaching to competition exercise, Tencent experts personally teach Ti-One platform operation strategy!
- [my advanced OpenGL learning journey] learning notes of OpenGL coordinate system
- Jenkins 镜像无法更新插件中心的3种解决方法
- Jenkins的便捷式安装
- MongoDB入門實戰教程:學習總結目錄
猜你喜欢

日志记录真没你想的那么简单

Mysql之Binlog

Mongodb Getting started Practical Tutoriel: Learning Summary Table des matières

实现领域驱动设计 - 使用ABP框架 - 领域逻辑 & 应用逻辑

MongoDB入門實戰教程:學習總結目錄

一文详解JackSon配置信息

60 divine vs Code plug-ins!!

The penetration of 5g users of operators is far slower than that of 4G. The popularity of 5g still depends on China Radio and television

几种常见的DoS攻击

The catch-up of domestic chips has scared Qualcomm, the leader of mobile phone chips in the United States, and made moves to cope with the competition
随机推荐
Remote connection raspberry pie in VNC Viewer Mode
Learning these 10 kinds of timed tasks, I'm a little floating
MySQL toolset: the official export tool mysqlpump
2021-04-25: given an array arr and a positive number m, the
FreeRTOS新建任务不执行问题解决办法
安裝ImageMagick7.1庫以及php的Imagick擴展
几种常见的DoS攻击
为什么企业实施WMS仓储管理系统很容易失败
【Prometheus】5. Alertmanager alarm (incomplete)
QTreeWidget作为单例模式以dll返回的两个问题
Design of CAN bus controller based on FPGA (Part 2)
leetcode 139. Word Break 单词拆分(中等)
Golang+redis reentrant lock
国产最长寿的热销手机,苹果也不是对手,总算让国产手机找回面子
【面试高频题】难度 3/5,可直接构造的序列 DP 题
Arrays API
Two problems of qtreewidget returning as DLL in singleton mode
The penetration of 5g users of operators is far slower than that of 4G. The popularity of 5g still depends on China Radio and television
2021-04-24: handwriting Code: topology sorting.
Implement Domain Driven Design - use ABP framework - domain logic & application logic