This is a creation in Article, where the information may have evolved or changed.
Topic Link http://hero.csdn.net/Question/Details?ID=215&ExamID=210
Topic details
The competition is by Microsoft Bing Dictionary title, Bing Dictionary (Http://cn.bing.com/dict/?form=BDVSP4&mkt=zh-CN&setlang=ZH) is Microsoft launches the new Generation English learning engine, It contains a lot of our common words. But in real life, we can often see some irregular strings, resulting in dictionaries can not be properly included, but can we extract the regular word from an irregular string?
For example, there is a string "iinbinbing" that intercepts characters ' B ', ' I ', ' n ', ' G ' in different positions into the word "Bing". If counted from 1, then the 4 letters of the ' B ' i ' n ' ' g ' appear (4,5,6,10) (4,5,9,10), (4,8,9,10) and (7,8,9,10), so that a total of 4 words "Bing" can be combined. Our question is: now given any string, only the lowercase ' b ' i ' N ' G ' 4 letters, how many words can be combined into Bing? The string length does not exceed 10000, because the result may be larger, output the result after the remainder of 10^9 + 7 is taken. The most common thought is the solution of 4 loops, but the low efficiency code is as follows
Package Mainimport ("FMT" "Math/rand" "Time") const (strlen int = $) Func main () {rand. Seed (time. Now (). Unix ()) Bing: = []string{"B", "I", "N", "G"} var Strall [strlen]string for I: = 0; i < strlen; i++ {Strall[i] = fmt. Sprintf ("%s", Bing[rand. INTN (Len (Bing))])}//strall: = [strlen]string{"i", "I", "n", "B", "I", "n", "B", "I", "N", "G", "G"} FMT. Println (Strall) var (arr_b, Arr_i, Arr_n, Arr_g []int count int64 = 0) for I : = 0; i < strlen; i++ {if strall[i] = = "B" {arr_b = append (Arr_b, i)} else if strall[i] = = "I" {arr_ i = append (Arr_i, i)} else if strall[i] = = "N" {arr_n = append (Arr_n, i)} else if strall[i] = = "G" {arr_g = append (Arr_g, i)}}//fmt. Println (Arr_b, Arr_i, Arr_n, arr_g) for I: = 0; I < Len (arr_b); i++ {for J: = 0, J < Len (Arr_i), J + + {for k: = 0; K < Len (arr_n); k++ {for H: = 0, h < Len (arr_g), h++ {if arr_b[i] < Arr_i[j] && Arr_i[j] < Arr_n[k] && Arr_n[k] < Arr_g[h] {//fmt. Println (arr_b[i]+1, arr_i[j]+1, arr_n[k]+1, arr_g[h]+1) count++} }}}} fmt. Println (count% 1000000007)}
1000, it's going to be 3-5 seconds, too slow.
The following 2 links are explained by others
Http://www.cnblogs.com/muzihai1988/p/3500383.html
Http://www.cnblogs.com/WhyEngine/p/3522309.html
Declaration: This article is CuZn (www.wulinlw.org) original manuscript, reproduced please retain the copyright