当前位置:网站首页>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 :
边栏推荐
- [interview high frequency questions] sequential DP questions with difficulty of 3/5 and direct construction
- FreeRTOS新建任务不执行问题解决办法
- 【面试高频题】难度 3/5,可直接构造的序列 DP 题
- MongoDB入门实战教程:学习总结目录
- Remember: never use UTF-8 in MySQL
- How to obtain ECS metadata
- 【Prometheus】5. Alertmanager alarm (incomplete)
- 高速公路服务区智能一体机解决方案
- The first in China! Tencent cloud key management system passes password application verification test
- 【附下载】汉化版Awvs安装与简单使用
猜你喜欢
![clang: warning: argument unused during compilation: ‘-no-pie‘ [-Wunused-command-line-argument]](/img/f0/42f394dbc989d381387c7b953d2a39.jpg)
clang: warning: argument unused during compilation: ‘-no-pie‘ [-Wunused-command-line-argument]
![[C language questions -- leetcode 12 questions] take you off and fly into the garbage](/img/ca/a356a867f3b7ef2814080fb76b9bfb.png)
[C language questions -- leetcode 12 questions] take you off and fly into the garbage

如何轻松实现在线K歌房,与王心凌合唱《山海》

MongoDB入门实战教程:学习总结目录

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

为什么企业实施WMS仓储管理系统很容易失败
![Software test [high frequency] interview questions sorted out by staying up late (latest in 2022)](/img/33/2c2256fd98b908ddaf5573f644ad7f.png)
Software test [high frequency] interview questions sorted out by staying up late (latest in 2022)

Wi-Fi 7 来啦,它到底有多强?

构建Go命令行程序工具链

nifi从入门到实战(保姆级教程)——环境篇
随机推荐
The cold winter can't stop the determination to enter the big factory. The Android interview has a complete knowledge structure, and everything you need to master in the interview is here!
Software test [high frequency] interview questions sorted out by staying up late (latest in 2022)
New de debugging
Several common DoS attacks
存在安全隐患 部分冒险家混动版将召回
2021-04-18: given a two-dimensional array matrix, the value in it is either 1 or 0,
Here comes Wi Fi 7. How strong is it?
Intelij 中的 Database Tools可以连接但是无法显示SCHEMA, TABLES
VNC Viewer方式的远程连接树莓派
Database tools in intelij can connect but cannot display schema, tables
【附下载】汉化版Awvs安装与简单使用
高速公路服务区智能一体机解决方案
"Industry foresight" future development trend of intelligent security monitoring industry
CAP:多重注意力机制,有趣的细粒度分类方案 | AAAI 2021
Paper: Google TPU
【C语言刷题——Leetcode12道题】带你起飞,飞进垃圾堆
Industry cases of successful digital transformation
How to use nested tags in thymeleaf3 Tags
Nature刊登量子计算重大进展:有史以来第一个量子集成电路实现
Istio FAQ: region awareness does not take effect