5. longest-palindromic-substring, golang

code:

func longestPalindrome(s string) string {
    nLen := len(s)
    if 0 == nLen {
        return ""
    }
    
    /*with no thought of those characters that have many bytes, 
    solve this problem using Manacher's Algorithm, 
    https://blog.csdn.net/sky527759/article/details/102892788, 
	it explains what is the Manacher's Algorithm
    */
    
    //"babad" -> "#b#a#b#a#d#" -> "&#b#a#b#a#d#|", avoid judging out-of-array
    strMaLen := nLen * 2 + 3
    strManacher := make([]byte, strMaLen)
    strManacher[0], strManacher[strMaLen - 2], strManacher[strMaLen - 1] = '&', '#', '|'
    for i, j, endPos := 1, 0, strMaLen - 2; i < endPos; i += 2 {
        strManacher[i], strManacher[i + 1] = '#', s[j]
        j++
    }
    
    //fmt.Printf("nLen:%d, s:%v\n", nLen, s)
    //fmt.Printf("strMaLen:%d, strManacher:%v\n", strMaLen, string(strManacher))
    //return ""
    
    //accomplish Manacher's Algorithm
    //according TGPL, anManacher[0] ~ anManacher[strMaLen - 1] are 0 after initialization
    anManacher := make([]int, strMaLen)
	nMaxLen, nPos := 0, 0
    nMidPos, nRightPos, nRightPosTemp := 0, 0, 0
    for i, endPos := 1, strMaLen - 1; i < endPos; i++ {
        if i < nRightPos {
            anManacher[i] = int(math.Min(float64(anManacher[2 * nMidPos - i]), float64(nRightPos - i + 1)))
        } else {
            anManacher[i] = 1;
        }
        
       //fmt.Printf("i:%d\n", i)
		for strManacher[i - anManacher[i]] == strManacher[i + anManacher[i]] {
			anManacher[i]++
        }
        
		nRightPosTemp = i + anManacher[i] - 1
        if nRightPosTemp > nRightPos {
			nMidPos, nRightPos = i, nRightPosTemp
			
			if anManacher[i] > nMaxLen {
				nPos, nMaxLen = i, anManacher[i]
			}
        }//if nRightPosTemp > nRightPos 
    }//for i, endPos := 1, strMaLen - 1; i < endPos; i++ 
    
    //fmt.Printf("nPos:%d, nMaxLen:%d\n", nPos, nMaxLen)
    return s[(nPos - nMaxLen + 1) / 2 : (nPos + nMaxLen - 1) / 2]
}

result:

在这里插入图片描述

personal opinion:
The Manacher’s algorithm is so sweet, I like it !

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
function pdemodel [pde_fig,ax]=pdeinit; pdetool('appl_cb',5); set(ax,'DataAspectRatio',[1 1 1]); set(ax,'PlotBoxAspectRatio',[1.5 1 1]); set(ax,'XLim',[-1.5 1.5]); set(ax,'YLim',[-1 1]); set(ax,'XTick',[ -1.5,... -1.2,... -0.90000000000000002,... -0.60000000000000009,... -0.30000000000000004,... 0,... 0.30000000000000004,... 0.60000000000000009,... 0.90000000000000002,... 1.2,... 1.5,... ]); set(ax,'YTickMode','auto'); pdetool('gridon','on'); % Geometry description: pderect([-1 0.5 0.5 -0.5],'R1'); set(findobj(get(pde_fig,'Children'),'Tag','PDEEval'),'String','R1') % Boundary conditions: pdetool('changemode',0) pdesetbd(4,... 'dir',... 1,... '1',... '0') pdesetbd(3,... 'dir',... 1,... '1',... '0') pdesetbd(2,... 'dir',... 1,... '1',... '0') pdesetbd(1,... 'dir',... 1,... '1',... '200') % Mesh generation: setappdata(pde_fig,'Hgrad',1.3); setappdata(pde_fig,'refinemethod','regular'); setappdata(pde_fig,'jiggle',char('on','mean','')); setappdata(pde_fig,'MesherVersion','preR2013a'); pdetool('initmesh') % PDE coefficients: pdeseteq(1,... '1.0',... '0.0',... '0',... '1.0',... '0:10',... '0.0',... '0.0',... '[0 100]') setappdata(pde_fig,'currparam',... ['1.0';... '0 ']) % Solve parameters: setappdata(pde_fig,'solveparam',... char('0','1000','10','pdeadworst',... '0.5','longest','0','1E-4','','fixed','Inf')) % Plotflags and user data strings: setappdata(pde_fig,'plotflags',[2 1 1 1 1 1 1 1 0 0 0 1 1 1 1 0 0 1]); setappdata(pde_fig,'colstring',''); setappdata(pde_fig,'arrowstring',''); setappdata(pde_fig,'deformstring',''); setappdata(pde_fig,'heightstring',''); % Solve PDE: pdetool('solve')
07-23
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值