Beispiel #1
0
func main() {
	scanner := bufio.NewScanner(os.Stdin)
	scanner.Split(bufio.ScanWords)
	nums := []int{}
	for scanner.Scan() {
		x, err := strconv.Atoi(scanner.Text())
		if err != nil {
			log.Fatal(err)
		}
		nums = append(nums, x)
	}

	if err := scanner.Err(); err != nil {
		log.Fatal(err)
	}

	p := nums[1:]

	lis := seq.LongestIncreasingSubseqInts(p)
	display(lis)

	lds2 := seq.LongestIncreasingSubseqIndex(sort.Reverse(sort.IntSlice(p)))
	{
		r := make([]int, len(lds2))
		for i, ri := range lds2 {
			r[i] = p[ri]
		}
		display(r)
	}
}
Beispiel #2
0
func TestLongestIncSubseq(t *testing.T) {
	for k := 0; k < 100; k++ {
		fmt.Print(".")
		// Random permutation
		n := 10 + rand.Intn(11)
		p := make([]int, n)
		for i := range p {
			p[i] = i + 1
		}
		// Shuffle
		for i := 0; i < len(p)-1; i++ {
			r := rand.Intn(len(p) - 1 - i)
			j := i + 1 + r
			p[i], p[j] = p[j], p[i]
		}

		fast := seq.LongestIncreasingSubseqInts(p)
		slow := seq.SlowLGIS(p)
		if len(fast) != len(slow) {
			t.Fatalf("LGIS(%v): expected %v, got %v", p, slow, fast)
		}
	}
}