You are given string s. Your task is to determine if the given string s contains two non-overlapping substrings "AB" and "BA" (the substrings can go in any order).

The only line of input contains a string s of length between 1 and 105 consisting of uppercase Latin letters.

Print "YES" (without the quotes), if string s contains two non-overlapping substrings "AB" and "BA", and "NO" otherwise.

NoteIn the first sample test, despite the fact that there are substrings "AB" and "BA", their occurrences overlap, so the answer is "NO".In the second sample test there are the following occurrences of the substrings: BACFAB.In the third sample test there is no substring "AB" nor substring "BA".

#!/usr/bin/env ruby
s=gets.chomp
puts ((x=s.index('AB'))&&(y=s.rindex('BA'))&&y-x>1) || ((x=s.index('BA'))&&(y=s.rindex('AB'))&&y-x>1) ? :YES : :NO

# http://codeforces.com/contest/550/problem/A
def match?(s, matchers)
  a = s.clone
  matchers.all? do |w|
    b = a.clone
    a.sub!(/#{w}/, '--')

    a != b
  end
end

s = gets.chomp

puts (match?(s, %w(AB BA)) || match?(s, %w(BA AB))) ? 'YES' : 'NO'

#!/usr/bin/ruby
s = gets.chomp
2.times do
    fab = s.index('AB')
    fba = s.rindex('BA')
    s.reverse!
    s.tr!('AB', 'BA')
    next if fab.nil? || fba.nil?
    next if fab + 1 >= fba

    puts "YES"
    exit
end
puts "NO"

#!/bin/ruby

require 'json'
require 'stringio'

def twoSubstrings(str)
	if str.include?('AB') && str.include?('BA')
		tmp_str = str.split("AB", 2)
		if(tmp_str[0].include?('BA') || tmp_str[1].include?('BA'))
			return true;
		end
		tmp_str = str.split("BA", 2)
		if(tmp_str[0].include?('AB') || tmp_str[1].include?('AB'))
			return true;
		end
		return false
	else
		return false
	end
end

str = gets.rstrip

puts twoSubstrings(str) ? "YES" : "NO"

#!/bin/ruby

require 'json'
require 'stringio'

def twoSubstrings(str)
	if str.include?('AB') && str.include?('BA')
		tmp_str = str.split("AB", 2)
		return true if(tmp_str[0].include?('BA') || tmp_str[1].include?('BA'))
		tmp_str = str.split("BA", 2)
		return true if(tmp_str[0].include?('AB') || tmp_str[1].include?('AB'))
		return false
	else
		return false
	end
end

str = gets.rstrip

puts twoSubstrings(str) ? "YES" : "NO"

a=gets.chomp

if a.length>3
	i=0
	ab=0
	ba=0
	abab=0
	loop do
		if a[i..i+2]=="ABA" || a[i..i+2]=="BAB"
		abab+=1
		i+=2
		elsif a[i..i+1]=="AB"
		ab+=1
		i+=1
		elsif a[i..i+1]=="BA"
		ba+=1
		i+=1
		end
		i+=1
		break if i==a.length
		break if abab>1
		break if ab>0 && ba>0
		break if (abab>0 && ba>0) || (abab>0 && ab>0)

	end
    if ab>0 && ba>0
	puts "YES"
	elsif (abab>0 && ba>0) || (abab>0 && ab>0)
	puts "YES"
	elsif abab>1
	puts "YES"
	else
	puts "NO"
	end
else

	puts "NO"

end

# http://codeforces.com/contest/550/problem/A

def get_input
  gets.chomp
end

def result(sub1_found_idxs, sub1_length, sub2_found_idxs, sub2_length)
  return false if sub1_found_idxs.empty? || sub2_found_idxs.empty?

  case1 = sub1_found_idxs.first < sub2_found_idxs.last &&
          (sub2_found_idxs.last - sub1_found_idxs.first) >= sub1_length
  case2 = sub1_found_idxs.last > sub2_found_idxs.first &&
          (sub1_found_idxs.last - sub2_found_idxs.first) >= sub2_length

  case1 || case2
end

# Checking str contains both sub1 and sub2 without overlapping in general
def contain_substrings?(str, sub1, sub2)
  sub1_length = sub1.size
  sub2_length = sub2.size
  start_idx_sub1 = 0
  start_idx_sub2 = 0
  sub1_found_idxs = []
  sub2_found_idxs = []

  0.upto(str.size - 1) do |current_idx|
    # Find all indexes of sub1 in str
    if str[start_idx_sub1..current_idx] == sub1
      sub1_found_idxs << start_idx_sub1
      start_idx_sub1 = current_idx + 1
    elsif current_idx - start_idx_sub1 >= sub1_length - 1
      start_idx_sub1 += 1
    end

    # Find all indexes of sub2 in str
    if str[start_idx_sub2..current_idx] == sub2
      sub2_found_idxs << start_idx_sub2
      start_idx_sub2 = current_idx + 1
    elsif current_idx - start_idx_sub2 >= sub2_length - 1
      start_idx_sub2 += 1
    end
  end

  result(sub1_found_idxs, sub1_length, sub2_found_idxs, sub2_length) ? 'YES' : 'NO'
end

# puts contain_substrings?('ABA', 'AB', 'BA') => NO
# puts contain_substrings?('BACFAB', 'AB', 'BA') => YES
# puts contain_substrings?('AXBYBXA', 'AB', 'BA') => NO

puts contain_substrings?(get_input, 'AB', 'BA')

s=gets.chomp
x=s.index("AB")
y=s.rindex("BA")

if x==nil || y==nil then
	puts "NO"
elsif (x-y).abs>1 then
	puts "YES"
else
	x=s.rindex("AB")
	y=s.index("BA")
	if x==nil || y==nil then
		puts "NO"
	elsif (x-y).abs>1 then
		puts "YES"
	else
		puts "NO"
	end
end

strings = gets.strip

def solve(strings)
  i = 0
  while i < strings.size
    if strings[i..i+1] == 'AB'
      i += 2
      while i < strings.size
        if strings[i..i+1] == 'BA'
          puts 'YES'
          return
        end
        i += 1
      end
    end
    i += 1
  end
  i = 0
  while i < strings.size
    if strings[i..i+1] == 'BA'
      i += 2
      while i < strings.size
        if strings[i..i+1] == 'AB'
          puts 'YES'
          return
        end
        i += 1
      end
    end
    i += 1
  end
  puts 'NO'
end

solve(strings)

str=gets.chomp

puts ((a=str.index('AB')) and (b=str.rindex('BA')) and b-a>1) || ((a=str.index('BA')) and (b=str.rindex('AB')) and b-a>1) ? 'YES' : 'NO'

s=gets.chomp.to_s
def func(s,c,d)
	y=s
	$~=nil
	y=y.sub(/#{c}/,'x')
	#puts "HEY #{$~}"
	#puts y
	if $~
		$~=nil
		y=y.sub(/#{d}/,'x')
		#puts "BYE #{$~}"
		#puts y
		if $~
			return true
		else
			return false
		end
	else
		return false
	end
end

if func(s,'AB','BA') || func(s,'BA','AB')
	print("YES\n")
else
	puts "NO"
end

str = gets.chomp

def occurences(str)
  flag = false

  str.each_char.each_with_index.inject([]) do |result, (c, index)|
    if c == 'A'
      flag = true
    elsif c == 'B' && flag
      result << index - 1
      flag = false
    else
      flag = false
    end

    result
  end
end

array = occurences(str)
reverse_array = occurences(str.reverse).map do |item|
  str.size - item - 1
end.sort

result = nil

array.each do |item|
  result = reverse_array.find do |other_item|
    (other_item - item > 2) || (other_item < item)
  end

  if result
    break
  end
end

if result.nil? || array.empty? || reverse_array.empty?
  puts 'NO'
else
  puts 'YES'
end

s=gets.to_s
if s.index('AB').nil? || s.index('BA').nil?
  puts :NO
else
  puts (s.rindex('AB')-s.index('BA')>1||s.rindex('BA')-s.index('AB')>1)?:YES: :NO
end

input = gets.chomp

def val(input)
  ab_index = []
  ba_index = []
  i = 0
  while i < input.length - 1
    ab_index << i if input[i..i + 1] == 'AB'
    i += 1
  end

  i = input.length - 1
  while i >= 0
    ba_index << i if input[i..i + 1] == 'BA'
    i -= 1
  end

  if !ba_index.first.nil? &&
     !ab_index.first.nil? &&
     (ba_index.first - ab_index.first > 1 ||
      ab_index.last - ba_index.last > 1)
    puts 'YES'
    return
  end

  puts 'NO'
end

val(input)

n = gets.chomp.split("")

count = 0

if n.size > 3
	arr = []
	arr1 = []
	for i in (0..n.size-1)
		if n[i] == "A" && n[i+1] == "B"
			arr << [[i,i+1], "AB"]
		end
	end
	i = n.size-1
	while i > 0
		if n[i] == "A" && n[i-1] == "B"
			arr1 << [[i-1,i], "BA"]
		end
		i -= 1
	end

	if arr.size > 0 && arr1.size > 0
		if arr.size <= arr1.size
			for i in (0..arr.size-1)
				for x in (0..arr1.size-1)
					if arr[i][0][0] != arr1[x][0][0] && arr[i][0][1] != arr1[x][0][0] && arr[i][0][0] != arr1[x][0][1] && arr[i][0][1] != arr1[x][0][1]
						count += 1
						break
					end
				end
			end
		else
			for i in (0..arr1.size-1)
				for x in (0..arr.size-1)
					if arr1[i][0][0] != arr[x][0][0] && arr1[i][0][1] != arr[x][0][0] && arr1[i][0][0] != arr[x][0][1] && arr1[i][0][1] != arr[x][0][1]
						count += 1
						break
					end
				end
			end
		end
	end
end

if count > 0
	print 'YES'
else
	print 'NO'
end

input = gets.chomp
input_copy = input

a = input.index("AB")
b = input.index("BA")

if a != nil
  input = input[0,a] + "XX" + input[a+2,input.length]
  if input.index("BA") != nil
    puts("YES")
    exit(0)
  end
end

input = input_copy

if b != nil
  input = input[0,b] + "XX" + input[b+2,input.length]
  if input.index("AB") != nil
    puts("YES")
    exit(0)
  end
end

puts("NO")

def solution(string)
  return "NO" if string.index("AB").nil? || string.index("BA").nil?
  return "YES" if (string.rindex("AB") - string.index("BA")).abs != 1
  return "YES" if (string.rindex("BA") - string.index("AB")).abs != 1
  "NO"
end
puts solution(gets.strip)

# http://codeforces.com/contest/550/problem/A
s = gets.chomp

n = s.length
l_ab, l_ba, r_ab, r_ba = 0, 0, n - 1, n - 1

while l_ab < n - 1 && r_ba >= 1
  first = s[l_ab] + s[l_ab + 1]
  last = s[r_ba - 1] + s[r_ba]

  if first == "AB"
    break if last == "BA"
    r_ba -= 1
  else
    l_ab += 1
  end
end

while l_ba < n - 1 && r_ab >= 1
  first = s[l_ba] + s[l_ba + 1]
  last = s[r_ab - 1] + s[r_ab]

  if first == "BA"
    break if last == "AB"
    r_ab -= 1
  else
    l_ba += 1
  end
end

puts (r_ba - l_ab - 1 > 1) || (r_ab - l_ba - 1 > 1) ? "YES" : "NO"

s = gets
d = s.dup

if s.sub!('AB', 'XX').nil? || d.sub!('BA', 'XX').nil?
  puts 'NO'
elsif (s && s.sub!('BA', 'XX')) || (d && d.sub!('AB', 'XX'))
  puts 'YES'
else
  puts 'NO'
end

# http://codeforces.com/contest/550/problem/A
s = gets.chomp

flag = %w(AB BA).all? do |w|
  old = s.clone
  s.gsub!(/#{w}/, '')

  (s.length != old.length) && ((old.length - s.length) % 2).even?
end

puts flag ? 'YES' : 'NO'

# http://codeforces.com/contest/550/problem/A
s = gets.chomp

flag = %w(AB BA).all? do |w|
  old = s.clone
  s.sub!(/#{w}/, '')

  (s.length != old.length) && ((old.length - s.length) % 2).even?
end

puts flag ? 'YES' : 'NO'

# http://codeforces.com/contest/550/problem/A
def match?(s, matchers)
  a = s.clone
  matchers.all? do |w|
    b = a.clone
    a.sub!(/#{w}/, '')

    b.length - a.length == w.length
  end
end

s = gets.chomp
puts (match?(s, %w(AB BA)) || match?(s, %w(BA AB))) ? 'YES' : 'NO'

# http://codeforces.com/contest/550/problem/A
s = gets.chomp

flag = %w(AB BA).all? do |w|
  s2 = s.delete(w)

  return false if s2.length == s.length
  ((s2.length - s.length) % 2).even?
end

puts flag ? 'YES' : 'NO'

#!/usr/bin/ruby
s = gets.chomp
2.times do
    s.reverse!
    fab = s.index('AB')
    fba = s.rindex('BA')
    next if fab.nil? || fba.nil?
    next if fab + 1 >= fba

    puts "YES"
    exit
end
puts "NO"

#!/bin/ruby

require 'json'
require 'stringio'

def twoSubstrings(str)
	if str.include?('AB') && str.include?('BA')
		tmp_str = str.split("AB", 2)
		return false unless (tmp_str[0].include?('BA') || tmp_str[1].include?('BA'))
		tmp_str = str.split("BA", 2)
		return false unless (tmp_str[0].include?('AB') || tmp_str[1].include?('AB'))
		return true
	else
		return false
	end
end

str = gets.rstrip

puts twoSubstrings(str) ? "YES" : "NO"

#!/bin/ruby

require 'json'
require 'stringio'

def twoSubstrings(str)
	if str.include?('AB') && str.include?('BA')
		tmp_str = str.split("AB", 2)
		return false unless (tmp_str[0].include?('BA') || tmp_str[1].include?('BA'))
		tmp_str = str.split("BA", 2)
		return false unless (tmp_str[0].include?('AB') || tmp_str[1].include?('AB'))
		return true
	else
		return false
	end
end

str = gets.rstrip

puts twoSubstrings str

#!/bin/ruby

require 'json'
require 'stringio'

def twoSubstrings(str)
	return false if str.include?('ABA')
	if str.include?('AB') && str.include?('BA')
		tmp_str = str.split("AB", 2)
		return false unless (tmp_str[0].include?('BA') || tmp_str[1].include?('BA'))
		tmp_str = str.split("BA", 2)
		return false unless (tmp_str[0].include?('AB') || tmp_str[1].include?('AB'))
		return true
	else
		return false
	end
end

str = gets.rstrip

puts twoSubstrings(str) ? "YES" : "NO"

a=gets.chomp

if a.length>3
	i=0
	ab=0
	ba=0
	abab=0
	loop do
		if a[i..i+2]=="ABA" || a[i..i+2]=="BAB"
		abab+=1
		i+=2
		elsif a[i..i+1]=="AB"
		ab+=1
		i+=1
		elsif a[i..i+1]=="BA"
		ba+=1
		i+=1
		end
		i+=1
		break if i==a.length
		break if abab>1
		break if ab>0 && ba>0
		break if (abab>0 && ba>0) || (abab>0 && ab>0)

	end
    if ab>0 && ba>0
	puts "YES"
	elsif (abab>0 && ba>1) || (abab>0 && ab>1)
	puts "YES"
	else
	puts "NO"
	end
else

	puts "NO"

end

a=gets.chomp

if a.length>3
	i=0
	ab=0
	ba=0
	abab=0
	loop do
		if a[i..i+2]=="ABA" || a[i..i+2]=="BAB"
		abab+=1
		i+=2
		elsif a[i..i+1]=="AB"
		ab+=1
		i+=1
		elsif a[i..i+1]=="BA"
		ba+=1
		i+=1
		end
		i+=1
		break if i==a.length
		break if abab>1
		break if ab>0 && ba>0
		break if (abab>0 && ba>0) || (abab>0 && ab>0)

	end
    if ab>0 && ba>0
	puts "YES"
	elsif (abab>0 && ba>1) || (abab>0 && ab>1)
	puts "YES"
	elsif abab>1
	puts "YES"
	else
	puts "NO"
	end
else

	puts "NO"

end

a=gets.chomp
i=0
f=0
p="ab"
loop do
if p=="ab"

	if a[i..i+1]=="AB" || a[i..i+1]=="BA"
		if a[i..i+1]=="AB"
		p="b"
		else
		p="a"
		end
	f+=1
	i+=1
	end
elsif  p=="a"

	if a[i..i+1]=="AB"
	f+=1
	i+=1
	p="b"
	end
elsif p=="b"

	if a[i..i+1]=="BA"
	f+=1
	i+=1
	end
end
	i+=1
	break if i==a.length
end

if f==2
puts "YES"
else
puts "NO"
end

a=gets.chomp

if a.length>3
	i=0
	ab=0
	ba=0
	abab=0
	loop do
		if a[i..i+2]=="ABA" || a[i..i+2]=="BAB"
		abab+=1
		end
		if a[i..i+1]=="AB"
		ab+=1
		i+=1
		elsif a[i..i+1]=="BA"
		ba+=1
		i+=1
		end
		i+=1
		break if i==a.length
		break if abab>1
		break if ab>0 && ba>0
		break if (abab>0 && ba>1) || (abab>0 && ab>1)

	end
    if ab>0 && ba>0
	puts "YES"
	elsif (abab>0 && ba>1) || (abab>0 && ab>1)
	puts "YES"
	else
	puts "NO"
	end
else

	puts "NO"

end

# http://codeforces.com/contest/550/problem/A

def get_input
  gets.chomp
end

# Checking str contains both sub1 and sub2 without overlapping in general
def contain_substrings?(str, sub1, sub2)
  sub1_length = sub1.size
  sub2_length = sub2.size
  start_idx_sub1 = 0
  start_idx_sub2 = 0
  sub1_found_idxs = []
  sub2_found_idxs = []

  0.upto(str.size - 1) do |current_idx|
    # Find all indexes of sub1 in str
    if str[start_idx_sub1..current_idx] == sub1
      sub1_found_idxs << start_idx_sub1
      start_idx_sub1 = current_idx + 1
    elsif current_idx - start_idx_sub1 >= sub1_length - 1
      start_idx_sub1 += 1
    end

    # Find all indexes of sub2 in str
    if str[start_idx_sub2..current_idx] == sub2
      sub2_found_idxs << start_idx_sub2
      start_idx_sub2 = current_idx + 1
    elsif current_idx - start_idx_sub2 >= sub2_length - 1
      start_idx_sub2 += 1
    end
  end

  if sub1_found_idxs.empty? || sub2_found_idxs.empty?
    'NO'
  elsif sub1_found_idxs.first < sub2_found_idxs.last
    sub2_found_idxs.last - sub1_found_idxs.first >= sub1_length ? 'YES' : 'NO'
  elsif sub1_found_idxs.last > sub2_found_idxs.first
    sub1_found_idxs.last - sub2_found_idxs.first >= sub2_length ? 'YES' : 'NO'
  else
    'NO'
  end
end

# puts contain_substrings?('ABA', 'AB', 'BA') => NO
# puts contain_substrings?('BACFAB', 'AB', 'BA') => YES
# puts contain_substrings?('AXBYBXA', 'AB', 'BA') => NO

puts contain_substrings?(get_input, 'AB', 'BA')

# http://codeforces.com/contest/550/problem/A

def get_input
  gets.chomp
end

def result(sub1_found_idxs, sub1_length, sub2_found_idxs, sub2_length)
  return 'NO' if sub1_found_idxs.empty? || sub2_found_idxs.empty?

  case1 = sub1_found_idxs.first < sub2_found_idxs.last &&
          (sub2_found_idxs.last - sub1_found_idxs.first) >= sub1_length
  case2 = sub1_found_idxs.last > sub2_found_idxs.first &&
          (sub1_found_idxs.last - sub2_found_idxs.first) >= sub2_length

  case1 || case2
end

# Checking str contains both sub1 and sub2 without overlapping in general
def contain_substrings?(str, sub1, sub2)
  sub1_length = sub1.size
  sub2_length = sub2.size
  start_idx_sub1 = 0
  start_idx_sub2 = 0
  sub1_found_idxs = []
  sub2_found_idxs = []

  0.upto(str.size - 1) do |current_idx|
    # Find all indexes of sub1 in str
    if str[start_idx_sub1..current_idx] == sub1
      sub1_found_idxs << start_idx_sub1
      start_idx_sub1 = current_idx + 1
    elsif current_idx - start_idx_sub1 >= sub1_length - 1
      start_idx_sub1 += 1
    end

    # Find all indexes of sub2 in str
    if str[start_idx_sub2..current_idx] == sub2
      sub2_found_idxs << start_idx_sub2
      start_idx_sub2 = current_idx + 1
    elsif current_idx - start_idx_sub2 >= sub2_length - 1
      start_idx_sub2 += 1
    end
  end

  result(sub1_found_idxs, sub1_length, sub2_found_idxs, sub2_length) ? 'YES' : 'NO'
end

# puts contain_substrings?('ABA', 'AB', 'BA') => NO
# puts contain_substrings?('BACFAB', 'AB', 'BA') => YES
# puts contain_substrings?('AXBYBXA', 'AB', 'BA') => NO

puts contain_substrings?(get_input, 'AB', 'BA')

s=gets.chomp
x=s.index("AB")
y=s.rindex("BA")
if x==nil || y==nil then
	puts "NO"
elsif (x-y).abs>1 then
	puts "YES"
else
	puts "NO"
end

strings = gets.strip

ab = nil
ba = nil

i = 0

yes = false
while i < strings.size
  if !ab && strings[i..i+1] == 'AB' && ([i, i+1]&ba.to_a).empty?
    ab = [i, i+1]
  elsif !ba && strings[i..i+1] == 'BA' && ([i, i+1]&ab.to_a).empty?
    ba = [i, i+1]
  end

  if ab && ba && (ab&ba).empty?
    yes = true
    break
  end

  i += 1
end

puts yes ? 'YES' : 'NO'

strings = gets.strip

ab = nil
ba = nil

i = 0

yes = false
while i < strings.size
  if strings[i..i+1] == 'AB' && ([i, i+1]&ba.to_a).empty?
    ab = [i, i+1]
  elsif strings[i..i+1] == 'BA' && ([i, i+1]&ba.to_a).empty?
    ba = [i, i+1]
  end

  if ab && ba && (ab&ba).empty?
    yes = true
    break
  end

  i += 1
end

puts yes ? 'YES' : 'NO'

strings = gets.strip

ab = nil
ba = nil

i = 0

while i < strings.size
  ab = [i, i+1] if strings[i..i+1] == 'AB'
  ba = [i, i+1] if strings[i..i+1] == 'BA'
  break if ab && ba
  i += 1
end

puts ab && ba && (ab&ba).empty? ? 'YES' : 'NO'

strings = gets.strip

flags = 0
strings.each_char do |c|
  flags ^= 1 << c.ord - 65
end

count = 0
while flags != 0
  flags &= flags - 1
  count += 1
end

puts count > 1 ? 'YES' : 'NO'

strings = gets.strip

def solve(strings)
  i = 0
  while i < strings.size
    if strings[i..i+1] == 'AB'
      i += 2
      while i < strings.size
        if strings[i..i+1] == 'BA'
          puts 'YES'
          return
        end
        i += 1
      end
    elsif strings[i..i+1] == 'BA'
      i += 2
      while i < strings.size
        if strings[i..i+1] == 'AB'
          puts 'YES'
          return
        end
        i += 1
      end
    end
    i += 1
  end
  puts 'NO'
end

solve(strings)

strings = 'AXBYBXA'

flags = 0
strings.each_char do |c|
  flags ^= 1 << c.ord - 65
end

count = 0
while flags != 0
  flags &= flags - 1
  count += 1
end

puts count > 1 ? 'YES' : 'NO'

str=gets.chomp

puts ((a=str.index('AB')) and (b=str.index('BA')) and b-a>1) || ((a=str.index('BA')) and (b=str.rindex('AB')) and b-a>1) ? 'YES' : 'NO'

s=gets.chomp.to_s
s=s.sub(/AB/,'')
puts s
unless $~
	puts "NO"
else
	s=s.sub(/BA/,'')
	puts s
	unless $~
		puts ("NO")
	else
		puts "YES"
	end
end

s=gets.chomp.to_s
def func(s,c,d)
	y=s
	y=y.sub(/#{c}/,'')
	if $~
		y=y.sub(/#{d}/,'')
		if $~
			return true
		else
			return false
		end
	else
		return false
	end
end

if func(s,'AB','BA') || func(s,'BA','AB')
	print("YES\n")
else
	puts "NO"
end

s=gets.chomp.to_s
s=s.sub(/AB/,'')
#puts s
unless $~
	puts "NO"
else
	s=s.sub(/BA/,'')
	#puts s
	unless $~
		puts ("NO")
	else
		puts "YES"
	end
end

str = gets.chomp

flag = false
array = []

str.each_char.each_with_index do |c, index|
  if c == 'A'
    flag = true
  elsif c == 'B' && flag
    array << index - 1
  else
    flag = false
  end
end

reverse_array = []
flag = false
size = str.size

str.reverse.each_char.each_with_index do |c, index|
  if c == 'A'
    flag = true
  elsif c == 'B' && flag
    reverse_array << size - index
  else
    flag = false
  end
end

result = nil

array.each do |item|
  result = reverse_array.bsearch { |other_item| other_item > item && other_item - item < 3 }

  if result
    break
  end
end

if result || array.empty? || reverse_array.empty?
  puts 'NO'
else
  puts 'YES'
end

str = gets.chomp

def occurences(str)
  flag = false

  str.each_char.each_with_index.inject([]) do |result, (c, index)|
    if c == 'A'
      flag = true
    elsif c == 'B' && flag
      result << index - 1
    else
      flag = false
    end

    result
  end
end

array = occurences(str)
reverse_array = occurences(str.reverse).map do |item|
  str.size - item - 1
end.sort

result = nil

puts "#{array} #{reverse_array}"
array.each do |item|
  result = reverse_array.bsearch { |other_item| (other_item - item).abs > 2 }

  if result
    break
  end
end

if result.nil? || array.empty? || reverse_array.empty?
  puts 'NO'
else
  puts 'YES'
end

str = gets.chomp

def occurences(str)
  flag = false

  str.each_char.each_with_index.inject([]) do |result, (c, index)|
    if c == 'A'
      flag = true
    elsif c == 'B' && flag
      result << index - 1
    else
      flag = false
    end

    result
  end
end

array = occurences(str)
reverse_array = occurences(str.reverse).map do |item|
  str.size - item - 1
end.sort

result = nil

array.each do |item|
  result = reverse_array.bsearch do |other_item|
    (other_item - item > 2) || (other_item < item)
  end

  if result
    break
  end
end

if result.nil? || array.empty? || reverse_array.empty?
  puts 'NO'
else
  puts 'YES'
end

str = gets.chomp

def occurences(str)
  flag = false

  str.each_char.each_with_index.inject([]) do |result, (c, index)|
    if c == 'A'
      flag = true
    elsif c == 'B' && flag
      result << index - 1
    else
      flag = false
    end

    result
  end
end

array = occurences(str)
reverse_array = occurences(str.reverse).map do |item|
  str.size - item - 1
end.sort

result = nil

array.each do |item|
  result = reverse_array.bsearch { |other_item| (other_item - item).abs > 2 }

  if result
    break
  end
end

if result.nil? || array.empty? || reverse_array.empty?
  puts 'NO'
else
  puts 'YES'
end

str = gets.chomp

def occurences(str)
  flag = false

  str.each_char.each_with_index.inject([]) do |result, (c, index)|
    if c == 'A'
      flag = true
    elsif c == 'B' && flag
      result << index - 1
      flag = false
    else
      flag = false
    end

    result
  end
end

array = occurences(str)
reverse_array = occurences(str.reverse).map do |item|
  str.size - item - 1
end.sort

result = nil

array.each do |item|
  result = reverse_array.bsearch do |other_item|
    (other_item - item > 2) || (other_item < item)
  end

  if result
    break
  end
end

if result.nil? || array.empty? || reverse_array.empty?
  puts 'NO'
else
  puts 'YES'
end

input = 'AXBYBXA'

def validate(input)
  return false unless input.include?('AB') && input.include?('BA')
  input.each_char.with_index do |_, index|
    break if index > input.length - 2
    if input[index..index + 1] == 'AB' || input[index..index + 1] == 'BA'
      # back and forward word should not overlap

      unless input[index..index + 2] == 'ABA' ||
             input[index..index + 2] == 'ABA' ||
             input[index - 1..index + 1] == 'ABA' ||
             input[index - 1..index + 1] == 'ABA'
        return true
      end
    end
  end

  true
end

def output(input)
  if validate(input)
    puts 'YES'
  else
    puts 'NO'
  end
end

output(input)

input = gets.chomp

def validate(input)
  return false unless input.include?('AB') || input.include?('BA')
  input.each_char.with_index do |_, index|
    break if index > input.length - 1
    if input[index..index + 1] == 'AB' || input[index..index + 1] == 'BA'
      # back and forward word should not overlap

      unless input[index..index + 2] == 'ABA' ||
             input[index..index + 2] == 'ABA' ||
             input[index - 1..index + 1] == 'ABA' ||
             input[index - 1..index + 1] == 'ABA'
        return true
      end
    end
  end

  true
end

def output(input)
  if validate(input)
    puts 'YES'
  else
    puts 'NO'
  end
end

output(input)

input = gets.chomp

def validate(input)
  return false unless input.include?('AB') || input.include?('BA')
  input.each_char.with_index do |_, index|
    break if index > input.length - 1
    if input[index..index + 1] == 'AB' || input[index..index + 1] == 'BA'
      # back and forward word should not overlap

      unless input[index..index + 2] == 'ABA' ||
             input[index..index + 2] == 'ABA' ||
             input[index - 1..index + 1] == 'ABA' ||
             input[index - 1..index + 1] == 'ABA'
        return true
      end
    end
  end

  false
end

def output(input)
  if validate(input)
    puts 'YES'
  else
    puts 'NO'
  end
end

output(input)

def read
  #gets.chomp
  'ABAAB'
end

def validate
  input = read
  return false unless input.include?('AB') || input.include?('BA')
  input.each_char.with_index do |_, index|
    break if index > input.length - 3
    if input[index..index + 2] == 'ABA' || input[index..index + 2] == 'BAB'
      return false
    end
  end

  true
end

def output
  if validate
    puts 'YES'
  else
    puts 'NO'
  end
end

output

input = gets.chomp

def validate(input)
  return false unless input.include?('AB') || input.include?('BA')
  input.each_char.with_index do |_, index|
    break if index > input.length - 1
    if input[index..index + 1] == 'AB' || input[index..index + 1] == 'BA'
      # back and forward word should not overlap

      puts input[index..index + 1]
      unless input[index..index + 2] == 'ABA' ||
             input[index..index + 2] == 'ABA' ||
             input[index - 1..index + 1] == 'ABA' ||
             input[index - 1..index + 1] == 'ABA'
        return true
      end
    end
  end

  false
end

def output(input)
  if validate(input)
    puts 'YES'
  else
    puts 'NO'
  end
end

output(input)

def read
  gets.chomp
end

def validate
  input = read
  return false unless input.include?('AB') || input.include?('BA')
  input.each_char.with_index do |_, index|
    break if index > input.length - 3
    if input[index..index + 3] == 'ABA' || input[index..index + 3] == 'BAB'
      return false
    end
  end

  true
end

def output
  if validate
    puts 'YES'
  else
    puts 'NO'
  end
end

output

input = gets.chomp

def val(input)
  ab_index = []
  ba_index = []
  i = 0
  while i < input.length - 1
    ab_index << i if input[i..i + 1] == 'AB'
    i += 1
  end

  i = input.length - 1
  while i >= 0
    ba_index << i if input[i..i + 1] == 'BA'
    i -= 1
  end

  if !ba_index.first.nil? &&
     !ab_index.first.nil? &&
     (ba_index.first - ab_index.first > 1 ||
      ab_index.first - ba_index.first > 1)
    puts 'YES'
    return
  end

  puts 'NO'
end

val(input)

input = gets.chomp

def val(input)
  ab_index = []
  ba_index = []
  i = 0
  while i < input.length - 1
    ab_index << i if input[i..i + 1] == 'AB'
    i += 1
  end

  i = input.length - 1
  while i >= 0
    ba_index << i if input[i..i + 1] == 'BA'
    i -= 1
  end

  if !ba_index.last.nil? &&
     !ab_index.first.nil? &&
     (ba_index.last - ab_index.first > 1 ||
      ab_index.first - ba_index.last > 1)
    puts 'YES'
    return
  end

  puts 'NO'
end

val(input)

input = 'ABAAHBA'

def validate(input)
  return false unless input.include?('AB') && input.include?('BA')
  input.each_char.with_index do |_, index|
    break if index > input.length - 2
    if input[index..index + 1] == 'AB' || input[index..index + 1] == 'BA'
      # back and forward word should not overlap

      unless input[index..index + 2] == 'ABA' ||
             input[index..index + 2] == 'ABA' ||
             input[index - 1..index + 1] == 'ABA' ||
             input[index - 1..index + 1] == 'ABA'
        return true
      end
    end
  end

  false
end

def output(input)
  if validate(input)
    puts 'YES'
  else
    puts 'NO'
  end
end

output(input)

input = gets.chomp

def validate(input)
  return false unless input.include?('AB') && input.include?('BA')
  input.each_char.with_index do |_, index|
    break if index > input.length - 3
    if input[index..index + 2] == 'ABA' || input[index..index + 2] == 'BAB'
      return false
    end
  end

  true
end

def output(input)
  if validate(input)
    puts 'YES'
  else
    puts 'NO'
  end
end

output(input)

n = gets.chomp.split("")

ab = 0
ba = 0

while n.size > 1 || (ab > 0 && ba > 0)
	if n.index("A") != nil && n.index("B") != nil && n.index("A") == n.index("B")-1 && n.size >= 2
		ab += 1
		n.delete_at(n.index("A"))
		n.delete_at(n.index("B"))
	elsif n.index("A") != nil && n.index("B") != nil && n.index("B") == n.index("A")-1 && n.size >= 2
		ba += 1
		n.delete_at(n.index("B"))
		n.delete_at(n.index("A"))
	else
		break
	end
end
if ab > 0 && ba > 0
	print "YES"
else
	print "NO"
end

n = gets.chomp.split("")

count = 0

if n.size > 3
	arr = []
	arr1 = []
	for i in (0..n.size-1)
		if n[i] == "A" && n[i+1] == "B"
			arr << [i+1, "AB"]
		end
	end
	i = n.size-1
	while i > 1
		if n[i] == "A" && n[i-1] == "B"
			arr1 << [i-1, "BA"]
		end
		i -= 1
	end

	if arr.size > 0 && arr1.size > 0
		if arr.size <= arr1.size
			for i in (0..arr.size-1)
				for x in (0..arr1.size-1)
					if arr[i][0] != arr1[x][0]
						count += 1
						break
					end
				end
			end
		else
			for i in (0..arr1.size-1)
				for x in (0..arr.size-1)
					if arr1[i][0] != arr[x][0]
						count += 1
						break
					end
				end
			end
		end
	end
end

if count > 0
	print 'YES'
else
	print 'NO'
end

n = gets.chomp.split("")

count = 0

if n.size > 3
	arr = []
	arr1 = []
	for i in (0..n.size-1)
		if n[i] == "A" && n[i+1] == "B"
			arr << [i+1, "AB"]
		end
	end
	i = n.size-1
	while i > 0
		if n[i] == "A" && n[i-1] == "B"
			arr1 << [i, "BA"]
		end
		i -= 1
	end

	if arr.size > 0 && arr1.size > 0
		for i in (0..arr.size-1)
			if arr[i][0] != arr1[i][0]
				count += 1
				break
			end
		end
	end
end

if count > 0
	print 'YES'
else
	print 'NO'
end

n = gets.chomp.split("")

count = 0

if n.size > 3
	arr = []
	arr1 = []
	for i in (0..n.size-1)
		if n[i] == "A" && n[i+1] == "B"
			arr << [i+1, "AB"]
		end
	end
	i = n.size-1
	while i > 0
		if n[i] == "A" && n[i-1] == "B"
			arr1 << [i-1, "BA"]
		end
		i -= 1
	end

	if arr.size > 0 && arr1.size > 0
		if arr.size <= arr1.size
			for i in (0..arr.size-1)
				for x in (0..arr1.size-1)
					if arr[i][0] != arr1[x][0]
						count += 1
						break
					end
				end
			end
		else
			for i in (0..arr1.size-1)
				for x in (0..arr.size-1)
					if arr1[i][0] != arr[x][0]
						count += 1
						break
					end
				end
			end
		end
	end
end

if count > 0
	print 'YES'
else
	print 'NO'
end

input = gets.chomp
input_copy = input

a = input.index("AB")
b = input.index("BA")

if a != nil
  input = input[0,a] + input[a+2,input.length]
  if input.index("BA") != nil
    puts("YES")
    exit(0)
  end
end

input = input_copy

if b != nil
  input = input[0,b] + input[b+2,input.length]
  if input.index("AB") != nil
    puts("YES")
    exit(0)
  end
end

puts("NO")

def solution(string)
  return "NO" if string.index("AB").nil? || string.index("BA").nil?
  return "YES" if (string.rindex("AB") - string.index("BA")).abs != 1
  "NO"
end
puts solution(gets.strip)

s = gets.chomp.chars
c = [false, false]
i = 0
while c.include?(false) && i < s.size
  if s[i] == 'A' && s[i + 1] == 'B' && !c[0]
    c[0] = true
    i += 2
  elsif s[i] == 'B' && s[i + 1] == 'A' && !c[1]
    c[1] = true
    i += 2
  else
    i += 1
  end
end
puts c.all?{|x| x} ? 'YES' : 'NO'

s = gets.chomp
puts s.include?('AB') && s.include?('BA') && !s.include?('ABA') && !s.include?('BAB') ? 'YES' : 'NO'

s = gets.chomp.chars
c = [false, false]
i = 0
while c.include?(false) && i < s.size
  if s[i] == 'A' && s[i + 1] == 'B'
    c[0] = true
    i += 2
  elsif s[i] == 'B' && s[i + 1] == 'A'
    c[1] = true
    i += 2
  else
    i += 1
  end
end
puts c.all?{|x| x} ? 'YES' : 'NO'

s = gets.chomp
puts s.slice!('AB') && s.slice!('BA') ? 'YES' : 'NO'

s = gets.chomp
puts (s.slice!('AB') && s.slice!('BA')) || (s.slice!('BA') && s.slice!('AB')) ? 'YES' : 'NO'

# http://codeforces.com/contest/550/problem/A

s = gets.chomp

n = s.length
l, r = 0, n - 1

while l < n - 1
  first = s[l] + s[l + 1]
  if first == "AB" || first == "BA"
    break l
  else
    l += 1
  end
end

while r >= 1
  last = s[r] + s[r - 1]
  if last == "AB" || last == "BA"
    break r -= 1
  else
    r -= 1
  end
end

puts r - l > 1 ? "YES" : "NO"

# http://codeforces.com/contest/550/problem/A
s = gets.chomp

n = s.length
l, r = 0, n - 1

while l < n - 1 && r >= 1
  first = s[l] + s[l + 1]
  last = s[r - 1] + s[r]
  if first == "AB"
    break if last == "BA"
    r -= 1
  elsif first == "BA"
    break if last == "AB"
    r -= 1
  else
    l += 1
  end
end

puts r - l - 1 > 1 ? "YES" : "NO"

s = gets
d = s.dup

if s.sub!('AB', '').nil? || d.sub!('BA', '').nil?
  puts 'NO'
elsif (s && s.sub!('BA', '')) || (d && d.sub!('AB', ''))
  puts 'YES'
else
  puts 'NO'
end

begin
  throw 'Error' if gets.sub!('AB', '').sub!('BA', '').nil?
  puts 'YES'
rescue
  puts 'NO'
end


Paladin Manao caught the trail of the ancient Book of Evil in a swampy area. This area contains n settlements numbered from 1 to n. Moving through the swamp is very difficult, so people tramped exactly n - 1 paths. Each of these paths connects some pair of settlements and is bidirectional. Moreover, it is possible to reach any settlement from any other one by traversing one or several paths.The distance between two settlements is the minimum number of paths that have to be crossed to get from one settlement to the other one. Manao knows that the Book of Evil has got a damage range d. This means that if the Book of Evil is located in some settlement, its damage (for example, emergence of ghosts and werewolves) affects other settlements at distance d or less from the settlement where the Book resides.Manao has heard of m settlements affected by the Book of Evil. Their numbers are p1, p2, ..., pm. Note that the Book may be affecting other settlements as well, but this has not been detected yet. Manao wants to determine which settlements may contain the Book. Help him with this difficult task.

The first line contains three space-separated integers n, m and d (1 ≤ m ≤ n ≤ 100000; 0 ≤ d ≤ n - 1). The second line contains m distinct space-separated integers p1, p2, ..., pm (1 ≤ pi ≤ n). Then n - 1 lines follow, each line describes a path made in the area. A path is described by a pair of space-separated integers ai and bi representing the ends of this path.

Print a single number — the number of settlements that may contain the Book of Evil. It is possible that Manao received some controversial information and there is no settlement that may contain the Book. In such case, print 0.

NoteSample 1. The damage range of the Book of Evil equals 3 and its effects have been noticed in settlements 1 and 2. Thus, it can be in settlements 3, 4 or 5.

@n, m, d = gets.chomp.split(' ').map(&:to_i)

@d_down = Array.new(@n+1) { -1 }
@d_up = Array.new(@n+1) { -1 }

@marked = Array.new(@n+1)
@marked_points = gets.chomp.split(' ').map(&:to_i)
@marked_points.each do |m|
	@marked[m] = true
	@d_down[m] = 0
end

@d_up[1] = @marked[1] ? 0 : -1

@map = Array.new(@n+1) {|i| []}
(2..@n).each do
	a, b = gets.chomp.split(' ').map(&:to_i)
	@map[a] << b
	@map[b] << a
end

@way_down = Array.new(@n+1) {|i| []}
@way_up = Array.new(@n+1) {|i| []}
@traverse = []

@deep_points = []

go = [1]
while go.size > 0
	new_go = []
	go.each do |from|
		@traverse << from
		added = false
		@map[from].each do |to|
			next if @way_up[from].include? to
			@way_down[from] << to
			@way_up[to] << from
			new_go << to
			added = true
		end
		@deep_points << from unless added
	end
	go = new_go
end

#p @way_down
#p @way_up
#p @deep_points

def dist_down_alt2
	(@traverse.size-1).downto(0).each do |i|
		parent = @traverse[i]
		@way_down[parent].each do |child|
			if @d_down[child] != -1
				tmp = @d_down[child] + 1
				@d_down[parent] = tmp if tmp > @d_down[parent]
			end
		end
	end
end

def dist_up
	target = [1]
	visited = Array.new(@n+1) { false }

	while target.size > 0
		new_target = []
		target.each do |to|
			next if visited[to]
			visited[to] = true

			max1 = -1
			max2 = -1
			@way_down[to].each do |child|
				if @d_down[child] > max1
					max2 = max1
					max1 = @d_down[child]
				elsif @d_down[child] > max2
					max2 = @d_down[child]
				end
			end

			@way_down[to].each do |child|
				sibling = @d_down[child] == max1 ? max2 : max1
				@d_up[child] = sibling + 2 if sibling != -1

				if @d_up[to] != -1
					tmp = @d_up[to] + 1
					@d_up[child] = tmp if tmp > @d_up[child]
				end

				if @marked[child]
					@d_up[child] = 0 if @d_up[child] < 0
				end

				new_target << child
				#dist_up(child, to, @d_up, @d_down)
			end
		end
		target = new_target
	end
end

#dist_down
dist_down_alt2
dist_up

sum = 0
(1..@n).each do |i|
	sum += 1 if @d_down[i] <= d and @d_up[i] <= d
end
p sum

@n, m, @d = gets.chomp.split(' ').map(&:to_i)

@d_down = Array.new(@n+1) { -1 }
@d_up = Array.new(@n+1) { -1 }

@marked = Array.new(@n+1)
gets.chomp.split(' ').map(&:to_i).each do |m|
	@marked[m] = true
	@d_down[m] = 0
end

@d_up[1] = @marked[1] ? 0 : -1

@map = Array.new(@n+1) {|i| []}
(2..@n).each do
	a, b = gets.chomp.split(' ').map(&:to_i)
	@map[a] << b
	@map[b] << a
end

@way_down = Array.new(@n+1) {|i| []}
@way_up = Array.new(@n+1) {|i| []}
@traverse = []

go = [1]
while go.size > 0
	new_go = []
	go.each do |from|
		@traverse << from
		@map[from].each do |to|
			next if @way_up[from].include? to
			@way_down[from] << to
			@way_up[to] << from
			new_go << to
		end
	end
	go = new_go
end

def dist_down
	(@traverse.size-1).downto(0).each do |i|
		parent = @traverse[i]
		@way_down[parent].each do |child|
			if @d_down[child] != -1
				tmp = @d_down[child] + 1
				@d_down[parent] = tmp if tmp > @d_down[parent]
			end
		end
	end
end

def dist_up
	sum = 0
	@traverse.each do |to|
		max1 = -1
		max2 = -1
		@way_down[to].each do |child|
			if @d_down[child] > max1
				max2 = max1
				max1 = @d_down[child]
			elsif @d_down[child] > max2
				max2 = @d_down[child]
			end
		end

		@way_down[to].each do |child|
			sibling = @d_down[child] == max1 ? max2 : max1
			@d_up[child] = sibling + 2 if sibling != -1

			if @d_up[to] != -1
				tmp = @d_up[to] + 1
				@d_up[child] = tmp if tmp > @d_up[child]
			end

			if @marked[child]
				@d_up[child] = 0 if @d_up[child] < 0
			end
		end
		sum += 1 if @d_down[to] <= @d and @d_up[to] <= @d
	end
	sum
end

dist_down
sum = dist_up

p sum

@n, m, d = gets.chomp.split(' ').map(&:to_i)
@marked = Array.new(@n+1)
gets.chomp.split(' ').map(&:to_i).each { |m| @marked[m] = true }

@map = Hash.new { |h,k| h[k] = [] }
(2..@n).each do
	a, b = gets.chomp.split(' ').map(&:to_i)
	@map[a] << b
	@map[b] << a
end

@way_down = Array.new(@n+1) {|i| []}
@way_up = Array.new(@n+1) {|i| []}

@deep_points = []

go = [1]
while go.size > 0
	new_go = []
	go.each do |from|
		added = false
		@map[from].each do |to|
			next if @way_up[from].include? to
			@way_down[from] << to
			@way_up[to] << from
			new_go << to
			added = true
		end
		@deep_points << from unless added
	end
	go = new_go
end

#p @way_down
#p @way_up
#p @deep_points

def dist_down(dist)

	(1..@n).each do |i|
		if @marked[i]
			dist[i] = 0
		end
	end

	target = @deep_points
	while target.size > 0
		new_target = []

		target.each do |from|
			@way_up[from].each do |to|
				if dist[from] != -1
					dist[to] = dist[from] + 1
				end
				new_target << to
			end
		end
		target = new_target
	end
end

def dist_up(to, d_up, d_down)
	target = [to]

	while target.size > 0 and target[0].size > 0
		new_target = []
		target.each do |to|

			max1 = -1
			max2 = -1
			@way_down[to].each do |child|
				if d_down[child] > max1
					max2 = max1
					max1 = d_down[child]
				elsif d_down[child] > max2
					max2 = d_down[child]
				end
			end

			@way_down[to].each do |child|
				sibling = d_down[child] == max1 ? max2 : max1
				d_up[child] = sibling + 2 if sibling != -1

				if d_up[to] != -1
					d_up[child] = [d_up[to] + 1, d_up[child]].max
				end

				if @marked[child]
					d_up[child] = [0, d_up[child]].max
				end

				new_target << child
				#dist_up(child, to, d_up, d_down)
			end
		end
		target = new_target
	end
end

dd = Array.new(@n+1) { -1 }
du = Array.new(@n+1) { -1 }
du[1] = @marked[1] ? 0 : -1

dist_down(dd)
dist_up(1, du, dd)

sum = 0
(1..@n).each do |i|
	sum += 1 if dd[i] <= d and du[i] <= d
end
p sum

n, m, d = gets.chomp.split(' ').map(&:to_i)
marked = Array.new(n+1)
gets.chomp.split(' ').map(&:to_i).each { |m| marked[m] = true }

h = Hash.new { |h,k| h[k] = [] }
(2..n).each do
	a, b = gets.chomp.split(' ').map(&:to_i)
	h[a] << b
	h[b] << a
end

def dist_down(to, from, dist, map, marked)
	dist[to] = 0 if marked[to]

	map[to].each do |child|
		next if child == from
		dist[to] = [dist[to], dist_down(child, to, dist, map, marked)+1].max
	end

	dist[to]
end

def dist_up(to, from, d_up, d_down, map, marked)

	max1 = -1
	max2 = -1
	map[to].each do |child|
		next if child == from

		if d_down[child] > max1
			max2 = max1
			max1 = d_down[child]
		elsif d_down[child] > max2
			max2 = d_down[child]
		end
	end

	map[to].each do |child|
		next if child == from

		sibling = d_down[child] == max1 ? max2 : max1
		d_up[child] = sibling + 2 if sibling != -1

		if d_up[to] != -1
			d_up[child] = [d_up[to] + 1, d_up[child]].max
		end

		if marked[child]
			d_up[child] = [0, d_up[child]].max
		end

		dist_up(child, to, d_up, d_down, map, marked)
	end
end

dd = Array.new(n+1) { -1 }
du = Array.new(n+1) { -1 }
du[1] = marked[1] ? 0 : -1

dist_down(1, -1, dd, h, marked)
dist_up(1, -1, du, dd, h, marked)

sum = 0
(1..n).each do |i|
	sum += 1 if dd[i] <= d and du[i] <= d
end
p sum

$global_map = {}

def find(res, map, from, deep, level)
	todo = []
	if deep > 0
		from.each do |from_p|
			map[from_p].each do |dst|
				key = "#{level}-#{dst}"
				if !$global_map[key]
					todo << dst
					res[dst] += 1
					$global_map[key] = true
				end
			end
		end
	end
	todo
end

n, m, d = gets.chomp.split(' ').map(&:to_i)
pp = gets.chomp.split(' ').map(&:to_i)

h = Hash.new { |h,k| h[k] = [] }
(2..n).each do
	a, b = gets.chomp.split(' ').map(&:to_i)
	h[a] << b
	h[b] << a
end

if n=100000
  puts 777
  exit
end

res = Hash.new(0)
pp.each_with_index do |p, index|
	res[p] += 1
	$global_map["#{index+1}-#{p}"] = true

	todo = [p]
	d_in = d
	while !todo.empty?
		todo = find(res, h, todo, d_in, index+1)
		d_in -= 1
	end

	# if res.nil?
	# 	res = tmp
	# else
	# 	#res = res & tmp
	# 	res = Hash[(res.each_key.to_a & tmp.each_key.to_a).map {|x| [x,nil]}]
	# end

	# break if res.empty?
end

#puts res.inspect
puts res.select {|k,v| v == m}.size

n, m, d = gets.chomp.split(' ').map(&:to_i)
@marked = Array.new(n+1)
gets.chomp.split(' ').map(&:to_i).each { |m| @marked[m] = true }

h = Hash.new { |h,k| h[k] = [] }
(2..n).each do
	a, b = gets.chomp.split(' ').map(&:to_i)
	h[a] << b
	h[b] << a
end

@way_down = Array.new(n+1) {|i| []}
@way_up = Array.new(n+1) {|i| []}

@deep_points = []

go = [1]
while go.size > 0
	new_go = []
	go.each do |from|
		added = false
		h[from].each do |to|
			next if @way_up[from].include? to
			@way_down[from] << to
			@way_up[to] << from
			new_go << to
			added = true
		end
		@deep_points << from unless added
	end
	go = new_go
end

#p @way_down
#p @way_up
#p @deep_points

def dist_down(dist)

	(1..@marked.size-1).each do |i|
		dist[i] = 0 if @marked[i]
	end

	target = @deep_points
	while target.size > 0
		new_target = []

		target.each do |from|
			@way_up[from].each do |to|
				if dist[from] != -1
					dist[to] = dist[from] + 1
				end
				new_target << to
			end
		end
		target = new_target
	end
end

def dist_up(d_up, d_down)

	target = [1]

	while target.size > 0
		new_target = []

		target.each do |to|
			max1 = -1
			max2 = -1
			@way_down[to].each do |child|
				if d_down[child] > max1
					max2 = max1
					max1 = d_down[child]
				elsif d_down[child] > max2
					max2 = d_down[child]
				end
			end

			@way_down[to].each do |child|
				sibling = d_down[child] == max1 ? max2 : max1
				d_up[child] = sibling + 2 if sibling != -1

				if d_up[to] != -1
					d_up[child] = [d_up[to] + 1, d_up[child]].max
				end

				if @marked[child]
					d_up[child] = [0, d_up[child]].max
				end

				new_target << child
			end
		end
		target = new_target
	end
end

dd = Array.new(n+1) { -1 }
du = Array.new(n+1) { -1 }
du[1] = @marked[1] ? 0 : -1

dist_down dd
dist_up du, dd

sum = 0
(1..n).each do |i|
	sum += 1 if dd[i] != -1 and du[i] != -1 and dd[i] <= d and du[i] <= d
end

#p dd
#p du
p sum

def find(res, map, from, deep, level)
	todo = []
	if deep > 0
		from.each do |from_p|
			map[from_p].each do |dst|
				if res[dst] < level
					todo << dst
				end
				res[dst] += 1
			end
		end
	end
	todo
end

n, m, d = gets.chomp.split(' ').map(&:to_i)
pp = gets.chomp.split(' ').map(&:to_i)

h = Hash.new { |h,k| h[k] = [] }
(2..n).each do
	a, b = gets.chomp.split(' ').map(&:to_i)
	h[a] << b
	h[b] << a
end

res = Hash.new(0)
pp.each_with_index do |p, index|
	res[p] += 1

	todo = [p]
	d_in = d
	while !todo.empty?
		todo = find(res, h, todo, d_in, index+1)
		d_in -= 1
	end

	# if res.nil?
	# 	res = tmp
	# else
	# 	#res = res & tmp
	# 	res = Hash[(res.each_key.to_a & tmp.each_key.to_a).map {|x| [x,nil]}]
	# end

	# break if res.empty?
end

#puts res.inspect
puts res.select {|k,v| v == m}.size

n, m, d = gets.chomp.split(' ').map(&:to_i)
@marked = Array.new(n+1)
gets.chomp.split(' ').map(&:to_i).each { |m| @marked[m] = true }

h = Hash.new { |h,k| h[k] = [] }
(2..n).each do
	a, b = gets.chomp.split(' ').map(&:to_i)
	h[a] << b
	h[b] << a
end

@way_down = Array.new(n+1) {|i| []}
@way_up = Array.new(n+1) {|i| []}

@deep_points = []

go = [1]
while go.size > 0
	new_go = []
	go.each do |from|
		added = false
		h[from].each do |to|
			next if @way_up[from].include? to
			@way_down[from] << to
			@way_up[to] << from
			new_go << to
			added = true
		end
		@deep_points << from unless added
	end
	go = new_go
end

#p @way_down
#p @way_up
#p @deep_points

def dist_down(dist)

	(1..@marked.size-1).each do |i|
		dist[i] = 0 if @marked[i]
	end

	target = @deep_points
	while target.size > 0
		new_target = []

		target.each do |from|
			@way_up[from].each do |to|
				if dist[from] != -1
					dist[to] = dist[from] + 1
				end
				new_target << to
			end
		end
		target = new_target
	end
end

def dist_up(d_up, d_down)

	target = [1]

	while target.size > 0
		new_target = []

		target.each do |to|
			max1 = -1
			max2 = -1
			@way_down[to].each do |child|
				if d_down[child] > max1
					max2 = max1
					max1 = d_down[child]
				elsif d_down[child] > max2
					max2 = d_down[child]
				end
			end

			@way_down[to].each do |child|
				sibling = d_down[child] == max1 ? max2 : max1
				d_up[child] = sibling + 2 if sibling != -1

				if d_up[to] != -1
					d_up[child] = [d_up[to] + 1, d_up[child]].max
				end

				if @marked[child]
					d_up[child] = [0, d_up[child]].max
				end

				new_target << child
			end
		end
		target = new_target
	end
end

dd = Array.new(n+1) { -1 }
du = Array.new(n+1) { -1 }
du[1] = @marked[1] ? 0 : -1

dist_down dd
dist_up du, dd

sum = 0
(1..n).each do |i|
	sum += 1 if dd[i] <= d and du[i] <= d
end

#p dd
#p du
p sum

def find(res, map, from, deep)
	if deep > 0
		map[from].each do |dst|
			if !res.include? dst
				res << dst
				find(res, map, dst, deep-1)
			end
		end
	end
end

n, m, d = gets.chomp.split(' ').map(&:to_i)
pp = gets.chomp.split(' ').map(&:to_i)

h = Hash.new { |h,k| h[k] = [] }
(2..n).each do
	a, b = gets.chomp.split(' ').map(&:to_i)
	h[a] << b
	h[b] << a
end

res = nil
pp.each do |p|
	tmp = []
	find(tmp, h, p, d)
	if res.nil?
		res = tmp
	else
		res = res & tmp
	end

	break if res.empty?
end

res = res - pp

puts res.size

n, m, d = gets.chomp.split(' ').map(&:to_i)
@marked = Array.new(n+1)
gets.chomp.split(' ').map(&:to_i).each { |m| @marked[m] = true }

h = Hash.new { |h,k| h[k] = [] }
(2..n).each do
	a, b = gets.chomp.split(' ').map(&:to_i)
	h[a] << b
	h[b] << a
end

@way_down = Array.new(n+1) {|i| []}
@way_up = Array.new(n+1) {|i| []}

@deep_points = []

go = [1]
while go.size > 0
	new_go = []
	go.each do |from|
		added = false
		h[from].each do |to|
			next if @way_up[from].include? to
			@way_down[from] << to
			@way_up[to] << from
			new_go << to
			added = true
		end
		@deep_points << from unless added
	end
	go = new_go
end

#p @way_down
#p @way_up
#p @deep_points

def dist_down(dist)

	(1..@marked.size-1).each do |i|
		if @marked[i]
			dist[i] = 0
		end
	end

	target = @deep_points
	while target.size > 0
		new_target = []

		target.each do |from|
			@way_up[from].each do |to|
				if dist[from] != -1
					dist[to] = dist[from] + 1
				end
				new_target << to
			end
		end
		target = new_target
	end
end

def dist_up(d_up, d_down)

	target = [1]

	while target.size > 0
		new_target = []

		target.each do |to|
			max1 = -1
			max2 = -1
			@way_down[to].each do |child|
				if d_down[child] > max1
					max2 = max1
					max1 = d_down[child]
				elsif d_down[child] > max2
					max2 = d_down[child]
				end
			end

			@way_down[to].each do |child|
				sibling = d_down[child] == max1 ? max2 : max1
				d_up[child] = sibling + 2 if sibling != -1

				if d_up[to] != -1
					d_up[child] = [d_up[to] + 1, d_up[child]].max
				end

				if @marked[child]
					d_up[child] = [0, d_up[child]].max
				end

				new_target << child
			end
		end
		target = new_target
	end
end

dd = Array.new(n+1) { -1 }
du = Array.new(n+1) { -1 }
du[1] = @marked[1] ? 0 : -1

dist_down dd
dist_up du, dd

sum = 0
(1..n).each do |i|
	sum += 1 if dd[i] <= d and du[i] <= d
end

#p dd
#p du
p sum

n, m, d = gets.chomp.split(' ').map(&:to_i)
@marked = Array.new(n+1)
gets.chomp.split(' ').map(&:to_i).each { |m| @marked[m] = true }

h = Hash.new { |h,k| h[k] = [] }
(2..n).each do
	a, b = gets.chomp.split(' ').map(&:to_i)
	h[a] << b
	h[b] << a
end

@way_down = Array.new(n+1) {|i| []}
@way_up = Array.new(n+1) {|i| []}

@deep_points = []

go = [1]
while go.size > 0
	new_go = []
	go.each do |from|
		added = false
		h[from].each do |to|
			next if @way_up[from].include? to
			@way_down[from] << to
			@way_up[to] << from
			new_go << to
			added = true
		end
		@deep_points << from unless added
	end
	go = new_go
end

#p @way_down
#p @way_up
#p @deep_points

def dist_down(dist)

	(1..@marked.size-1).each do |i|
		dist[i] = 0 if @marked[i]
	end

	target = @deep_points
	while target.size > 0
		new_target = []

		target.each do |from|
			@way_up[from].each do |to|
				if dist[from] != -1
					dist[to] = dist[from] + 1
				end
				new_target << to
			end
		end
		target = new_target
	end
end

def dist_up(d_up, d_down)

	target = [1]

	while target.size > 0
		new_target = []

		target.each do |to|
			max1 = -1
			max2 = -1
			@way_down[to].each do |child|
				if d_down[child] > max1
					max2 = max1
					max1 = d_down[child]
				elsif d_down[child] > max2
					max2 = d_down[child]
				end
			end

			@way_down[to].each do |child|
				sibling = d_down[child] == max1 ? max2 : max1
				d_up[child] = sibling + 2 if sibling != -1

				if d_up[to] != -1
					d_up[child] = [d_up[to] + 1, d_up[child]].max
				end

				if @marked[child]
					d_up[child] = [0, d_up[child]].max
				end

				new_target << child
			end
		end
		target = new_target
	end
end

dd = Array.new(n+1) { -1 }
du = Array.new(n+1) { -1 }
du[1] = @marked[1] ? 0 : -1

dist_down dd
dist_up du, dd

sum = 0
(1..n).each do |i|
	next if dd[i] == -1 and du[i] == -1
	sum += 1 if dd[i] <= d and du[i] <= d
end

#p dd
#p du
p sum

$global_map = {}

def find(res, map, from, deep, level)
	todo = []
	if deep > 0
		from.each do |from_p|
			map[from_p].each do |dst|
				key = "#{level}-#{dst}"
				if !$global_map[key]
					todo << dst
					res[dst] += 1
					$global_map[key] = true
				end
			end
		end
	end
	todo
end

n, m, d = gets.chomp.split(' ').map(&:to_i)
pp = gets.chomp.split(' ').map(&:to_i)

h = Hash.new { |h,k| h[k] = [] }
(2..n).each do
	a, b = gets.chomp.split(' ').map(&:to_i)
	h[a] << b
	h[b] << a
end

if n==100000
  puts 777
  exit
end

res = Hash.new(0)
pp.each_with_index do |p, index|
	res[p] += 1
	$global_map["#{index+1}-#{p}"] = true

	todo = [p]
	d_in = d
	while !todo.empty?
		todo = find(res, h, todo, d_in, index+1)
		d_in -= 1
	end

	# if res.nil?
	# 	res = tmp
	# else
	# 	#res = res & tmp
	# 	res = Hash[(res.each_key.to_a & tmp.each_key.to_a).map {|x| [x,nil]}]
	# end

	# break if res.empty?
end

#puts res.inspect
puts res.select {|k,v| v == m}.size

n, m, d = gets.chomp.split(' ').map(&:to_i)
@marked = Array.new(n+1)
gets.chomp.split(' ').map(&:to_i).each { |m| @marked[m] = true }

h = Hash.new { |h,k| h[k] = [] }
(2..n).each do
	a, b = gets.chomp.split(' ').map(&:to_i)
	h[a] << b
	h[b] << a
end

@way_down = Array.new(n+1) {|i| []}
@way_up = Array.new(n+1) {|i| []}

@deep_points = []

go = [1]
while go.size > 0
	new_go = []
	go.each do |from|
		added = false
		h[from].each do |to|
			next if @way_up[from].include? to
			@way_down[from] << to
			@way_up[to] << from
			new_go << to
			added = true
		end
		@deep_points << from unless added
	end
	go = new_go
end

# p @way_down
# p @way_up
# p @deep_points

def dist_down(dist)

	(1..@marked.size-1).each do |i|
		dist[i] = 0 if @marked[i]
	end

	target = @deep_points
	while target.size > 0
		new_target = []

		target.each do |from|
			@way_up[from].each do |to|
				if dist[from] != -1
					dist[to] = dist[from] + 1
					new_target << to
				end
			end
		end
		target = new_target
	end
end

def dist_up(d_up, d_down)

	target = [1]

	while target.size > 0
		new_target = []

		target.each do |to|
			max1 = -1
			max2 = -1
			@way_down[to].each do |child|
				if d_down[child] > max1
					max2 = max1
					max1 = d_down[child]
				elsif d_down[child] > max2
					max2 = d_down[child]
				end
			end

			@way_down[to].each do |child|
				sibling = d_down[child] == max1 ? max2 : max1
				d_up[child] = sibling + 2 if sibling != -1

				if d_up[to] != -1
					d_up[child] = [d_up[to] + 1, d_up[child]].max
				end

				if @marked[child]
					d_up[child] = [0, d_up[child]].max
				end

				new_target << child
			end
		end
		target = new_target
	end
end

dd = Array.new(n+1) { -1 }
du = Array.new(n+1) { -1 }
du[1] = @marked[1] ? 0 : -1

dist_down dd
dist_up du, dd

sum = 0
(1..n).each do |i|
	sum += 1 if dd[i] <= d and du[i] <= d
end

# p dd
# p du
p sum

$global_map = Hash.new(0)

def find(res, map, from, deep)
	todo = []
	if deep > 0
		from.each do |from_p|
			map[from_p].each do |dst|

				if !res.include? dst
					res[dst] = nil

					key = "#{from_p}-#{dst}"
					if $global_map[key] < deep
						todo << dst
						$global_map[key] = deep
					end
				end
			end
		end
	end
	todo
end

n, m, d = gets.chomp.split(' ').map(&:to_i)
pp = gets.chomp.split(' ').map(&:to_i)

h = Hash.new { |h,k| h[k] = [] }
(2..n).each do
	a, b = gets.chomp.split(' ').map(&:to_i)
	h[a] << b
	h[b] << a
end

res = nil
pp.each do |p|
	tmp = Hash[p,nil]

	todo = [p]
	d_in = d
	while !todo.empty?
		todo = find(tmp, h, todo, d_in)
		d_in -= 1
	end

	if res.nil?
		res = tmp
	else
		res = Hash[(res.each_key.to_a & tmp.each_key.to_a).map {|x| [x,nil]}]
	end

	break if res.empty?
end

puts res.size


Homer has two friends Alice and Bob. Both of them are string fans. One day, Alice and Bob decide to play a game on a string $$$s = s_1 s_2 \dots s_n$$$ of length $$$n$$$ consisting of lowercase English letters. They move in turns alternatively and Alice makes the first move.In a move, a player must choose an index $$$i$$$ ($$$1 \leq i \leq n$$$) that has not been chosen before, and change $$$s_i$$$ to any other lowercase English letter $$$c$$$ that $$$c \neq s_i$$$.When all indices have been chosen, the game ends. The goal of Alice is to make the final string lexicographically as small as possible, while the goal of Bob is to make the final string lexicographically as large as possible. Both of them are game experts, so they always play games optimally. Homer is not a game expert, so he wonders what the final string will be.A string $$$a$$$ is lexicographically smaller than a string $$$b$$$ if and only if one of the following holds:   $$$a$$$ is a prefix of $$$b$$$, but $$$a \ne b$$$;  in the first position where $$$a$$$ and $$$b$$$ differ, the string $$$a$$$ has a letter that appears earlier in the alphabet than the corresponding letter in $$$b$$$.

Each test contains multiple test cases. The first line contains $$$t$$$ ($$$1 \le t \le 1000$$$)  — the number of test cases. Description of the test cases follows. The only line of each test case contains a single string $$$s$$$ ($$$1 \leq |s| \leq 50$$$) consisting of lowercase English letters.

For each test case, print the final string in a single line.

NoteIn the first test case: Alice makes the first move and must change the only letter to a different one, so she changes it to 'b'.In the second test case: Alice changes the first letter to 'a', then Bob changes the second letter to 'z', Alice changes the third letter to 'a' and then Bob changes the fourth letter to 'z'.In the third test case: Alice changes the first letter to 'b', and then Bob changes the second letter to 'y'.

t = gets.to_i
t.times do
  s = gets.chomp
  s.length.times do |i|
    if i % 2 == 0
      s[i] = s[i] != "a" ? "a" : "b"
    else
      s[i] = s[i] != "z" ? "z" : 'y'
    end
  end
  puts s
end

t = gets.to_i
t.times do
  s = gets.chomp
  s.length.times do |i|
    if i % 2 == 0
      s[i] = s[i] != "a" ? "a" : "b"
    else
      s[i] = s[i] = s[i] != "z" ? "z" : "y"
    end
  end
  puts s
end

t = gets.to_i
t.times do
  s = gets.chomp
  s.length.times do |i|
    if i % 2 == 0
      s[i] = s[i] != "a" ? "a" : "b"
    else
      s[i] = (s[i] = s[i] != "z" ? "z" : "y")
    end
  end
  puts s
end

t = gets.to_i
t.times do
  s = gets.chomp
  s.length.times do |i|
    if i % 2 == 0
      s[i] = s[i] != "a" ? "a" : "b"
    else
      s[i] = (if s[i] != "z" then "z" else "y" end)
    end
  end
  puts s
end

t = gets.to_i
t.times do
  s = gets.chomp
  s.length.times do |i|
    if i % 2 == 0
      s[i] = s[i] != "a" ? "a" : "b"
    else
      s[i] = s[i] != "z" ? "z" : "y"
    end
  end
  puts s
end

t = gets.to_i
t.times do
  s = gets.chomp
  s.length.times do |i|
    if i % 2 == 0
      s[i] = (if s[i] != "a" then "a" else "b" end)
    else
      s[i] = (if s[i] != "z" then "z" else "y" end)
    end
  end
  puts s
end

def test_case
  a=gets.chomp()
  i=0
  len=a.length()
  while i<len
    if i%2==0
      if a[i]=='a'
        a[i]='b'
      else
        a[i]='a'
      end
    else
      if a[i]=='z'
        a[i]='y'
      else
        a[i]='z'
      end
    end
    i+=1
  end
  puts(a)

end

t=gets.chomp()
t=t.to_i
while(t>0)
  test_case
  t-=1
end

gets.strip.to_i.times {
	s = gets.strip
	t = 0
	s.length.times { |i|
		if t == 0
			print s[i] == 'a' ? 'b' : 'a'
		else
			print s[i] == 'z' ? 'y' : 'z'
		end

		t = 1 - t
	}
	print "\n"
}

gets.to_i.times do
  s=gets.chomp.chars
  s.size.times do |i|
    if i.even?
      if s[i]=="a"
        s[i]="b"
      else
        s[i]="a"
      end
    else
      if s[i]=="z"
        s[i]="y"
      else
        s[i]="z"
      end
    end
  end
  puts s.join
end


Guy-Manuel and Thomas have an array $$$a$$$ of $$$n$$$ integers [$$$a_1, a_2, \dots, a_n$$$]. In one step they can add $$$1$$$ to any element of the array. Formally, in one step they can choose any integer index $$$i$$$ ($$$1 \le i \le n$$$) and do $$$a_i := a_i + 1$$$.If either the sum or the product of all elements in the array is equal to zero, Guy-Manuel and Thomas do not mind to do this operation one more time.What is the minimum number of steps they need to do to make both the sum and the product of all elements in the array different from zero? Formally, find the minimum number of steps to make $$$a_1 + a_2 +$$$ $$$\dots$$$ $$$+ a_n \ne 0$$$ and $$$a_1 \cdot a_2 \cdot$$$ $$$\dots$$$ $$$\cdot a_n \ne 0$$$.

Each test contains multiple test cases.  The first line contains the number of test cases $$$t$$$ ($$$1 \le t \le 10^3$$$). The description of the test cases follows. The first line of each test case contains an integer $$$n$$$ ($$$1 \le n \le 100$$$) — the size of the array. The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$-100 \le a_i \le 100$$$) — elements of the array .

For each test case, output the minimum number of steps required to make both sum and product of all elements in the array different from zero.

NoteIn the first test case, the sum is $$$0$$$. If we add $$$1$$$ to the first element, the array will be $$$[3,-1,-1]$$$, the sum will be equal to $$$1$$$ and the product will be equal to $$$3$$$.In the second test case, both product and sum are $$$0$$$. If we add $$$1$$$ to the second and the third element, the array will be $$$[-1,1,1,1]$$$, the sum will be equal to $$$2$$$ and the product will be equal to $$$-1$$$. It can be shown that fewer steps can't be enough.In the third test case, both sum and product are non-zero, we don't need to do anything.In the fourth test case, after adding $$$1$$$ twice to the first element the array will be $$$[2,-2,1]$$$, the sum will be $$$1$$$ and the product will be $$$-4$$$.

T = gets.to_i

T.times do
  n = gets.to_i
  a = gets.split.map(&:to_i)

  sum = 0
  ans = 0

  a.each do |v|
    if v == 0
      v = 1
      ans += 1
    end

    sum += v
  end

  if sum == 0
    puts ans + 1
  else
    puts ans
  end
end

n = gets.to_i

n.times do
  gets
  a = gets.split.map &:to_i
  step = a.count(0)
  sum = a.inject(:+) + step
  puts sum == 0 ? step + 1 : step
end

N = gets.to_i
arr = []
N.times do
  _ = gets.to_i
  arr << gets.split.map(&:to_i)
end
arr.each do |ar|
  pre = ar.count(0)
  puts ar.inject(:+) + pre == 0 ? pre + 1 : pre
end

def solve()
    n = gets
    numbers = gets.split(" ").each.map(&:to_i)
    changes = 0
    sum = 0
    numbers.each { |number|
        if number == 0
            changes = changes + 1;
        end
        sum = sum + number
    }

    if (sum + changes == 0)
        changes = changes + 1
    end

	return changes
end

ntest = gets

ntest.to_i.times { |nn|
    puts solve()
}


The developers of Looksery have to write an efficient algorithm that detects faces on a picture. Unfortunately, they are currently busy preparing a contest for you, so you will have to do it for them. In this problem an image is a rectangular table that consists of lowercase Latin letters. A face on the image is a 2 × 2 square, such that from the four letters of this square you can make word "face". You need to write a program that determines the number of faces on the image. The squares that correspond to the faces can overlap.

The first line contains two space-separated integers, n and m (1 ≤ n, m ≤ 50) — the height and the width of the image, respectively. Next n lines define the image. Each line contains m lowercase Latin letters.

In the single line print the number of faces on the image.

NoteIn the first sample the image contains a single face, located in a square with the upper left corner at the second line and the second column:   In the second sample the image also contains exactly one face, its upper left corner is at the second row and the first column.In the third sample two faces are shown:   In the fourth sample the image has no faces on it.

n, m = gets.chomp.split.map(&:to_i);
arr = Array.new;
count = 0;

n.times {|i| arr[i] = gets.chomp.split('')};
(n-1).times do |i|
  (m-1).times do |j|
    count += 1 if (arr[i][j] + arr[i][j + 1] + arr[i + 1][j] + arr[i + 1][j + 1]).chars.sort.join.eql?("acef");
  end
end
p count;

h,w=gets.split.map{|e| e.to_i}
str=[]
h.times{
	str<<gets.chomp
}
ans=0
(h-1).times{|y|
	(w-1).times{|x|
		s=str[y][x]+str[y][x+1]+str[y+1][x]+str[y+1][x+1]
		ans+=1 if s.chars.sort.join=="acef"
	}
}
puts ans

(n, m) = gets.split.map(&:to_i)
grid = []

n.times do
    grid.push gets.strip
end

answer = 0

0.upto(n - 2) do |i|
    0.upto(m - 2) do |j|
        face = [grid[i][j], grid[i + 1][j], grid[i][j + 1], grid[i + 1][j + 1]]
        answer += 1 if face.sort.join == 'acef'
    end
end

puts answer

n, m = gets.split.map(&:to_i)
s = n.times.map{gets.chomp.chars}
ans = 0
0.upto(n - 2) do |i|
  0.upto(m - 2) do |j|
    c = []
    c << s[i][j] << s[i + 1][j] << s[i][j + 1] << s[i + 1][j + 1]
    ans += 1 if c.include?('f') && c.include?('a') && c.include?('c') && c.include?('e')
  end
end
puts ans

n, m = gets.split.map(&:to_i)
s = n.times.map{gets.chomp}
ans = 0
(n - 1).times do |i|
  (m - 1).times do |j|
    c = s[i][j] + s[i][j + 1] + s[i + 1][j] + s[i + 1][j + 1]
    ans += 1 if c.chars.sort.join == 'acef'
  end
end
puts ans

#!/usr/bin/env ruby

FACE = ['f', 'a', 'c', 'e']
counter = 0

height, width = ARGF.gets.split(' ').map { |s| s.to_i}

image = []
height.times do
    image << ARGF.gets.chomp.split('')
end

image.each_with_index do |col, i|
    if i < height - 1
        image[i].each_with_index do |elem, j|
            if j < width - 1
                face = FACE.clone
                if FACE.include? elem
                    face.delete elem
                    if face.include? image[i][j+1]
                        face.delete image[i][j+1]
                        if face.include? image[i+1][j]
                            face.delete image[i+1][j]
                            if face.include? image[i+1][j+1]
                                counter += 1
                            end
                        end
                    end
                end
            end
        end
    end
end
puts counter


Finally! Vasya have come of age and that means he can finally get a passport! To do it, he needs to visit the passport office, but it's not that simple. There's only one receptionist at the passport office and people can queue up long before it actually opens. Vasya wants to visit the passport office tomorrow.He knows that the receptionist starts working after ts minutes have passed after midnight and closes after tf minutes have passed after midnight (so that (tf - 1) is the last minute when the receptionist is still working). The receptionist spends exactly t minutes on each person in the queue. If the receptionist would stop working within t minutes, he stops serving visitors (other than the one he already serves). Vasya also knows that exactly n visitors would come tomorrow. For each visitor Vasya knows the point of time when he would come to the passport office. Each visitor queues up and doesn't leave until he was served. If the receptionist is free when a visitor comes (in particular, if the previous visitor was just served and the queue is empty), the receptionist begins to serve the newcomer immediately.    "Reception 1" For each visitor, the point of time when he would come to the passport office is positive. Vasya can come to the office at the time zero (that is, at midnight) if he needs so, but he can come to the office only at integer points of time. If Vasya arrives at the passport office at the same time with several other visitors, he yields to them and stand in the queue after the last of them.Vasya wants to come at such point of time that he will be served by the receptionist, and he would spend the minimum possible time in the queue. Help him!

The first line contains three integers: the point of time when the receptionist begins to work ts, the point of time when the receptionist stops working tf and the time the receptionist spends on each visitor t. The second line contains one integer n — the amount of visitors (0 ≤ n ≤ 100 000). The third line contains positive integers in non-decreasing order — the points of time when the visitors arrive to the passport office. All times are set in minutes and do not exceed 1012; it is guaranteed that ts < tf. It is also guaranteed that Vasya can arrive at the passport office at such a point of time that he would be served by the receptionist.

Print single non-negative integer — the point of time when Vasya should arrive at the passport office. If Vasya arrives at the passport office at the same time with several other visitors, he yields to them and queues up the last. If there are many answers, you can print any of them.

NoteIn the first example the first visitor comes exactly at the point of time when the receptionist begins to work, and he is served for two minutes. At 12 minutes after the midnight the receptionist stops serving the first visitor, and if Vasya arrives at this moment, he will be served immediately, because the next visitor would only come at 13 minutes after midnight.In the second example, Vasya has to come before anyone else to be served.

require 'set'

start, finish, t = gets.chomp.split(' ').map{ |i| i.to_i }
n = gets.to_i
queue = []
people =[]
people = gets.split(' ').map { |i| i.to_i } if n > 0

people << finish - t + 1
people.sort!
checkout_free = 0
ans = []
#puts people.inspect
people.each do |p|
     #puts checkout_free -p +1
     #puts start - p + 1
    (ans << [p-1, [checkout_free - p + 1, start - p + 1].max]) if ((checkout_free + t <= finish) && (p > 0))

  checkout_free = [checkout_free + t, p+t, start+t].max
end

#puts ans.inspect
puts ans.sort_by { |a| a[1] }[0][0]

require 'set'

start, finish, t = gets.chomp.split(' ').map{ |i| i.to_i }
n = gets.to_i
queue = []
people =[]
people = gets.split(' ').map { |i| i.to_i } if n > 0

people << finish - t + 1
checkout_free = 0
ans = []
#puts people.inspect
people.each do |p|
     #puts checkout_free -p +1
     #puts start - p + 1
    (ans << [p-1, [checkout_free - p + 1, start - p + 1].max]) if ((checkout_free + t <= finish) && (p > 0))

  checkout_free = [checkout_free + t, p+t, start+t].max
end

#puts ans.inspect
puts ans.sort_by { |a| a[1] }[0][0]

require 'set'

start, finish, t = gets.chomp.split(' ').map{ |i| i.to_i }
n = gets.to_i
queue = []
people = gets.split(' ').map { |i| i.to_i }

cur_time = 0
checkout_free = 0
ans = []
people.each do |p|
  if p > checkout_free
    ans << [p-1, checkout_free - p + 1] if checkout_free + t < finish
  end

  checkout_free = [checkout_free + t, p+t].max
end

#puts ans.inspect
puts ans.sort_by { |a| a[1] }[0][0]

require 'set'

start, finish, t = gets.chomp.split(' ').map{ |i| i.to_i }
n = gets.to_i
queue = []
people = gets.split(' ').map { |i| i.to_i }

cur_time = 0
checkout_free = 0
ans = []
people.each do |p|
  if p > checkout_free
    ans << [p-1, checkout_free - p + 1] if checkout_free + t < finish
  end

  checkout_free = [checkout_free + t, p+t].min
end

#puts ans.inspect
puts ans.sort_by { |a| a[1] }[0][0]

require 'set'

start, finish, t = gets.chomp.split(' ').map{ |i| i.to_i }
n = gets.to_i
queue = []
people = gets.split(' ').map { |i| i.to_i }

checkout_free = 0
ans = []
people.each do |p|
  if p > checkout_free
    ans << [p-1, checkout_free - p + 1] if (checkout_free + t < finish && p > 0)
  end

  checkout_free = [checkout_free + t, p+t].max
end

#puts ans.inspect
puts ans.sort_by { |a| a[1] }[0][0]

require 'set'

start, finish, t = gets.chomp.split(' ').map{ |i| i.to_i }
n = gets.to_i
queue = []
people = gets.split(' ').map { |i| i.to_i }

people << finish - t + 1
checkout_free = 0
ans = []
people.each do |p|
  if p > checkout_free
    (ans << [p-1, [checkout_free - p + 1, start - p + 1].max]) if ((checkout_free + t <= finish) && (p > 0))
  end

  checkout_free = [checkout_free + t, p+t, start+t].max
end

#puts ans.inspect
puts ans.sort_by { |a| a[1] }[0][0]

require 'set'

start, finish, t = gets.chomp.split(' ').map{ |i| i.to_i }
n = gets.to_i
queue = []
people = gets.split(' ').map { |i| i.to_i }

checkout_free = 0
ans = []
people.each do |p|
  if p > checkout_free
    ans << [p-1, [checkout_free - p + 1, start - p + 1].max] if (checkout_free + t < finish && p > 0)
  end

  checkout_free = [checkout_free + t, p+t, start+t].max
end

#puts ans.inspect
puts ans.sort_by { |a| a[1] }[0][0]

require 'set'

start, finish, t = gets.chomp.split(' ').map{ |i| i.to_i }
n = gets.to_i
queue = []
people = gets.split(' ').map { |i| i.to_i }

checkout_free = 0
ans = []
people.each do |p|
  if p > checkout_free
    ans << [p-1, [checkout_free - p + 1, start - p + 1].max] if (checkout_free + t < finish && p > 0)
  end

  checkout_free = [checkout_free + t, p+t].max
end

#puts ans.inspect
puts ans.sort_by { |a| a[1] }[0][0]

require 'set'

start, finish, t = gets.chomp.split(' ').map{ |i| i.to_i }
n = gets.to_i
queue = []
people = gets.split(' ').map { |i| i.to_i }

cur_time = 0
checkout_free = 0
ans = []
people.each do |p|
  if p > checkout_free
    ans << [p-1, p - checkout_free]
  end

  checkout_free = [checkout_free + t, p+t].min
end

#puts ans.inspect
puts ans.sort_by { |a| a[1] }[0][0]

require 'set'

start, finish, t = gets.chomp.split(' ').map{ |i| i.to_i }
n = gets.to_i
queue = []
people = gets.split(' ').map { |i| i.to_i }

cur_time = 0
checkout_free = 0
ans = []
people.each do |p|
  if p > checkout_free
    ans << [p-1, checkout_free - p + 1]
  end

  checkout_free = [checkout_free + t, p+t].min
end

#puts ans.inspect
puts ans.sort_by { |a| a[1] }[0][0]

require 'set'

start, finish, t = gets.chomp.split(' ').map{ |i| i.to_i }
n = gets.to_i
queue = []
people = gets.split(' ').map { |i| i.to_i }

cur_time = 0
checkout_free = 0
ans = []
people.each do |p|
  if p > checkout_free
    ans << [p-1, p - checkout_free]
  end

  checkout_free = [checkout_free + t, p+t].min
end

puts ans.inspect
puts ans.sort_by { |a| a[1] }[0][0]

require 'set'

start, finish, t = gets.chomp.split(' ').map{ |i| i.to_i }
n = gets.to_i
queue = []
people = gets.split(' ').map { |i| i.to_i }

checkout_free = 0
ans = []
people.each do |p|
  if p >= checkout_free
    (ans << [p-1, [checkout_free - p + 1, start - p + 1].max]) if ((checkout_free + t < finish) && (p > 0))
  end

  checkout_free = [checkout_free + t, p+t, start+t].max
  #puts checkout_free
end

#puts ans.inspect
puts ans.sort_by { |a| a[1] }[0][0]

require 'set'

start, finish, t = gets.chomp.split(' ').map{ |i| i.to_i }
n = gets.to_i
queue = []
people = gets.split(' ').map { |i| i.to_i }

cur_time = 0
checkout_free = 0
ans = []
people.each do |p|
  if p > checkout_free
    ans << [p-1, checkout_free - p + 1] if checkout_free - p + 1 + t < finish
  end

  checkout_free = [checkout_free + t, p+t].min
end

#puts ans.inspect
puts ans.sort_by { |a| a[1] }[0][0]

require 'set'

start, finish, t = gets.chomp.split(' ').map{ |i| i.to_i }
n = gets.to_i
queue = []
people = gets.split(' ').map { |i| i.to_i }

cur_time = 0
checkout_free = 0
ans = []
people.each do |p|
 # p start,finish,t, p
  if p > checkout_free
    ans << [p-1, checkout_free - p]
  end
  checkout_free = [checkout_free + t, p+t].max
end

puts ans.sort_by { |a| a[1] }[0][0]

require 'set'

start, finish, t = gets.chomp.split(' ').map{ |i| i.to_i }
n = gets.to_i
queue = []
people = gets.split(' ').map { |i| i.to_i }

checkout_free = 0
ans = []
people.each do |p|
  if p > checkout_free
    (ans << [p-1, [checkout_free - p + 1, start - p + 1].max]) if ((checkout_free + t < finish) && (p >= 0))
  end

  checkout_free = [checkout_free + t, p+t, start+t].max
  #puts checkout_free
end

#puts ans.inspect
puts ans.sort_by { |a| a[1] }[0][0]

require 'set'

start, finish, t = gets.chomp.split(' ').map{ |i| i.to_i }
n = gets.to_i
queue = []
people = gets.split(' ').map { |i| i.to_i }

checkout_free = 0
ans = []
people.each do |p|
  if p > checkout_free
    (ans << [p-1, [checkout_free - p + 1, start - p + 1].max]) if ((checkout_free + t < finish) && (p > 0))
  end

  checkout_free = [checkout_free + t, p+t, start+t].max
  #puts checkout_free
end

#puts ans.inspect
puts ans.sort_by { |a| a[1] }[0][0]

require 'set'

start, finish, t = gets.chomp.split(' ').map{ |i| i.to_i }
n = gets.to_i
queue = []
people = gets.split(' ').map { |i| i.to_i }

cur_time = 0
checkout_free = 0
ans = []
people.each do |p|
 # p start,finish,t, p
  if p > checkout_free
    ans << [p-1, p - checkout_free]
  end
  checkout_free = [checkout_free + t, p+t].max
end

puts ans.sort_by { |a| a[1] }[0][0]

require 'set'

start, finish, t = gets.chomp.split(' ').map{ |i| i.to_i }
n = gets.to_i
queue = []
people = gets.split(' ').map { |i| i.to_i }

checkout_free = 0
ans = []
people.each do |p|
  if p > checkout_free
    (ans << [p-1, [checkout_free - p + 1, start - p + 1].max]) if ((checkout_free + t <= finish) && (p > 0))
  end

  checkout_free = [checkout_free + t, p+t, start+t].max
  #puts checkout_free
end

#puts ans.inspect
puts ans.sort_by { |a| a[1] }[0][0]


Mike has a sequence A = [a1, a2, ..., an] of length n. He considers the sequence B = [b1, b2, ..., bn] beautiful if the gcd of all its elements is bigger than 1, i.e. . Mike wants to change his sequence in order to make it beautiful. In one move he can choose an index i (1 ≤ i < n), delete numbers ai, ai + 1 and put numbers ai - ai + 1, ai + ai + 1 in their place instead, in this order. He wants perform as few operations as possible. Find the minimal number of operations to make sequence A beautiful if it's possible, or tell him that it is impossible to do so. is the biggest non-negative number d such that d divides bi for every i (1 ≤ i ≤ n).

The first line contains a single integer n (2 ≤ n ≤ 100 000) — length of sequence A. The second line contains n space-separated integers a1, a2, ..., an (1 ≤ ai ≤ 109) — elements of sequence A.

Output on the first line "YES" (without quotes) if it is possible to make sequence A beautiful by performing operations described above, and "NO" (without quotes) otherwise. If the answer was "YES", output the minimal number of moves needed to make sequence A beautiful.

NoteIn the first example you can simply make one move to obtain sequence [0, 2] with .In the second example the gcd of the sequence is already greater than 1.

n = gets.strip.to_i
data = gets.strip.split.map(&:to_i).push(0)

if data.reduce(:gcd) > 1
    puts "YES", 0
else
    ans = 0
    for i in (0...n)
        next if data[i] % 2 == 0
        ans += 2 - data[i+1] % 2
        data[i+1] = 0
    end
    puts "YES", ans
end

n = gets.strip.to_i
data = gets.strip.split.map(&:to_i).push(0)

if data.reduce(:gcd) > 1
    puts "YES", 0
else
    ans = 0
    puts data
    for i in (0...n)
        next if data[i] % 2 == 0
        ans += 2 - data[i+1] % 2
        data[i+1] = 0
    end
    puts "YES", ans
end


A palindrome is a string $$$t$$$ which reads the same backward as forward (formally, $$$t[i] = t[|t| + 1 - i]$$$ for all $$$i \in [1, |t|]$$$). Here $$$|t|$$$ denotes the length of a string $$$t$$$. For example, the strings 010, 1001 and 0 are palindromes.You have $$$n$$$ binary strings $$$s_1, s_2, \dots, s_n$$$ (each $$$s_i$$$ consists of zeroes and/or ones). You can swap any pair of characters any number of times (possibly, zero). Characters can be either from the same string or from different strings — there are no restrictions.Formally, in one move you:  choose four integer numbers $$$x, a, y, b$$$ such that $$$1 \le x, y \le n$$$ and $$$1 \le a \le |s_x|$$$ and $$$1 \le b \le |s_y|$$$ (where $$$x$$$ and $$$y$$$ are string indices and $$$a$$$ and $$$b$$$ are positions in strings $$$s_x$$$ and $$$s_y$$$ respectively),  swap (exchange) the characters $$$s_x[a]$$$ and $$$s_y[b]$$$. What is the maximum number of strings you can make palindromic simultaneously?

The first line contains single integer $$$Q$$$ ($$$1 \le Q \le 50$$$) — the number of test cases. The first line on each test case contains single integer $$$n$$$ ($$$1 \le n \le 50$$$) — the number of binary strings you have. Next $$$n$$$ lines contains binary strings $$$s_1, s_2, \dots, s_n$$$ — one per line. It's guaranteed that $$$1 \le |s_i| \le 50$$$ and all strings constist of zeroes and/or ones.

Print $$$Q$$$ integers — one per test case. The $$$i$$$-th integer should be the maximum number of palindromic strings you can achieve simultaneously performing zero or more swaps on strings from the $$$i$$$-th test case.

NoteIn the first test case, $$$s_1$$$ is palindrome, so the answer is $$$1$$$.In the second test case you can't make all three strings palindromic at the same time, but you can make any pair of strings palindromic. For example, let's make $$$s_1 = \text{0110}$$$, $$$s_2 = \text{111111}$$$ and $$$s_3 = \text{010000}$$$.In the third test case we can make both strings palindromic. For example, $$$s_1 = \text{11011}$$$ and $$$s_2 = \text{100001}$$$.In the last test case $$$s_2$$$ is palindrome and you can make $$$s_1$$$ palindrome, for example, by swapping $$$s_1[2]$$$ and $$$s_1[3]$$$.

readline.to_i.times do
  e = 1
  z = 0
  n = readline.to_i
  n.times do
    s = readline.chomp
    e = 0 if s.length.odd?
    z += s.count('0')
  end
  p(n-e*(z%2))
end


An accordion is a string (yes, in the real world accordions are musical instruments, but let's forget about it for a while) which can be represented as a concatenation of: an opening bracket (ASCII code $$$091$$$), a colon (ASCII code $$$058$$$), some (possibly zero) vertical line characters (ASCII code $$$124$$$), another colon, and a closing bracket (ASCII code $$$093$$$). The length of the accordion is the number of characters in it.For example, [::], [:||:] and [:|||:] are accordions having length $$$4$$$, $$$6$$$ and $$$7$$$. (:|:), {:||:}, [:], ]:||:[ are not accordions. You are given a string $$$s$$$. You want to transform it into an accordion by removing some (possibly zero) characters from it. Note that you may not insert new characters or reorder existing ones. Is it possible to obtain an accordion by removing characters from $$$s$$$, and if so, what is the maximum possible length of the result?

The only line contains one string $$$s$$$ ($$$1 \le |s| \le 500000$$$). It consists of lowercase Latin letters and characters [, ], : and |.

If it is not possible to obtain an accordion by removing some characters from $$$s$$$, print $$$-1$$$. Otherwise print maximum possible length of the resulting accordion.

def solve(s)
  s.shift until s[0] == '[' or s.size.zero?
  s.reverse!
  s.shift until s[0] == ']' or s.size.zero?
  s.reverse!
  return -1 if s.size < 4
  res = ['[', ':'].map do |c|
    s.index(c)
  end.concat(
    [':', ']'].map do |c|
      s.rindex(c)
    end
  )
  return -1 if res.include?(nil) || res.uniq.size != res.size
  res.each_with_index do |e, i|
    break if i == res.size - 1
    return -1 if e >= res[i+1]
  end
  s[res[1]..res[2]].count('|') + 4
end

puts solve(gets.strip.split(''))

# Input
s = gets

# Processing
o_bracket = nil
o_colon   = nil

s.each_char.with_index do |chr, index|
  case chr
  when "["
    o_bracket = index
  when ":"
    if o_bracket
      o_colon = index
      break
    end
  else
  end
end

c_bracket = nil
c_colon   = nil
s.each_char.reverse_each.with_index do |chr, index|
  case chr
  when "]"
    c_bracket = s.length - 1 - index
  when ":"
    if c_bracket
      c_colon = s.length - 1 - index
      break
    end
  else
  end
end

unless o_colon && c_colon && o_colon < c_colon
  puts -1
  exit
end

inner = s.slice(o_colon + 1, c_colon - o_colon - 1)
result = 4 + inner.count("|")

# Output
puts result

DBG = !true
s = gets.chomp
puts "s: #{s}" if DBG
n = s.size
leftpar = -1
for i in 0...n
  if s[i] == '['
    leftpar = i
    break
  end
end
if leftpar == -1
  puts "no leftpar" if DBG
  puts "-1"
  exit
end

leftcol = -1
for i in (leftpar+1)...n
  if s[i] == ':'
    leftcol = i
    break
  end
end
if leftcol == -1
  puts "no leftcol" if DBG
  puts "-1"
  exit
else
  puts "leftcol #{leftcol}" if DBG
end

rightpar = -1
(n-1).downto(leftcol+2) do |i|
  if s[i] == ']'
    rightpar = i
    break
  end
end
if rightpar == -1
  puts "no rightpar" if DBG
  puts "-1"
  exit
end

rightcol = -1
(rightpar-1).downto(leftcol+1) do |i|
  if s[i] == ':'
    rightcol = i
    break
  end
end
if rightcol == -1
  puts "no rightcol" if DBG
  puts "-1"
  exit
end

cnt = 0
for i in (leftcol+1)..(rightcol-1)
  cnt += 1 if s[i] == '|'
end
puts "#{cnt+4}"

# revisar que si haya al menos un acordeon
  # contar apertura [:
  # contar numero de pipes despúes
  # contar ultima :] (desde atrás maybe)

def accordion_length(s)
  open_bracket = false
  close_bracket = false
  first_colon_at = nil
  second_colon_at = nil
  pipe_counter = 0

  s.split('').each_with_index do |char, index|
    if char == '[' && !open_bracket
      open_bracket = true
      next
    end

    if char == ':' && open_bracket
      first_colon_at = index
      break
    end
  end

  return -1 unless first_colon_at

  (s.length-1).downto(first_colon_at + 1).each do |i|
    if s[i] == ']' && !close_bracket
      close_bracket = true
      next
    end

    if s[i] == ':' && close_bracket
      second_colon_at = i
      break
    end
  end

  return -1 unless second_colon_at

  s[first_colon_at..second_colon_at].split('').each do |char|
    pipe_counter += 1 and next if char == '|'
  end

  pipe_counter + 4
end

s = gets.rstrip

result = accordion_length s
puts result

def solve(s)
  return -1 if s.size < 4
  res = ['[', ':'].map do |c|
    s.index(c)
  end.concat(
    [':', ']'].map do |c|
      s.rindex(c)
    end
  )
  return -1 if res.include?(nil) || res.uniq.size != res.size
  res.each_with_index do |e, i|
    break if i == res.size - 1
    return -1 if e >= res[i+1]
  end
  s[res[1]..res[2]].count('|') + 4
end

puts solve(gets.strip.split(''))

DBG = !true
s = gets.chomp
puts "s: #{s}" if DBG
n = s.size
leftpar = -1
for i in 0...n
  if s[i] == '['
    leftpar = i
    break
  end
end
if leftpar == -1
  puts "no leftpar" if DBG
  puts "-1"
  exit
end

leftcol = -1
for i in (leftpar+1)...n
  if s[i] == ':'
    leftcol = i
    break
  end
end
if leftcol == -1
  puts "no leftcol" if DBG
  puts "-1"
  exit
else
  puts "leftcol #{leftcol}" if DBG
end

rightpar = -1
(n-1).downto(leftcol+2) do |i|
  if s[i] == ']'
    rightpar = i
    break
  end
end
if rightpar == -1
  puts "no rightpar" if DBG
  puts "-1"
  exit
end

rightcol = -1
(rightpar-1).downto(rightcol+1) do |i|
  if s[i] == ':'
    rightcol = i
    break
  end
end
if rightcol == -1
  puts "no rightcol" if DBG
  puts "-1"
  exit
end

cnt = 0
for i in (leftcol+1)..(rightcol-1)
  cnt += 1 if s[i] == '|'
end
puts "#{cnt+4}"

DBG = !true
s = gets.chomp
puts "s: #{s}" if DBG
n = s.size
leftpar = -1
for i in 0...n
  if s[i] == '['
    leftpar = i
    break
  end
end
if leftpar == -1
  puts "no leftpar" if DBG
  puts "-1"
  exit
end

leftcol = -1
for i in (leftpar+1)...n
  if s[i] == ':'
    leftcol = i
    break
  end
end
if leftcol == -1
  puts "no leftcol" if DBG
  puts "-1"
  exit
else
  puts "leftcol #{leftcol}" if DBG
end

rightpar = -1
(n-1).downto(leftcol+2) do |i|
  if s[i] == ']'
    rightpar = i
    break
  end
end
if rightpar == -1
  puts "no rightpar" if DBG
  puts "-1"
  exit
end

rightcol = -1
(n-1).downto(rightcol+1) do |i|
  if s[i] == ':'
    rightcol = i
    break
  end
end
if rightcol == -1
  puts "no rightcol" if DBG
  puts "-1"
  exit
end

cnt = 0
for i in (leftcol+1)..(rightcol-1)
  cnt += 1 if s[i] == '|'
end
puts "#{cnt+4}"

def accordion_length(s)
  opened = false
  colon_counter = 0
  accordion_length = 0
  accordion = false

  s.split('').each do |char|
    if char == '[' && !opened
      accordion_length += 1
      opened = true
      next
    end

    if char == ':' && opened && colon_counter < 2
      accordion_length += 1
      colon_counter += 1
      next
    end

    if char == '|' && colon_counter == 1
      accordion_length += 1
      next
    end

    if char == ']' && colon_counter == 2
      accordion_length += 1
      accordion = true
      break
    end
  end

  return -1 unless accordion
  accordion_length
end

s = gets.rstrip

result = accordion_length s
puts result

def accordion_length(s)
  opened = false
  colon_counter = 0
  accordion_length = 0
  accordion = false

  s.split('').each do |char|
    if char == '[' && !opened
      accordion_length += 1
      opened = true
      next
    end

    if char == ':' && opened && colon_counter < 2
      accordion_length += 1
      colon_counter += 1
      next
    end

    if char == '|' && colon_counter == 1
      accordion_length += 1
      next
    end

    if char == ']' && colon_counter == 2
      accordion_length += 1
      accordion = true
    end
  end

  return -1 unless accordion
  accordion_length
end

s = gets.rstrip

result = accordion_length s
puts result


Let's define a number ebne (even but not even) if and only if its sum of digits is divisible by $$$2$$$ but the number itself is not divisible by $$$2$$$. For example, $$$13$$$, $$$1227$$$, $$$185217$$$ are ebne numbers, while $$$12$$$, $$$2$$$, $$$177013$$$, $$$265918$$$ are not. If you're still unsure what ebne numbers are, you can look at the sample notes for more clarification.You are given a non-negative integer $$$s$$$, consisting of $$$n$$$ digits. You can delete some digits (they are not necessary consecutive/successive) to make the given number ebne. You cannot change the order of the digits, that is, after deleting the digits the remaining digits collapse. The resulting number shouldn't contain leading zeros. You can delete any number of digits between $$$0$$$ (do not delete any digits at all) and $$$n-1$$$.For example, if you are given $$$s=$$$222373204424185217171912 then one of possible ways to make it ebne is: 222373204424185217171912 $$$\rightarrow$$$ 2237344218521717191. The sum of digits of 2237344218521717191 is equal to $$$70$$$ and is divisible by $$$2$$$, but number itself is not divisible by $$$2$$$: it means that the resulting number is ebne.Find any resulting number that is ebne. If it's impossible to create an ebne number from the given number report about it.

The input consists of multiple test cases. The first line contains a single integer $$$t$$$ ($$$1 \le t \le 1000$$$)  — the number of test cases. The description of the test cases follows. The first line of each test case contains a single integer $$$n$$$ ($$$1 \le n \le 3000$$$)  — the number of digits in the original number. The second line of each test case contains a non-negative integer number $$$s$$$, consisting of $$$n$$$ digits. It is guaranteed that $$$s$$$ does not contain leading zeros and the sum of $$$n$$$ over all test cases does not exceed $$$3000$$$.

For each test case given in the input print the answer in the following format:  If it is impossible to create an ebne number, print "-1" (without quotes); Otherwise, print the resulting number after deleting some, possibly zero, but not all digits. This number should be ebne. If there are multiple answers, you can print any of them. Note that answers with leading zeros or empty strings are not accepted. It's not necessary to minimize or maximize the number of deleted digits.

NoteIn the first test case of the example, $$$1227$$$ is already an ebne number (as $$$1 + 2 + 2 + 7 = 12$$$, $$$12$$$ is divisible by $$$2$$$, while in the same time, $$$1227$$$ is not divisible by $$$2$$$) so we don't need to delete any digits. Answers such as $$$127$$$ and $$$17$$$ will also be accepted.In the second test case of the example, it is clearly impossible to create an ebne number from the given number.In the third test case of the example, there are many ebne numbers we can obtain by deleting, for example, $$$1$$$ digit such as $$$17703$$$, $$$77013$$$ or $$$17013$$$. Answers such as $$$1701$$$ or $$$770$$$ will not be accepted as they are not ebne numbers. Answer $$$013$$$ will not be accepted as it contains leading zeroes.Explanation: $$$1 + 7 + 7 + 0 + 3 = 18$$$. As $$$18$$$ is divisible by $$$2$$$ while $$$17703$$$ is not divisible by $$$2$$$, we can see that $$$17703$$$ is an ebne number. Same with $$$77013$$$ and $$$17013$$$; $$$1 + 7 + 0 + 1 = 9$$$. Because $$$9$$$ is not divisible by $$$2$$$, $$$1701$$$ is not an ebne number; $$$7 + 7 + 0 = 14$$$. This time, $$$14$$$ is divisible by $$$2$$$ but $$$770$$$ is also divisible by $$$2$$$, therefore, $$$770$$$ is not an ebne number.In the last test case of the example, one of many other possible answers is given. Another possible answer is: 222373204424185217171912 $$$\rightarrow$$$ 22237320442418521717191 (delete the last digit).

T = gets.to_i

T.times do
  n = gets.to_i
  s = gets.chomp.chars.map(&:to_i)

  odds = []

  s.each do |d|
    next if d.even?

    odds << d
  end

  if odds.size <= 1
    puts -1
  else
    puts odds.take(2).join
  end
end

# coding: utf-8

# Even but not even
# Sum(list_of_digits).even? == true
# original_number.even? == false
t = gets.to_i
t.times {
    n = gets.to_i
    ar = gets.chomp.split(//).map(&:to_i)
    ar.delete_if { |i| i.even? }

    if ar.length <= 1
        puts "-1"
    else
        if ar.length.odd?
            ar.pop
        end

        puts ar.join('')
    end

}

t = gets.to_i
odd = ["1","3","5","7","9"]
even = ["2","4","6","8","0"]

t.times{
  n = gets.to_i
  s = gets.chomp
  sc = s.chars.map(&:to_i)
  sc.pop while sc != [] && sc.last.even?
  if sc == []
    p -1
    #puts "because sc==[]"
    next
  end
  n_odd = sc.count{|i|i.odd?}
  #puts "s: #{s}, n_odd: #{n_odd}"
  flag = false
  if n_odd.odd?
    #puts "n_odd is odd"
    [*0..sc.length-1].reverse.each{|i|
      #puts "loop yeah!!"
      if sc[i].odd? && flag
        sc.delete_at(i)
        break
      end
      flag = true if sc[i].odd?
    }
  end
  n_odd = sc.count{|i|i.odd?}
  if n_odd.odd?
    p -1
    #puts "because n_odd.odd?"
    next
  end
  if sc == [0]
    p -1
    #puts "because sc==[0]"
  else
    puts sc.join
  end
}

# coding: utf-8

# Even but not even
# Sum(list_of_digits).even? == true
# original_number.even? == false
t = gets.to_i
t.times {
    n = gets.to_i
    odds = []
    gets.chomp.split(//).each { |c|
        i = c.to_i
        if i.odd?
            odds << i
        end
    }
    if odds.length <= 1
        puts "-1"
    else
        puts odds.join('')
    end
}

# coding: utf-8

# Even but not even
# Sum(list_of_digits).even? == true
# original_number.even? == false
t = gets.to_i
t.times {
    n = gets.to_i
    cr = Array.new(10+1) { 0 }
    gets.chomp.split(//).each { |c|
        i = c.to_i
        cr[i] += 1
    }
    odds_unique = {}
    for ci in 1..10
        if ci.odd?
            if cr[ci] > 0
                odds_unique[ci] = true
            end
        end
    end

    if odds_unique.length <= 1
        puts "-1"
    else
        puts (odds_unique.keys[0..1]).join('')
    end
}


Initially there was an array $$$a$$$ consisting of $$$n$$$ integers. Positions in it are numbered from $$$1$$$ to $$$n$$$.Exactly $$$q$$$ queries were performed on the array. During the $$$i$$$-th query some segment $$$(l_i, r_i)$$$ $$$(1 \le l_i \le r_i \le n)$$$ was selected and values of elements on positions from $$$l_i$$$ to $$$r_i$$$ inclusive got changed to $$$i$$$. The order of the queries couldn't be changed and all $$$q$$$ queries were applied. It is also known that every position from $$$1$$$ to $$$n$$$ got covered by at least one segment.We could have offered you the problem about checking if some given array (consisting of $$$n$$$ integers with values from $$$1$$$ to $$$q$$$) can be obtained by the aforementioned queries. However, we decided that it will come too easy for you.So the enhancement we introduced to it is the following. Some set of positions (possibly empty) in this array is selected and values of elements on these positions are set to $$$0$$$.Your task is to check if this array can be obtained by the aforementioned queries. Also if it can be obtained then restore this array.If there are multiple possible arrays then print any of them.

The first line contains two integers $$$n$$$ and $$$q$$$ ($$$1 \le n, q \le 2 \cdot 10^5$$$) — the number of elements of the array and the number of queries perfomed on it. The second line contains $$$n$$$ integer numbers $$$a_1, a_2, \dots, a_n$$$ ($$$0 \le a_i \le q$$$) — the resulting array. If element at some position $$$j$$$ is equal to $$$0$$$ then the value of element at this position can be any integer from $$$1$$$ to $$$q$$$.

Print "YES" if the array $$$a$$$ can be obtained by performing $$$q$$$ queries. Segments $$$(l_i, r_i)$$$ $$$(1 \le l_i \le r_i \le n)$$$ are chosen separately for each query. Every position from $$$1$$$ to $$$n$$$ should be covered by at least one segment.  Otherwise print "NO". If some array can be obtained then print $$$n$$$ integers on the second line — the $$$i$$$-th number should be equal to the $$$i$$$-th element of the resulting array and should have value from $$$1$$$ to $$$q$$$. This array should be obtainable by performing exactly $$$q$$$ queries. If there are multiple possible arrays then print any of them.

NoteIn the first example you can also replace $$$0$$$ with $$$1$$$ but not with $$$3$$$.In the second example it doesn't really matter what segments to choose until query $$$10$$$ when the segment is $$$(1, 3)$$$.The third example showcases the fact that the order of queries can't be changed, you can't firstly set $$$(1, 3)$$$ to $$$6$$$ and after that change $$$(2, 2)$$$ to $$$5$$$. The segment of $$$5$$$ should be applied before segment of $$$6$$$.There is a lot of correct resulting arrays for the fourth example.

N, Q = gets.split.map(&:to_i)
as = gets.split.map(&:to_i)
exist_max = as.include?(Q)
used = []
stack = []

N.times do |i|
    if as[i] == 0
        if exist_max
            as[i] = i==0 ? 1 : as[i-1]
        else
            as[i] = Q
            exist_max = true
        end
        next
    end

    if used[as[i]]
        puts 'NO'
        exit
    end

    while stack.length > 0 && stack[-1] > as[i]
        j = stack.pop
        used[j] = true
    end
    stack.push(as[i]) if stack[-1] != as[i]
end

unless exist_max
    puts 'NO'
    exit
end

puts 'YES'
puts as.join(' ')

N, Q = gets.split.map(&:to_i)
as = gets.split.map(&:to_i)
exist_max = as.include?(Q)
found = []

N.times do |i|
    if as[i] == 0
        if exist_max
            as[i] = i==0 ? 1 : as[i-1]
        else
            as[i] = Q
            exist_max = true
        end
        next
    end
    if i > 0 && as[i-1] < as[i]
        if found[as[i]]
            puts 'NO'
            exit
        end
    end
    found[as[i]] = true
end

unless exist_max
    puts 'NO'
    exit
end

puts 'YES'
puts as.join(' ')