Longest repeating string Golang

Source: Internet
Author: User

The longest repeating string puzzle

Package Mainimport ("FMT" "strings") type Index map[int]inttype Counter map[string]indexvar c = Make (Counter) func SE Trecord (Match string, Index int) {i, OK: = C[match] If!ok {i = make (index) c[match] = i retu     RN} I[index]++}func Filteroverlap () {var keys []string for k: = range c {keys = append (keys, k)} For _, Xkey: = Range keys {for k: = Range C {if strings. Hasprefix (Xkey, k) && Xkey! = k {//FMT. Printf ("Delete key=%v\n", k) Delete (c, k) continue}}}}func Calc (samp Le []byte) {var i, N, xi, XJ, Yi, yj int n = len (sample) var A, b string for i = 0; i < n; i++ {fo R XI = i; Xi < n-1;                    xi++ {for XJ = XI + 1, XJ < n; xj++ {for yi = xj + 1; Yi < n; yi++ { YJ = Yi + (xj-xi) A = string (Sample[xi:xj]) b = string (sample[Yi:yj])                If a = = b {Setrecord (A, xi) Setrecord (b, Yi)} }}}}}func main () {var s = "12342342341234" Calc ([]byte (s)) Filteroverlap () for K, V: = range C {fmt. Printf ("%v:%v\n", K, Len (v))}}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.