golang 敏感字段加密

一道华为机考题,当时没做出,思绪太乱了

微信图片_20220513224137.jpg
package main

import (
    "bufio"
    "fmt"
    "os"
    "strconv"
)

type Stack struct {
    list []byte
}

func main() {
    var command string
    input := bufio.NewScanner(os.Stdin)
    input.Scan()
    n, _ := strconv.Atoi(input.Text())
    input.Scan()
    command = input.Text()
    res := dealCommand(command,n)
    fmt.Println(res)
}

func dealCommand(s string,n int) string {
    var s1 string
    var s2 string
    var tags []int
    stack := NewStack()
    var i,j = 0,len(s)-1
    // 整理字符串,去除字符串首位下划线
    for s[i] == '_' {
        i++
    }
    for s[j] == '_' {
        j--
    }
    s1 = s[i:j+1]
    //继续整理字符串,对双引号括着的字段外的下划线去重
    for i =0; i< len(s1); i++ {
        if i>0 && s1[i]=='_' && s1[i-1] =='_' && stack.IsEmpty(){
            continue
        }
        if s1[i] == '"' && stack.IsEmpty() { //如果栈是空的,遇到双引号进栈
            stack.Push(s1[i])
            s2 = s2 + string(s1[i])
            continue
        }
        if s1[i] == '"' && !stack.IsEmpty() { //轮到下一个双引号来了必出栈
            stack.Pop()
            s2 = s2 + string(s1[i])
            continue
        }
        s2 = s2 + string(s1[i])
    }
    for i =0;i

下面是输入输出(前两行输入,后一行输出)

6
__assd_1342___jsjd_"skdd__ddd"_jss_44__""_
assd_1342_jsjd_"skdd__ddd"_jss_44_******
3
__assd_1342___jsjd_"skdd__ddd"_jss_44__""_
assd_1342_jsjd_******_jss_44_""
0
__assd_1342___jsjd_"skdd__ddd"_jss_44__""_
******_1342_jsjd_"skdd__ddd"_jss_44_""

你可能感兴趣的:(golang 敏感字段加密)