Almost every text editor has a built-in function of center text alignment. The developers of the popular in Berland text editor «Textpad» decided to introduce this functionality into the fourth release of the product.You are to implement the alignment in the shortest possible time. Good luck!

The input file consists of one or more lines, each of the lines contains Latin letters, digits and/or spaces. The lines cannot start or end with a space. It is guaranteed that at least one of the lines has positive length. The length of each line and the total amount of the lines do not exceed 1000.

Format the given text, aligning it center. Frame the whole text with characters «*» of the minimum size. If a line cannot be aligned perfectly (for example, the line has even length, while the width of the block is uneven), you should place such lines rounding down the distance to the left or to the right edge and bringing them closer left or right alternatively (you should start with bringing left). Study the sample tests carefully to understand the output format better.

welcome to the
Codeforces
Beta
Round 5

and
good luck

package main

import (
	"bytes"
	"fmt"
	"io/ioutil"
	"os"
)

func main() {
	bs, _ := ioutil.ReadAll(os.Stdin)
	writer := bytes.NewBuffer(make([]byte, 0, 1000000))

	s := bytes.Split(bs, []byte("\r\n"))
	maxlen := -1
	for i := 0; i < len(s); i++ {
		if maxlen < len(s[i]) {
			maxlen = len(s[i])
		}
	}

	star := make([]byte, maxlen+2)
	for i := 0; i < maxlen+2; i++ {
		star[i] = '*'
	}
	fmt.Fprintf(writer, "%s\n", string(star))

	flag := 0
	for i := 0; i < len(s); i++ {
		if i == len(s)-1 && len(s[i]) == 0 {
			continue
		}
		fmt.Fprintf(writer, "*")
		leftSpace := 0
		if (maxlen % 2) == (len(s[i]) % 2) {
			leftSpace = (maxlen - len(s[i])) / 2
		} else {
			leftSpace = (maxlen-len(s[i]))/2 + flag
			flag = 1 - flag
		}
		for j := 0; j < leftSpace; j++ {
			fmt.Fprintf(writer, " ")
		}
		fmt.Fprintf(writer, "%s", string(s[i]))
		for j := 0; j < maxlen-leftSpace-len(s[i]); j++ {
			fmt.Fprintf(writer, " ")
		}
		fmt.Fprintf(writer, "*\n")
	}
	fmt.Fprintf(writer, "%s\n", string(star))
	writer.WriteTo(os.Stdout)
}

package main

import (
	"bufio"
	"bytes"
	"fmt"
	"os"
	"strings"
)

func main() {

	scanner := bufio.NewScanner(os.Stdin)

	allLines := make([]string, 0, 1000)
	maxLen := 0

	for scanner.Scan() {
		line := scanner.Text()

		if len(line) > maxLen {
			maxLen = len(line)
		}

		allLines = append(allLines, line)
	}

	var buffer bytes.Buffer

	buffer.WriteString(strings.Repeat("*", maxLen+2))
	buffer.WriteByte('\n')

	moreLeft := false

	for _, line := range allLines {
		leftOver := maxLen - len(line)
		leftOverEach := int(leftOver / 2)

		paddingLeft := leftOverEach
		paddingRight := leftOverEach

		if leftOver%2 != 0 && moreLeft {
			paddingLeft = leftOverEach + 1
			paddingRight = leftOverEach
			moreLeft = !moreLeft
		} else if leftOver%2 != 0 {
			paddingLeft = leftOverEach
			paddingRight = leftOverEach + 1
			moreLeft = !moreLeft
		}

		buffer.WriteString("*")
		buffer.WriteString(strings.Repeat(" ", paddingLeft))
		buffer.WriteString(line)
		buffer.WriteString(strings.Repeat(" ", paddingRight))
		buffer.WriteString("*")
		buffer.WriteByte('\n')
	}

	buffer.WriteString(strings.Repeat("*", maxLen+2))
	buffer.WriteByte('\n')

	fmt.Println(buffer.String())
}

package main

import (
	"bufio"
	"fmt"
	"os"
	"strconv"
	"strings"
)

var scanner *bufio.Scanner

func solve() {
	scanner = bufio.NewScanner(os.Stdin)
	const MaxTokenLength = 1024 * 1024
	scanner.Buffer(make([]byte, 0, MaxTokenLength), MaxTokenLength)
	scanner.Split(bufio.ScanLines)

	var lines []string
	mxline := 0
	for i := 0; scanner.Scan(); i++ {
		line := scanner.Text()
		lines = append(lines, line)
		if mxline < len(line) {
			mxline = len(line)
		}
	}

	fmt.Println(strings.Repeat("*", mxline+2))
	goLeft := false
	for _, line := range lines {
		aligns := mxline - len(line)
		laligns := aligns / 2
		raligns := aligns / 2
		if aligns%2 == 1 {
			if goLeft {
				goLeft = false
				laligns++
			} else {
				goLeft = true
				raligns++
			}
		}
		fmt.Print("*",
			strings.Repeat(" ", laligns),
			line,
			strings.Repeat(" ", raligns), "*\n")

	}
	fmt.Println(strings.Repeat("*", mxline+2))
}

func main() {
	solve()
}

func readString() string {
	scanner.Scan()
	return scanner.Text()
}

func readInt() int {
	val, _ := strconv.Atoi(readString())
	return val
}

func readInt64() int64 {
	v, _ := strconv.ParseInt(readString(), 10, 64)
	return v
}

package main

import (
  "os"
  "bufio"
  "strings"
  "strconv"
  "fmt"
)

var scanner *bufio.Scanner

func getI64() int64 {
  scanner.Scan()
  x, _ := strconv.ParseInt(scanner.Text(), 10, 64)
  return x
}
func getI() int {
  return int(getI64())
}
func getF() float64 {
  scanner.Scan()
  x, _ := strconv.ParseFloat(scanner.Text(), 64)
  return x
}
func getS() string {
  scanner.Scan()
  return scanner.Text()
}

func main() {
  reader := bufio.NewReader(os.Stdin)
  writer := bufio.NewWriter(os.Stdout)
  defer writer.Flush()

  lines := []string{}
  n := 0
  for {
    line, err := reader.ReadString('\n')
    if err != nil {
      break
    }
    line = strings.TrimSpace(line)
    lines = append(lines, line)
    if len(line) > n {
      n = len(line)
    }
  }
  stars := make([]byte, n+2)
  for i := 0; i < n+2; i++ {
    stars[i] = '*'
  }
  writer.WriteString(fmt.Sprintf("%s\n", string(stars)))
  padRight := true
  for _, line := range lines {
    pad := n - len(line)
    left, right := pad/2, pad/2
    if pad%2 == 1 {
      if padRight {
        right++
      } else {
        left++
      }
      padRight = !padRight
    }
    writer.WriteString("*")
    for i := 0; i < left; i++ {
      writer.WriteString(" ")
    }
    writer.WriteString(line)
    for i := 0; i < right; i++ {
      writer.WriteString(" ")
    }
    writer.WriteString("*\n")
  }
  writer.WriteString(fmt.Sprintf("%s\n", string(stars)))
}

package main

import (
	"bufio"
	"fmt"
	"os"
)

const star = "*"
const space = " "

func make_stars(size int) string {
	var line string
	for i := 0; i < size; i++ {
		line += star
	}
	return line
}

func make_space(size int) string {
	var line string
	for i := 0; i < size; i++ {
		line += space
	}
	return line
}

func main() {
	scanner := bufio.NewScanner(os.Stdin)
	writer := bufio.NewWriter(os.Stdout)
	defer writer.Flush()
	lines := make([]string, 0, 1001)
	inputs := make([]string, 0, 1001)
	var block_size, input_len int
	for scanner.Scan() {
		ip := scanner.Text()
		inputs = append(inputs, ip)
		input_len = len(ip)
		if input_len > block_size {
			block_size = input_len
		}
	}
	lines = append(lines, make_stars(block_size+2))
	var line string
	var left_margin, right_margin int
	var right_align bool
	for _, ip := range inputs {
		line = ""
		input_len = len(ip)
		line += star
		margin := block_size - input_len
		if margin%2 == 0 {
			left_margin = margin / 2
			right_margin = left_margin
		} else {
			if right_align {
				right_margin = margin / 2
				left_margin = right_margin + 1
				right_align = false
			} else {
				left_margin = margin / 2
				right_margin = left_margin + 1
				right_align = true
			}
		}
		line += make_space(left_margin)
		line += ip
		line += make_space(right_margin)
		line += star
		lines = append(lines, line)
	}
	lines = append(lines, make_stars(block_size+2))
	for _, l := range lines {
		fmt.Fprintln(writer, l)
	}
}

package main

import (
	"bufio"
	"os"
	"strings"
	"fmt"
	"strconv"
	"errors"
	"runtime"
)

func max(a, b int) int {
	if a > b {
		return a
	} else {
		return b
	}
}

func stars(n int) string {
	s := ""
	for i := 0; i < n; i++ {
		s += "*"
	}
	return s
}

func main() {
	lines := make([]string, 0)
	maxlen := 0
	line, err := nextLine()
	for err == nil {
		maxlen = max(maxlen, len(line))
		lines = append(lines, line)
		line, err = nextLine()
		if len(lines) % 100 == 0 {
			runtime.GC()
		}
	}
	lines = append([]string{stars(maxlen)}, lines...)
	lines = append(lines, stars(maxlen))
	left := true
	for i := range lines {
		if i % 100 == 0 {
			runtime.GC()
		}
		if (maxlen - len(lines[i])) % 2 != 0 {
			if left {
				lines[i] += " "
			} else {
				lines[i] = " " + lines[i]
			}
			left = !left
		}
		for len(lines[i]) != maxlen {
			lines[i] = " " + lines[i] + " "
		}
		lines[i] = "*" + lines[i] + "*"
	}
	for _, l := range lines {
		printf("%s\n", l)
	}
}

var lineScanner *bufio.Scanner
var wordScanner *bufio.Scanner

func init() {
	lineScanner = bufio.NewScanner(os.Stdin)
	wordScanner = bufio.NewScanner(strings.NewReader(""))
	wordScanner.Split(bufio.ScanWords)
}
func printf(format string, a ...interface{}) {
	fmt.Printf(format, a...)
}
func nextLine() (string, error) {
	if !lineScanner.Scan() {
		if err := lineScanner.Err(); err != nil {
			panic(err)
		}
		return "", errors.New("nextLine: EOF reached")
	}
	return lineScanner.Text(), nil
}

func nextWord() string {
	for !wordScanner.Scan() {
		if err := wordScanner.Err(); err != nil {
			panic(err)
		}
		line, err := nextLine()
		if err != nil {
			return ""
		}
		wordScanner = bufio.NewScanner(strings.NewReader(line))
		wordScanner.Split(bufio.ScanWords)
	}
	return wordScanner.Text()
}

func nextInt() int {
	res, err := strconv.Atoi(nextWord())
	if err != nil {
		panic(err)
	}
	return res
}

func nextInt64() int64 {
	res, err := strconv.ParseInt(nextWord(), 10, 64)
	if err != nil {
		panic(err)
	}
	return res
}

func nextFloat64() float64 {
	res, err := strconv.ParseFloat(nextWord(), 64)
	if err != nil {
		panic(err)
	}
	return res
}

package main

import (
	"bytes"
	"fmt"
	"io/ioutil"
	"os"
)

func main() {
	bs, _ := ioutil.ReadAll(os.Stdin)

	s := bytes.Split(bs, []byte("\n"))
	maxlen := -1
	for i := 0; i < len(s); i++ {
		if maxlen < len(s[i]) {
			maxlen = len(s[i])
		}
	}

	for i := 0; i < maxlen+2; i++ {
		fmt.Printf("*")
	}
	fmt.Printf("\n")

	flag := 0
	for i := 0; i < len(s); i++ {
		if i == len(s)-1 && len(s[i]) == 0 {
			continue
		}
		fmt.Printf("*")
		leftSpace := 0
		if (maxlen % 2) == (len(s[i]) % 2) {
			leftSpace = (maxlen - len(s[i])) / 2
		} else {
			leftSpace = (maxlen-len(s[i]))/2 + flag
			flag = 1 - flag
		}
		for j := 0; j < leftSpace; j++ {
			fmt.Printf(" ")
		}
		fmt.Printf("%s", string(s[i]))
		for j := 0; j < maxlen-leftSpace-len(s[i]); j++ {
			fmt.Printf(" ")
		}
		fmt.Printf("*\n")
	}

	for i := 0; i < maxlen+2; i++ {
		fmt.Printf("*")
	}
	fmt.Printf("\n")
}

package main

import (
	"bytes"
	"fmt"
	"io/ioutil"
	"os"
)

func main() {
	bs, _ := ioutil.ReadAll(os.Stdin)

	s := bytes.Split(bs, []byte("\r\n"))
	maxlen := -1
	for i := 0; i < len(s); i++ {
		if maxlen < len(s[i]) {
			maxlen = len(s[i])
		}
	}

	for i := 0; i < maxlen+2; i++ {
		fmt.Printf("*")
	}
	fmt.Printf("\n")

	flag := 0
	for i := 0; i < len(s); i++ {
		fmt.Printf("*")
		leftSpace := 0
		if (maxlen % 2) == (len(s[i]) % 2) {
			leftSpace = (maxlen - len(s[i])) / 2
		} else {
			leftSpace = (maxlen-len(s[i]))/2 + flag
			flag = 1 - flag
		}
		for j := 0; j < leftSpace; j++ {
			fmt.Printf(" ")
		}
		fmt.Printf("%s", string(s[i]))
		for j := 0; j < maxlen-leftSpace-len(s[i]); j++ {
			fmt.Printf(" ")
		}
		fmt.Printf("*\n")
	}

	for i := 0; i < maxlen+2; i++ {
		fmt.Printf("*")
	}
	fmt.Printf("\n")
}

package main

import (
	"bufio"
	"bytes"
	"fmt"
	"os"
	"strings"
)

func main() {

	scanner := bufio.NewScanner(os.Stdin)

	allLines := make([]string, 0, 1000)
	maxLen := 0

	for scanner.Scan() {
		line := scanner.Text()

		if len(line) > maxLen {
			maxLen = len(line)
		}

		allLines = append(allLines, line)
	}

	var buffer bytes.Buffer

	buffer.WriteString(strings.Repeat("*", maxLen+2))
	buffer.WriteByte('\n')

	for _, line := range allLines {
		leftOver := maxLen - len(line)
		leftOverEach := int(leftOver / 2)

		paddingLeft := leftOverEach
		paddingRight := leftOverEach

		if leftOver%2 != 0 {
			paddingLeft = leftOverEach
			paddingRight = leftOverEach + 1
		}

		buffer.WriteString("*")
		buffer.WriteString(strings.Repeat(" ", paddingLeft))
		buffer.WriteString(line)
		buffer.WriteString(strings.Repeat(" ", paddingRight))
		buffer.WriteString("*")
		buffer.WriteByte('\n')
	}

	buffer.WriteString(strings.Repeat("*", maxLen+2))
	buffer.WriteByte('\n')

	fmt.Println(buffer.String())
}


Caisa is going to have a party and he needs to buy the ingredients for a big chocolate cake. For that he is going to the biggest supermarket in town.Unfortunately, he has just s dollars for sugar. But that's not a reason to be sad, because there are n types of sugar in the supermarket, maybe he able to buy one. But that's not all. The supermarket has very unusual exchange politics: instead of cents the sellers give sweets to a buyer as a change. Of course, the number of given sweets always doesn't exceed 99, because each seller maximizes the number of dollars in the change (100 cents can be replaced with a dollar).Caisa wants to buy only one type of sugar, also he wants to maximize the number of sweets in the change. What is the maximum number of sweets he can get? Note, that Caisa doesn't want to minimize the cost of the sugar, he only wants to get maximum number of sweets as change.

The first line contains two space-separated integers n, s (1 ≤ n, s ≤ 100). The i-th of the next n lines contains two integers xi, yi (1 ≤ xi ≤ 100; 0 ≤ yi < 100), where xi represents the number of dollars and yi the number of cents needed in order to buy the i-th type of sugar.

Print a single integer representing the maximum number of sweets he can buy, or -1 if he can't buy any type of sugar.

NoteIn the first test sample Caisa can buy the fourth type of sugar, in such a case he will take 50 sweets as a change.

package main

import (
	"bufio"
	"fmt"
	"os"
	"strconv"
)

var scanner = bufio.NewScanner(os.Stdin)
var writer = bufio.NewWriter(os.Stdout)

//Wrong
func main() {
	defer writer.Flush()
	scanner.Split(bufio.ScanWords)

	n, s := nextInt(), nextInt()
	x, y := 0, 0
	max, dif := 0, 0
	canBuy := false

	for i := 0; i < n; i++ {
		x, y =	nextInt(), nextInt()

		if (y == 0 && x <= s) || (y > 0 &&  x + 1 <= s) {
				canBuy = true
			if y != 0 {
				dif = 100 - y

				if dif > max {
					max = dif
				}
			}
		}
	}

	if canBuy {
		println(max)
	} else {
		println(-1)
	}
}

func next() string {
	if !scanner.Scan() {
		panic("Scan error")
	}
	return scanner.Text()
}

func nextInt() int {
	n, err := strconv.Atoi(next())
	if err != nil {
		panic(err)
	}
	return n
}

func nextFloat() float64 {
	n, err := strconv.ParseFloat(next(), 64)
	if err != nil {
		panic(err)
	}
	return n
}

func println(a ...interface{}) {
	fmt.Fprintln(writer, a...)
}

func print(a ...interface{}) {
	fmt.Fprint(writer, a...)
}

func printf(format string, a ...interface{}) {
	fmt.Fprintf(writer, format, a...)
}

package main

import (
	"fmt"
)

func main() {
	var n, s int
	if _, err := fmt.Scanf("%d %d\n", &n, &s); err != nil {
		return
	}

	var max_sweets int = -1
	for i := 0; i < n; i++ {
		var x, y int
		if _, err := fmt.Scanf("%d %d\n", &x, &y); err != nil {
			return
		}

		if s >= x+1 || (y == 0 && s >= x) {
			if y > 0 && 100-y > max_sweets {
				max_sweets = 100 - y
			} else if y == 0 && max_sweets < 0 {
				max_sweets = 0
			}
		}
	}

	fmt.Println(max_sweets)
}

package main

import (
	"bufio"
	"bytes"
	"errors"
	"flag"
	"fmt"
	"io"
	"log"
	"os"
	"runtime/pprof"
	"strconv"
	"time"
	"unicode/utf8"
	"math/rand"
)

// ******************************** CODE STARTS ********************************

const (
)

var (
)

func solve() {
	n := readInt()
	s	:= readInt()
	res := -1
	for i := 0; i < n; i++ {
		d := readInt()
		c := readInt()
		if c > 0 {
			d++
		}
		if d <= s && (res == -1 || res < (100 - c) % 100) {
			res = (100 - c) % 100
		}
	}
	printInt(res)
	printLine()
}
// ********************************* CODE ENDS *********************************

// IO.

var (
	scanner *Scanner
	writer  *bufio.Writer
	logger  *log.Logger
)

func main() {
	dbg := flag.Bool("dbg", false, "Equals true, if run is in debug mode.")
	opt := flag.Bool("opt", false, "Equals true, if run is in optimized mode.")
	flag.Parse()

	var (
		input  *os.File
		output *os.File
	)

	if *opt || *dbg {
		startTime := time.Now()
		defer func() {
			println(fmt.Sprintf("Time: %.5fs", time.Since(startTime).Seconds()))
		}()
		input, _ = os.Open("/home/outside/coding/workspace/go/io/input.txt")
		output, _ = os.Create("/home/outside/coding/workspace/go/io/output.txt")
		logger = log.New(output, "log: ", 0)
		if *dbg {
			cpuprofile, _ := os.Create("/home/outside/coding/workspace/go/io/cpuprofile.txt")
			pprof.StartCPUProfile(cpuprofile)
			defer cpuprofile.Close()
			defer pprof.StopCPUProfile()
		}
	} else {
		input = os.Stdin
		output = os.Stdout
		// input, _ = os.Open("")
		// output, _ = os.Create("")
	}
	defer input.Close()
	defer output.Close()

	scanner = NewScanner(input)
	scanner.Split(bufio.ScanWords)

	writer = bufio.NewWriter(output)
	defer writer.Flush()

	rand.Seed(int64(time.Now().Nanosecond()))
	solve()
}

// Read.

func readString() string {
	scanner.Scan()
	ans := scanner.Text()
	return ans
}

func readLine() string {
	scanner.Split(bufio.ScanLines)
	defer scanner.Split(bufio.ScanWords)
	return readString()
}

func readInt() int {
	ans, _ := strconv.ParseInt(readString(), 10, 0)
	return int(ans)
}

func readInt64() int64 {
	ans, _ := strconv.ParseInt(readString(), 10, 64)
	return ans
}

func readFloat64() float64 {
	ans, _ := strconv.ParseFloat(readString(), 64)
	return ans
}

// Write.

func printString(value string) {
	writer.WriteString(value)
}

func printLine() {
	printString("\n")
}

func printSpace() {
	printString(" ")
}

func printInt(value int) {
	printString(strconv.FormatInt(int64(value), 10))
}

func printInt64(value int64) {
	printString(strconv.FormatInt(value, 10))
}

func printFloat64(value float64) {
	printString(strconv.FormatFloat(value, 'f', 20, 64))
}

// Log.

func logln(value ...interface{}) {
	writer.Flush()
	logger.Println(value...)
}
func logf(format string, value ...interface{}) {
	writer.Flush()
	logger.Printf(format, value...)
}

// Tuple

type Tuple struct {
	a, b int
}

func (p Tuple) Equals(key Key) bool {
	return p.a == key.(Tuple).a
}

func (p Tuple) Less(key Key) bool {
	return p.a < key.(Tuple).a
}

type TupleSlice []Tuple

func (p TupleSlice) Len() int {
	return len(p)
}

func (p TupleSlice) Less(j, i int) bool {
	return p[j].a < p[i].a
}

func (p TupleSlice) Swap(j, i int) {
	p[j], p[i] = p[i], p[j]
}

// Min, max, abs.

func maxInt(a, b int) int {
	if a > b {
		return a
	} else {
		return b
	}
}

func minInt(a, b int) int {
	if a < b {
		return a
	} else {
		return b
	}
}

func absInt(a int) int {
	if a > 0 {
		return a
	} else {
		return -a
	}
}

func maxInt64(a, b int64) int64 {
	if a > b {
		return a
	} else {
		return b
	}
}

func minInt64(a, b int64) int64 {
	if a < b {
		return a
	} else {
		return b
	}
}

func absInt64(a int64) int64 {
	if a > 0 {
		return a
	} else {
		return -a
	}
}

func maxFloat64(a, b float64) float64 {
	if a > b {
		return a
	} else {
		return b
	}
}

func minFloat64(a, b float64) float64 {
	if a < b {
		return a
	} else {
		return b
	}
}

func absFloat64(a float64) float64 {
	if a > 0 {
		return a
	} else {
		return -a
	}
}

// Set Key interface implementations.

type Int int

func (p Int) Equals(key Key) bool {
	return int(p) == int(key.(Int))
}

func (p Int) Less(key Key) bool {
	return int(p) < int(key.(Int))
}

// Set.

type Key interface {
	Equals(key Key) bool
	Less(key Key) bool
}

type node struct {
	key   Key
	left  *node
	right *node
}

func newNode(key Key) *node {
	return &node{key, nil, nil}
}

type SplayTree struct {
	root *node
	tmp  *node
	len  int
}

func NewSplayTree() *SplayTree {
	return &SplayTree{nil, newNode(nil), 0}
}

func rotateLeft(x, p *node) {
	p.right = x.left
	x.left = p
}

func rotateRight(x, p *node) {
	p.left = x.right
	x.right = p
}

func (p *SplayTree) splay(x *node, key Key) *node {
	if x == nil {
		return nil
	}

	left := p.tmp
	right := p.tmp

	for {
		if key.Less(x.key) {
			y := x.left
			if y == nil {
				break
			}
			if key.Less(y.key) { // zig-zig
				rotateRight(y, x)
				x = y
				if x.left == nil {
					break
				}
			}
			// link right
			right.left = x
			right = x
			// move left
			x = x.left
		} else if x.key.Less(key) {
			y := x.right
			if y == nil {
				break
			}
			if y.key.Less(key) { // zig-zig
				rotateLeft(y, x)
				x = y
				if x.right == nil {
					break
				}
			}
			// link left
			left.right = x
			left = x
			// move right
			x = x.right
		} else {
			break
		}
	}

	left.right = x.left
	right.left = x.right
	x.left = p.tmp.right
	x.right = p.tmp.left

	return x
}

// left <= key < right
func (p *SplayTree) split(key Key) (left, right *node) {
	p.root = p.splay(p.root, key)
	if p.root.key.Equals(key) || p.root.key.Less(key) {
		right := p.root.right
		p.root.right = nil
		return p.root, right
	} else {
		left := p.root.left
		p.root.left = nil
		return left, p.root
	}
}

// keys from left tree must be less then keys from right tree
func (p *SplayTree) join(left, right *node) *node {
	if left == nil {
		return right
	} else if right == nil {
		return left
	}
	left = p.splay(left, right.key)
	left.right = right
	return left
}

type Set interface {
	Len() int
	Insert(key Key) error
	Find(key Key) bool
	Remove(key Key) error
}

func NewSet() Set {
	return Set(NewSplayTree())
}

func (p *SplayTree) Len() int {
	return p.len
}

func (p *SplayTree) Insert(key Key) error {
	if p.root == nil {
		p.root = newNode(key)
		p.len++
	} else {
		p.root = p.splay(p.root, key)
		if p.root.key.Equals(key) {
			return errors.New("Such key already exists")
		} else {
			left, right := p.split(key)
			p.root = newNode(key)
			p.root.left = left
			p.root.right = right
			p.len++
		}
	}
	return nil
}

func (p *SplayTree) Find(key Key) bool {
	if p.root == nil {
		return false
	}
	p.root = p.splay(p.root, key)
	return p.root.key.Equals(key)
}

func (p *SplayTree) Remove(key Key) error {
	p.root = p.splay(p.root, key)
	if p.root == nil || !p.root.key.Equals(key) {
		return errors.New("Such key doesn't exist")
	}
	p.root = p.join(p.split(key))
	return nil
}

// Copied scan.go
// Scanner provides a convenient interface for reading data such as
// a file of newline-delimited lines of text. Successive calls to
// the Scan method will step through the 'tokens' of a file, skipping
// the bytes between the tokens. The specification of a token is
// defined by a split function of type SplitFunc; the default split
// function breaks the input into lines with line termination stripped. Split
// functions are defined in this package for scanning a file into
// lines, bytes, UTF-8-encoded runes, and space-delimited words. The
// client may instead provide a custom split function.
//
// Scanning stops unrecoverably at EOF, the first I/O error, or a token too
// large to fit in the buffer. When a scan stops, the reader may have
// advanced arbitrarily far past the last token. Programs that need more
// control over error handling or large tokens, or must run sequential scans
// on a reader, should use bufio.Reader instead.
//
type Scanner struct {
	r            io.Reader // The reader provided by the client.
	split        SplitFunc // The function to split the tokens.
	maxTokenSize int       // Maximum size of a token; modified by tests.
	token        []byte    // Last token returned by split.
	buf          []byte    // Buffer used as argument to split.
	start        int       // First non-processed byte in buf.
	end          int       // End of data in buf.
	err          error     // Sticky error.
}

// SplitFunc is the signature of the split function used to tokenize the
// input. The arguments are an initial substring of the remaining unprocessed
// data and a flag, atEOF, that reports whether the Reader has no more data
// to give. The return values are the number of bytes to advance the input
// and the next token to return to the user, plus an error, if any. If the
// data does not yet hold a complete token, for instance if it has no newline
// while scanning lines, SplitFunc can return (0, nil, nil) to signal the
// Scanner to read more data into the slice and try again with a longer slice
// starting at the same point in the input.
//
// If the returned error is non-nil, scanning stops and the error
// is returned to the client.
//
// The function is never called with an empty data slice unless atEOF
// is true. If atEOF is true, however, data may be non-empty and,
// as always, holds unprocessed text.
type SplitFunc func(data []byte, atEOF bool) (advance int, token []byte, err error)

// Errors returned by Scanner.
var (
	ErrTooLong         = errors.New("bufio.Scanner: token too long")
	ErrNegativeAdvance = errors.New("bufio.Scanner: SplitFunc returns negative advance count")
	ErrAdvanceTooFar   = errors.New("bufio.Scanner: SplitFunc returns advance count beyond input")
)

const (
	// Maximum size used to buffer a token. The actual maximum token size
	// may be smaller as the buffer may need to include, for instance, a newline.
	MaxScanTokenSize = 1 << 20
)

// NewScanner returns a new Scanner to read from r.
// The split function defaults to ScanLines.
func NewScanner(r io.Reader) *Scanner {
	return &Scanner{
		r:            r,
		split:        ScanLines,
		maxTokenSize: MaxScanTokenSize,
		buf:          make([]byte, 4096), // Plausible starting size; needn't be large.
	}
}

// Err returns the first non-EOF error that was encountered by the Scanner.
func (s *Scanner) Err() error {
	if s.err == io.EOF {
		return nil
	}
	return s.err
}

// Bytes returns the most recent token generated by a call to Scan.
// The underlying array may point to data that will be overwritten
// by a subsequent call to Scan. It does no allocation.
func (s *Scanner) Bytes() []byte {
	return s.token
}

// Text returns the most recent token generated by a call to Scan
// as a newly allocated string holding its bytes.
func (s *Scanner) Text() string {
	return string(s.token)
}

// Scan advances the Scanner to the next token, which will then be
// available through the Bytes or Text method. It returns false when the
// scan stops, either by reaching the end of the input or an error.
// After Scan returns false, the Err method will return any error that
// occurred during scanning, except that if it was io.EOF, Err
// will return nil.
func (s *Scanner) Scan() bool {
	// Loop until we have a token.
	for {
		// See if we can get a token with what we already have.
		if s.end > s.start {
			advance, token, err := s.split(s.buf[s.start:s.end], s.err != nil)
			if err != nil {
				s.setErr(err)
				return false
			}
			if !s.advance(advance) {
				return false
			}
			s.token = token
			if token != nil {
				return true
			}
		}
		// We cannot generate a token with what we are holding.
		// If we've already hit EOF or an I/O error, we are done.
		if s.err != nil {
			// Shut it down.
			s.start = 0
			s.end = 0
			return false
		}
		// Must read more data.
		// First, shift data to beginning of buffer if there's lots of empty space
		// or space is neded.
		if s.start > 0 && (s.end == len(s.buf) || s.start > len(s.buf)/2) {
			copy(s.buf, s.buf[s.start:s.end])
			s.end -= s.start
			s.start = 0
		}
		// Is the buffer full? If so, resize.
		if s.end == len(s.buf) {
			if len(s.buf) >= s.maxTokenSize {
				s.setErr(ErrTooLong)
				return false
			}
			newSize := len(s.buf) * 2
			if newSize > s.maxTokenSize {
				newSize = s.maxTokenSize
			}
			newBuf := make([]byte, newSize)
			copy(newBuf, s.buf[s.start:s.end])
			s.buf = newBuf
			s.end -= s.start
			s.start = 0
			continue
		}
		// Finally we can read some input. Make sure we don't get stuck with
		// a misbehaving Reader. Officially we don't need to do this, but let's
		// be extra careful: Scanner is for safe, simple jobs.
		for loop := 0; ; {
			n, err := s.r.Read(s.buf[s.end:len(s.buf)])
			s.end += n
			if err != nil {
				s.setErr(err)
				break
			}
			if n > 0 {
				break
			}
			loop++
			if loop > 100 {
				s.setErr(io.ErrNoProgress)
				break
			}
		}
	}
}

// advance consumes n bytes of the buffer. It reports whether the advance was legal.
func (s *Scanner) advance(n int) bool {
	if n < 0 {
		s.setErr(ErrNegativeAdvance)
		return false
	}
	if n > s.end-s.start {
		s.setErr(ErrAdvanceTooFar)
		return false
	}
	s.start += n
	return true
}

// setErr records the first error encountered.
func (s *Scanner) setErr(err error) {
	if s.err == nil || s.err == io.EOF {
		s.err = err
	}
}

// Split sets the split function for the Scanner. If called, it must be
// called before Scan. The default split function is ScanLines.
func (s *Scanner) Split(split SplitFunc) {
	s.split = split
}

// Split functions

// ScanBytes is a split function for a Scanner that returns each byte as a token.
func ScanBytes(data []byte, atEOF bool) (advance int, token []byte, err error) {
	if atEOF && len(data) == 0 {
		return 0, nil, nil
	}
	return 1, data[0:1], nil
}

var errorRune = []byte(string(utf8.RuneError))

// ScanRunes is a split function for a Scanner that returns each
// UTF-8-encoded rune as a token. The sequence of runes returned is
// equivalent to that from a range loop over the input as a string, which
// means that erroneous UTF-8 encodings translate to U+FFFD = "\xef\xbf\xbd".
// Because of the Scan interface, this makes it impossible for the client to
// distinguish correctly encoded replacement runes from encoding errors.
func ScanRunes(data []byte, atEOF bool) (advance int, token []byte, err error) {
	if atEOF && len(data) == 0 {
		return 0, nil, nil
	}

	// Fast path 1: ASCII.
	if data[0] < utf8.RuneSelf {
		return 1, data[0:1], nil
	}

	// Fast path 2: Correct UTF-8 decode without error.
	_, width := utf8.DecodeRune(data)
	if width > 1 {
		// It's a valid encoding. Width cannot be one for a correctly encoded
		// non-ASCII rune.
		return width, data[0:width], nil
	}

	// We know it's an error: we have width==1 and implicitly r==utf8.RuneError.
	// Is the error because there wasn't a full rune to be decoded?
	// FullRune distinguishes correctly between erroneous and incomplete encodings.
	if !atEOF && !utf8.FullRune(data) {
		// Incomplete; get more bytes.
		return 0, nil, nil
	}

	// We have a real UTF-8 encoding error. Return a properly encoded error rune
	// but advance only one byte. This matches the behavior of a range loop over
	// an incorrectly encoded string.
	return 1, errorRune, nil
}

// dropCR drops a terminal \r from the data.
func dropCR(data []byte) []byte {
	if len(data) > 0 && data[len(data)-1] == '\r' {
		return data[0 : len(data)-1]
	}
	return data
}

// ScanLines is a split function for a Scanner that returns each line of
// text, stripped of any trailing end-of-line marker. The returned line may
// be empty. The end-of-line marker is one optional carriage return followed
// by one mandatory newline. In regular expression notation, it is `\r?\n`.
// The last non-empty line of input will be returned even if it has no
// newline.
func ScanLines(data []byte, atEOF bool) (advance int, token []byte, err error) {
	if atEOF && len(data) == 0 {
		return 0, nil, nil
	}
	if i := bytes.IndexByte(data, '\n'); i >= 0 {
		// We have a full newline-terminated line.
		return i + 1, dropCR(data[0:i]), nil
	}
	// If we're at EOF, we have a final, non-terminated line. Return it.
	if atEOF {
		return len(data), dropCR(data), nil
	}
	// Request more data.
	return 0, nil, nil
}

// isSpace reports whether the character is a Unicode white space character.
// We avoid dependency on the unicode package, but check validity of the implementation
// in the tests.
func isSpace(r rune) bool {
	if r <= '\u00FF' {
		// Obvious ASCII ones: \t through \r plus space. Plus two Latin-1 oddballs.
		switch r {
		case ' ', '\t', '\n', '\v', '\f', '\r':
			return true
		case '\u0085', '\u00A0':
			return true
		}
		return false
	}
	// High-valued ones.
	if '\u2000' <= r && r <= '\u200a' {
		return true
	}
	switch r {
	case '\u1680', '\u180e', '\u2028', '\u2029', '\u202f', '\u205f', '\u3000':
		return true
	}
	return false
}

// ScanWords is a split function for a Scanner that returns each
// space-separated word of text, with surrounding spaces deleted. It will
// never return an empty string. The definition of space is set by
// unicode.IsSpace.
func ScanWords(data []byte, atEOF bool) (advance int, token []byte, err error) {
	// Skip leading spaces.
	start := 0
	for width := 0; start < len(data); start += width {
		var r rune
		r, width = utf8.DecodeRune(data[start:])
		if !isSpace(r) {
			break
		}
	}
	if atEOF && len(data) == 0 {
		return 0, nil, nil
	}
	// Scan until space, marking end of word.
	for width, i := 0, start; i < len(data); i += width {
		var r rune
		r, width = utf8.DecodeRune(data[i:])
		if isSpace(r) {
			return i + width, data[start:i], nil
		}
	}
	// If we're at EOF, we have a final, non-empty, non-terminated word. Return it.
	if atEOF && len(data) > start {
		return len(data), data[start:], nil
	}
	// Request more data.
	return 0, nil, nil
}

package main

import (
	"bufio"
	"fmt"
	"os"
)

func main() {
	reader := bufio.NewReader(os.Stdin)
	var n, s int
	fmt.Fscan(reader, &n, &s)
	var dollors = make([]int, n)
	var cents = make([]int, n)
	for i := 0; i < n; i++ {
		fmt.Fscan(reader, &dollors[i], &cents[i])
	}
	var sweets int = -1
	for i := 0; i < n; i++ {
		var exchange = (100 - cents[i]) % 100
		if dollors[i]*100+cents[i] <= s*100 && sweets < exchange {
			sweets = exchange
		}
	}
	fmt.Println(sweets)
}

package main

import (
	"fmt"
)

func max(x, y int) int {
	if y > x {
		return y
	}
	return x
}

func main() {
	var n, s int
	var xs, ys [100]int
	fmt.Scan(&n)
	fmt.Scan(&s)
	for i := 0; i < n; i++ {
		fmt.Scan(&xs[i])
		fmt.Scan(&ys[i])
	}
	z := -1
	for i := 0; i < n; i++ {
		change := s*100 - (xs[i]*100 + ys[i])
		if change >= 0 {
			z = max(z, change%100)
		}
	}
	fmt.Println(z)
}

package main

import (
	"bufio"
	"fmt"
	"os"
)

func main() {
	reader := bufio.NewReader(os.Stdin)
	var n, s int
	fmt.Fscan(reader, &n, &s)

	var count, attempt int
	for i := 0; i < n; i++ {
		var x, y int
		fmt.Fscan(reader, &x, &y)

		if x > s || x == s && y > 0 {
			attempt++
			continue
		} else if y == 0 {
			continue

		}

		count = max(count, 100-y)
	}

	if attempt == n {
		fmt.Println(-1)
		return
	}

	fmt.Println(count)
}

func max(a, b int) int {
	if a > b {
		return a
	} else {
		return b
	}
}

package main

import (
	"bufio"
	"fmt"
	"os"
	"strconv"
)

var scanner = bufio.NewScanner(os.Stdin)
var writer = bufio.NewWriter(os.Stdout)

//Wrong
func main() {
	defer writer.Flush()
	scanner.Split(bufio.ScanWords)

	n, s := nextInt(), nextInt()
	x, y := 0, 0
	max, dif := 0, 0
	canBuy := false

	for i := 0; i < n; i++ {
		x, y =	nextInt(), nextInt()

		if (y == 0 && x <= s) || (y > 0 &&  x + 1 <= s) {
				canBuy = true
		}

		if canBuy && y != 0 {
			dif = 100 - y

			if dif > max {
				max = dif
			}
		}
	}

	if canBuy {
		println(max)
	} else {
		println(-1)
	}
}

func next() string {
	if !scanner.Scan() {
		panic("Scan error")
	}
	return scanner.Text()
}

func nextInt() int {
	n, err := strconv.Atoi(next())
	if err != nil {
		panic(err)
	}
	return n
}

func nextFloat() float64 {
	n, err := strconv.ParseFloat(next(), 64)
	if err != nil {
		panic(err)
	}
	return n
}

func println(a ...interface{}) {
	fmt.Fprintln(writer, a...)
}

func print(a ...interface{}) {
	fmt.Fprint(writer, a...)
}

func printf(format string, a ...interface{}) {
	fmt.Fprintf(writer, format, a...)
}

package main

import (
	"fmt"
)

func main() {
	var n, s int
	if _, err := fmt.Scanf("%d %d\n", &n, &s); err != nil {
		return
	}

	var max_sweets int = -1
	for i := 0; i < n; i++ {
		var x, y int
		if _, err := fmt.Scanf("%d %d\n", &x, &y); err != nil {
			return
		}

		if s >= x+1 || (y == 0 && s >= x) {
			if y > 0 && 100-y > max_sweets {
				max_sweets = 100 - y
			}
		}
	}

	fmt.Println(max_sweets)
}

package main

import (
	"bufio"
	"fmt"
	"os"
)

func main() {
	reader := bufio.NewReader(os.Stdin)
	var n, s int
	fmt.Fscan(reader, &n, &s)
	var dollors = make([]int, n)
	var cents = make([]int, n)
	for i := 0; i < n; i++ {
		fmt.Fscan(reader, &dollors[i], &cents[i])
	}
	var sweets int = -1
	for i := 0; i < n; i++ {
		var exchange = (100 - cents[i]) % 100
		if dollors[i]*100+cents[i] < s*100 && sweets < exchange {
			sweets = exchange
		}
	}
	fmt.Println(sweets)
}

package main

import (
	"bufio"
	"fmt"
	"os"
)

func main() {
	reader := bufio.NewReader(os.Stdin)
	var n, s int
	fmt.Fscan(reader, &n, &s)
	var dollors = make([]int, n)
	var cents = make([]int, n)
	for i := 0; i < n; i++ {
		fmt.Fscan(reader, &dollors[i], &cents[i])
	}
	var sweets int = -1
	for i := 0; i < n; i++ {
		var exchange = (100 - cents[i]) % 100
		if dollors[i] < s && sweets < exchange {
			sweets = exchange
		}
	}
	fmt.Println(sweets)
}

package main

import (
	"bufio"
	"fmt"
	"os"
)

func main() {
	reader := bufio.NewReader(os.Stdin)
	var n, s int
	fmt.Fscan(reader, &n, &s)

	var count, attempt int
	for i := 0; i < n; i++ {
		var x, y int
		fmt.Fscan(reader, &x, &y)

		if x > s {
			attempt++
			continue
		} else if y == 0 {
			continue

		}

		count = max(count, 100-y)
	}

	if attempt == n {
		fmt.Println(-1)
		return
	}

	fmt.Println(count)
}

func max(a, b int) int {
	if a > b {
		return a
	} else {
		return b
	}
}

package main

import (
	"bufio"
	"fmt"
	"os"
)

func main() {
	reader := bufio.NewReader(os.Stdin)
	var n, s int
	fmt.Fscan(reader, &n, &s)

	var count, attempt int
	for i := 0; i < n; i++ {
		var x, y int
		fmt.Fscan(reader, &x, &y)

		if x > s || x == s && y > 0 {
			attempt++
			continue
		} else if x == s && y == 0 {
			continue

		}

		count = max(count, 100-y)
	}

	if attempt == n {
		fmt.Println(-1)
		return
	}

	fmt.Println(count)
}

func max(a, b int) int {
	if a > b {
		return a
	} else {
		return b
	}
}

package main

import (
	"bufio"
	"fmt"
	"os"
)

func main() {
	reader := bufio.NewReader(os.Stdin)
	var n, s int
	fmt.Fscan(reader, &n, &s)

	var count int = -1
	for n > 0 {
		var x, y int
		fmt.Fscan(reader, &x, &y)

		if x > s || y == 0 {
			n--
			continue
		}

		count = max(count, 100-y)
		n--
	}

	fmt.Println(count)
}

func max(a, b int) int {
	if a > b {
		return a
	} else {
		return b
	}
}

package main

import (
	"bufio"
	"fmt"
	"os"
)

func main() {
	reader := bufio.NewReader(os.Stdin)
	var n, s int
	fmt.Fscan(reader, &n, &s)

	var count, attempt int
	for i := 0; i < n; i++ {
		var x, y int
		fmt.Fscan(reader, &x, &y)

		if x >= s {
			attempt++
			continue
		} else if y == 0 {
			continue

		}

		count = max(count, 100-y)
	}

	if attempt == n {
		fmt.Println(-1)
		return
	}

	fmt.Println(count)
}

func max(a, b int) int {
	if a > b {
		return a
	} else {
		return b
	}
}


You are given a text consisting of $$$n$$$ space-separated words. There is exactly one space character between any pair of adjacent words. There are no spaces before the first word and no spaces after the last word. The length of text is the number of letters and spaces in it. $$$w_i$$$ is the $$$i$$$-th word of text. All words consist only of lowercase Latin letters.Let's denote a segment of words $$$w[i..j]$$$ as a sequence of words $$$w_i, w_{i + 1}, \dots, w_j$$$. Two segments of words $$$w[i_1 .. j_1]$$$ and $$$w[i_2 .. j_2]$$$ are considered equal if $$$j_1 - i_1 = j_2 - i_2$$$, $$$j_1 \ge i_1$$$, $$$j_2 \ge i_2$$$, and for every $$$t \in [0, j_1 - i_1]$$$ $$$w_{i_1 + t} = w_{i_2 + t}$$$. For example, for the text "to be or not to be" the segments $$$w[1..2]$$$ and $$$w[5..6]$$$ are equal, they correspond to the words "to be".An abbreviation is a replacement of some segments of words with their first uppercase letters. In order to perform an abbreviation, you have to choose at least two non-intersecting equal segments of words, and replace each chosen segment with the string consisting of first letters of the words in the segment (written in uppercase). For example, for the text "a ab a a b ab a a b c" you can replace segments of words $$$w[2..4]$$$ and $$$w[6..8]$$$ with an abbreviation "AAA" and obtain the text "a AAA b AAA b c", or you can replace segments of words $$$w[2..5]$$$ and $$$w[6..9]$$$ with an abbreviation "AAAB" and obtain the text "a AAAB AAAB c".What is the minimum length of the text after at most one abbreviation?

The first line of the input contains one integer $$$n$$$ ($$$1 \le n \le 300$$$) — the number of words in the text. The next line contains $$$n$$$ space-separated words of the text $$$w_1, w_2, \dots, w_n$$$. Each word consists only of lowercase Latin letters. It is guaranteed that the length of text does not exceed $$$10^5$$$.

Print one integer — the minimum length of the text after at most one abbreviation.

NoteIn the first example you can obtain the text "TB or not TB".In the second example you can obtain the text "a AAAB AAAB c".In the third example you can obtain the text "AB aa AB bb".

package main

import (
	"bufio"
	. "fmt"
	"io"
	"os"
)

// github.com/EndlessCheng/codeforces-go
func CF1003F(_r io.Reader, _w io.Writer) {
	in := bufio.NewReader(_r)
	out := bufio.NewWriter(_w)
	defer out.Flush()

	n, s := 0, ""
	Fscan(in, &n)
	a := make([]int, n)
	sz := make([]int, n+1)
	all := n - 1
	id := map[string]int{}
	for i := range a {
		Fscan(in, &s)
		v, ok := id[s]
		if !ok {
			v = len(id)
			id[s] = v
		}
		a[i] = v
		sz[i+1] = sz[i] + len(s)
		all += len(s)
	}

	search := func(s []int) (cnt int) {
		n := len(s)
		f := make([]int, n)
		c := 0
		for i := 1; i < n; i++ {
			b := s[i]
			for c > 0 && s[c] != b {
				c = f[c-1]
			}
			if s[c] == b {
				c++
			}
			f[i] = c
		}
		c = 0
		for _, b := range a {
			for c > 0 && s[c] != b {
				c = f[c-1]
			}
			if s[c] == b {
				c++
			}
			if c == n {
				cnt++
				c = 0
			}
		}
		return
	}
	ans := all
	for r := 1; r <= n; r++ {
		for l := 0; l < r; l++ {
			if c := search(a[l:r]); c > 1 {
				if s := all - c*(sz[r]-sz[l]-1); s < ans {
					ans = s
				}
			}
		}
	}
	Fprint(out, ans)
}

func main() { CF1003F(os.Stdin, os.Stdout) }

package main

import (
	"bufio"
	. "fmt"
	"io"
	"os"
	"strings"
)

// github.com/EndlessCheng/codeforces-go
func CF1003F(_r io.Reader, _w io.Writer) {
	in := bufio.NewReader(_r)
	out := bufio.NewWriter(_w)
	defer out.Flush()

	var n int
	Fscan(in, &n)
	text := make([]rune, n)
	sum := make([]int, n+1)
	allSum := n - 1
	id := map[string]int{}
	for i := range text {
		var s string
		Fscan(in, &s)
		if _, ok := id[s]; !ok {
			id[s] = len(id) + 1
		}
		text[i] = rune(id[s])
		sum[i+1] = sum[i] + len(s)
		allSum += len(s)
	}

	s := string(text)
	ans := allSum
	for r := 1; r <= n; r++ {
		for l := 0; l < r; l++ {
			if c := strings.Count(s, string(text[l:r])); c > 1 {
				if s := allSum - c*(sum[r]-sum[l]-1); s < ans {
					ans = s
				}
			}
		}
	}
	Fprint(out, ans)
}

func main() { CF1003F(os.Stdin, os.Stdout) }

package main

import (
	"bufio"
	. "fmt"
	"io"
	"os"
)

// github.com/EndlessCheng/codeforces-go
func CF1003F(_r io.Reader, _w io.Writer) {
	in := bufio.NewReader(_r)
	out := bufio.NewWriter(_w)
	defer out.Flush()

	n, s := 0, ""
	Fscan(in, &n)
	a := make([]int, n)
	sz := make([]int, n+1)
	all := n - 1
	id := map[string]int{}
	for i := range a {
		Fscan(in, &s)
		if _, ok := id[s]; !ok {
			id[s] = len(id)
		}
		a[i] = id[s]
		sz[i+1] = sz[i] + len(s)
		all += len(s)
	}

	search := func(s []int) (cnt int) {
		n := len(s)
		f := make([]int, n)
		c := 0
		for i := 1; i < n; i++ {
			b := s[i]
			for c > 0 && s[c] != b {
				c = f[c-1]
			}
			if s[c] == b {
				c++
			}
			f[i] = c
		}
		c = 0
		for _, b := range a {
			for c > 0 && s[c] != b {
				c = f[c-1]
			}
			if s[c] == b {
				c++
			}
			if c == n {
				cnt++
				c = 0
			}
		}
		return
	}
	ans := all
	for r := 1; r <= n; r++ {
		for l := 0; l < r; l++ {
			if c := search(a[l:r]); c > 1 {
				if s := all - c*(sz[r]-sz[l]-1); s < ans {
					ans = s
				}
			}
		}
	}
	Fprint(out, ans)
}

func main() { CF1003F(os.Stdin, os.Stdout) }

package main

import (
	"bufio"
	."fmt"
	"os"
	."strings"
)

// github.com/EndlessCheng/codeforces-go
func main() {
	in := bufio.NewReader(os.Stdin)
	n,s := 0,""
	Fscan(in, &n)
	a := make([]rune, n)
	p := make([]int, n+1)
	L := n - 1
	m := map[string]int{}
	for i := range a {
		Fscan(in, &s)
		if _, ok := m[s]; !ok {
			m[s] = len(m)
		}
		a[i] = rune(m[s])
		p[i+1] = p[i] + len(s)
		L += len(s)
	}
	s = string(a)
	b := L
	for r := 1; r <= n; r++ {
		for l := 0; l < r; l++ {
			if c := Count(s, string(a[l:r])); c > 1 {
				if s := L - c*(p[r]-p[l]-1); s < b {
					b = s
				}
			}
		}
	}
	Print(b)
}

package main

import (
	"bufio"
	. "fmt"
	"io"
	"os"
	"strings"
)

// github.com/EndlessCheng/codeforces-go
func CF1003F(_r io.Reader, _w io.Writer) {
	in := bufio.NewReader(_r)
	out := bufio.NewWriter(_w)
	defer out.Flush()

	var n int
	Fscan(in, &n)
	text := make([]rune, n)
	sum := make([]int, n+1)
	allSum := n - 1
	id := map[string]int{}
	for i := range text {
		var s string
		Fscan(in, &s)
		if _, ok := id[s]; !ok {
			id[s] = len(id) + 1
		}
		text[i] = rune(id[s])
		sum[i+1] = sum[i] + len(s)
		allSum += len(s)
	}

	s := string(text)
	ans := allSum
	for r := 1; r <= n; r++ {
		for l := 0; l < r; l++ {
			if c := strings.Count(s, s[l:r]); c > 1 {
				if s := allSum - c*(sum[r]-sum[l]-1); s < ans {
					ans = s
				}
			}
		}
	}
	Fprint(out, ans)
}

func main() { CF1003F(os.Stdin, os.Stdout) }

package main

import (
	"bufio"
	."fmt"
	"os"
	."strings"
)

// github.com/EndlessCheng/codeforces-go
func main() {
	in := bufio.NewReader(os.Stdin)
	n,s := 0,""
	Fscan(in, &n)
	a := make([]rune, n)
	p := make([]int, n+1)
	L := n - 1
	m := map[string]int{}
	for i := range a {
		Fscan(in, &s)
		if _, ok := m[s]; !ok {
			m[s] = len(m)
		}
		a[i] = rune(m[s])
		p[i+1] = p[i] + len(s)
		L += len(s)
	}
	s = string(a)
	b := L
	for r := 1; r <= n; r++ {
		for l := 0; l < r; l++ {
			if c := Count(s, string(a[l:r])); c > 1 {
				if s := l - c*(p[r]-p[l]-1); s < b {
					b = s
				}
			}
		}
	}
	Print(b)
}

package main

import (
	"bufio"
	."fmt"
	"os"
	."strings"
)

// github.com/EndlessCheng/codeforces-go
func main() {
	in := bufio.NewReader(os.Stdin)
	n,s := 0,""
	Fscan(in, &n)
	a := make([]rune, n)
	p := make([]int, n+1)
	l := n - 1
	m := map[string]int{}
	for i := range a {
		Fscan(in, &s)
		if _, ok := m[s]; !ok {
			m[s] = len(m)
		}
		a[i] = rune(m[s])
		p[i+1] = p[i] + len(s)
		l += len(s)
	}
	s = string(a)
	b := l
	for r := 1; r <= n; r++ {
		for l := 0; l < r; l++ {
			if c := Count(s, string(a[l:r])); c > 1 {
				if s := l - c*(p[r]-p[l]-1); s < b {
					b = s
				}
			}
		}
	}
	Print(b)
}

package main

import (
	"bufio"
	."fmt"
	"os"
	."strings"
)

// github.com/EndlessCheng/codeforces-go
func main() {
	in := bufio.NewReader(os.Stdin)
	n,s := 0,""
	Fscan(in, &n)
	a := make([]rune, n)
	p := make([]int, n+1)
	l := n - 1
	m := map[string]int{}
	for i := range a {
		Fscan(in, &s)
		if _, ok := m[s]; !ok {
			m[s] = len(m)+1
		}
		a[i] = rune(m[s])
		p[i+1] = p[i] + len(s)
		l += len(s)
	}
	s = string(a)
	b := l
	for r := 1; r <= n; r++ {
		for l := 0; l < r; l++ {
			if c := Count(s, string(a[l:r])); c > 1 {
				if s := l - c*(p[r]-p[l]-1); s < b {
					b = s
				}
			}
		}
	}
	Print(b)
}

package main

import (
	"bufio"
	. "fmt"
	"io"
	"os"
	"strings"
)

// github.com/EndlessCheng/codeforces-go
func CF1003F(_r io.Reader, _w io.Writer) {
	in := bufio.NewReader(_r)
	out := bufio.NewWriter(_w)
	defer out.Flush()

	var n int
	Fscan(in, &n)
	text := make([]rune, n)
	sum := make([]int, n+1)
	allSum := n - 1
	id := map[string]int{}
	for i := range text {
		var s string
		Fscan(in, &s)
		if _, ok := id[s]; !ok {
			id[s] = len(id)
		}
		text[i] = rune(id[s])
		sum[i+1] = sum[i] + len(s)
		allSum += len(s)
	}

	s := string(text)
	ans := allSum
	for r := 1; r <= n; r++ {
		for l := 0; l < r; l++ {
			if c := strings.Count(s, s[l:r]); c > 1 {
				if s := allSum - c*(sum[r]-sum[l]-1); s < ans {
					ans = s
				}
			}
		}
	}
	Fprint(out, ans)
}

func main() { CF1003F(os.Stdin, os.Stdout) }


Mihai plans to watch a movie. He only likes palindromic movies, so he wants to skip some (possibly zero) scenes to make the remaining parts of the movie palindromic.You are given a list $$$s$$$ of $$$n$$$ non-empty strings of length at most $$$3$$$, representing the scenes of Mihai's movie.A subsequence of $$$s$$$ is called awesome if it is non-empty and the concatenation of the strings in the subsequence, in order, is a palindrome.Can you help Mihai check if there is at least one awesome subsequence of $$$s$$$?A palindrome is a string that reads the same backward as forward, for example strings "z", "aaa", "aba", "abccba" are palindromes, but strings "codeforces", "reality", "ab" are not.A sequence $$$a$$$ is a non-empty subsequence of a non-empty sequence $$$b$$$ if $$$a$$$ can be obtained from $$$b$$$ by deletion of several (possibly zero, but not all) elements.

The first line of the input contains a single integer $$$t$$$ ($$$1 \le t \le 100$$$) — the number of test cases. The description of test cases follows. The first line of each test case contains a single integer $$$n$$$ ($$$1 \le n \le 10^5$$$) — the number of scenes in the movie. Then follows $$$n$$$ lines, the $$$i$$$-th of which containing a single non-empty string $$$s_i$$$ of length at most $$$3$$$, consisting of lowercase Latin letters. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$10^5$$$.

For each test case, print "YES" if there is an awesome subsequence of $$$s$$$, or "NO" otherwise (case insensitive).

6

5

zx

ab

cc

zx

ba

2

ab

bad

4

co

def

orc

es

3

a

b

c

3

ab

cd

cba

2

ab

ab

NoteIn the first test case, an awesome subsequence of $$$s$$$ is $$$[ab, cc, ba]$$$

package main

import (
	"bufio"
	"fmt"
	"os"
	"strconv"
	"strings"
)

func main() {
	in := bufio.NewReader(os.Stdin)

	t := readInt(in)
	b := strings.Builder{}
	for i := 0; i < t; i++ {
		str := solve(in)
		b.WriteString(str + "\n")
	}
	fmt.Println(b.String())
}

func solve(in *bufio.Reader) string {
	n := readInt(in)
	scenes := make([]string, n)
	tot := map[string]int{}
	l2 := map[string]int{}
	l3 := map[string]int{}

	for i := 0; i < n; i++ {
		str, _ := in.ReadString('\n')
		str = strings.ReplaceAll(str, "\r", "")
		str = strings.ReplaceAll(str, "\n", "")
		scenes[i] = str
		tot[scenes[i]]++
	}

	for i := 0; i < n; i++ {
		str := scenes[i]
		if len(str) == 1 {
			return "YES"
		}
		if isPalindrome(str) {
			return "YES"
		}
		tot[str]--
		rev := reverse(str)
		if val := tot[rev]; val != 0 {
			return "YES"
		}

		if len(str) == 2 {
			l2[str]++
			rev2 := reverse(str)
			if val := l3[rev2]; val != 0 {
				return "YES"
			}
		} else {
			l3[str[:2]]++
			rev3 := reverse(str[:2])
			if val := tot[rev3]; val != 0 {
				return "YES"
			}
			rev3 = reverse(str[1:])
			if val := l2[rev3]; val != 0 {
				return "YES"
			}
		}
	}

	return "NO"
}

func reverse(str string) string {
	b := strings.Builder{}
	for i := 0; i < len(str); i++ {
		b.WriteByte(str[len(str) - 1 - i])
	}
	return b.String()
}

func isPalindrome(str string) bool {
	for i := 0; i < len(str)/2; i++ {
		if str[i] != str[len(str) - 1 - i] {
			return false
		}
	}
	return true
}

func readInt(in *bufio.Reader) int {
nStr, _ := in.ReadString('\n')
nStr = strings.ReplaceAll(nStr, "\r", "")
nStr = strings.ReplaceAll(nStr, "\n", "")
n, _ := strconv.Atoi(nStr)
return n
}

func readLineNumbs(in *bufio.Reader) []string {
line, _ := in.ReadString('\n')
line = strings.ReplaceAll(line, "\r", "")
line = strings.ReplaceAll(line, "\n", "")
numbs := strings.Split(line, " ")
return numbs
}

func readArrInt(in *bufio.Reader) []int {
	numbs := readLineNumbs(in)
	arr := make([]int, len(numbs))
	for i, n := range numbs {
		val, _ := strconv.Atoi(n)
		arr[i] = val
	}
	return arr
}

package main

import (
	"bufio"
	"fmt"
	"os"
)

func main() {
	r := bufio.NewReader(os.Stdin)
	w := bufio.NewWriter(os.Stdout)
	defer w.Flush()

	var T int
	for fmt.Fscan(r, &T); T > 0; T-- {

		var n int
		var found bool
		fmt.Fscan(r, &n)

		pf := make(map[string]bool)

		for i := 0; i < n; i++ {
			var t string
			fmt.Fscan(r, &t)

			if found {
				continue
			} else if t[0] == t[len(t)-1] {
				found = true
			} else if len(t) == 2 {
				for j := 'a'; j <= 'z'; j++ {
					if pf[string([]byte{t[1],t[0]}) + string(j)] {
						found = true
						break
					} else {
						pf[t + string(j)] = true
					}
				}
			} else {
				if pf[string([]byte{t[2],t[1],t[0]})] {
					found = true
				} else {
					pf[t] = true
				}
			}
		}

		if found {
			fmt.Fprintln(w, "YES")
		} else {
			fmt.Fprintln(w, "NO")
		}
	}

}

package main

import (
	"bufio"
	"fmt"
	"os"
)

var (
	in  = bufio.NewReader(os.Stdin)
	out = bufio.NewWriter(os.Stdout)
)

func scan(a ...interface{})             { fmt.Fscan(in, a...) }
func printf(f string, a ...interface{}) { fmt.Fprintf(out, f, a...) }

func isPal(s []byte) bool {
	for i := 0; i < len(s)-i-1; i++ {
		if s[i] != s[len(s)-i-1] {
			return false
		}
	}
	return true
}

func solveCase() bool {
	var n int
	scan(&n)
	var foundPal = false
	a := make([][]byte, n)
	for i := 0; i < n; i++ {
		scan(&a[i])
		foundPal = foundPal || isPal(a[i])
	}
	if foundPal {
		return true
	}

	seen := make(map[string]bool)
	seen2 := make(map[string]bool)
	for _, s := range a {
		if len(s) == 2 {
			// ab ba
			if seen[string([]byte{s[1], s[0]})] {
				return true
			}
			// abc ba
			if seen2[string([]byte{s[1], s[0]})] {
				return true
			}
		} else if len(s) == 3 {
			// abc cba
			if seen[string([]byte{s[2], s[1], s[0]})] {
				return true
			}
			// ab cba
			if seen[string([]byte{s[2], s[1]})] {
				return true
			}
			seen2[string([]byte{s[0], s[1]})] = true
		}
		seen[string(s)] = true
	}
	return false
}

func main() {
	defer out.Flush()

	var ntc int
	scan(&ntc)
	for t := 0; t < ntc; t++ {
		if solveCase() {
			printf("YES\n")
		} else {
			printf("NO\n")
		}
	}
}

package main

import (
	"bufio"
	"fmt"
	"os"
)

//-----------------------------------------------

var (
	reader = bufio.NewReader(os.Stdin)
	writer = bufio.NewWriter(os.Stdout)
)

func scan(a ...interface{})    { fmt.Fscan(reader, a...) }
func print(a ...interface{})   { fmt.Fprint(writer, a...) }
func println(a ...interface{}) { fmt.Fprintln(writer, a...) }

//-----------------------------------------------

func isPal(s string) bool {
	i := 0
	j := len(s) - 1
	for i < j {
		if s[i] != s[j] {
			return false
		}
		i++
		j--
	}
	return true
}

func revStr(s string) string {
	rev := make([]byte, 0)
	l := len(s)
	for i := l - 1; i >= 0; i-- {
		rev = append(rev, s[i])
	}
	return string(rev)
}

//bac ab
//ab cba
//abc cba
func foo(arr []string, n int) bool {
	mp := make(map[string]struct{}, 0)
	mp2 := make(map[string]struct{}, 0)
	for i := 0; i < n; i++ {
		s := arr[i]
		if isPal(s) {
			return true
		}
		rev := revStr(s)
		if _, ok := mp[rev]; ok {
			return true
		}
		if len(s) == 2 {
			if _, ok := mp2[rev]; ok {
				return true
			}
		} else {
			if _, ok := mp[rev[0:2]]; ok {
				return true
			}
			mp2[s[0:2]] = struct{}{}
		}
		mp[s] = struct{}{}
	}
	return false
}

func output(f bool) string {
	if f {
		return "YES"
	}
	return "NO"
}

var arr []string

func main() {
	defer writer.Flush()
	var t, n int
	scan(&t)
	arr = make([]string, 100005)
	for i := 0; i < t; i++ {
		scan(&n)

		for i := 0; i < n; i++ {
			scan(&arr[i])
		}
		println(output(foo(arr, n)))
	}
}

package main

import (
	"bufio"
	"fmt"
	"os"
)

func min(a, b int)int{
	if(a < b){
		return a
	}
	return b
}

func reverse(s string) (res string) {
	for _, v := range s {
		res = string(v) + res
	}
	return res
}

var T, N int
var S []string

func solve() string {
	mp := map[string]int{}
	for i := 0; i < N; i++ {
		if S[i] == reverse(S[i]) {
			return "YES"
		}
	}

	for i := 0; i < N; i++ {
		s := S[i]
		rs := reverse(s)

		if _, ok := mp[rs]; ok {
			return "YES"
		}

		if len(s) == 2 {
			for a := 'a'; a <= 'z'; a++ {
				if _, ok := mp[rs + string(a)]; ok {
					return "YES";
				}
			}

		} else if len(s) == 3 {
			if _, ok := mp[reverse(s[1:])]; ok {
				return "YES"
			}
		}
		mp[s] = 1
	}

	return "NO"
}

func main(){
	in := bufio.NewReader(os.Stdin)
	fmt.Fscan(in, &T)
	for ; T > 0; T-- {
		fmt.Fscan(in, &N)
		S = make([]string, N)
		for i := 0; i < N; i++ {
			fmt.Fscan(in, &S[i])
		}

		fmt.Println(solve())
	}
}

package main

import (
	"bufio"
	."fmt"
	"io"
	"os"
)

// github.com/EndlessCheng/codeforces-go
func CF1628B(_r io.Reader, out io.Writer) {
	in := bufio.NewReader(_r)
	var T, n int
	var s string
	for Fscan(in, &T); T > 0; T-- {
		ok, s2, s3 := false, [26]int{}, [26][26]int{}
		for Fscan(in, &n); n > 0; n-- {
			if Fscan(in, &s); ok {
			} else if len(s) == 1 || s[0] == s[len(s)-1] {
				ok = true
			} else if len(s) == 2 {
				x, y := s[0]-'a', s[1]-'a'
				if s2[y]>>x&1 > 0 || s3[y][x] > 0 {
					ok = true
				}
				s2[x] |= 1 << y
			} else {
				x, y, z := s[0]-'a', s[1]-'a', s[2]-'a'
				if s2[z]>>y&1 > 0 || s3[z][y]>>x&1 > 0 {
					ok = true
				}
				s3[x][y] |= 1 << z
			}
		}
		if ok {
			Fprintln(out, "YES")
		} else {
			Fprintln(out, "NO")
		}
	}
}

func main() { CF1628B(os.Stdin, os.Stdout) }

package main

import (
	"bufio"
	. "fmt"
	"io"
	"os"
)

// github.com/EndlessCheng/codeforces-go
func CF1628B(_r io.Reader, out io.Writer) {
	in := bufio.NewReader(_r)
	var T, n int
	var s string
	for Fscan(in, &T); T > 0; T-- {
		ok, s2, s3 := false, [26]int{}, [26][26]int{}
		for Fscan(in, &n); n > 0; n-- {
			if Fscan(in, &s); ok {
			} else if len(s) == 1 || s[0] == s[len(s)-1] {
				ok = true
			} else if len(s) == 2 {
				x, y := s[0]-'a', s[1]-'a'
				if s2[y]>>x&1 > 0 || s3[y][x] > 0 {
					ok = true
				}
				s2[x] |= 1 << y
			} else {
				x, y, z := s[0]-'a', s[1]-'a', s[2]-'a'
				if s2[z]>>y&1 > 0 || s3[z][y]>>x&1 > 0 {
					ok = true
				}
				s3[x][y] |= 1 << z
			}
		}
		if ok {
			Fprintln(out, "YES")
		} else {
			Fprintln(out, "NO")
		}
	}
}

func main() { CF1628B(os.Stdin, os.Stdout) }

package main

import (
	"bufio"
	"fmt"
	"os"
)

var in = bufio.NewReader(os.Stdin)

func isPalindrome(t string) bool {
	if len(t) <= 1 {
		return true
	}

	n := len(t)
	for i := 0; i < n/2; i++ {
		if t[i] != t[n-1-i] {
			return false
		}
	}
	return true
}

func reverseStr(t string) string {
	n := len(t)
	tmp := make([]rune, n)
	for i, ti := range t {
		tmp[n-1-i] = ti
	}
	return string(tmp)
}

func solve() {
	var n int
	fmt.Fscan(in, &n)

	s := make([]string, n)
	for i := 0; i < n; i++ {
		fmt.Fscan(in, &s[i])
	}

	for _, si := range s {
		if isPalindrome(si) {
			fmt.Println("YES")
			return
		}
	}

	founds := make(map[string]bool)
	for _, si := range s {
		revSi := reverseStr(si)
		if _, ok := founds[revSi]; ok {
			fmt.Println("YES")
			return
		}

		if len(si) == 2 {
			base := []rune(revSi)
			for c := 'a'; c <= 'z'; c++ {
				cur := string(append(base, c))
				if _, ok := founds[cur]; ok {
					fmt.Println("YES")
					return
				}
			}
		} else if len(si) == 3 {
			if _, ok := founds[revSi[:2]]; ok {
				fmt.Println("YES")
				return
			}
		} else {
			panic("impossible")
		}
		founds[si] = true
	}
	fmt.Println("NO")
}

func main() {
	var t int
	fmt.Fscan(in, &t)

	for i := 0; i < t; i++ {
		solve()
	}
}

package main

import (
	"bufio"
	"fmt"
	"os"
	"strconv"
	"strings"
)

func main() {
	in := bufio.NewReader(os.Stdin)

	t := readInt(in)
	b := strings.Builder{}
	for i := 0; i < t; i++ {
		str := solve(in)
		b.WriteString(str + "\n")
	}
	fmt.Println(b.String())
}

func solve(in *bufio.Reader) string {
	n := readInt(in)
	scenes := make([]string, n)
	tot := map[string]int{}
	l2 := map[string]int{}
	l3 := map[string]int{}

	for i := 0; i < n; i++ {
		str, _ := in.ReadString('\n')
		str = strings.ReplaceAll(str, "\r", "")
		str = strings.ReplaceAll(str, "\n", "")
		scenes[i] = str
		tot[scenes[i]]++
	}

	// 1. check if in right we have reverse(str)
	// 2. len(str) == 2
	//    check if in left3 we have reverse(str)
	// 3. len(str) == 3
	//	  check if in left2 we have reverse(str[1:])
	for i := 0; i < n; i++ {
		str := scenes[i]
		if len(str) == 1 {
			return "YES"
		}
		tot[str]--
		rev := reverse(str)
		if val := tot[rev]; val != 0 {
			return "YES"
		}

		if len(str) == 2 {
			l2[str]++
			rev2 := reverse(str)
			if val := l3[rev2]; val != 0 {
				return "YES"
			}
		} else {
			l3[str[:2]]++
			rev3 := reverse(str[:2])
			if val := tot[rev3]; val != 0 {
				return "YES"
			}
			rev3 = reverse(str[1:])
			if val := l2[rev3]; val != 0 {
				return "YES"
			}
		}
	}

	return "NO"
}

func reverse(str string) string {
	b := strings.Builder{}
	for i := 0; i < len(str); i++ {
		b.WriteByte(str[len(str) - 1 - i])
	}
	return b.String()
}

func isPalindrome(str string) bool {
	for i := 0; i < len(str)/2; i++ {
		if str[i] != str[len(str) - 1 - i] {
			return false
		}
	}
	return true
}

func readInt(in *bufio.Reader) int {
nStr, _ := in.ReadString('\n')
nStr = strings.ReplaceAll(nStr, "\r", "")
nStr = strings.ReplaceAll(nStr, "\n", "")
n, _ := strconv.Atoi(nStr)
return n
}

func readLineNumbs(in *bufio.Reader) []string {
line, _ := in.ReadString('\n')
line = strings.ReplaceAll(line, "\r", "")
line = strings.ReplaceAll(line, "\n", "")
numbs := strings.Split(line, " ")
return numbs
}

func readArrInt(in *bufio.Reader) []int {
	numbs := readLineNumbs(in)
	arr := make([]int, len(numbs))
	for i, n := range numbs {
		val, _ := strconv.Atoi(n)
		arr[i] = val
	}
	return arr
}

package main

import (
	"bufio"
	"fmt"
	"os"
	"strconv"
	"strings"
)

func main() {
	in := bufio.NewReader(os.Stdin)

	t := readInt(in)
	b := strings.Builder{}
	for i := 0; i < t; i++ {
		str := solve(in)
		b.WriteString(str + "\n")
	}
	fmt.Println(b.String())
}

func solve(in *bufio.Reader) string {
	n := readInt(in)
	scenes := make([]string, n)
	tot := map[string]int{}
	l2 := map[string]int{}
	l3 := map[string]int{}

	for i := 0; i < n; i++ {
		str, _ := in.ReadString('\n')
		str = strings.ReplaceAll(str, "\r", "")
		str = strings.ReplaceAll(str, "\n", "")
		scenes[i] = str
		tot[scenes[i]]++
	}

	// 1. check if in right we have reverse(str)
	// 2. len(str) == 2
	//    check if in left3 we have reverse(str)
	// 3. len(str) == 3
	//	  check if in left2 we have reverse(str[1:])
	for i := 0; i < n; i++ {
		str := scenes[i]
		if len(str) == 1 {
			return "YES"
		}
		tot[str]--
		rev := reverse(str)
		if val := tot[rev]; val != 0 {
			return "YES"
		}

		if len(str) == 2 {
			l2[str]++
			rev2 := reverse(str)
			if val := l3[rev2]; val != 0 {
				return "YES"
			}
		} else {
			l3[str[:2]]++
			rev3 := reverse(str[1:])
			if val := l2[rev3]; val != 0 {
				return "YES"
			}
		}
	}

	return "NO"
}

func reverse(str string) string {
	b := strings.Builder{}
	for i := 0; i < len(str); i++ {
		b.WriteByte(str[len(str) - 1 - i])
	}
	return b.String()
}

func isPalindrome(str string) bool {
	for i := 0; i < len(str)/2; i++ {
		if str[i] != str[len(str) - 1 - i] {
			return false
		}
	}
	return true
}

func readInt(in *bufio.Reader) int {
nStr, _ := in.ReadString('\n')
nStr = strings.ReplaceAll(nStr, "\r", "")
nStr = strings.ReplaceAll(nStr, "\n", "")
n, _ := strconv.Atoi(nStr)
return n
}

func readLineNumbs(in *bufio.Reader) []string {
line, _ := in.ReadString('\n')
line = strings.ReplaceAll(line, "\r", "")
line = strings.ReplaceAll(line, "\n", "")
numbs := strings.Split(line, " ")
return numbs
}

func readArrInt(in *bufio.Reader) []int {
	numbs := readLineNumbs(in)
	arr := make([]int, len(numbs))
	for i, n := range numbs {
		val, _ := strconv.Atoi(n)
		arr[i] = val
	}
	return arr
}

package main

import (
	"bufio"
	"fmt"
	"os"
	"strconv"
	"strings"
)

func main() {
	in := bufio.NewReader(os.Stdin)

	t := readInt(in)
	b := strings.Builder{}
	for i := 0; i < t; i++ {
		str := solve(in)
		b.WriteString(str + "\n")
	}
	fmt.Println(b.String())
}

func solve(in *bufio.Reader) string {
	n := readInt(in)
	scenes := make([]string, n)
	mp := map[string]int{}
	mp2 := map[string]int{}
	mp3 := map[string]int{}

	ret := false
	for i := 0; i < n; i++ {
		str, _ := in.ReadString('\n')
		str = strings.ReplaceAll(str, "\r", "")
		str = strings.ReplaceAll(str, "\n", "")
		scenes[i] = str
		if len(scenes[i]) == 1 {
			ret = true
		}
		mp[scenes[i]]++
	}
	if ret {
		return "YES"
	}

	for i := 0; i < n; i++ {
		str := scenes[i]
		mp[str]--
		if isPalindrome(str) {
			return "YES"
		}
		if val := mp[reverse(str)]; val != 0 {
			return "YES"
		}

		if len(str) == 3 {
			mp3[str[:2]]++
			rstr := str[1:]
			if val := mp2[reverse(rstr)]; val != 0 {
				return "YES"
			}
			lstr := str[:2]
			if val := mp[reverse(lstr)]; val != 0 {
				return "YES'"
			}
		} else {
			mp2[str]++
			if val := mp3[reverse(str)]; val != 0 {
				return "YES"
			}
		}
	}

	return "NO"
}

func reverse(str string) string {
	b := strings.Builder{}
	for i := 0; i < len(str); i++ {
		b.WriteByte(str[len(str) - 1 - i])
	}
	return b.String()
}

func isPalindrome(str string) bool {
	for i := 0; i < len(str)/2; i++ {
		if str[i] != str[len(str) - 1 - i] {
			return false
		}
	}
	return true
}

func readInt(in *bufio.Reader) int {
nStr, _ := in.ReadString('\n')
nStr = strings.ReplaceAll(nStr, "\r", "")
nStr = strings.ReplaceAll(nStr, "\n", "")
n, _ := strconv.Atoi(nStr)
return n
}

func readLineNumbs(in *bufio.Reader) []string {
line, _ := in.ReadString('\n')
line = strings.ReplaceAll(line, "\r", "")
line = strings.ReplaceAll(line, "\n", "")
numbs := strings.Split(line, " ")
return numbs
}

func readArrInt(in *bufio.Reader) []int {
	numbs := readLineNumbs(in)
	arr := make([]int, len(numbs))
	for i, n := range numbs {
		val, _ := strconv.Atoi(n)
		arr[i] = val
	}
	return arr
}

package main

import (
	"bufio"
	"fmt"
	"os"
	"strconv"
	"strings"
)

func main() {
	in := bufio.NewReader(os.Stdin)

	t := readInt(in)
	b := strings.Builder{}
	for i := 0; i < t; i++ {
		str := solve(in)
		b.WriteString(str + "\n")
	}
	fmt.Println(b.String())
}

func solve(in *bufio.Reader) string {
	n := readInt(in)
	scenes := make([]string, n)
	mp := map[string]int{}
	mp2 := map[string]int{}

	ret := false
	for i := 0; i < n; i++ {
		str, _ := in.ReadString('\n')
		str = strings.ReplaceAll(str, "\r", "")
		str = strings.ReplaceAll(str, "\n", "")
		scenes[i] = str
		if len(scenes[i]) == 1 {
			ret = true
		}
		mp[scenes[i]]++
	}
	if ret {
		return "YES"
	}

	for i := 0; i < n; i++ {
		str := scenes[i]
		if isPalindrome(str) {
			return "YES"
		}
		if _, ok := mp[reverse(str)]; ok {
			return "YES"
		}

		if len(str) == 3 {
			rstr := str[1:]
			if _, ok := mp[reverse(rstr)]; ok {
				return "YES"
			}
			mp2[reverse(str[:2])]++
		} else {
			if _, ok := mp2[str]; ok {
				return "YES"
			}
		}
	}

	return "NO"
}

func reverse(str string) string {
	b := strings.Builder{}
	for i := 0; i < len(str); i++ {
		b.WriteByte(str[len(str) - 1 - i])
	}
	return b.String()
}

func isPalindrome(str string) bool {
	for i := 0; i < len(str)/2; i++ {
		if str[i] != str[len(str) - 1 - i] {
			return false
		}
	}
	return true
}

func readInt(in *bufio.Reader) int {
nStr, _ := in.ReadString('\n')
nStr = strings.ReplaceAll(nStr, "\r", "")
nStr = strings.ReplaceAll(nStr, "\n", "")
n, _ := strconv.Atoi(nStr)
return n
}

func readLineNumbs(in *bufio.Reader) []string {
line, _ := in.ReadString('\n')
line = strings.ReplaceAll(line, "\r", "")
line = strings.ReplaceAll(line, "\n", "")
numbs := strings.Split(line, " ")
return numbs
}

func readArrInt(in *bufio.Reader) []int {
	numbs := readLineNumbs(in)
	arr := make([]int, len(numbs))
	for i, n := range numbs {
		val, _ := strconv.Atoi(n)
		arr[i] = val
	}
	return arr
}

package main

import (
	"bufio"
	"fmt"
	"os"
	"strconv"
	"strings"
)

func main() {
	in := bufio.NewReader(os.Stdin)

	t := readInt(in)
	b := strings.Builder{}
	for i := 0; i < t; i++ {
		str := solve(in)
		b.WriteString(str + "\n")
	}
	fmt.Println(b.String())
}

func solve(in *bufio.Reader) string {
	n := readInt(in)
	scenes := make([]string, n)
	mp := map[string]int{}

	ret := false
	for i := 0; i < n; i++ {
		str, _ := in.ReadString('\n')
		str = strings.ReplaceAll(str, "\r", "")
		str = strings.ReplaceAll(str, "\n", "")
		scenes[i] = str
		if len(scenes[i]) == 1 {
			ret = true
		}
		mp[scenes[i]] = i
	}
	if ret {
		return "YES"
	}

	for i := 0; i < n; i++ {
		str := scenes[i]
		pal := isPalindrome(str)
		if pal {
			return "YES"
		}
		if len(str) == 2 {
			if _, ok := mp[reverse(str)]; ok {
				return "YES"
			}
		} else {
			l := str[:2]
			r := str[1:]
			rl := reverse(l)
			vall, okl := mp[rl]
			if vall > i && okl {
				return "YES"
			}
			rr := reverse(r)
			valr, okr := mp[rr]
			if valr < i && okr {
				return "YES"
			}
		}
	}

	return "NO"
}

func reverse(str string) string {
	b := strings.Builder{}
	for i := 0; i < len(str); i++ {
		b.WriteByte(str[len(str) - 1 - i])
	}
	return b.String()
}

func isPalindrome(str string) bool {
	for i := 0; i < len(str)/2; i++ {
		if str[i] != str[len(str) - 1 - i] {
			return false
		}
	}
	return true
}

func readInt(in *bufio.Reader) int {
nStr, _ := in.ReadString('\n')
nStr = strings.ReplaceAll(nStr, "\r", "")
nStr = strings.ReplaceAll(nStr, "\n", "")
n, _ := strconv.Atoi(nStr)
return n
}

func readLineNumbs(in *bufio.Reader) []string {
line, _ := in.ReadString('\n')
line = strings.ReplaceAll(line, "\r", "")
line = strings.ReplaceAll(line, "\n", "")
numbs := strings.Split(line, " ")
return numbs
}

func readArrInt(in *bufio.Reader) []int {
	numbs := readLineNumbs(in)
	arr := make([]int, len(numbs))
	for i, n := range numbs {
		val, _ := strconv.Atoi(n)
		arr[i] = val
	}
	return arr
}

package main

import (
	"bufio"
	"fmt"
	"os"
	"strconv"
	"strings"
)

func main() {
	in := bufio.NewReader(os.Stdin)

	t := readInt(in)
	b := strings.Builder{}
	for i := 0; i < t; i++ {
		b.WriteString(solve(in) + "\n")
	}
	fmt.Println(b.String())
}

func solve(in *bufio.Reader) string {
	n := readInt(in)
	scenes := make([]string, n)
	mp := map[string]struct{}{}

	for i := 0; i < n; i++ {
		str, _ := in.ReadString('\n')
		str = strings.ReplaceAll(str, "\r", "")
		str = strings.ReplaceAll(str, "\n", "")
		scenes[i] = str
		if len(scenes[i]) == 1 {
			return "YES"
		}
		mp[scenes[i]] = struct{}{}
	}

	for i := 0; i < n - 1; i++ {
		str1 := scenes[i]
		for j := i + 1; j < n; j++ {
			str2 := scenes[j]
			if isPalindrome(str1 + str2) {
				return "YES"
			}
		}
	}

	return "NO"
}

func isPalindrome(str string) bool {
	for i := 0; i < len(str)/2; i++ {
		if str[i] != str[len(str) - 1 - i] {
			return false
		}
	}
	return true
}

func readInt(in *bufio.Reader) int {
nStr, _ := in.ReadString('\n')
nStr = strings.ReplaceAll(nStr, "\r", "")
nStr = strings.ReplaceAll(nStr, "\n", "")
n, _ := strconv.Atoi(nStr)
return n
}

func readLineNumbs(in *bufio.Reader) []string {
line, _ := in.ReadString('\n')
line = strings.ReplaceAll(line, "\r", "")
line = strings.ReplaceAll(line, "\n", "")
numbs := strings.Split(line, " ")
return numbs
}

func readArrInt(in *bufio.Reader) []int {
	numbs := readLineNumbs(in)
	arr := make([]int, len(numbs))
	for i, n := range numbs {
		val, _ := strconv.Atoi(n)
		arr[i] = val
	}
	return arr
}

package main

import (
	"bufio"
	"fmt"
	"os"
)

func main() {
	r := bufio.NewReader(os.Stdin)
	w := bufio.NewWriter(os.Stdout)
	defer w.Flush()

	var T int
	for fmt.Fscan(r, &T); T > 0; T-- {

		var n int
		var found bool
		fmt.Fscan(r, &n)

		m := make(map[string]bool)

		for i := 0; i < n; i++ {
			var t string
			fmt.Fscan(r, &t)

			if m[t] || (len(t) == 3 && m[t[1:]]) || len(t) == 1 { found = true }
			var s string

			for i := range t { s += string(t[len(t)-1-i]) }
			// fmt.Fprintln(w, "*** s ", s) // **
			m[s] = true
			if len(s) == 3 { m[s[:2]] = true }
		}

		if found {
			fmt.Fprintln(w, "YES")
		} else {
			fmt.Fprintln(w, "NO")
		}
	}

}

package main

import (
	"bufio"
	"fmt"
	"os"
)

func main() {
	r := bufio.NewReader(os.Stdin)
	w := bufio.NewWriter(os.Stdout)
	defer w.Flush()

	var T int
	for fmt.Fscan(r, &T); T > 0; T-- {

		var n int
		var found bool
		fmt.Fscan(r, &n)

		m := make(map[string]bool)

		for i := 0; i < n; i++ {
			var t string
			fmt.Fscan(r, &t)

			if m[t] || (len(t) == 3 && m[t[1:]]) || len(t) == 1 { found = true }
			var s string

			for i := range t { s += string(t[len(t)-1-i]) }
			// fmt.Fprintln(w, "*** s ", s) // **
			m[s] = true
			if len(s) == 3 { m[s[1:]] = true }
		}

		if found {
			fmt.Fprintln(w, "YES")
		} else {
			fmt.Fprintln(w, "NO")
		}
	}

}

package main

import (
	"bufio"
	"fmt"
	"os"
)

var (
	in  = bufio.NewReader(os.Stdin)
	out = bufio.NewWriter(os.Stdout)
)

func scan(a ...interface{})             { fmt.Fscan(in, a...) }
func printf(f string, a ...interface{}) { fmt.Fprintf(out, f, a...) }

func isPal(s []byte) bool {
	for i := 0; i < len(s)-i-1; i++ {
		if s[i] != s[len(s)-i-1] {
			return false
		}
	}
	return true
}

func solveCase() bool {
	var n int
	scan(&n)
	var foundPal = false
	a := make([][]byte, n)
	for i := 0; i < n; i++ {
		scan(&a[i])
		foundPal = foundPal || isPal(a[i])
	}
	if foundPal {
		return true
	}

	seen := make(map[string]bool)
	seen2 := make(map[string]bool)
	for _, s := range a {
		if len(s) == 2 {
			// ab ba
			if seen[string([]byte{s[1], s[0]})] {
				return true
			}
			// abc ba
			if seen2[string([]byte{s[1], s[0]})] {
				return true
			}
		} else if len(s) == 3 {
			// abc cba
			if seen[string([]byte{s[2], s[1], s[0]})] {
				return true
			}
			// ab cba
			if seen[string([]byte{s[2], s[1]})] {
				return true
			}
			seen[string([]byte{s[2], s[1]})] = true
		}
		seen[string(s)] = true
	}
	return false
}

func main() {
	defer out.Flush()

	var ntc int
	scan(&ntc)
	for t := 0; t < ntc; t++ {
		if solveCase() {
			printf("YES\n")
		} else {
			printf("NO\n")
		}
	}
}

package main

import (
	"bufio"
	"fmt"
	"os"
)

func min(a, b int)int{
	if(a < b){
		return a
	}
	return b
}

func reverse(s string) (res string) {
	for _, v := range s {
		res = string(v) + res
	}
	return res
}

var T, N int
var S []string

func solve() string {
	mp := map[string]int{}
	for i := 0; i < N; i++ {
		mp[S[i]] = 1
		if len(S[i]) == 1 || S[i] == reverse(S[i]) {
			return "YES"
		}
	}

	for i := 0; i < N; i++ {
		s := S[i]
		rs := reverse(s)

		if _, ok := mp[rs]; ok {
			return "YES"
		}

		for a := 'a'; a <= 'z'; a++ {
			if _, ok := mp[string(a) + rs]; ok {
				return "YES"
			}
		}
	}

	return "NO"
}

func main(){
	in := bufio.NewReader(os.Stdin)
	fmt.Fscan(in, &T)
	for ; T > 0; T-- {
		fmt.Fscan(in, &N)
		S = make([]string, N)
		for i := 0; i < N; i++ {
			fmt.Fscan(in, &S[i])
		}

		fmt.Println(solve())
	}
}


First-rate specialists graduate from Berland State Institute of Peace and Friendship. You are one of the most talented students in this university. The education is not easy because you need to have fundamental knowledge in different areas, which sometimes are not related to each other. For example, you should know linguistics very well. You learn a structure of Reberland language as foreign language. In this language words are constructed according to the following rules. First you need to choose the "root" of the word — some string which has more than 4 letters. Then several strings with the length 2 or 3 symbols are appended to this word. The only restriction —  it is not allowed to append the same string twice in a row. All these strings are considered to be suffixes of the word (this time we use word "suffix" to describe a morpheme but not the few last characters of the string as you may used to). Here is one exercise that you have found in your task list. You are given the word s. Find all distinct strings with the length 2 or 3, which can be suffixes of this word according to the word constructing rules in Reberland language. Two strings are considered distinct if they have different length or there is a position in which corresponding characters do not match. Let's look at the example: the word abacabaca is given. This word can be obtained in the following ways: , where the root of the word is overlined, and suffixes are marked by "corners". Thus, the set of possible suffixes for this word is {aca, ba, ca}.

The only line contains a string s (5 ≤ |s| ≤ 104) consisting of lowercase English letters.

On the first line print integer k — a number of distinct possible suffixes. On the next k lines print suffixes.  Print suffixes in lexicographical (alphabetical) order.

NoteThe first test was analysed in the problem statement. In the second example the length of the string equals 5. The length of the root equals 5, so no string can be used as a suffix.

package main

import (
  "fmt"
  "sort"
)

func main() {
  var s string
  fmt.Scan(&s)

  dp := make([][]int, 10010)
  for i := range dp {
    dp[i] = make([]int, 5)
  }

  L := len(s)
  dp[L][2] = 1
  dp[L][3] = 1
  res := make(map[string]bool)
  for x:=L-2; x>=5; x-- {
    if x+2 == L {
      dp[x][2] = 1
    } else if x+3 == L {
      dp[x][3] = 1
    } else {
      if dp[x+2][2] == 1 && s[x:x+2] != s[x+2:x+4] {
          dp[x][2] = 1
      }
      if dp[x+2][3] == 1 {
        dp[x][2] = 1
      }

      if dp[x+3][2] == 1 {
        dp[x][3] = 1
      }
      if dp[x+3][3] == 1 && s[x:x+3] != s[x+3:x+6] {
        dp[x][3] = 1
      }
    }

    if dp[x][2]==1 {
      res[s[x:x+2]] = true
    }
    if dp[x][3]==1 {
      res[s[x:x+3]] = true
    }
  }
  //fmt.Println(len(res))
  //sort.Strings(res)
  sm := make([]string, len(res))
  j := 0
  for k, _ := range res {
    sm[j] = k
    j++
  }
  sort.Strings(sm)
  fmt.Println(len(sm))
  for _,v := range sm {
    fmt.Println(v)
  }
  //for _,v := range res {
  //  fmt.Println(v)
  //}
}

// http://codeforces.com/contest/667/problem/C
package main

import (
	"fmt"
	"bufio"
	"os"
	"sort"
)

// (c) Dmitriy Blokhin (sv.dblokhin@gmail.com)

func main () {
	var str string

	//f, _ := os.Open("input.txt")
	//input := bufio.NewReader(f)
	input := bufio.NewReader(os.Stdin)

	fmt.Fscanln(input, &str)

	n := len(str)
	ans := make(map[string]int)
	dp := make([]string, n + 1)
	dp[n] = "_"
	for i := n - 1; i >= 4; i-- {
		if len(dp[i + 1]) > 0 {

			if i - 2 >= 4 {
				sub := str[i - 2 + 1:i + 1]

				if len(dp[i + 1]) > 0 && sub != dp[i + 1] {
					ans[sub]++
					dp[i - 1] += sub
				}
			}

			if i - 3 >= 4 {
				sub := str[i - 3 + 1:i + 1]

				if len(dp[i + 1]) > 0 && sub != dp[i + 1] {
					ans[sub]++
					dp[i - 2] += sub
				}
			}
		}
	}

	// Answer
	fmt.Println(len(ans))
	strs := []string{}
	for i, _ := range ans {
		strs = append(strs, i)
	}

	sort.Strings(strs)
	for i := 0; i < len(strs); i++ {
		fmt.Println(strs[i])
	}
}

package main

import (
  "fmt"
  "sort"
)

type ResultSet []string

func suffix(s string) ResultSet {
  var dict = make(map[string]ResultSet)

  if len(s) < 2 {
    return ResultSet{}
  }
  if len(s) <= 3 {
    return ResultSet{s}
  }
  dict[s[:1]] = ResultSet{}
  dict[s[:2]] = ResultSet{s[:2]}
  dict[s[:3]] = ResultSet{s[:3]}

  for i:=3; i<len(s); i++ {
    a := s[i-1:i+1]
    //fmt.Println(a)
    //fmt.Println(s[i-3:i-1])

    var x []string = make([]string,0)
    if a != s[i-3:i-1] {
      if len(dict[s[:i-1]]) > 0 {
        x = append(dict[s[:i-1]], a)
      }
    } else {
      if len(dict[s[:i-1]]) == 1 {
        x = append(dict[s[:i-1]], a)
      }
    }

    b := s[i-2:i+1]
    var y []string = make([]string, 0)
    if (i < 5) || (b != s[i-5:i-2]) {
      if len(dict[s[:i-2]]) > 0 {
        y = append(dict[s[:i-2]], b)
      }
    } else {
      if len(dict[s[:i-2]]) == 0 {
        y = append(dict[s[:i-2]], b)
      }
    }

    dict[s[:i+1]] = append(x, y...)

  }
  //print_dict(dict)
  return dict[s]
}
func print_dict(d map[string]ResultSet) {
  for k,v := range d {
    fmt.Printf("%v->%v   ", k, v)
  }
}

func main() {
  //fmt.Println(suffix("yzzzz"))
  var s string
  fmt.Scan(&s)
  var res = make(map[string]int)
  for i:=5; i<len(s); i++ {
    for _,v := range suffix(s[i:]) {
      res[v] = 1
    }
  }
  fmt.Println(len(res))
  sm := make([]string, len(res))
  j := 0
  for k, _ := range res {
    sm[j] = k
    j++
  }
  sort.Strings(sm)
  for _,v := range sm {
    fmt.Println(v)
  }
  //fmt.Println(res)

}

package main

import (
  "fmt"
  "sort"
)

type ResultSet []string

func suffix(s string) ResultSet {
  var dict = make(map[string]ResultSet)

  if len(s) < 2 {
    return ResultSet{}
  }
  if len(s) <= 3 {
    return ResultSet{s}
  }
  dict[s[:1]] = ResultSet{}
  dict[s[:2]] = ResultSet{s[:2]}
  dict[s[:3]] = ResultSet{s[:3]}

  for i:=3; i<len(s); i++ {
    a := s[i-1:i+1]
    //fmt.Println(a)
    //fmt.Println(s[i-3:i-1])

    var x []string = make([]string,0)
    if a != s[i-3:i-1] {
      if len(dict[s[:i-1]]) > 0 {
        x = append(dict[s[:i-1]], a)
      }
    } /*else {
      x = dict[s[:i-1]]
    }*/

    b := s[i-2:i+1]
    var y []string = make([]string, 0)
    if (i < 5) || (b != s[i-5:i-2]) {
      if len(dict[s[:i-2]]) > 0 {
        y = append(dict[s[:i-2]], b)
      }
    } /*else {
      y = dict[s[:i-2]]
    }*/

    dict[s[:i+1]] = append(x, y...)

  }
  //print_dict(dict)
  return dict[s]
}
func print_dict(d map[string]ResultSet) {
  for k,v := range d {
    fmt.Printf("%v->%v   ", k, v)
  }
}

func main() {
  s := "abacabaca"
  var res = make(map[string]int)
  for i:=5; i<len(s); i++ {
    for _,v := range suffix(s[i:]) {
      res[v] = 1
    }
  }
  fmt.Println(len(res))
  sm := make([]string, len(res))
  j := 0
  for k, _ := range res {
    sm[j] = k
    j++
  }
  sort.Strings(sm)
  for _,v := range sm {
    fmt.Println(v)
  }
  //fmt.Println(res)

}

package main

import (
  "fmt"
  "sort"
)

func main() {
  var s string
  fmt.Scan(&s)

  dp := make([][]int, 100010)
  for i := range dp {
    dp[i] = make([]int, 5)
  }

  L := len(s)
  dp[L][2] = 1

  res := make([]string, 0)
  for x:=L-2; x>=5; x-- {
    if x+2 == L {
      dp[x][2] = 1
    } else if x+3 == L {
      dp[x][3] = 1
    } else {
      if dp[x+2][2] == 1 && s[x:x+2] != s[x+2:x+4] {
          dp[x][2] = 1
      }
      if dp[x+2][3] == 1 {
        dp[x][2] = 1
      }

      if dp[x+3][2] == 1 {
        dp[x][3] = 1
      }
      if dp[x+3][3] == 1 && s[x:x+3] != s[x+3:x+6] {
        dp[x][3] = 1
      }
    }

    if dp[x][2]==1 {
      res = append(res, s[x:x+2])
    }
    if dp[x][3]==1 {
      res = append(res, s[x:x+3])
    }
  }
  fmt.Println(len(res))
  sort.Strings(res)
  for _,v := range res {
    fmt.Println(v)
  }
}

package main

import (
  "fmt"
  "sort"
)

type ResultSet []string

func suffix(s string) ResultSet {
  var dict = make(map[string]ResultSet)

  if len(s) < 2 {
    return ResultSet{}
  }
  if len(s) <= 3 {
    return ResultSet{s}
  }
  dict[s[:1]] = ResultSet{}
  dict[s[:2]] = ResultSet{s[:2]}
  dict[s[:3]] = ResultSet{s[:3]}

  for i:=3; i<len(s); i++ {
    a := s[i-1:i+1]
    //fmt.Println(a)
    //fmt.Println(s[i-3:i-1])

    var x []string = make([]string,0)
    if a != s[i-3:i-1] {
      if len(dict[s[:i-1]]) > 0 {
        x = append(dict[s[:i-1]], a)
      }
    } else {
      if len(dict[s[:i-1]]) == 1 {
        x = append(dict[s[:i-1]], a)
      }
    }

    b := s[i-2:i+1]
    var y []string = make([]string, 0)
    if (i < 5) || (b != s[i-5:i-2]) {
      if len(dict[s[:i-2]]) > 0 {
        y = append(dict[s[:i-2]], b)
      }
    } else {
      if len(dict[s[:i-2]]) == 1 {
        y = append(dict[s[:i-2]], b)
      }
    }

    dict[s[:i+1]] = append(x, y...)

  }
  //print_dict(dict)
  return dict[s]
}
func print_dict(d map[string]ResultSet) {
  for k,v := range d {
    fmt.Printf("%v->%v   ", k, v)
  }
}

func main() {
  //fmt.Println(suffix("yzzzz"))
  var s string
  fmt.Scan(&s)
  var res = make(map[string]int)
  for i:=5; i<len(s); i++ {
    for _,v := range suffix(s[i:]) {
      res[v] = 1
    }
  }
  fmt.Println(len(res))
  sm := make([]string, len(res))
  j := 0
  for k, _ := range res {
    sm[j] = k
    j++
  }
  sort.Strings(sm)
  for _,v := range sm {
    fmt.Println(v)
  }
  //fmt.Println(res)

}

package main

import (
  "fmt"
  "sort"
)

type ResultSet []string

func suffix(s string) ResultSet {
  var dict = make(map[string]ResultSet)

  if len(s) < 2 {
    return ResultSet{}
  }
  if len(s) <= 3 {
    return ResultSet{s}
  }
  dict[s[:1]] = ResultSet{}
  dict[s[:2]] = ResultSet{s[:2]}
  dict[s[:3]] = ResultSet{s[:3]}

  for i:=3; i<len(s); i++ {
    a := s[i-1:i+1]
    //fmt.Println(a)
    //fmt.Println(s[i-3:i-1])

    var x []string = make([]string,0)
    if a != s[i-3:i-1] {
      if len(dict[s[:i-1]]) > 0 {
        x = append(dict[s[:i-1]], a)
      }
    } /*else {
      x = dict[s[:i-1]]
    }*/

    b := s[i-2:i+1]
    var y []string = make([]string, 0)
    if (i < 5) || (b != s[i-5:i-2]) {
      if len(dict[s[:i-2]]) > 0 {
        y = append(dict[s[:i-2]], b)
      }
    } /*else {
      y = dict[s[:i-2]]
    }*/

    dict[s[:i+1]] = append(x, y...)

  }
  //print_dict(dict)
  return dict[s]
}
func print_dict(d map[string]ResultSet) {
  for k,v := range d {
    fmt.Printf("%v->%v   ", k, v)
  }
}

func main() {
  var s string
  fmt.Scan(&s)
  var res = make(map[string]int)
  for i:=5; i<len(s); i++ {
    for _,v := range suffix(s[i:]) {
      res[v] = 1
    }
  }
  fmt.Println(len(res))
  sm := make([]string, len(res))
  j := 0
  for k, _ := range res {
    sm[j] = k
    j++
  }
  sort.Strings(sm)
  for _,v := range sm {
    fmt.Println(v)
  }
  //fmt.Println(res)

}

// http://codeforces.com/contest/667/problem/C
package main

import (
	"fmt"
	"bufio"
	"os"
	"sort"
)

// (c) Dmitriy Blokhin (sv.dblokhin@gmail.com)

func main () {
	var str string

	//f, _ := os.Open("input.txt")
	//input := bufio.NewReader(f)
	input := bufio.NewReader(os.Stdin)

	fmt.Fscanln(input, &str)

	n := len(str)
	ans := make(map[string]int)
	dp := make([]bool, n + 1)
	dp[n] = true
	for i := n - 1; i >= 4; i-- {
		if dp[i + 1] {

			if i - 2 >= 4 {
				sub := str[i - 2 + 1:i + 1]

				// check doublicate
				dublicate := false
				if i - 2 + 5 <= n {
					dublicate = sub == str[i - 2 + 3:i - 2 + 5]
				}

				if !dublicate {
					ans[sub]++
					dp[i - 1] = true
				}
			}

			if i - 3 >= 4 {
				sub := str[i - 3 + 1:i + 1]

				// check doublicate
				dublicate := false
				if i + 4 <= n {
					dublicate = sub == str[i - 3 + 4:i + 4]
				}

				if !dublicate {
					ans[sub]++
					dp[i - 2] = true
				}
			}
		}
	}

	// Answer
	fmt.Println(len(ans))
	strs := []string{}
	for i, _ := range ans {
		strs = append(strs, i)
	}

	sort.Strings(strs)
	for i := 0; i < len(strs); i++ {
		fmt.Println(strs[i])
	}
}

// http://codeforces.com/contest/667/problem/C
package main

import (
	"fmt"
	"bufio"
	"os"
	"sort"
)

// (c) Dmitriy Blokhin (sv.dblokhin@gmail.com)

func main () {
	var str string

	//f, _ := os.Open("input.txt")
	//input := bufio.NewReader(f)
	input := bufio.NewReader(os.Stdin)

	fmt.Fscanln(input, &str)

	n := len(str)
	ans := make(map[string]int)
	dp := make([]bool, n)
	dp[n - 1] = true
	for i := n - 1; i >= 4; i-- {
		if dp[i] {

			if i - 2 >= 4 {
				sub := str[i - 2 + 1:i + 1]

				// check doublicate
				dublicate := false
				if i - 4 >= 4 {
					dublicate = sub == str[i - 4 + 1:i - 4 + 1]
				}

				if !dublicate {
					ans[sub]++
					dp[i - 2] = true
				}

			}

			if i - 3 >= 4 {
				sub := str[i - 3 + 1:i + 1]

				// check doublicate
				dublicate := false
				if i - 6 >= 4 {
					dublicate = sub == str[i - 6 + 1:i - 6 + 4]
				}

				if !dublicate {
					ans[sub]++
					dp[i - 3] = true
				}
			}
		}

	}

	// Answer
	fmt.Println(len(ans))
	strs := []string{}
	for i, _ := range ans {
		strs = append(strs, i)
	}

	sort.Strings(strs)
	for i := 0; i < len(strs); i++ {
		fmt.Println(strs[i])
	}
}

// http://codeforces.com/contest/667/problem/C
package main

import (
	"fmt"
	"bufio"
	"os"
	"sort"
)

// (c) Dmitriy Blokhin (sv.dblokhin@gmail.com)

func main () {
	var str string

	//f, _ := os.Open("input.txt")
	//input := bufio.NewReader(f)
	input := bufio.NewReader(os.Stdin)

	fmt.Fscanln(input, &str)

	n := len(str)
	ans := make(map[string]int)
	dp := make([]bool, n + 1)
	dp[n] = true
	for i := n - 1; i >= 4; i-- {
		if dp[i + 1] {

			if i - 2 >= 4 {
				sub := str[i - 2 + 1:i + 1]

				// check doublicate
				dublicate := false
				if i - 2 + 5 <= n {
					dublicate = sub == str[i - 2 + 3:i - 2 + 5]
				}

				if !dublicate {
					ans[sub]++
					dp[i - 1] = true
				}
			}

			if i - 3 >= 4 {
				sub := str[i - 3 + 1:i + 1]

				// check doublicate
				dublicate := false
				if i + 4 <= n {
					dublicate = sub == str[i - 3 + 4:i + 4]
				}

				if !dublicate {
					ans[sub]++
					dp[i - 2] = true
				}
			}
		}
	}

	// Answer
	fmt.Println(len(ans))
	strs := []string{}
	for i, _ := range ans {
		strs = append(strs, i)
	}

	//sort.Strings(strs)
	for i := 0; i < len(strs); i++ {
		fmt.Println(strs[i])
	}
	sort.Strings(strs)
}