priyam2k

BANSTR

Jan 11th, 2020
307
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 4.28 KB | None | 0 0
  1. from __future__ import division, print_function
  2. def main():
  3.     for _ in range(int(input())):
  4.         n, points = map(int, input().split())
  5.         s = input()
  6.         pref_b = [0]*(n+1)
  7.         for i in range(1,n+1):
  8.             if s[i-1] == 'b':
  9.                 pref_b[i] = 1 + pref_b[i-1]
  10.             else :
  11.                 pref_b[i] = pref_b[i-1]
  12.         count_a = 0
  13.         answer_index = -1
  14.         for i in range(n - 1, -1, -1):
  15.             temp_points = points
  16.             count_b = pref_b[i+1]
  17.             x = min(count_a, points)
  18.             temp_points -= x
  19.             if temp_points//2 + x >= count_b:
  20.                 answer_index = i
  21.                 break
  22.             if s[i] == 'a':
  23.                 count_a+=1
  24.         result = list(s)
  25.         if answer_index == -1:
  26.             print(''.join(result))
  27.             continue
  28.        
  29.         for i in range(answer_index+1):
  30.             result[i] = 'a'
  31.         total_replace = count_b
  32.         while total_replace*2 + (count_b - total_replace) > points:
  33.             total_replace -=1
  34.         swaps_required = count_b - total_replace
  35.         j = -1
  36.         while swaps_required > 0:
  37.             if result[j] == 'a':
  38.                 result[j] = 'b'
  39.                 swaps_required -= 1
  40.             j -= 1
  41.         print(''.join(result))
  42.    
  43. ######## Python 2 and 3 footer by Pajenegod and c1729
  44.    
  45. # Note because cf runs old PyPy3 version which doesn't have the sped up
  46. # unicode strings, PyPy3 strings will many times be slower than pypy2.
  47. # There is a way to get around this by using binary strings in PyPy3
  48. # but its syntax is different which makes it kind of a mess to use.
  49.  
  50. # So on cf, use PyPy2 for best string performance.
  51.  
  52. py2 = round(0.5)
  53. if py2:
  54.     from future_builtins import ascii, filter, hex, map, oct, zip
  55.     range = xrange
  56.  
  57. import os, sys
  58. from io import IOBase, BytesIO
  59.  
  60. BUFSIZE = 8192
  61. class FastIO(BytesIO):
  62.     newlines = 0
  63.  
  64.     def __init__(self, file):
  65.         self._file = file
  66.         self._fd = file.fileno()
  67.         self.writable = "x" in file.mode or "w" in file.mode
  68.         self.write = super(FastIO, self).write if self.writable else None
  69.  
  70.     def _fill(self):
  71.         s = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
  72.         self.seek((self.tell(), self.seek(0,2), super(FastIO, self).write(s))[0])
  73.         return s
  74.  
  75.     def read(self):
  76.         while self._fill(): pass
  77.         return super(FastIO,self).read()
  78.  
  79.     def readline(self):
  80.         while self.newlines == 0:
  81.             s = self._fill(); self.newlines = s.count(b"\n") + (not s)
  82.         self.newlines -= 1
  83.         return super(FastIO, self).readline()
  84.  
  85.     def flush(self):
  86.         if self.writable:
  87.             os.write(self._fd, self.getvalue())
  88.             self.truncate(0), self.seek(0)
  89.  
  90. class IOWrapper(IOBase):
  91.     def __init__(self, file):
  92.         self.buffer = FastIO(file)
  93.         self.flush = self.buffer.flush
  94.         self.writable = self.buffer.writable
  95.         if py2:
  96.             self.write = self.buffer.write
  97.             self.read = self.buffer.read
  98.             self.readline = self.buffer.readline
  99.         else:
  100.             self.write = lambda s:self.buffer.write(s.encode('ascii'))
  101.             self.read = lambda:self.buffer.read().decode('ascii')
  102.             self.readline = lambda:self.buffer.readline().decode('ascii')
  103.  
  104.  
  105. sys.stdin, sys.stdout = IOWrapper(sys.stdin), IOWrapper(sys.stdout)
  106. input = lambda: sys.stdin.readline().rstrip('\r\n')
  107.  
  108. # Cout implemented in Python
  109. import sys
  110. class ostream:
  111.     def __lshift__(self,a):
  112.         sys.stdout.write(str(a))
  113.         return self
  114. cout = ostream()
  115. endl = '\n'
  116.  
  117. # Read all remaining integers in stdin, type is given by optional argument, this is fast
  118. def readnumbers(zero = 0):
  119.     conv = ord if py2 else lambda x:x
  120.     A = []; numb = zero; sign = 1; i = 0; s = sys.stdin.buffer.read()
  121.     try:
  122.         while True:
  123.             if s[i] >= b'0' [0]:
  124.                 numb = 10 * numb + conv(s[i]) - 48
  125.             elif s[i] == b'-' [0]: sign = -1
  126.             elif s[i] != b'\r' [0]:
  127.                 A.append(sign*numb)
  128.                 numb = zero; sign = 1
  129.             i += 1
  130.     except:pass
  131.     if s and s[-1] >= b'0' [0]:
  132.         A.append(sign*numb)
  133.     return A
  134.  
  135. if __name__== "__main__":
  136.   main()
Add Comment
Please, Sign In to add comment