HackerRank "Yet Another KMP Problem" !! wow

本文探讨了一个有趣的KMP算法问题,重点在于算法思维而非技能。文章通过四个步骤详细解析了如何构造具有最少匹配前缀的字符串,并确保该字符串字典序最小。

It is a super interesting problem - it's not about the algorithm SKILLs, it is about algorithm THINKING.

In this problem, the goal is actually to have as less matched prefix as possible. Here is the thinking process:

1. How does KMP (prefix jump table calc.) work? It always starts with the first char - only if when the first char matches, we continue counting matching.

2. Given #1, we need to reduce the matched cases # to minimum of #1 - that means, the starting char is the char with minimal freq.

3. What's next? Remember we have another requirement: lexicographically smallest, so after the char0 at #2, we can put string like this: aaaa...bb...ccc...

4. But, what if least-freq char is the smallest char, like 'a' ? string like aaaaa..aabbb will have matched count as 1+ 2 +3..., so we need to interleave the aaa...aa, with b - if avail

Strategy is: STEP BY STEP & OBSERVE

转载于:https://www.cnblogs.com/tonix/p/8364682.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值