当前位置:网站首页>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 :
边栏推荐
- 八大误区,逐个击破(终篇):云难以扩展、定制性差,还会让管理员失去控制权?
- How to write a great online user manual in 7 steps
- Leaders of Hangcheng street, Bao'an District and their delegation visited lianchengfa for investigation
- Tcp/udp Fundamentals
- Emmet语法规范
- How to open a domestic futures account? Which futures company is safer to open an account?
- LeetCode 473. 火柴拼正方形
- 教你如何用网页开发APP
- Technology sharing | wvp+zlmediakit realizes streaming playback of camera gb28181
- 国元期货交易软件正规吗?如何安全下载?
猜你喜欢

图扑软件数字孪生智慧水务,突破海绵城市发展困境

LeetCode 1079. movable-type printing

Leaders of Hangcheng street, Bao'an District and their delegation visited lianchengfa for investigation

技术分享| WVP+ZLMediaKit实现摄像头GB28181推流播放

Rstudio 1.4 software installation package and installation tutorial

RStudio 1.4软件安装包和安装教程

Why is only one value displayed on your data graph?

金九银十,靠这个细节,offer拿到手软!

官宣.NET 7 预览版5

GL Studio 5 安装与体验
随机推荐
实现vscode写markdown文档+图片自动上传至腾讯云cos
How to separate image processing? What should I pay attention to when separating layers?
[golang] quick review guide quickreview (VIII) -- goroutine
The "open source star picking program" container pulls private images from harbor, which is a necessary skill for cloud native advanced technology
GL Studio 5 安装与体验
Can the biggest gamefi crash victim survive the bear market in May| May Monthly Report
【Golang】快速复习指南QuickReview(一)——字符串string
重庆 奉节耀奎塔,建成后当地连中五名进士,是川江航运的安全塔
【Golang】快速复习指南QuickReview(六)——struct
How to use data warehouse to create time series
Tcp/udp Fundamentals
国内期货开户怎么开?哪家期货公司开户更安全?
Postman tutorial - teach you API interface testing by hand
Use of the vs2022scanf function. An error is reported when using scanf - the return value is ignored: Solutions
@@Script implementation of ishell automatic deployment
Yaokui tower in Fengjie, Chongqing, after its completion, will be the safety tower for Sichuan river shipping with five local scholars in the company
Logstash start -r parameter
【Golang】快速复习指南QuickReview(七)——interface
20 provinces and cities announce the road map of the meta universe
Are internal consultants and external consultants in SAP implementation projects difficult or successful?