main.go 704 B

1234567891011121314151617181920212223242526272829303132333435363738394041
  1. package main
  2. import (
  3. "fmt"
  4. )
  5. func main() {
  6. var T, K int
  7. fmt.Scan(&T)
  8. for cid := 1; cid <= T; cid++ {
  9. var S string
  10. fmt.Scan(&S)
  11. fmt.Scan(&K)
  12. n := len(S)
  13. happy := make([]bool, n)
  14. for i, c := range S {
  15. happy[i] = c == '+'
  16. }
  17. flip := 0
  18. for i := 0; i < n-K+1; i++ { // O(nK)
  19. if happy[i] == false { // The only way to flip the leftmost pancake is to flip it from its position.
  20. flip++
  21. for j := 0; j < K; j++ {
  22. happy[i+j] = !happy[i+j]
  23. }
  24. }
  25. }
  26. for i := n - K; i < n; i++ {
  27. if happy[i] == false {
  28. flip = -1
  29. break
  30. }
  31. }
  32. fmt.Printf("Case #%d: ", cid)
  33. if flip == -1 {
  34. fmt.Printf("IMPOSSIBLE\n")
  35. } else {
  36. fmt.Printf("%d\n", flip)
  37. }
  38. }
  39. }