Created
August 4, 2018 02:12
-
-
Save sko00o/fecb20dc6ba4543321b5d5472ff8b71e to your computer and use it in GitHub Desktop.
RepeatKiller.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# -*- coding: utf-8 -*- | |
# Author: SKo00o | |
def kill(str): | |
p = 'X' | |
o = [] | |
f = False | |
for x in str: | |
if x != p: | |
if f: | |
f = False | |
o += x | |
else: | |
if not f: | |
o = o[:-1] | |
f = True | |
p = x | |
r = ''.join(o) | |
return r | |
def rkill(str): | |
f = True | |
while f: | |
nstr = kill(str) | |
if nstr == str: | |
return nstr | |
str = nstr | |
def mkstr(str, idx): | |
if idx < 0 or idx > len(str): | |
return str | |
return str[:idx] + str[idx] + str[idx:] | |
def getidx(str): | |
idx = [] | |
for i in range(len(str)): | |
if i == 0: | |
idx.append(i) | |
elif str[i] != str[i - 1]: | |
idx.append(i) | |
return idx | |
if __name__ == '__main__': | |
T = int(raw_input()) | |
for _ in range(T): | |
k = raw_input() | |
r = -1 | |
for i in getidx(k): | |
s = mkstr(k, i) | |
e = rkill(s) | |
d = len(s) - len(e) | |
if d > r: | |
r = d | |
print r | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment