当前位置:网站首页>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 :
边栏推荐
- Cap: multiple attention mechanism, interesting fine-grained classification scheme | AAAI 2021
- Install the imagemagick7.1 library and the imageick extension for PHP
- One article explains Jackson configuration information in detail
- Two problems of qtreewidget returning as DLL in singleton mode
- 使用阿里云RDS for SQL Server性能洞察优化数据库负载-初识性能洞察
- Summary of common tools and usage
- How to easily realize online karaoke room and sing "mountain sea" with Wang Xinling
- 【Prometheus】6. Prometheus and kubernetes (incomplete)
- Arrays API
- How to efficiently transfer enterprise business data?
猜你喜欢

CAP:多重注意力机制,有趣的细粒度分类方案 | AAAI 2021

【应用推荐】最近大火的Apifox & Apipost 上手体验与选型建议

Solution of intelligent all in one machine in expressway service area

MySQL binlog
![[download attached] installation and simple use of Chinese version of awvs](/img/3b/f26617383690c86edff465c9a1099e.png)
[download attached] installation and simple use of Chinese version of awvs

使用阿里云RDS for SQL Server性能洞察优化数据库负载-初识性能洞察

【面试高频题】难度 3/5,可直接构造的序列 DP 题

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

I just came back from the Ali software test. I worked for Alibaba P7 in 3+1, with an annual salary of 28*15

Mysql之Binlog
随机推荐
The equipment is connected to the easycvr platform through the national standard gb28181. How to solve the problem of disconnection?
FreeRTOS新建任务不执行问题解决办法
April 23, 2021: there are n cities in the TSP problem, and there is a distance between any two cities
如何轻松实现在线K歌房,与王心凌合唱《山海》
Nature刊登量子计算重大进展:有史以来第一个量子集成电路实现
QoS Technology in network
Very exciting! 12000 words summarized the theory of network technology, reviewing the old and learning the new
【云原生 | Kubernetes篇】Kubernetes基础入门(三)
Build go command line program tool chain
2021-05-04: given a non negative integer C, you need to judge whether there are two integers a and B, so that a*a+b*b=c.
QTreeWidget作为单例模式以dll返回的两个问题
The first in China! Tencent cloud key management system passes password application verification test
Is it safe for futures companies to open accounts
MongoDB入门实战教程:学习总结目录
Solution to the problem that FreeRTOS does not execute new tasks
MongoDB入門實戰教程:學習總結目錄
【面试高频题】难度 3/5,可直接构造的序列 DP 题
Paper: Google TPU
Design of CAN bus controller based on FPGA (Part 2)
用 Oasis 开发一个跳一跳(一)—— 场景搭建