当前位置:网站首页>December 29, 2021: the elimination rules of a subsequence are as follows: 1. In a subsequence
December 29, 2021: the elimination rules of a subsequence are as follows: 1. In a subsequence
2022-06-23 20:34:00 【Fuda scaffold constructor's daily question】
2021-12-29: The elimination rule of a subsequence is as follows :
1、 In a subsequence , If '1' On the left '0', So these two characters ->"01" Can eliminate ;
2、 In a subsequence , If '3' On the left '2', So these two characters ->"23" Can eliminate ;
3、 When a part of this subsequence is eliminated , Think that other characters will automatically stick together , You can continue to look for opportunities to eliminate .
such as , Some subsequence "0231", First eliminate "23", Then the rest of the characters are pasted together and become "01", If you continue to erase, there will be no characters ,
If a subsequence passes the best way , Can be eliminated , So this subsequence is called “ Total elimination subsequence ”,
One consists only of '0'、'1'、'2'、'3' A string of four characters str, Many subsequences can be generated , return “ Total elimination subsequence ” Maximum length of ,
character string str length <= 200.
Real written test , Forget which company , But it's absolutely big .
answer 2021-12-29:
recursive .
The code to use golang To write . The code is as follows :
package main
import "fmt"
func main() {
str1 := "010101"
fmt.Println(maxDisappear(str1))
str2 := "021331"
fmt.Println(maxDisappear(str2))
}
// str[L...R] On , Subsequences that can be eliminated , What's the longest ?
func f(str string, L, R int) int {
if L >= R {
return 0
}
if L == R-1 {
if (str[L] == '0' && str[R] == '1') || (str[L] == '2' && str[R] == '3') {
return 2
} else {
return 0
}
//return (str[L] == '0' && str[R] == '1') || (str[L] == '2' && str[R] == '3') ? 2 : 0;
}
// L...R There are several characters > 2
// str[L...R] On , Subsequences that can be eliminated , What's the longest ?
// possibility 1, Subsequences that can be eliminated are not considered at all str[L], What's the longest ?
p1 := f(str, L+1, R)
if str[L] == '1' || str[L] == '3' {
return p1
}
// str[L] =='0' perhaps '2'
// '0' Look for '1'
// '2' Look for '3'
find := byte('1')
if str[L] != '0' {
find = '3'
}
//find := str[L] == '0' ? '1' : '3';
p2 := 0
// L() ......
for i := L + 1; i <= R; i++ {
// L(0) ..... i(1) i+1....R
if str[i] == find {
p2 = getMax(p2, f(str, L+1, i-1)+2+f(str, i+1, R))
}
}
return getMax(p1, p2)
}
func maxDisappear(str string) int {
if len(str) == 0 {
return 0
}
return disappear(str, 0, len(str)-1)
}
// s[l..r] In terms of scope , As the title says , The length of the longest subsequence that can be eliminated
func disappear(s string, l, r int) int {
if l >= r {
return 0
}
if l == r-1 {
if (s[l] == '0' && s[r] == '1') || (s[l] == '2' && s[r] == '3') {
return 2
} else {
return 0
}
//return (s[l] == '0' && s[r] == '1') || (s[l] == '2' && s[r] == '3') ? 2 : 0;
}
p1 := disappear(s, l+1, r)
if s[l] == '1' || s[l] == '3' {
return p1
}
p2 := 0
find := byte('1')
if s[l] != '0' {
find = '3'
}
//find := s[l] == '0' ? '1' : '3';
for i := l + 1; i <= r; i++ {
if s[i] == find {
p2 = getMax(p2, disappear(s, l+1, i-1)+2+disappear(s, i+1, r))
}
}
return getMax(p1, p2)
}
func getMax(a, b int) int {
if a > b {
return a
} else {
return b
}
}The results are as follows :
边栏推荐
- 80% of people will be wrong about the three counter intuitive questions?
- Command line add user set password never expires add remote group add administrator group
- 增加双因素认证,不惧密码泄露,更不惧123456
- 技术分享| WVP+ZLMediaKit实现摄像头GB28181推流播放
- [golang] delving into strings -- from byte run string to unicode and UTF-8
- 国内期货开户怎么开?哪家期货公司开户更安全?
- [golang] quick review guide quickreview (III) - Map
- How to write a great online user manual in 7 steps
- Why is only one value displayed on your data graph?
- Kotlin jetpack compose Tab的渲染 AnimatedVisibility的使用
猜你喜欢

「开源摘星计划」Containerd拉取Harbor中的私有镜像,云原生进阶必备技能

Interpreting the 2022 agile coaching industry status report

35 year old crisis? It has become a synonym for programmers

Importance and purpose of test

Hardware development notes (6): basic process of hardware development, making a USB to RS232 module (5): creating USB package library and associating principle graphic devices

Use of the vs2022scanf function. An error is reported when using scanf - the return value is ignored: Solutions

Elastricearch's fragmentation principle of the second bullet

Kubernetes 资源拓扑感知调度优化

Zabbix监控- Aruba AP运行数据

Leaders of Hangcheng street, Bao'an District and their delegation visited lianchengfa for investigation
随机推荐
Are internal consultants and external consultants in SAP implementation projects difficult or successful?
The substring() method in. JS can be used to intercept all characters after the specified string
每日刷题记录 (二)
Tcp/udp Fundamentals
CPS 22 January additional incentive rules
Newbeecoder. UI new open source control library DataGrid instructions
【Golang】跟着源码学技巧系列之对象池sync.Pool
八大误区,逐个击破(终篇):云难以扩展、定制性差,还会让管理员失去控制权?
【Golang】深究字符串——从byte rune string到Unicode与UTF-8
Why is only one value displayed on your data graph?
Eight misunderstandings, broken one by one (final): the cloud is difficult to expand, the customization is poor, and the administrator will lose control?
Use of the vs2022scanf function. An error is reported when using scanf - the return value is ignored: Solutions
「开源摘星计划」Containerd拉取Harbor中的私有镜像,云原生进阶必备技能
Official announcement. Net 7 preview 5
UGeek大咖说 | 可观测之超融合存储系统的应用与设计
Goldfish rhca memoirs: do447 managing user and team access -- effectively managing users with teams
35岁危机?内卷成程序员代名词了…
How to dispose of the words on the picture? How do I add text to a picture?
Kubernetes 资源拓扑感知调度优化
【Golang】快速复习指南QuickReview(六)——struct