message stringlengths 2 23.4k | message_type stringclasses 2 values | message_id int64 0 1 | conversation_id int64 129 108k | cluster float64 6 6 | __index_level_0__ int64 258 216k |
|---|---|---|---|---|---|
Provide tags and a correct Python 3 solution for this coding contest problem.
A has a string consisting of some number of lowercase English letters 'a'. He gives it to his friend B who appends some number of letters 'b' to the end of this string. Since both A and B like the characters 'a' and 'b', they have made sure that at this point, at least one 'a' and one 'b' exist in the string.
B now gives this string to C and he appends some number of letters 'c' to the end of the string. However, since C is a good friend of A and B, the number of letters 'c' he appends is equal to the number of 'a' or to the number of 'b' in the string. It is also possible that the number of letters 'c' equals both to the number of letters 'a' and to the number of letters 'b' at the same time.
You have a string in your hands, and you want to check if it is possible to obtain the string in this way or not. If it is possible to obtain the string, print "YES", otherwise print "NO" (without the quotes).
Input
The first and only line consists of a string S ( 1 ≤ |S| ≤ 5 000 ). It is guaranteed that the string will only consist of the lowercase English letters 'a', 'b', 'c'.
Output
Print "YES" or "NO", according to the condition.
Examples
Input
aaabccc
Output
YES
Input
bbacc
Output
NO
Input
aabc
Output
YES
Note
Consider first example: the number of 'c' is equal to the number of 'a'.
Consider second example: although the number of 'c' is equal to the number of the 'b', the order is not correct.
Consider third example: the number of 'c' is equal to the number of 'b'. | instruction | 0 | 56,909 | 6 | 113,818 |
Tags: implementation
Correct Solution:
```
s = input()
i1 = 0
i = 0
while s[i] == "a":
i += 1
i1 += 1
if i == len(s):
break
i2 = 0
if i != len(s):
while s[i] == "b":
i += 1
i2 += 1
if i == len(s):
break
i3 = 0
if i != len(s):
while s[i] == "c":
i += 1
i3 += 1
if i == len(s):
break
if i == len(s) and i1 > 0 and i2 > 0 and (i3 == i1 or i3 == i2):
print("YES")
else:
print("NO")
``` | output | 1 | 56,909 | 6 | 113,819 |
Provide tags and a correct Python 3 solution for this coding contest problem.
A has a string consisting of some number of lowercase English letters 'a'. He gives it to his friend B who appends some number of letters 'b' to the end of this string. Since both A and B like the characters 'a' and 'b', they have made sure that at this point, at least one 'a' and one 'b' exist in the string.
B now gives this string to C and he appends some number of letters 'c' to the end of the string. However, since C is a good friend of A and B, the number of letters 'c' he appends is equal to the number of 'a' or to the number of 'b' in the string. It is also possible that the number of letters 'c' equals both to the number of letters 'a' and to the number of letters 'b' at the same time.
You have a string in your hands, and you want to check if it is possible to obtain the string in this way or not. If it is possible to obtain the string, print "YES", otherwise print "NO" (without the quotes).
Input
The first and only line consists of a string S ( 1 ≤ |S| ≤ 5 000 ). It is guaranteed that the string will only consist of the lowercase English letters 'a', 'b', 'c'.
Output
Print "YES" or "NO", according to the condition.
Examples
Input
aaabccc
Output
YES
Input
bbacc
Output
NO
Input
aabc
Output
YES
Note
Consider first example: the number of 'c' is equal to the number of 'a'.
Consider second example: although the number of 'c' is equal to the number of the 'b', the order is not correct.
Consider third example: the number of 'c' is equal to the number of 'b'. | instruction | 0 | 56,910 | 6 | 113,820 |
Tags: implementation
Correct Solution:
```
state = 1
s = input()
if ((s.count('a') == 0) or (s.count('b') == 0) or ((s.count('c') != s.count('a') and (s.count('c') != s.count('b'))))):
print('NO')
exit()
for el in s:
if el == 'b':
state = max(state, 2)
if el == 'c':
state = max(state, 3)
if state != 1 and el == 'a':
print("NO")
exit()
if state != 2 and el == 'b':
print("NO")
exit()
if state != 3 and el == 'c':
print("NO")
exit()
print("YES")
``` | output | 1 | 56,910 | 6 | 113,821 |
Provide tags and a correct Python 3 solution for this coding contest problem.
A has a string consisting of some number of lowercase English letters 'a'. He gives it to his friend B who appends some number of letters 'b' to the end of this string. Since both A and B like the characters 'a' and 'b', they have made sure that at this point, at least one 'a' and one 'b' exist in the string.
B now gives this string to C and he appends some number of letters 'c' to the end of the string. However, since C is a good friend of A and B, the number of letters 'c' he appends is equal to the number of 'a' or to the number of 'b' in the string. It is also possible that the number of letters 'c' equals both to the number of letters 'a' and to the number of letters 'b' at the same time.
You have a string in your hands, and you want to check if it is possible to obtain the string in this way or not. If it is possible to obtain the string, print "YES", otherwise print "NO" (without the quotes).
Input
The first and only line consists of a string S ( 1 ≤ |S| ≤ 5 000 ). It is guaranteed that the string will only consist of the lowercase English letters 'a', 'b', 'c'.
Output
Print "YES" or "NO", according to the condition.
Examples
Input
aaabccc
Output
YES
Input
bbacc
Output
NO
Input
aabc
Output
YES
Note
Consider first example: the number of 'c' is equal to the number of 'a'.
Consider second example: although the number of 'c' is equal to the number of the 'b', the order is not correct.
Consider third example: the number of 'c' is equal to the number of 'b'. | instruction | 0 | 56,911 | 6 | 113,822 |
Tags: implementation
Correct Solution:
```
import sys
import os
import math
import re
s = input()
#first b, last a
#first c, last b
#last a,
As = [ i for i, ltr in enumerate(s) if ltr == 'a']
Bs = [i for i, ltr in enumerate(s) if ltr == 'b']
Cs = [i for i, ltr in enumerate(s) if ltr == 'c']
if (len(s) < 3 or len(Bs) == 0 or len(As) == 0 or len(Cs) == 0):
print('NO')
exit(0)
if Bs[0] < As[len(As)-1] or Cs[0] < Bs[len(Bs)-1] or Cs[0] < As[len(As) -1]:
print('NO')
exit(0)
#if count of cs is same as count of as or bs then yes
if len(Cs) == len(As) or len(Cs) == len(Bs):
print('YES')
exit(0)
print('NO')
``` | output | 1 | 56,911 | 6 | 113,823 |
Provide tags and a correct Python 3 solution for this coding contest problem.
A has a string consisting of some number of lowercase English letters 'a'. He gives it to his friend B who appends some number of letters 'b' to the end of this string. Since both A and B like the characters 'a' and 'b', they have made sure that at this point, at least one 'a' and one 'b' exist in the string.
B now gives this string to C and he appends some number of letters 'c' to the end of the string. However, since C is a good friend of A and B, the number of letters 'c' he appends is equal to the number of 'a' or to the number of 'b' in the string. It is also possible that the number of letters 'c' equals both to the number of letters 'a' and to the number of letters 'b' at the same time.
You have a string in your hands, and you want to check if it is possible to obtain the string in this way or not. If it is possible to obtain the string, print "YES", otherwise print "NO" (without the quotes).
Input
The first and only line consists of a string S ( 1 ≤ |S| ≤ 5 000 ). It is guaranteed that the string will only consist of the lowercase English letters 'a', 'b', 'c'.
Output
Print "YES" or "NO", according to the condition.
Examples
Input
aaabccc
Output
YES
Input
bbacc
Output
NO
Input
aabc
Output
YES
Note
Consider first example: the number of 'c' is equal to the number of 'a'.
Consider second example: although the number of 'c' is equal to the number of the 'b', the order is not correct.
Consider third example: the number of 'c' is equal to the number of 'b'. | instruction | 0 | 56,912 | 6 | 113,824 |
Tags: implementation
Correct Solution:
```
S = input()
A, B, C = [], [], []
for i, v in enumerate(S):
if v == 'a':
A += [i]
if v == 'b':
B += [i]
if v == 'c':
C += [i]
if A != [] and B != [] and C != [] and A[-1] < B[0] and B[-1] < C[0] and (len(C) == len(A) or len(C) == len(B)):
print("YES")
else:
print("NO")
``` | output | 1 | 56,912 | 6 | 113,825 |
Provide tags and a correct Python 3 solution for this coding contest problem.
A has a string consisting of some number of lowercase English letters 'a'. He gives it to his friend B who appends some number of letters 'b' to the end of this string. Since both A and B like the characters 'a' and 'b', they have made sure that at this point, at least one 'a' and one 'b' exist in the string.
B now gives this string to C and he appends some number of letters 'c' to the end of the string. However, since C is a good friend of A and B, the number of letters 'c' he appends is equal to the number of 'a' or to the number of 'b' in the string. It is also possible that the number of letters 'c' equals both to the number of letters 'a' and to the number of letters 'b' at the same time.
You have a string in your hands, and you want to check if it is possible to obtain the string in this way or not. If it is possible to obtain the string, print "YES", otherwise print "NO" (without the quotes).
Input
The first and only line consists of a string S ( 1 ≤ |S| ≤ 5 000 ). It is guaranteed that the string will only consist of the lowercase English letters 'a', 'b', 'c'.
Output
Print "YES" or "NO", according to the condition.
Examples
Input
aaabccc
Output
YES
Input
bbacc
Output
NO
Input
aabc
Output
YES
Note
Consider first example: the number of 'c' is equal to the number of 'a'.
Consider second example: although the number of 'c' is equal to the number of the 'b', the order is not correct.
Consider third example: the number of 'c' is equal to the number of 'b'. | instruction | 0 | 56,913 | 6 | 113,826 |
Tags: implementation
Correct Solution:
```
import sys
s = input()
a = 0
b = 0
c = 0
state = 'a'
for i in s:
if state == 'a':
if i == 'a':
a += 1
elif i == 'b':
b += 1
state = 'b'
elif i == 'c':
state = 'c'
c += 1
else:
print("NO")
sys.exit()
elif state == 'b':
if i == 'a':
print("NO")
sys.exit()
elif i == 'b':
b += 1
elif i == 'c':
state = 'c'
c += 1
else:
print("NO")
sys.exit()
elif state == 'c':
if i == 'a':
print("NO")
sys.exit()
elif i == 'b':
print("NO")
sys.exit()
elif i == 'c':
state = 'c'
c += 1
else:
print("NO")
sys.exit()
if a == 0 or b == 0 or (c != a and c != b):
print("NO")
else:
print("YES")
``` | output | 1 | 56,913 | 6 | 113,827 |
Provide tags and a correct Python 3 solution for this coding contest problem.
A has a string consisting of some number of lowercase English letters 'a'. He gives it to his friend B who appends some number of letters 'b' to the end of this string. Since both A and B like the characters 'a' and 'b', they have made sure that at this point, at least one 'a' and one 'b' exist in the string.
B now gives this string to C and he appends some number of letters 'c' to the end of the string. However, since C is a good friend of A and B, the number of letters 'c' he appends is equal to the number of 'a' or to the number of 'b' in the string. It is also possible that the number of letters 'c' equals both to the number of letters 'a' and to the number of letters 'b' at the same time.
You have a string in your hands, and you want to check if it is possible to obtain the string in this way or not. If it is possible to obtain the string, print "YES", otherwise print "NO" (without the quotes).
Input
The first and only line consists of a string S ( 1 ≤ |S| ≤ 5 000 ). It is guaranteed that the string will only consist of the lowercase English letters 'a', 'b', 'c'.
Output
Print "YES" or "NO", according to the condition.
Examples
Input
aaabccc
Output
YES
Input
bbacc
Output
NO
Input
aabc
Output
YES
Note
Consider first example: the number of 'c' is equal to the number of 'a'.
Consider second example: although the number of 'c' is equal to the number of the 'b', the order is not correct.
Consider third example: the number of 'c' is equal to the number of 'b'. | instruction | 0 | 56,914 | 6 | 113,828 |
Tags: implementation
Correct Solution:
```
I = lambda:int(input())
ID = lambda:map(int, input().split())
IL = lambda:list(ID())
s = input()
a = s.count('a')
b = s.count('b')
c = s.count('c')
n = len(s)
aa = s[:a]
ba = s[a:a+b]
ca = s[a+b:]
if (c ==a or c == b) and list(set(aa))==['a'] and list(set(ba))== ['b']:
print('YES')
else:
print('NO')
``` | output | 1 | 56,914 | 6 | 113,829 |
Provide tags and a correct Python 3 solution for this coding contest problem.
A has a string consisting of some number of lowercase English letters 'a'. He gives it to his friend B who appends some number of letters 'b' to the end of this string. Since both A and B like the characters 'a' and 'b', they have made sure that at this point, at least one 'a' and one 'b' exist in the string.
B now gives this string to C and he appends some number of letters 'c' to the end of the string. However, since C is a good friend of A and B, the number of letters 'c' he appends is equal to the number of 'a' or to the number of 'b' in the string. It is also possible that the number of letters 'c' equals both to the number of letters 'a' and to the number of letters 'b' at the same time.
You have a string in your hands, and you want to check if it is possible to obtain the string in this way or not. If it is possible to obtain the string, print "YES", otherwise print "NO" (without the quotes).
Input
The first and only line consists of a string S ( 1 ≤ |S| ≤ 5 000 ). It is guaranteed that the string will only consist of the lowercase English letters 'a', 'b', 'c'.
Output
Print "YES" or "NO", according to the condition.
Examples
Input
aaabccc
Output
YES
Input
bbacc
Output
NO
Input
aabc
Output
YES
Note
Consider first example: the number of 'c' is equal to the number of 'a'.
Consider second example: although the number of 'c' is equal to the number of the 'b', the order is not correct.
Consider third example: the number of 'c' is equal to the number of 'b'. | instruction | 0 | 56,915 | 6 | 113,830 |
Tags: implementation
Correct Solution:
```
s = input()
a = [0] * 3
k = 0
ok = True
for i in s:
c = ord(i) - ord('a')
if c == k:
a[k] += 1
elif c == k + 1:
k += 1
a[k] += 1
else:
ok = False
break
if a[0] == 0 or a[1] == 0 or (a[0] != a[2] and a[1] != a[2]):
ok = False
if ok: print('YES')
else: print('NO')
``` | output | 1 | 56,915 | 6 | 113,831 |
Provide tags and a correct Python 3 solution for this coding contest problem.
<image>
Input
The first line of the input is a string (between 1 and 50 characters long, inclusive). Each character will be a letter of English alphabet, lowercase or uppercase.
The second line of the input is an integer between 0 and 26, inclusive.
Output
Output the required string.
Examples
Input
AprilFool
14
Output
AprILFooL | instruction | 0 | 57,412 | 6 | 114,824 |
Tags: *special, implementation
Correct Solution:
```
_27 = input()
_16 = int(input())
_27 = _27.lower()
_4 = ""
for _26 in range(len(_27)):
_19 = _27[_26]
if ord(_19) < _16 + 97:
_4 = _4 + _19.upper()
else:
_4 = _4 + _19.lower()
print(_4)
``` | output | 1 | 57,412 | 6 | 114,825 |
Provide tags and a correct Python 3 solution for this coding contest problem.
<image>
Input
The first line of the input is a string (between 1 and 50 characters long, inclusive). Each character will be a letter of English alphabet, lowercase or uppercase.
The second line of the input is an integer between 0 and 26, inclusive.
Output
Output the required string.
Examples
Input
AprilFool
14
Output
AprILFooL | instruction | 0 | 57,413 | 6 | 114,826 |
Tags: *special, implementation
Correct Solution:
```
a = list(input().lower())
b = int(input())
for i in range(len(a)):
if ord(a[i]) - ord('a') < b:
a[i] = a[i].upper()
print(''.join(a))
``` | output | 1 | 57,413 | 6 | 114,827 |
Provide tags and a correct Python 3 solution for this coding contest problem.
<image>
Input
The first line of the input is a string (between 1 and 50 characters long, inclusive). Each character will be a letter of English alphabet, lowercase or uppercase.
The second line of the input is an integer between 0 and 26, inclusive.
Output
Output the required string.
Examples
Input
AprilFool
14
Output
AprILFooL | instruction | 0 | 57,414 | 6 | 114,828 |
Tags: *special, implementation
Correct Solution:
```
text = input().lower()
caps = int(input())+97
for letter in text:
print(letter.upper(), end='')if letter < chr(caps) else print(letter, end='')
print()
``` | output | 1 | 57,414 | 6 | 114,829 |
Provide tags and a correct Python 3 solution for this coding contest problem.
<image>
Input
The first line of the input is a string (between 1 and 50 characters long, inclusive). Each character will be a letter of English alphabet, lowercase or uppercase.
The second line of the input is an integer between 0 and 26, inclusive.
Output
Output the required string.
Examples
Input
AprilFool
14
Output
AprILFooL | instruction | 0 | 57,415 | 6 | 114,830 |
Tags: *special, implementation
Correct Solution:
```
s = input()
n = int(input())
d = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'
e = 'abcdefghijklmnopqrstuvwxyz'
for i in s:
z = ord(i) - ord('a') if ord('a') <= ord(i) and ord(i) <= ord('z') else ord(i) - ord('A')
if z < n:
print(d[z], end = '')
else:
print(e[z], end = '')
``` | output | 1 | 57,415 | 6 | 114,831 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Let’s define a grid to be a set of tiles with 2 rows and 13 columns. Each tile has an English letter written in it. The letters don't have to be unique: there might be two or more tiles with the same letter written on them. Here is an example of a grid:
ABCDEFGHIJKLM
NOPQRSTUVWXYZ
We say that two tiles are adjacent if they share a side or a corner. In the example grid above, the tile with the letter 'A' is adjacent only to the tiles with letters 'B', 'N', and 'O'. A tile is not adjacent to itself.
A sequence of tiles is called a path if each tile in the sequence is adjacent to the tile which follows it (except for the last tile in the sequence, which of course has no successor). In this example, "ABC" is a path, and so is "KXWIHIJK". "MAB" is not a path because 'M' is not adjacent to 'A'. A single tile can be used more than once by a path (though the tile cannot occupy two consecutive places in the path because no tile is adjacent to itself).
You’re given a string s which consists of 27 upper-case English letters. Each English letter occurs at least once in s. Find a grid that contains a path whose tiles, viewed in the order that the path visits them, form the string s. If there’s no solution, print "Impossible" (without the quotes).
Input
The only line of the input contains the string s, consisting of 27 upper-case English letters. Each English letter occurs at least once in s.
Output
Output two lines, each consisting of 13 upper-case English characters, representing the rows of the grid. If there are multiple solutions, print any of them. If there is no solution print "Impossible".
Examples
Input
ABCDEFGHIJKLMNOPQRSGTUVWXYZ
Output
YXWVUTGHIJKLM
ZABCDEFSRQPON
Input
BUVTYZFQSNRIWOXXGJLKACPEMDH
Output
Impossible | instruction | 0 | 58,517 | 6 | 117,034 |
Tags: brute force, constructive algorithms, implementation, strings
Correct Solution:
```
l, d = [], {}
for i, c in enumerate(input()):
if c in d:
u, v = (d[c] + i + 1) // 2, i
else:
l.append(c)
d[c] = i
s = ''.join(l * 2)
print(s[u:u + 13] + '\n' + s[u + 25:u + 12:-1] if u < v else "Impossible")
``` | output | 1 | 58,517 | 6 | 117,035 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Let’s define a grid to be a set of tiles with 2 rows and 13 columns. Each tile has an English letter written in it. The letters don't have to be unique: there might be two or more tiles with the same letter written on them. Here is an example of a grid:
ABCDEFGHIJKLM
NOPQRSTUVWXYZ
We say that two tiles are adjacent if they share a side or a corner. In the example grid above, the tile with the letter 'A' is adjacent only to the tiles with letters 'B', 'N', and 'O'. A tile is not adjacent to itself.
A sequence of tiles is called a path if each tile in the sequence is adjacent to the tile which follows it (except for the last tile in the sequence, which of course has no successor). In this example, "ABC" is a path, and so is "KXWIHIJK". "MAB" is not a path because 'M' is not adjacent to 'A'. A single tile can be used more than once by a path (though the tile cannot occupy two consecutive places in the path because no tile is adjacent to itself).
You’re given a string s which consists of 27 upper-case English letters. Each English letter occurs at least once in s. Find a grid that contains a path whose tiles, viewed in the order that the path visits them, form the string s. If there’s no solution, print "Impossible" (without the quotes).
Input
The only line of the input contains the string s, consisting of 27 upper-case English letters. Each English letter occurs at least once in s.
Output
Output two lines, each consisting of 13 upper-case English characters, representing the rows of the grid. If there are multiple solutions, print any of them. If there is no solution print "Impossible".
Examples
Input
ABCDEFGHIJKLMNOPQRSGTUVWXYZ
Output
YXWVUTGHIJKLM
ZABCDEFSRQPON
Input
BUVTYZFQSNRIWOXXGJLKACPEMDH
Output
Impossible | instruction | 0 | 58,518 | 6 | 117,036 |
Tags: brute force, constructive algorithms, implementation, strings
Correct Solution:
```
s = input()
arr = [0] * 26
for i in s:
arr[ord(i) - 65] += 1
l = chr(arr.index(2) + 65)
a = s.find(l)
b = s.rfind(l)
ans = b - a - 1
x = 27 + a + ans // 2
if ans == 0:
print('Impossible')
else:
s = 3 * s
print(s[x - 12:x + 1])
s1 = s[x + 1:x + 15][::-1]
s1 = s1[:s1.find(l)] + s1[s1.find(l) + 1:]
print(s1)
``` | output | 1 | 58,518 | 6 | 117,037 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Let’s define a grid to be a set of tiles with 2 rows and 13 columns. Each tile has an English letter written in it. The letters don't have to be unique: there might be two or more tiles with the same letter written on them. Here is an example of a grid:
ABCDEFGHIJKLM
NOPQRSTUVWXYZ
We say that two tiles are adjacent if they share a side or a corner. In the example grid above, the tile with the letter 'A' is adjacent only to the tiles with letters 'B', 'N', and 'O'. A tile is not adjacent to itself.
A sequence of tiles is called a path if each tile in the sequence is adjacent to the tile which follows it (except for the last tile in the sequence, which of course has no successor). In this example, "ABC" is a path, and so is "KXWIHIJK". "MAB" is not a path because 'M' is not adjacent to 'A'. A single tile can be used more than once by a path (though the tile cannot occupy two consecutive places in the path because no tile is adjacent to itself).
You’re given a string s which consists of 27 upper-case English letters. Each English letter occurs at least once in s. Find a grid that contains a path whose tiles, viewed in the order that the path visits them, form the string s. If there’s no solution, print "Impossible" (without the quotes).
Input
The only line of the input contains the string s, consisting of 27 upper-case English letters. Each English letter occurs at least once in s.
Output
Output two lines, each consisting of 13 upper-case English characters, representing the rows of the grid. If there are multiple solutions, print any of them. If there is no solution print "Impossible".
Examples
Input
ABCDEFGHIJKLMNOPQRSGTUVWXYZ
Output
YXWVUTGHIJKLM
ZABCDEFSRQPON
Input
BUVTYZFQSNRIWOXXGJLKACPEMDH
Output
Impossible | instruction | 0 | 58,519 | 6 | 117,038 |
Tags: brute force, constructive algorithms, implementation, strings
Correct Solution:
```
#!/usr/bin/env python3
l, d = [], {}
for i, c in enumerate(input()):
if c in d:
d1, len = d[c], (i-d[c]-1)//2 +1
d2 = i
else:
l.append(c)
d[c] = i
s = ''.join(l*2)
if d2-d1 <= 1:
print("Impossible")
else:
print(s[d1+26-13+len:d1+26] + s[d1:d1+len] + '\n' + s[d1+len:d1+len+13][::-1])
``` | output | 1 | 58,519 | 6 | 117,039 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Let’s define a grid to be a set of tiles with 2 rows and 13 columns. Each tile has an English letter written in it. The letters don't have to be unique: there might be two or more tiles with the same letter written on them. Here is an example of a grid:
ABCDEFGHIJKLM
NOPQRSTUVWXYZ
We say that two tiles are adjacent if they share a side or a corner. In the example grid above, the tile with the letter 'A' is adjacent only to the tiles with letters 'B', 'N', and 'O'. A tile is not adjacent to itself.
A sequence of tiles is called a path if each tile in the sequence is adjacent to the tile which follows it (except for the last tile in the sequence, which of course has no successor). In this example, "ABC" is a path, and so is "KXWIHIJK". "MAB" is not a path because 'M' is not adjacent to 'A'. A single tile can be used more than once by a path (though the tile cannot occupy two consecutive places in the path because no tile is adjacent to itself).
You’re given a string s which consists of 27 upper-case English letters. Each English letter occurs at least once in s. Find a grid that contains a path whose tiles, viewed in the order that the path visits them, form the string s. If there’s no solution, print "Impossible" (without the quotes).
Input
The only line of the input contains the string s, consisting of 27 upper-case English letters. Each English letter occurs at least once in s.
Output
Output two lines, each consisting of 13 upper-case English characters, representing the rows of the grid. If there are multiple solutions, print any of them. If there is no solution print "Impossible".
Examples
Input
ABCDEFGHIJKLMNOPQRSGTUVWXYZ
Output
YXWVUTGHIJKLM
ZABCDEFSRQPON
Input
BUVTYZFQSNRIWOXXGJLKACPEMDH
Output
Impossible | instruction | 0 | 58,520 | 6 | 117,040 |
Tags: brute force, constructive algorithms, implementation, strings
Correct Solution:
```
__author__ = 'Think'
word=input()
found=False
for i in range(2, 27):
if word[i] in word[:i-1]:
place2=i
place1=word[:i-1].index(word[i])
found=True
break
if not found:
print("Impossible")
else:
rows=[[""]*13, [""]*13]
start=(place1+place2+1)//2
pos=0
skip=0
while pos<26:
if (start+pos+skip)%27==place2:
skip=1
else:
point=(start+pos+skip)%27
if pos<13:
rows[0][pos]=word[point]
else:
rows[1][25-pos]=word[point]
pos+=1
for i in rows[0]:
print(i, end="")
print()
for j in rows[1]:
print(j, end="")
``` | output | 1 | 58,520 | 6 | 117,041 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Let’s define a grid to be a set of tiles with 2 rows and 13 columns. Each tile has an English letter written in it. The letters don't have to be unique: there might be two or more tiles with the same letter written on them. Here is an example of a grid:
ABCDEFGHIJKLM
NOPQRSTUVWXYZ
We say that two tiles are adjacent if they share a side or a corner. In the example grid above, the tile with the letter 'A' is adjacent only to the tiles with letters 'B', 'N', and 'O'. A tile is not adjacent to itself.
A sequence of tiles is called a path if each tile in the sequence is adjacent to the tile which follows it (except for the last tile in the sequence, which of course has no successor). In this example, "ABC" is a path, and so is "KXWIHIJK". "MAB" is not a path because 'M' is not adjacent to 'A'. A single tile can be used more than once by a path (though the tile cannot occupy two consecutive places in the path because no tile is adjacent to itself).
You’re given a string s which consists of 27 upper-case English letters. Each English letter occurs at least once in s. Find a grid that contains a path whose tiles, viewed in the order that the path visits them, form the string s. If there’s no solution, print "Impossible" (without the quotes).
Input
The only line of the input contains the string s, consisting of 27 upper-case English letters. Each English letter occurs at least once in s.
Output
Output two lines, each consisting of 13 upper-case English characters, representing the rows of the grid. If there are multiple solutions, print any of them. If there is no solution print "Impossible".
Examples
Input
ABCDEFGHIJKLMNOPQRSGTUVWXYZ
Output
YXWVUTGHIJKLM
ZABCDEFSRQPON
Input
BUVTYZFQSNRIWOXXGJLKACPEMDH
Output
Impossible | instruction | 0 | 58,521 | 6 | 117,042 |
Tags: brute force, constructive algorithms, implementation, strings
Correct Solution:
```
s = input()
from collections import Counter
cnt = Counter(s)
sc = sorted(cnt.items(), key=(lambda x: x[1]), reverse=True)[0][0]
s = s[s.index(sc):] + s[:s.index(sc)]
if s[0] == s[1]:
print('Impossible')
exit(0)
sl, sr = s[:1+s[1:].index(sc)], s[s[1:].index(sc)+2:]
slu = sl[:(len(sl)+1)//2][::-1]
sru = sr[:13-len(slu)]
sld = sl[len(slu):]
srd = sr[len(sru):][::-1]
print(slu+sru)
print(sld+srd)
``` | output | 1 | 58,521 | 6 | 117,043 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Let’s define a grid to be a set of tiles with 2 rows and 13 columns. Each tile has an English letter written in it. The letters don't have to be unique: there might be two or more tiles with the same letter written on them. Here is an example of a grid:
ABCDEFGHIJKLM
NOPQRSTUVWXYZ
We say that two tiles are adjacent if they share a side or a corner. In the example grid above, the tile with the letter 'A' is adjacent only to the tiles with letters 'B', 'N', and 'O'. A tile is not adjacent to itself.
A sequence of tiles is called a path if each tile in the sequence is adjacent to the tile which follows it (except for the last tile in the sequence, which of course has no successor). In this example, "ABC" is a path, and so is "KXWIHIJK". "MAB" is not a path because 'M' is not adjacent to 'A'. A single tile can be used more than once by a path (though the tile cannot occupy two consecutive places in the path because no tile is adjacent to itself).
You’re given a string s which consists of 27 upper-case English letters. Each English letter occurs at least once in s. Find a grid that contains a path whose tiles, viewed in the order that the path visits them, form the string s. If there’s no solution, print "Impossible" (without the quotes).
Input
The only line of the input contains the string s, consisting of 27 upper-case English letters. Each English letter occurs at least once in s.
Output
Output two lines, each consisting of 13 upper-case English characters, representing the rows of the grid. If there are multiple solutions, print any of them. If there is no solution print "Impossible".
Examples
Input
ABCDEFGHIJKLMNOPQRSGTUVWXYZ
Output
YXWVUTGHIJKLM
ZABCDEFSRQPON
Input
BUVTYZFQSNRIWOXXGJLKACPEMDH
Output
Impossible | instruction | 0 | 58,522 | 6 | 117,044 |
Tags: brute force, constructive algorithms, implementation, strings
Correct Solution:
```
d = {} # chars to ints
di = {} # ints to chars
ct = 0
x = input()
bad = []
for i in x:
if (i not in d):
ct+=1
d[i] = ct
di[ct] = i
else:
bad = [ct, d[i]]
if (bad[0] == bad[1]):
print("Impossible")
quit()
def gen(x):
bot = []
for i in range(x+13, x, -1):
bot.append(i)
top = []
for i in range(x+14, 27): top.append(i)
for i in range(1, x+1): top.append(i)
return [top, bot]
def gen2(x):
top = []
bot = []
for i in range(x-1):
top.append(2*i+1)
bot.append(2*i+2)
for i in range(2*x-1, x+13):
top.append(i)
bot.append(2*x+25-i)
return [top, bot]
for i in range(1, 14):
if (bad[0]+bad[1] == 2*i or bad[0]+bad[1] == 2*i+1):
a, b = gen(i)
for j in a:
print(di[j], end = '')
print()
for j in b:
print(di[j], end = '')
for i in range(2, 15):
if bad[0]+bad[1] in [2*i+24, 2*i+25]:
a, b = gen2(i)
for j in a:
print(di[j], end = '')
print()
for j in b:
print(di[j], end = '')
``` | output | 1 | 58,522 | 6 | 117,045 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Let’s define a grid to be a set of tiles with 2 rows and 13 columns. Each tile has an English letter written in it. The letters don't have to be unique: there might be two or more tiles with the same letter written on them. Here is an example of a grid:
ABCDEFGHIJKLM
NOPQRSTUVWXYZ
We say that two tiles are adjacent if they share a side or a corner. In the example grid above, the tile with the letter 'A' is adjacent only to the tiles with letters 'B', 'N', and 'O'. A tile is not adjacent to itself.
A sequence of tiles is called a path if each tile in the sequence is adjacent to the tile which follows it (except for the last tile in the sequence, which of course has no successor). In this example, "ABC" is a path, and so is "KXWIHIJK". "MAB" is not a path because 'M' is not adjacent to 'A'. A single tile can be used more than once by a path (though the tile cannot occupy two consecutive places in the path because no tile is adjacent to itself).
You’re given a string s which consists of 27 upper-case English letters. Each English letter occurs at least once in s. Find a grid that contains a path whose tiles, viewed in the order that the path visits them, form the string s. If there’s no solution, print "Impossible" (without the quotes).
Input
The only line of the input contains the string s, consisting of 27 upper-case English letters. Each English letter occurs at least once in s.
Output
Output two lines, each consisting of 13 upper-case English characters, representing the rows of the grid. If there are multiple solutions, print any of them. If there is no solution print "Impossible".
Examples
Input
ABCDEFGHIJKLMNOPQRSGTUVWXYZ
Output
YXWVUTGHIJKLM
ZABCDEFSRQPON
Input
BUVTYZFQSNRIWOXXGJLKACPEMDH
Output
Impossible | instruction | 0 | 58,523 | 6 | 117,046 |
Tags: brute force, constructive algorithms, implementation, strings
Correct Solution:
```
s = input()
l, r = -1, -1
for c in 'QWERTYUIOPLKJHGFDSAZXCVBNM':
if s.count(c) == 2:
l, r = s.find(c), s.rfind(c)
break
if r - l < 2:
print('Impossible')
else:
res = [['_'] * 13 for x in range(2)]
i, j = 0, -1 - (r - l - 1) // 2
s = s[l:r] + s[r + 1:] + s[:l]
for c in s:
res[i][j] = c
if (i == 0 and j == -1) or (i == 1 and j == -13):
i ^= 1
else:
j += 1 if i == 0 else -1
for line in res:
print(*line, sep='')
``` | output | 1 | 58,523 | 6 | 117,047 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Let’s define a grid to be a set of tiles with 2 rows and 13 columns. Each tile has an English letter written in it. The letters don't have to be unique: there might be two or more tiles with the same letter written on them. Here is an example of a grid:
ABCDEFGHIJKLM
NOPQRSTUVWXYZ
We say that two tiles are adjacent if they share a side or a corner. In the example grid above, the tile with the letter 'A' is adjacent only to the tiles with letters 'B', 'N', and 'O'. A tile is not adjacent to itself.
A sequence of tiles is called a path if each tile in the sequence is adjacent to the tile which follows it (except for the last tile in the sequence, which of course has no successor). In this example, "ABC" is a path, and so is "KXWIHIJK". "MAB" is not a path because 'M' is not adjacent to 'A'. A single tile can be used more than once by a path (though the tile cannot occupy two consecutive places in the path because no tile is adjacent to itself).
You’re given a string s which consists of 27 upper-case English letters. Each English letter occurs at least once in s. Find a grid that contains a path whose tiles, viewed in the order that the path visits them, form the string s. If there’s no solution, print "Impossible" (without the quotes).
Input
The only line of the input contains the string s, consisting of 27 upper-case English letters. Each English letter occurs at least once in s.
Output
Output two lines, each consisting of 13 upper-case English characters, representing the rows of the grid. If there are multiple solutions, print any of them. If there is no solution print "Impossible".
Examples
Input
ABCDEFGHIJKLMNOPQRSGTUVWXYZ
Output
YXWVUTGHIJKLM
ZABCDEFSRQPON
Input
BUVTYZFQSNRIWOXXGJLKACPEMDH
Output
Impossible | instruction | 0 | 58,524 | 6 | 117,048 |
Tags: brute force, constructive algorithms, implementation, strings
Correct Solution:
```
s1=["" for j in range(13)]
s2=["" for j in range(13)]
s=input()
f=0
for i in range(27):
if f==0 and s.count(s[i])==2:
f=1
sp1=i
elif f==1 and s[i]==s[sp1]:
sp2=i
break
if sp2-sp1==1:
print("Impossible")
else:
tp=12-(sp2-sp1-1)//2
for i in range(27):
if i<=sp1:
if sp1-i<=tp:
s1[tp-sp1+i]=s[i]
elif sp1-i-tp-1<=12:
s2[sp1-i-tp-1]=s[i]
else:
s1[25-(sp1-i-tp-1)]=s[i]
else:
if i==sp2:
continue
elif i<sp2:
j=i
else:
j=i-1
if tp+(j-sp1)<=12:
s1[tp+(j-sp1)]=s[i]
elif 25-(tp+(j-sp1))<=12 and 25-(tp+(j-sp1))>=0:
s2[25-(tp+(j-sp1))]=s[i]
else:
s1[abs(26-(tp+(j-sp1)))]=s[i]
print("".join(s1))
print("".join(s2))
``` | output | 1 | 58,524 | 6 | 117,049 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Oleg the client and Igor the analyst are good friends. However, sometimes they argue over little things. Recently, they started a new company, but they are having trouble finding a name for the company.
To settle this problem, they've decided to play a game. The company name will consist of n letters. Oleg and Igor each have a set of n letters (which might contain multiple copies of the same letter, the sets can be different). Initially, the company name is denoted by n question marks. Oleg and Igor takes turns to play the game, Oleg moves first. In each turn, a player can choose one of the letters c in his set and replace any of the question marks with c. Then, a copy of the letter c is removed from his set. The game ends when all the question marks has been replaced by some letter.
For example, suppose Oleg has the set of letters {i, o, i} and Igor has the set of letters {i, m, o}. One possible game is as follows :
Initially, the company name is ???.
Oleg replaces the second question mark with 'i'. The company name becomes ?i?. The set of letters Oleg have now is {i, o}.
Igor replaces the third question mark with 'o'. The company name becomes ?io. The set of letters Igor have now is {i, m}.
Finally, Oleg replaces the first question mark with 'o'. The company name becomes oio. The set of letters Oleg have now is {i}.
In the end, the company name is oio.
Oleg wants the company name to be as lexicographically small as possible while Igor wants the company name to be as lexicographically large as possible. What will be the company name if Oleg and Igor always play optimally?
A string s = s1s2...sm is called lexicographically smaller than a string t = t1t2...tm (where s ≠ t) if si < ti where i is the smallest index such that si ≠ ti. (so sj = tj for all j < i)
Input
The first line of input contains a string s of length n (1 ≤ n ≤ 3·105). All characters of the string are lowercase English letters. This string denotes the set of letters Oleg has initially.
The second line of input contains a string t of length n. All characters of the string are lowercase English letters. This string denotes the set of letters Igor has initially.
Output
The output should contain a string of n lowercase English letters, denoting the company name if Oleg and Igor plays optimally.
Examples
Input
tinkoff
zscoder
Output
fzfsirk
Input
xxxxxx
xxxxxx
Output
xxxxxx
Input
ioi
imo
Output
ioi
Note
One way to play optimally in the first sample is as follows :
* Initially, the company name is ???????.
* Oleg replaces the first question mark with 'f'. The company name becomes f??????.
* Igor replaces the second question mark with 'z'. The company name becomes fz?????.
* Oleg replaces the third question mark with 'f'. The company name becomes fzf????.
* Igor replaces the fourth question mark with 's'. The company name becomes fzfs???.
* Oleg replaces the fifth question mark with 'i'. The company name becomes fzfsi??.
* Igor replaces the sixth question mark with 'r'. The company name becomes fzfsir?.
* Oleg replaces the seventh question mark with 'k'. The company name becomes fzfsirk.
For the second sample, no matter how they play, the company name will always be xxxxxx. | instruction | 0 | 58,553 | 6 | 117,106 |
Tags: games, greedy, sortings
Correct Solution:
```
s = list(map(str,input()))
c = list(map(str,input()))
n = len(s)
s.sort()
c.sort(reverse = True)
s = s[:(n+1)//2]
c = c[:n//2]
l_s = len(s)
l_c = len(c)
begin_s = 0
begin_c = 0
end_s = l_s-1
end_c = l_c-1
ans1 = []
ans2 = []
for i in range(n-1):
if i%2 == 0:
if s[begin_s] >= c[begin_c]:
ans2.append(s[end_s])
end_s -= 1
else:
ans1.append(s[begin_s])
begin_s += 1
else:
if c[begin_c] <= s[begin_s]:
ans2.append(c[end_c])
end_c -= 1
else:
ans1.append(c[begin_c])
begin_c += 1
if n%2:
ans1.append(s[begin_s])
else:
ans1.append(c[begin_c])
print(*ans1+list(reversed(ans2)),sep='')
``` | output | 1 | 58,553 | 6 | 117,107 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Oleg the client and Igor the analyst are good friends. However, sometimes they argue over little things. Recently, they started a new company, but they are having trouble finding a name for the company.
To settle this problem, they've decided to play a game. The company name will consist of n letters. Oleg and Igor each have a set of n letters (which might contain multiple copies of the same letter, the sets can be different). Initially, the company name is denoted by n question marks. Oleg and Igor takes turns to play the game, Oleg moves first. In each turn, a player can choose one of the letters c in his set and replace any of the question marks with c. Then, a copy of the letter c is removed from his set. The game ends when all the question marks has been replaced by some letter.
For example, suppose Oleg has the set of letters {i, o, i} and Igor has the set of letters {i, m, o}. One possible game is as follows :
Initially, the company name is ???.
Oleg replaces the second question mark with 'i'. The company name becomes ?i?. The set of letters Oleg have now is {i, o}.
Igor replaces the third question mark with 'o'. The company name becomes ?io. The set of letters Igor have now is {i, m}.
Finally, Oleg replaces the first question mark with 'o'. The company name becomes oio. The set of letters Oleg have now is {i}.
In the end, the company name is oio.
Oleg wants the company name to be as lexicographically small as possible while Igor wants the company name to be as lexicographically large as possible. What will be the company name if Oleg and Igor always play optimally?
A string s = s1s2...sm is called lexicographically smaller than a string t = t1t2...tm (where s ≠ t) if si < ti where i is the smallest index such that si ≠ ti. (so sj = tj for all j < i)
Input
The first line of input contains a string s of length n (1 ≤ n ≤ 3·105). All characters of the string are lowercase English letters. This string denotes the set of letters Oleg has initially.
The second line of input contains a string t of length n. All characters of the string are lowercase English letters. This string denotes the set of letters Igor has initially.
Output
The output should contain a string of n lowercase English letters, denoting the company name if Oleg and Igor plays optimally.
Examples
Input
tinkoff
zscoder
Output
fzfsirk
Input
xxxxxx
xxxxxx
Output
xxxxxx
Input
ioi
imo
Output
ioi
Note
One way to play optimally in the first sample is as follows :
* Initially, the company name is ???????.
* Oleg replaces the first question mark with 'f'. The company name becomes f??????.
* Igor replaces the second question mark with 'z'. The company name becomes fz?????.
* Oleg replaces the third question mark with 'f'. The company name becomes fzf????.
* Igor replaces the fourth question mark with 's'. The company name becomes fzfs???.
* Oleg replaces the fifth question mark with 'i'. The company name becomes fzfsi??.
* Igor replaces the sixth question mark with 'r'. The company name becomes fzfsir?.
* Oleg replaces the seventh question mark with 'k'. The company name becomes fzfsirk.
For the second sample, no matter how they play, the company name will always be xxxxxx. | instruction | 0 | 58,554 | 6 | 117,108 |
Tags: games, greedy, sortings
Correct Solution:
```
import sys
def main():
s=sys.stdin.readline().rstrip()
t=sys.stdin.readline().rstrip()
result=[]
iisl=0
iisr=len(s)//2+len(s)%2-1
iitl=0
iitr=len(s)//2-1
aas=list(sorted(list(s)))
aat=list(sorted(list(t),reverse=True))
rl=0
rr=len(s)-1
result=['?']*len(s)
for i in range(len(s)):
if i%2==0:
if aas[iisl]<aat[iitl]:
result[rl]=aas[iisl]
iisl+=1
rl+=1
else:
result[rr]=aas[iisr]
iisr-=1
rr-=1
else:
if aat[iitl]>aas[iisl]:
result[rl]=aat[iitl]
iitl+=1
rl+=1
else:
result[rr]=aat[iitr]
iitr-=1
rr-=1
sys.stdout.write(''.join(result)+'\n')
main()
``` | output | 1 | 58,554 | 6 | 117,109 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Oleg the client and Igor the analyst are good friends. However, sometimes they argue over little things. Recently, they started a new company, but they are having trouble finding a name for the company.
To settle this problem, they've decided to play a game. The company name will consist of n letters. Oleg and Igor each have a set of n letters (which might contain multiple copies of the same letter, the sets can be different). Initially, the company name is denoted by n question marks. Oleg and Igor takes turns to play the game, Oleg moves first. In each turn, a player can choose one of the letters c in his set and replace any of the question marks with c. Then, a copy of the letter c is removed from his set. The game ends when all the question marks has been replaced by some letter.
For example, suppose Oleg has the set of letters {i, o, i} and Igor has the set of letters {i, m, o}. One possible game is as follows :
Initially, the company name is ???.
Oleg replaces the second question mark with 'i'. The company name becomes ?i?. The set of letters Oleg have now is {i, o}.
Igor replaces the third question mark with 'o'. The company name becomes ?io. The set of letters Igor have now is {i, m}.
Finally, Oleg replaces the first question mark with 'o'. The company name becomes oio. The set of letters Oleg have now is {i}.
In the end, the company name is oio.
Oleg wants the company name to be as lexicographically small as possible while Igor wants the company name to be as lexicographically large as possible. What will be the company name if Oleg and Igor always play optimally?
A string s = s1s2...sm is called lexicographically smaller than a string t = t1t2...tm (where s ≠ t) if si < ti where i is the smallest index such that si ≠ ti. (so sj = tj for all j < i)
Input
The first line of input contains a string s of length n (1 ≤ n ≤ 3·105). All characters of the string are lowercase English letters. This string denotes the set of letters Oleg has initially.
The second line of input contains a string t of length n. All characters of the string are lowercase English letters. This string denotes the set of letters Igor has initially.
Output
The output should contain a string of n lowercase English letters, denoting the company name if Oleg and Igor plays optimally.
Examples
Input
tinkoff
zscoder
Output
fzfsirk
Input
xxxxxx
xxxxxx
Output
xxxxxx
Input
ioi
imo
Output
ioi
Note
One way to play optimally in the first sample is as follows :
* Initially, the company name is ???????.
* Oleg replaces the first question mark with 'f'. The company name becomes f??????.
* Igor replaces the second question mark with 'z'. The company name becomes fz?????.
* Oleg replaces the third question mark with 'f'. The company name becomes fzf????.
* Igor replaces the fourth question mark with 's'. The company name becomes fzfs???.
* Oleg replaces the fifth question mark with 'i'. The company name becomes fzfsi??.
* Igor replaces the sixth question mark with 'r'. The company name becomes fzfsir?.
* Oleg replaces the seventh question mark with 'k'. The company name becomes fzfsirk.
For the second sample, no matter how they play, the company name will always be xxxxxx. | instruction | 0 | 58,555 | 6 | 117,110 |
Tags: games, greedy, sortings
Correct Solution:
```
# Author : raj1307 - Raj Singh
# Date : 18.03.2021
from __future__ import division, print_function
import os,sys
from io import BytesIO, IOBase
if sys.version_info[0] < 3:
from __builtin__ import xrange as range
from future_builtins import ascii, filter, hex, map, oct, zip
def ii(): return int(input())
def si(): return input()
def mi(): return map(int,input().strip().split(" "))
def msi(): return map(str,input().strip().split(" "))
def li(): return list(mi())
def dmain():
sys.setrecursionlimit(1000000)
threading.stack_size(1024000)
thread = threading.Thread(target=main)
thread.start()
#from collections import deque, Counter, OrderedDict,defaultdict
#from heapq import nsmallest, nlargest, heapify,heappop ,heappush, heapreplace
#from math import log,sqrt,factorial,cos,tan,sin,radians
#from bisect import bisect,bisect_left,bisect_right,insort,insort_left,insort_right
#from decimal import *
#import threading
#from itertools import permutations
#Copy 2D list m = [x[:] for x in mark] .. Avoid Using Deepcopy
abc='abcdefghijklmnopqrstuvwxyz'
abd={'a': 0, 'b': 1, 'c': 2, 'd': 3, 'e': 4, 'f': 5, 'g': 6, 'h': 7, 'i': 8, 'j': 9, 'k': 10, 'l': 11, 'm': 12, 'n': 13, 'o': 14, 'p': 15, 'q': 16, 'r': 17, 's': 18, 't': 19, 'u': 20, 'v': 21, 'w': 22, 'x': 23, 'y': 24, 'z': 25}
mod=1000000007
#mod=998244353
inf = float("inf")
vow=['a','e','i','o','u']
dx,dy=[-1,1,0,0],[0,0,1,-1]
def getKey(item): return item[1]
def sort2(l):return sorted(l, key=getKey,reverse=True)
def d2(n,m,num):return [[num for x in range(m)] for y in range(n)]
def isPowerOfTwo (x): return (x and (not(x & (x - 1))) )
def decimalToBinary(n): return bin(n).replace("0b","")
def ntl(n):return [int(i) for i in str(n)]
def ncr(n,r): return factorial(n)//(factorial(r)*factorial(max(n-r,1)))
def ceil(x,y):
if x%y==0:
return x//y
else:
return x//y+1
def powerMod(x,y,p):
res = 1
x %= p
while y > 0:
if y&1:
res = (res*x)%p
y = y>>1
x = (x*x)%p
return res
def gcd(x, y):
while y:
x, y = y, x % y
return x
def isPrime(n) : # Check Prime Number or not
if (n <= 1) : return False
if (n <= 3) : return True
if (n % 2 == 0 or n % 3 == 0) : return False
i = 5
while(i * i <= n) :
if (n % i == 0 or n % (i + 2) == 0) :
return False
i = i + 6
return True
def read():
sys.stdin = open('input.txt', 'r')
sys.stdout = open('output.txt', 'w')
def main():
#for _ in range(1):
s=list(si())
t=list(si())
a=['']*len(s)
n=len(s)
i1=0
i2=n//2+(n%2)-1
j1=ceil(n,2)
j2=n-1
p=0
q=n-1
s.sort()
t.sort()
turn=0
f=[0]*26
g=[0]*26
for i in range(n):
f[ord(s[i])-ord('a')]+=1
g[ord(t[i])-ord('a')]+=1
cnt=0
while cnt<n:
if turn==0:
flag=1
for k in range(26):
if g[k] and k>(ord(s[i1])-ord('a')):
flag=0
break
if flag==0:
a[p]=s[i1]
p+=1
f[ord(s[i1])-ord('a')]-=1
i1+=1
else:
a[q]=s[i2]
f[ord(s[i2])-ord('a')]-=1
i2-=1
q-=1
else:
flag=1
for k in range(26):
if f[k] and k<(ord(t[j2])-ord('a')):
flag=0
break
if flag==0:
a[p]=t[j2]
p+=1
g[ord(t[j2])-ord('a')]-=1
j2-=1
else:
a[q]=t[j1]
g[ord(t[j1])-ord('a')]-=1
j1+=1
q-=1
cnt+=1
turn^=1
for i in a:
print(i,end='')
# region fastio
# template taken from https://github.com/cheran-senthil/PyRival/blob/master/templates/template.py
BUFSIZE = 8192
class FastIO(IOBase):
newlines = 0
def __init__(self, file):
self._fd = file.fileno()
self.buffer = BytesIO()
self.writable = "x" in file.mode or "r" not in file.mode
self.write = self.buffer.write if self.writable else None
def read(self):
while True:
b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
if not b:
break
ptr = self.buffer.tell()
self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr)
self.newlines = 0
return self.buffer.read()
def readline(self):
while self.newlines == 0:
b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
self.newlines = b.count(b"\n") + (not b)
ptr = self.buffer.tell()
self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr)
self.newlines -= 1
return self.buffer.readline()
def flush(self):
if self.writable:
os.write(self._fd, self.buffer.getvalue())
self.buffer.truncate(0), self.buffer.seek(0)
class IOWrapper(IOBase):
def __init__(self, file):
self.buffer = FastIO(file)
self.flush = self.buffer.flush
self.writable = self.buffer.writable
self.write = lambda s: self.buffer.write(s.encode("ascii"))
self.read = lambda: self.buffer.read().decode("ascii")
self.readline = lambda: self.buffer.readline().decode("ascii")
def print(*args, **kwargs):
"""Prints the values to a stream, or to sys.stdout by default."""
sep, file = kwargs.pop("sep", " "), kwargs.pop("file", sys.stdout)
at_start = True
for x in args:
if not at_start:
file.write(sep)
file.write(str(x))
at_start = False
file.write(kwargs.pop("end", "\n"))
if kwargs.pop("flush", False):
file.flush()
if sys.version_info[0] < 3:
sys.stdin, sys.stdout = FastIO(sys.stdin), FastIO(sys.stdout)
else:
sys.stdin, sys.stdout = IOWrapper(sys.stdin), IOWrapper(sys.stdout)
input = lambda: sys.stdin.readline().rstrip("\r\n")
# endregion
if __name__ == "__main__":
#read()
main()
#dmain()
# Comment Read()
``` | output | 1 | 58,555 | 6 | 117,111 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Oleg the client and Igor the analyst are good friends. However, sometimes they argue over little things. Recently, they started a new company, but they are having trouble finding a name for the company.
To settle this problem, they've decided to play a game. The company name will consist of n letters. Oleg and Igor each have a set of n letters (which might contain multiple copies of the same letter, the sets can be different). Initially, the company name is denoted by n question marks. Oleg and Igor takes turns to play the game, Oleg moves first. In each turn, a player can choose one of the letters c in his set and replace any of the question marks with c. Then, a copy of the letter c is removed from his set. The game ends when all the question marks has been replaced by some letter.
For example, suppose Oleg has the set of letters {i, o, i} and Igor has the set of letters {i, m, o}. One possible game is as follows :
Initially, the company name is ???.
Oleg replaces the second question mark with 'i'. The company name becomes ?i?. The set of letters Oleg have now is {i, o}.
Igor replaces the third question mark with 'o'. The company name becomes ?io. The set of letters Igor have now is {i, m}.
Finally, Oleg replaces the first question mark with 'o'. The company name becomes oio. The set of letters Oleg have now is {i}.
In the end, the company name is oio.
Oleg wants the company name to be as lexicographically small as possible while Igor wants the company name to be as lexicographically large as possible. What will be the company name if Oleg and Igor always play optimally?
A string s = s1s2...sm is called lexicographically smaller than a string t = t1t2...tm (where s ≠ t) if si < ti where i is the smallest index such that si ≠ ti. (so sj = tj for all j < i)
Input
The first line of input contains a string s of length n (1 ≤ n ≤ 3·105). All characters of the string are lowercase English letters. This string denotes the set of letters Oleg has initially.
The second line of input contains a string t of length n. All characters of the string are lowercase English letters. This string denotes the set of letters Igor has initially.
Output
The output should contain a string of n lowercase English letters, denoting the company name if Oleg and Igor plays optimally.
Examples
Input
tinkoff
zscoder
Output
fzfsirk
Input
xxxxxx
xxxxxx
Output
xxxxxx
Input
ioi
imo
Output
ioi
Note
One way to play optimally in the first sample is as follows :
* Initially, the company name is ???????.
* Oleg replaces the first question mark with 'f'. The company name becomes f??????.
* Igor replaces the second question mark with 'z'. The company name becomes fz?????.
* Oleg replaces the third question mark with 'f'. The company name becomes fzf????.
* Igor replaces the fourth question mark with 's'. The company name becomes fzfs???.
* Oleg replaces the fifth question mark with 'i'. The company name becomes fzfsi??.
* Igor replaces the sixth question mark with 'r'. The company name becomes fzfsir?.
* Oleg replaces the seventh question mark with 'k'. The company name becomes fzfsirk.
For the second sample, no matter how they play, the company name will always be xxxxxx. | instruction | 0 | 58,556 | 6 | 117,112 |
Tags: games, greedy, sortings
Correct Solution:
```
import sys
import math
from collections import defaultdict,deque
import heapq
oleg=list(sys.stdin.readline()[:-1])
igor=list(sys.stdin.readline()[:-1])
oleg.sort()
igor.sort()
igor.reverse()
n=len(oleg)
ans=[0 for _ in range(n)]
cnto,cnti=n//2+n%2,n//2
indo,indi=0,0
ind=0
oleg=oleg[:cnto]
igor=igor[:cnti]
igor.reverse()
sto,endo=0,len(oleg)-1
sti,endi=0,len(igor)-1
turn =True
indst,indend=0,n-1
'''if n==1:
print(min(oleg))
sys.exit()
if n==2:
if min(oleg)<max(igor):
print(oleg[0]+igor[-1])
else:
print(igor[-1]+oleg[0])
sys.exit()'''
#print(sto,'sto',endo,'endo',sti,'sti',endi,'endi')
#print(oleg,'oleg')
#print(igor,'igor')
while indst<=indend and (sto<=endo) and (sti<=endi):
#print(sto,'sto',endo,'endo',sti,'sti',endi,'endi')
if turn :
if oleg[sto]<igor[endi]:
ans[indst]=oleg[sto]
sto+=1
indst+=1
else:
ans[indend]=oleg[endo]
endo-=1
indend-=1
turn =False
else:
if igor[endi]>oleg[sto]:
ans[indst]=igor[endi]
endi-=1
indst+=1
else:
ans[indend]=igor[sti]
sti+=1
indend-=1
turn =True
if sto<=endo and indst<=indend:
ans[indst]=oleg[sto]
indst+=1
if sti<=endi and indst<=indend:
ans[indst]=igor[sti]
#print(ans,'as')
#print(indst,'indst',indend,'indend')
print(''.join(x for x in ans))
``` | output | 1 | 58,556 | 6 | 117,113 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Oleg the client and Igor the analyst are good friends. However, sometimes they argue over little things. Recently, they started a new company, but they are having trouble finding a name for the company.
To settle this problem, they've decided to play a game. The company name will consist of n letters. Oleg and Igor each have a set of n letters (which might contain multiple copies of the same letter, the sets can be different). Initially, the company name is denoted by n question marks. Oleg and Igor takes turns to play the game, Oleg moves first. In each turn, a player can choose one of the letters c in his set and replace any of the question marks with c. Then, a copy of the letter c is removed from his set. The game ends when all the question marks has been replaced by some letter.
For example, suppose Oleg has the set of letters {i, o, i} and Igor has the set of letters {i, m, o}. One possible game is as follows :
Initially, the company name is ???.
Oleg replaces the second question mark with 'i'. The company name becomes ?i?. The set of letters Oleg have now is {i, o}.
Igor replaces the third question mark with 'o'. The company name becomes ?io. The set of letters Igor have now is {i, m}.
Finally, Oleg replaces the first question mark with 'o'. The company name becomes oio. The set of letters Oleg have now is {i}.
In the end, the company name is oio.
Oleg wants the company name to be as lexicographically small as possible while Igor wants the company name to be as lexicographically large as possible. What will be the company name if Oleg and Igor always play optimally?
A string s = s1s2...sm is called lexicographically smaller than a string t = t1t2...tm (where s ≠ t) if si < ti where i is the smallest index such that si ≠ ti. (so sj = tj for all j < i)
Input
The first line of input contains a string s of length n (1 ≤ n ≤ 3·105). All characters of the string are lowercase English letters. This string denotes the set of letters Oleg has initially.
The second line of input contains a string t of length n. All characters of the string are lowercase English letters. This string denotes the set of letters Igor has initially.
Output
The output should contain a string of n lowercase English letters, denoting the company name if Oleg and Igor plays optimally.
Examples
Input
tinkoff
zscoder
Output
fzfsirk
Input
xxxxxx
xxxxxx
Output
xxxxxx
Input
ioi
imo
Output
ioi
Note
One way to play optimally in the first sample is as follows :
* Initially, the company name is ???????.
* Oleg replaces the first question mark with 'f'. The company name becomes f??????.
* Igor replaces the second question mark with 'z'. The company name becomes fz?????.
* Oleg replaces the third question mark with 'f'. The company name becomes fzf????.
* Igor replaces the fourth question mark with 's'. The company name becomes fzfs???.
* Oleg replaces the fifth question mark with 'i'. The company name becomes fzfsi??.
* Igor replaces the sixth question mark with 'r'. The company name becomes fzfsir?.
* Oleg replaces the seventh question mark with 'k'. The company name becomes fzfsirk.
For the second sample, no matter how they play, the company name will always be xxxxxx. | instruction | 0 | 58,557 | 6 | 117,114 |
Tags: games, greedy, sortings
Correct Solution:
```
a = sorted(input())
b = sorted(input(), reverse=True)
n = len(a)
a = ''.join(a[:(n+1)//2])
b = ''.join(b[:n//2])
name = ['']*(len(a)+len(b))
ia = ib = ic = 0
ja = len(a)-1
jb = len(b)-1
jc = len(name)-1
turn = 1
while ic <= jc:
if turn == 1:
if ib > jb: name[ic] = a[ia]; ic+= 1
elif a[ia] < b[ib]: name[ic] = a[ia]; ia+= 1; ic+= 1
else: name[jc] = a[ja]; ja-= 1; jc-= 1
else:
if ia > ja: name[ic] = b[ib]; ic+= 1
elif b[ib] > a[ia]: name[ic] = b[ib]; ib+= 1; ic+= 1
else: name[jc] = b[jb]; jb-= 1; jc-= 1
turn = 3-turn
print(''.join(name))
``` | output | 1 | 58,557 | 6 | 117,115 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Oleg the client and Igor the analyst are good friends. However, sometimes they argue over little things. Recently, they started a new company, but they are having trouble finding a name for the company.
To settle this problem, they've decided to play a game. The company name will consist of n letters. Oleg and Igor each have a set of n letters (which might contain multiple copies of the same letter, the sets can be different). Initially, the company name is denoted by n question marks. Oleg and Igor takes turns to play the game, Oleg moves first. In each turn, a player can choose one of the letters c in his set and replace any of the question marks with c. Then, a copy of the letter c is removed from his set. The game ends when all the question marks has been replaced by some letter.
For example, suppose Oleg has the set of letters {i, o, i} and Igor has the set of letters {i, m, o}. One possible game is as follows :
Initially, the company name is ???.
Oleg replaces the second question mark with 'i'. The company name becomes ?i?. The set of letters Oleg have now is {i, o}.
Igor replaces the third question mark with 'o'. The company name becomes ?io. The set of letters Igor have now is {i, m}.
Finally, Oleg replaces the first question mark with 'o'. The company name becomes oio. The set of letters Oleg have now is {i}.
In the end, the company name is oio.
Oleg wants the company name to be as lexicographically small as possible while Igor wants the company name to be as lexicographically large as possible. What will be the company name if Oleg and Igor always play optimally?
A string s = s1s2...sm is called lexicographically smaller than a string t = t1t2...tm (where s ≠ t) if si < ti where i is the smallest index such that si ≠ ti. (so sj = tj for all j < i)
Input
The first line of input contains a string s of length n (1 ≤ n ≤ 3·105). All characters of the string are lowercase English letters. This string denotes the set of letters Oleg has initially.
The second line of input contains a string t of length n. All characters of the string are lowercase English letters. This string denotes the set of letters Igor has initially.
Output
The output should contain a string of n lowercase English letters, denoting the company name if Oleg and Igor plays optimally.
Examples
Input
tinkoff
zscoder
Output
fzfsirk
Input
xxxxxx
xxxxxx
Output
xxxxxx
Input
ioi
imo
Output
ioi
Note
One way to play optimally in the first sample is as follows :
* Initially, the company name is ???????.
* Oleg replaces the first question mark with 'f'. The company name becomes f??????.
* Igor replaces the second question mark with 'z'. The company name becomes fz?????.
* Oleg replaces the third question mark with 'f'. The company name becomes fzf????.
* Igor replaces the fourth question mark with 's'. The company name becomes fzfs???.
* Oleg replaces the fifth question mark with 'i'. The company name becomes fzfsi??.
* Igor replaces the sixth question mark with 'r'. The company name becomes fzfsir?.
* Oleg replaces the seventh question mark with 'k'. The company name becomes fzfsirk.
For the second sample, no matter how they play, the company name will always be xxxxxx. | instruction | 0 | 58,558 | 6 | 117,116 |
Tags: games, greedy, sortings
Correct Solution:
```
s1 = input()
s2 = input()
n = len(s1)
s1 = sorted(s1)
s2 = sorted(s2)[::-1]
i = 0
j = 0
res = ["?"]*n
rear = n-1
front = 0
Neven = n % 2 == 0
n1 = (n+1)//2 - 1
n2 = n//2 - 1
for k in range(n):
if k % 2 == 0:
if s1[i] < s2[j]:
res[front] = s1[i]
front += 1
i += 1
else:
res[rear] = s1[n1]
rear -= 1
n1 -= 1
else:
if s1[i] < s2[j]:
res[front] = s2[j]
front += 1
j += 1
else:
res[rear] = s2[n2]
rear -= 1
n2 -= 1
print("".join(res))
``` | output | 1 | 58,558 | 6 | 117,117 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Oleg the client and Igor the analyst are good friends. However, sometimes they argue over little things. Recently, they started a new company, but they are having trouble finding a name for the company.
To settle this problem, they've decided to play a game. The company name will consist of n letters. Oleg and Igor each have a set of n letters (which might contain multiple copies of the same letter, the sets can be different). Initially, the company name is denoted by n question marks. Oleg and Igor takes turns to play the game, Oleg moves first. In each turn, a player can choose one of the letters c in his set and replace any of the question marks with c. Then, a copy of the letter c is removed from his set. The game ends when all the question marks has been replaced by some letter.
For example, suppose Oleg has the set of letters {i, o, i} and Igor has the set of letters {i, m, o}. One possible game is as follows :
Initially, the company name is ???.
Oleg replaces the second question mark with 'i'. The company name becomes ?i?. The set of letters Oleg have now is {i, o}.
Igor replaces the third question mark with 'o'. The company name becomes ?io. The set of letters Igor have now is {i, m}.
Finally, Oleg replaces the first question mark with 'o'. The company name becomes oio. The set of letters Oleg have now is {i}.
In the end, the company name is oio.
Oleg wants the company name to be as lexicographically small as possible while Igor wants the company name to be as lexicographically large as possible. What will be the company name if Oleg and Igor always play optimally?
A string s = s1s2...sm is called lexicographically smaller than a string t = t1t2...tm (where s ≠ t) if si < ti where i is the smallest index such that si ≠ ti. (so sj = tj for all j < i)
Input
The first line of input contains a string s of length n (1 ≤ n ≤ 3·105). All characters of the string are lowercase English letters. This string denotes the set of letters Oleg has initially.
The second line of input contains a string t of length n. All characters of the string are lowercase English letters. This string denotes the set of letters Igor has initially.
Output
The output should contain a string of n lowercase English letters, denoting the company name if Oleg and Igor plays optimally.
Examples
Input
tinkoff
zscoder
Output
fzfsirk
Input
xxxxxx
xxxxxx
Output
xxxxxx
Input
ioi
imo
Output
ioi
Note
One way to play optimally in the first sample is as follows :
* Initially, the company name is ???????.
* Oleg replaces the first question mark with 'f'. The company name becomes f??????.
* Igor replaces the second question mark with 'z'. The company name becomes fz?????.
* Oleg replaces the third question mark with 'f'. The company name becomes fzf????.
* Igor replaces the fourth question mark with 's'. The company name becomes fzfs???.
* Oleg replaces the fifth question mark with 'i'. The company name becomes fzfsi??.
* Igor replaces the sixth question mark with 'r'. The company name becomes fzfsir?.
* Oleg replaces the seventh question mark with 'k'. The company name becomes fzfsirk.
For the second sample, no matter how they play, the company name will always be xxxxxx. | instruction | 0 | 58,559 | 6 | 117,118 |
Tags: games, greedy, sortings
Correct Solution:
```
s=input()
s1=input()
n=len(s1)
sl=[]
sl1=[]
for i in range(n):
sl+=[s[i]]
sl1+=[s1[i]]
ans=sl1[::]
sl1.sort()
sl.sort()
sl1.reverse()
i=0;
i2=(n-1)//2
k=0
k2=(n//2)-1
j=len(s)-1
temp=0
for x in range(len(s1)):
if (x%2==0):
if (sl[i]<sl1[k]) :
ans[temp]=sl[i]
temp+=1
i+=1
elif (sl[i]>=sl1[k]) :
ans[j]=sl[i2]
i2-=1
j-=1
if (x%2==1) :
if (sl[i]<sl1[k]) :
ans[temp]=sl1[k]
temp+=1
k+=1
elif (sl[i]>=sl1[k]) :
ans[j]=sl1[k2]
j-=1
k2-=1
print(''.join(ans))
``` | output | 1 | 58,559 | 6 | 117,119 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Oleg the client and Igor the analyst are good friends. However, sometimes they argue over little things. Recently, they started a new company, but they are having trouble finding a name for the company.
To settle this problem, they've decided to play a game. The company name will consist of n letters. Oleg and Igor each have a set of n letters (which might contain multiple copies of the same letter, the sets can be different). Initially, the company name is denoted by n question marks. Oleg and Igor takes turns to play the game, Oleg moves first. In each turn, a player can choose one of the letters c in his set and replace any of the question marks with c. Then, a copy of the letter c is removed from his set. The game ends when all the question marks has been replaced by some letter.
For example, suppose Oleg has the set of letters {i, o, i} and Igor has the set of letters {i, m, o}. One possible game is as follows :
Initially, the company name is ???.
Oleg replaces the second question mark with 'i'. The company name becomes ?i?. The set of letters Oleg have now is {i, o}.
Igor replaces the third question mark with 'o'. The company name becomes ?io. The set of letters Igor have now is {i, m}.
Finally, Oleg replaces the first question mark with 'o'. The company name becomes oio. The set of letters Oleg have now is {i}.
In the end, the company name is oio.
Oleg wants the company name to be as lexicographically small as possible while Igor wants the company name to be as lexicographically large as possible. What will be the company name if Oleg and Igor always play optimally?
A string s = s1s2...sm is called lexicographically smaller than a string t = t1t2...tm (where s ≠ t) if si < ti where i is the smallest index such that si ≠ ti. (so sj = tj for all j < i)
Input
The first line of input contains a string s of length n (1 ≤ n ≤ 3·105). All characters of the string are lowercase English letters. This string denotes the set of letters Oleg has initially.
The second line of input contains a string t of length n. All characters of the string are lowercase English letters. This string denotes the set of letters Igor has initially.
Output
The output should contain a string of n lowercase English letters, denoting the company name if Oleg and Igor plays optimally.
Examples
Input
tinkoff
zscoder
Output
fzfsirk
Input
xxxxxx
xxxxxx
Output
xxxxxx
Input
ioi
imo
Output
ioi
Note
One way to play optimally in the first sample is as follows :
* Initially, the company name is ???????.
* Oleg replaces the first question mark with 'f'. The company name becomes f??????.
* Igor replaces the second question mark with 'z'. The company name becomes fz?????.
* Oleg replaces the third question mark with 'f'. The company name becomes fzf????.
* Igor replaces the fourth question mark with 's'. The company name becomes fzfs???.
* Oleg replaces the fifth question mark with 'i'. The company name becomes fzfsi??.
* Igor replaces the sixth question mark with 'r'. The company name becomes fzfsir?.
* Oleg replaces the seventh question mark with 'k'. The company name becomes fzfsirk.
For the second sample, no matter how they play, the company name will always be xxxxxx. | instruction | 0 | 58,560 | 6 | 117,120 |
Tags: games, greedy, sortings
Correct Solution:
```
import sys
from math import sqrt
def solve():
s = input() # Oleg's letters
t = input() # Igor's letters
n = len(s)
s = sorted(s)[:(n + 1) // 2]
t = sorted(t, reverse=True)[:n // 2]
'''
print(s)
print(t)
'''
ans = [None] * n
ansl = 0
ansr = n - 1
olgl = 0
olgr = len(s) - 1
igol = 0
igor = len(t) - 1
for i in range(n):
if i % 2 == 0:
if igol > igor or s[olgl] < t[igol]:
ans[ansl] = s[olgl]
ansl += 1
olgl += 1
else:
ans[ansr] = s[olgr]
ansr -= 1
olgr -= 1
pass
else:
if olgl > olgr or t[igol] > s[olgl]:
ans[ansl] = t[igol]
ansl += 1
igol += 1
else:
ans[ansr] = t[igor]
ansr -= 1
igor -= 1
pass
# print(ans)
ans = ''.join(ans)
print(ans)
if __name__ == '__main__':
solve()
``` | output | 1 | 58,560 | 6 | 117,121 |
Provide tags and a correct Python 3 solution for this coding contest problem.
An African crossword is a rectangular table n × m in size. Each cell of the table contains exactly one letter. This table (it is also referred to as grid) contains some encrypted word that needs to be decoded.
To solve the crossword you should cross out all repeated letters in rows and columns. In other words, a letter should only be crossed out if and only if the corresponding column or row contains at least one more letter that is exactly the same. Besides, all such letters are crossed out simultaneously.
When all repeated letters have been crossed out, we should write the remaining letters in a string. The letters that occupy a higher position follow before the letters that occupy a lower position. If the letters are located in one row, then the letter to the left goes first. The resulting word is the answer to the problem.
You are suggested to solve an African crossword and print the word encrypted there.
Input
The first line contains two integers n and m (1 ≤ n, m ≤ 100). Next n lines contain m lowercase Latin letters each. That is the crossword grid.
Output
Print the encrypted word on a single line. It is guaranteed that the answer consists of at least one letter.
Examples
Input
3 3
cba
bcd
cbc
Output
abcd
Input
5 5
fcofd
ooedo
afaoa
rdcdf
eofsf
Output
codeforces | instruction | 0 | 58,612 | 6 | 117,224 |
Tags: implementation, strings
Correct Solution:
```
n, m = map(int, input().split())
a = [input() for i in range(n)]
ans = ''
for i in range(n):
for j in range(m):
if a[i].count(a[i][j]) == 1 and ''.join(a[k][j] for k in range(n)).count(a[i][j]) == 1:
ans += a[i][j]
print(ans)
``` | output | 1 | 58,612 | 6 | 117,225 |
Provide tags and a correct Python 3 solution for this coding contest problem.
An African crossword is a rectangular table n × m in size. Each cell of the table contains exactly one letter. This table (it is also referred to as grid) contains some encrypted word that needs to be decoded.
To solve the crossword you should cross out all repeated letters in rows and columns. In other words, a letter should only be crossed out if and only if the corresponding column or row contains at least one more letter that is exactly the same. Besides, all such letters are crossed out simultaneously.
When all repeated letters have been crossed out, we should write the remaining letters in a string. The letters that occupy a higher position follow before the letters that occupy a lower position. If the letters are located in one row, then the letter to the left goes first. The resulting word is the answer to the problem.
You are suggested to solve an African crossword and print the word encrypted there.
Input
The first line contains two integers n and m (1 ≤ n, m ≤ 100). Next n lines contain m lowercase Latin letters each. That is the crossword grid.
Output
Print the encrypted word on a single line. It is guaranteed that the answer consists of at least one letter.
Examples
Input
3 3
cba
bcd
cbc
Output
abcd
Input
5 5
fcofd
ooedo
afaoa
rdcdf
eofsf
Output
codeforces | instruction | 0 | 58,613 | 6 | 117,226 |
Tags: implementation, strings
Correct Solution:
```
import sys
def readlines(type=int):
return list(map(type, sys.stdin.readline().split()))
def read(type=int):
return type(sys.stdin.readline().strip())
joint = lambda it, sep=" ": sep.join(
[str(i) if type(i) != list else sep.join(map(str, i)) for i in it])
def solve(crossword, n, m):
rowarr = [set() for _ in range(n)]
colarr = [set() for _ in range(m)]
for i, row in enumerate(crossword):
seen = set()
for letter in row:
if letter not in seen:
seen.add(letter)
else:
rowarr[i].add(letter)
for i in range(m):
seen = set()
for j in range(n):
if crossword[j][i] not in seen:
seen.add(crossword[j][i])
else:
colarr[i].add(crossword[j][i])
solution = ""
for i in range(n):
for j in range(m):
if crossword[i][j] not in rowarr[i] and crossword[i][j] not in colarr[j]:
solution += crossword[i][j]
return solution
def main():
n, m = readlines()
crossword = [read(str) for _ in range(n)]
print(solve(crossword, n, m))
main()
``` | output | 1 | 58,613 | 6 | 117,227 |
Provide tags and a correct Python 3 solution for this coding contest problem.
An African crossword is a rectangular table n × m in size. Each cell of the table contains exactly one letter. This table (it is also referred to as grid) contains some encrypted word that needs to be decoded.
To solve the crossword you should cross out all repeated letters in rows and columns. In other words, a letter should only be crossed out if and only if the corresponding column or row contains at least one more letter that is exactly the same. Besides, all such letters are crossed out simultaneously.
When all repeated letters have been crossed out, we should write the remaining letters in a string. The letters that occupy a higher position follow before the letters that occupy a lower position. If the letters are located in one row, then the letter to the left goes first. The resulting word is the answer to the problem.
You are suggested to solve an African crossword and print the word encrypted there.
Input
The first line contains two integers n and m (1 ≤ n, m ≤ 100). Next n lines contain m lowercase Latin letters each. That is the crossword grid.
Output
Print the encrypted word on a single line. It is guaranteed that the answer consists of at least one letter.
Examples
Input
3 3
cba
bcd
cbc
Output
abcd
Input
5 5
fcofd
ooedo
afaoa
rdcdf
eofsf
Output
codeforces | instruction | 0 | 58,614 | 6 | 117,228 |
Tags: implementation, strings
Correct Solution:
```
X = list(map(int, input().split()))
Sentence, Answer = "", ""
for i in range(X[0]):
Sentence += input()
for i in range(X[0]):
Temp = Sentence[i * X[1]:(i + 1) * X[1]]
for j in range(X[1]):
if Temp.count(Temp[j]) == 1 and Sentence[j::X[1]].count(Temp[j]) == 1:
Answer += Temp[j]
print(Answer)
# UB_CodeForces
# Advice: Falling down is an accident, staying down is a choice
# Location: Behind my desk in Bojnurd
# Caption: Waiting for going to Tehran
# CodeNumber: 683
``` | output | 1 | 58,614 | 6 | 117,229 |
Provide tags and a correct Python 3 solution for this coding contest problem.
An African crossword is a rectangular table n × m in size. Each cell of the table contains exactly one letter. This table (it is also referred to as grid) contains some encrypted word that needs to be decoded.
To solve the crossword you should cross out all repeated letters in rows and columns. In other words, a letter should only be crossed out if and only if the corresponding column or row contains at least one more letter that is exactly the same. Besides, all such letters are crossed out simultaneously.
When all repeated letters have been crossed out, we should write the remaining letters in a string. The letters that occupy a higher position follow before the letters that occupy a lower position. If the letters are located in one row, then the letter to the left goes first. The resulting word is the answer to the problem.
You are suggested to solve an African crossword and print the word encrypted there.
Input
The first line contains two integers n and m (1 ≤ n, m ≤ 100). Next n lines contain m lowercase Latin letters each. That is the crossword grid.
Output
Print the encrypted word on a single line. It is guaranteed that the answer consists of at least one letter.
Examples
Input
3 3
cba
bcd
cbc
Output
abcd
Input
5 5
fcofd
ooedo
afaoa
rdcdf
eofsf
Output
codeforces | instruction | 0 | 58,615 | 6 | 117,230 |
Tags: implementation, strings
Correct Solution:
```
# 90B
from sys import stdin
__author__ = 'artyom'
def index(ch):
return ord(ch) - 97
n, m = map(int, stdin.readline().strip().split())
grid = []
for _ in range(n):
grid.append(stdin.readline().strip())
a, res = [0] * 26, [[''] * m for __ in range(n)]
for j in range(m):
for i in range(n):
a[index(grid[i][j])] += 1
for i in range(n):
ind = index(grid[i][j])
res[i][j] = grid[i][j] if a[ind] == 1 and res[i][j] != '.' else '.'
a[ind] = 0
for i in range(n):
for j in range(m):
a[index(grid[i][j])] += 1
for j in range(m):
ind = index(grid[i][j])
res[i][j] = grid[i][j] if a[ind] == 1 and res[i][j] != '.' else '.'
a[ind] = 0
st = ''
for i in range(n):
for j in range(m):
if res[i][j] != '.':
st += res[i][j]
print(st)
``` | output | 1 | 58,615 | 6 | 117,231 |
Provide tags and a correct Python 3 solution for this coding contest problem.
An African crossword is a rectangular table n × m in size. Each cell of the table contains exactly one letter. This table (it is also referred to as grid) contains some encrypted word that needs to be decoded.
To solve the crossword you should cross out all repeated letters in rows and columns. In other words, a letter should only be crossed out if and only if the corresponding column or row contains at least one more letter that is exactly the same. Besides, all such letters are crossed out simultaneously.
When all repeated letters have been crossed out, we should write the remaining letters in a string. The letters that occupy a higher position follow before the letters that occupy a lower position. If the letters are located in one row, then the letter to the left goes first. The resulting word is the answer to the problem.
You are suggested to solve an African crossword and print the word encrypted there.
Input
The first line contains two integers n and m (1 ≤ n, m ≤ 100). Next n lines contain m lowercase Latin letters each. That is the crossword grid.
Output
Print the encrypted word on a single line. It is guaranteed that the answer consists of at least one letter.
Examples
Input
3 3
cba
bcd
cbc
Output
abcd
Input
5 5
fcofd
ooedo
afaoa
rdcdf
eofsf
Output
codeforces | instruction | 0 | 58,616 | 6 | 117,232 |
Tags: implementation, strings
Correct Solution:
```
x,y=map(int,(input()).split())
a=""
for __ in range(x):
a+=input()
i=0
b=""
while i<x*y:
n=i%y
f=0
m=i-n
for j in range(m,m+y):
if a[j]==a[i] and i!=j:
f=1
for j in range(n,len(a),y):
if a[j]==a[i] and i!=j:
f=1
if f==0:
b+=a[i]
i+=1
print(b)
``` | output | 1 | 58,616 | 6 | 117,233 |
Provide tags and a correct Python 3 solution for this coding contest problem.
An African crossword is a rectangular table n × m in size. Each cell of the table contains exactly one letter. This table (it is also referred to as grid) contains some encrypted word that needs to be decoded.
To solve the crossword you should cross out all repeated letters in rows and columns. In other words, a letter should only be crossed out if and only if the corresponding column or row contains at least one more letter that is exactly the same. Besides, all such letters are crossed out simultaneously.
When all repeated letters have been crossed out, we should write the remaining letters in a string. The letters that occupy a higher position follow before the letters that occupy a lower position. If the letters are located in one row, then the letter to the left goes first. The resulting word is the answer to the problem.
You are suggested to solve an African crossword and print the word encrypted there.
Input
The first line contains two integers n and m (1 ≤ n, m ≤ 100). Next n lines contain m lowercase Latin letters each. That is the crossword grid.
Output
Print the encrypted word on a single line. It is guaranteed that the answer consists of at least one letter.
Examples
Input
3 3
cba
bcd
cbc
Output
abcd
Input
5 5
fcofd
ooedo
afaoa
rdcdf
eofsf
Output
codeforces | instruction | 0 | 58,617 | 6 | 117,234 |
Tags: implementation, strings
Correct Solution:
```
n , m = map(int,input().split())
arr = []
for i in range(n):
arr.append(input())
res = list(zip(*arr))
for i in range(n):
for j in range(m):
if arr[i].count(arr[i][j])==1:
if res[j].count(arr[i][j])==1:
print(arr[i][j],end="")
``` | output | 1 | 58,617 | 6 | 117,235 |
Provide tags and a correct Python 3 solution for this coding contest problem.
An African crossword is a rectangular table n × m in size. Each cell of the table contains exactly one letter. This table (it is also referred to as grid) contains some encrypted word that needs to be decoded.
To solve the crossword you should cross out all repeated letters in rows and columns. In other words, a letter should only be crossed out if and only if the corresponding column or row contains at least one more letter that is exactly the same. Besides, all such letters are crossed out simultaneously.
When all repeated letters have been crossed out, we should write the remaining letters in a string. The letters that occupy a higher position follow before the letters that occupy a lower position. If the letters are located in one row, then the letter to the left goes first. The resulting word is the answer to the problem.
You are suggested to solve an African crossword and print the word encrypted there.
Input
The first line contains two integers n and m (1 ≤ n, m ≤ 100). Next n lines contain m lowercase Latin letters each. That is the crossword grid.
Output
Print the encrypted word on a single line. It is guaranteed that the answer consists of at least one letter.
Examples
Input
3 3
cba
bcd
cbc
Output
abcd
Input
5 5
fcofd
ooedo
afaoa
rdcdf
eofsf
Output
codeforces | instruction | 0 | 58,618 | 6 | 117,236 |
Tags: implementation, strings
Correct Solution:
```
def main():
n, m = map(int, input().split())
grid = [list(input()) for _ in range(n)]
res = ''
for r in range(n):
for c in range(m):
cur = grid[r][c]
found = False
for i in range(m):
if i != c and grid[r][i] == cur:
found = True
for i in range(n):
if i != r and grid[i][c] == cur:
found = True
if not found:
res += cur
print(res)
if __name__ == '__main__':
main()
``` | output | 1 | 58,618 | 6 | 117,237 |
Provide tags and a correct Python 3 solution for this coding contest problem.
An African crossword is a rectangular table n × m in size. Each cell of the table contains exactly one letter. This table (it is also referred to as grid) contains some encrypted word that needs to be decoded.
To solve the crossword you should cross out all repeated letters in rows and columns. In other words, a letter should only be crossed out if and only if the corresponding column or row contains at least one more letter that is exactly the same. Besides, all such letters are crossed out simultaneously.
When all repeated letters have been crossed out, we should write the remaining letters in a string. The letters that occupy a higher position follow before the letters that occupy a lower position. If the letters are located in one row, then the letter to the left goes first. The resulting word is the answer to the problem.
You are suggested to solve an African crossword and print the word encrypted there.
Input
The first line contains two integers n and m (1 ≤ n, m ≤ 100). Next n lines contain m lowercase Latin letters each. That is the crossword grid.
Output
Print the encrypted word on a single line. It is guaranteed that the answer consists of at least one letter.
Examples
Input
3 3
cba
bcd
cbc
Output
abcd
Input
5 5
fcofd
ooedo
afaoa
rdcdf
eofsf
Output
codeforces | instruction | 0 | 58,619 | 6 | 117,238 |
Tags: implementation, strings
Correct Solution:
```
import re
[n, m], rows = [int(i) for i in input().split()], []
cols = [''] * m
for i in range(n):
rows.append(input())
for row in rows:
for i, char in enumerate(row):
cols[i] += char
rows_copy, cols_copy = list(rows), list(cols)
for i, row in enumerate(rows_copy):
for j, char in enumerate(row):
if row.count(char) > 1 or cols_copy[j].count(char) > 1:
rows[i] = re.sub(char, '_', rows[i])
cols[j] = re.sub(char, '_', cols[j])
ans = []
for row in rows:
for i, c in enumerate(row):
if c != '_' and c in cols[i]:
ans.append(c)
print(''.join([str(x) for x in ans]))
``` | output | 1 | 58,619 | 6 | 117,239 |
Provide tags and a correct Python 3 solution for this coding contest problem.
IA has so many colorful magnets on her fridge! Exactly one letter is written on each magnet, 'a' or 'b'. She loves to play with them, placing all magnets in a row. However, the girl is quickly bored and usually thinks how to make her entertainment more interesting.
Today, when IA looked at the fridge, she noticed that the word formed by magnets is really messy. "It would look much better when I'll swap some of them!" — thought the girl — "but how to do it?". After a while, she got an idea. IA will look at all prefixes with lengths from 1 to the length of the word and for each prefix she will either reverse this prefix or leave it as it is. She will consider the prefixes in the fixed order: from the shortest to the largest. She wants to get the lexicographically smallest possible word after she considers all prefixes. Can you help her, telling which prefixes should be chosen for reversing?
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 ≠ 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.
Input
The first and the only line contains a string s (1 ≤ |s| ≤ 1000), describing the initial string formed by magnets. The string s consists only of characters 'a' and 'b'.
Output
Output exactly |s| integers. If IA should reverse the i-th prefix (that is, the substring from 1 to i), the i-th integer should be equal to 1, and it should be equal to 0 otherwise.
If there are multiple possible sequences leading to the optimal answer, print any of them.
Examples
Input
bbab
Output
0 1 1 0
Input
aaaaa
Output
1 0 0 0 1
Note
In the first example, IA can reverse the second and the third prefix and get a string "abbb". She cannot get better result, since it is also lexicographically smallest string obtainable by permuting characters of the initial string.
In the second example, she can reverse any subset of prefixes — all letters are 'a'. | instruction | 0 | 58,832 | 6 | 117,664 |
Tags: constructive algorithms, greedy, implementation
Correct Solution:
```
import sys
input = lambda :sys.stdin.readline().rstrip()
s=[*input()]
ans=[0]*(len(s))
for i in range(1,len(s)):
if s[i]=='a':
ans[i-1]^=1
ans[i]=1
print(*ans)
``` | output | 1 | 58,832 | 6 | 117,665 |
Provide tags and a correct Python 3 solution for this coding contest problem.
IA has so many colorful magnets on her fridge! Exactly one letter is written on each magnet, 'a' or 'b'. She loves to play with them, placing all magnets in a row. However, the girl is quickly bored and usually thinks how to make her entertainment more interesting.
Today, when IA looked at the fridge, she noticed that the word formed by magnets is really messy. "It would look much better when I'll swap some of them!" — thought the girl — "but how to do it?". After a while, she got an idea. IA will look at all prefixes with lengths from 1 to the length of the word and for each prefix she will either reverse this prefix or leave it as it is. She will consider the prefixes in the fixed order: from the shortest to the largest. She wants to get the lexicographically smallest possible word after she considers all prefixes. Can you help her, telling which prefixes should be chosen for reversing?
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 ≠ 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.
Input
The first and the only line contains a string s (1 ≤ |s| ≤ 1000), describing the initial string formed by magnets. The string s consists only of characters 'a' and 'b'.
Output
Output exactly |s| integers. If IA should reverse the i-th prefix (that is, the substring from 1 to i), the i-th integer should be equal to 1, and it should be equal to 0 otherwise.
If there are multiple possible sequences leading to the optimal answer, print any of them.
Examples
Input
bbab
Output
0 1 1 0
Input
aaaaa
Output
1 0 0 0 1
Note
In the first example, IA can reverse the second and the third prefix and get a string "abbb". She cannot get better result, since it is also lexicographically smallest string obtainable by permuting characters of the initial string.
In the second example, she can reverse any subset of prefixes — all letters are 'a'. | instruction | 0 | 58,833 | 6 | 117,666 |
Tags: constructive algorithms, greedy, implementation
Correct Solution:
```
s = input()
ans = [0]*len(s)
for i in range(1, len(s)):
if s[i] != s[i-1]:
ans[i-1] = 1
ans[-1] = 1 if s[-1] == 'a' else 0
print(*ans)
``` | output | 1 | 58,833 | 6 | 117,667 |
Provide tags and a correct Python 3 solution for this coding contest problem.
IA has so many colorful magnets on her fridge! Exactly one letter is written on each magnet, 'a' or 'b'. She loves to play with them, placing all magnets in a row. However, the girl is quickly bored and usually thinks how to make her entertainment more interesting.
Today, when IA looked at the fridge, she noticed that the word formed by magnets is really messy. "It would look much better when I'll swap some of them!" — thought the girl — "but how to do it?". After a while, she got an idea. IA will look at all prefixes with lengths from 1 to the length of the word and for each prefix she will either reverse this prefix or leave it as it is. She will consider the prefixes in the fixed order: from the shortest to the largest. She wants to get the lexicographically smallest possible word after she considers all prefixes. Can you help her, telling which prefixes should be chosen for reversing?
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 ≠ 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.
Input
The first and the only line contains a string s (1 ≤ |s| ≤ 1000), describing the initial string formed by magnets. The string s consists only of characters 'a' and 'b'.
Output
Output exactly |s| integers. If IA should reverse the i-th prefix (that is, the substring from 1 to i), the i-th integer should be equal to 1, and it should be equal to 0 otherwise.
If there are multiple possible sequences leading to the optimal answer, print any of them.
Examples
Input
bbab
Output
0 1 1 0
Input
aaaaa
Output
1 0 0 0 1
Note
In the first example, IA can reverse the second and the third prefix and get a string "abbb". She cannot get better result, since it is also lexicographically smallest string obtainable by permuting characters of the initial string.
In the second example, she can reverse any subset of prefixes — all letters are 'a'. | instruction | 0 | 58,834 | 6 | 117,668 |
Tags: constructive algorithms, greedy, implementation
Correct Solution:
```
s = input()
l = []
i = 0
while i < len(s):
l.append(0)
while i < len(s) and s[i] == 'b':
l[-1] += 1
i += 1
l.append(0)
while i < len(s) and s[i] == 'a':
l[-1] += 1
i += 1
# print(l)
i = 0
res = []
for j, e in enumerate(l):
i += e
if j % 2 == 0:
if i != e and l[j + 1] > 0:
if e:
res.append(i - 1)
elif e:
res.append(i - 1)
v = ['0'] * len(s)
for e in res:
v[e] = '1'
print(' '.join(v))
``` | output | 1 | 58,834 | 6 | 117,669 |
Provide tags and a correct Python 3 solution for this coding contest problem.
IA has so many colorful magnets on her fridge! Exactly one letter is written on each magnet, 'a' or 'b'. She loves to play with them, placing all magnets in a row. However, the girl is quickly bored and usually thinks how to make her entertainment more interesting.
Today, when IA looked at the fridge, she noticed that the word formed by magnets is really messy. "It would look much better when I'll swap some of them!" — thought the girl — "but how to do it?". After a while, she got an idea. IA will look at all prefixes with lengths from 1 to the length of the word and for each prefix she will either reverse this prefix or leave it as it is. She will consider the prefixes in the fixed order: from the shortest to the largest. She wants to get the lexicographically smallest possible word after she considers all prefixes. Can you help her, telling which prefixes should be chosen for reversing?
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 ≠ 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.
Input
The first and the only line contains a string s (1 ≤ |s| ≤ 1000), describing the initial string formed by magnets. The string s consists only of characters 'a' and 'b'.
Output
Output exactly |s| integers. If IA should reverse the i-th prefix (that is, the substring from 1 to i), the i-th integer should be equal to 1, and it should be equal to 0 otherwise.
If there are multiple possible sequences leading to the optimal answer, print any of them.
Examples
Input
bbab
Output
0 1 1 0
Input
aaaaa
Output
1 0 0 0 1
Note
In the first example, IA can reverse the second and the third prefix and get a string "abbb". She cannot get better result, since it is also lexicographically smallest string obtainable by permuting characters of the initial string.
In the second example, she can reverse any subset of prefixes — all letters are 'a'. | instruction | 0 | 58,835 | 6 | 117,670 |
Tags: constructive algorithms, greedy, implementation
Correct Solution:
```
"""
Greedy solution where if we are looking at a, then move b to front as much as possible
"""
def solve(s):
res = [0 for _ in range(len(s))]
for i in range(1, len(s)):
if s[i] == "a":
res[i] = 1
res[i-1] = 1 - res[i-1]
return res
lst = list((input()))
print ((" ").join(list(map(str, solve(lst)))))
``` | output | 1 | 58,835 | 6 | 117,671 |
Provide tags and a correct Python 3 solution for this coding contest problem.
IA has so many colorful magnets on her fridge! Exactly one letter is written on each magnet, 'a' or 'b'. She loves to play with them, placing all magnets in a row. However, the girl is quickly bored and usually thinks how to make her entertainment more interesting.
Today, when IA looked at the fridge, she noticed that the word formed by magnets is really messy. "It would look much better when I'll swap some of them!" — thought the girl — "but how to do it?". After a while, she got an idea. IA will look at all prefixes with lengths from 1 to the length of the word and for each prefix she will either reverse this prefix or leave it as it is. She will consider the prefixes in the fixed order: from the shortest to the largest. She wants to get the lexicographically smallest possible word after she considers all prefixes. Can you help her, telling which prefixes should be chosen for reversing?
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 ≠ 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.
Input
The first and the only line contains a string s (1 ≤ |s| ≤ 1000), describing the initial string formed by magnets. The string s consists only of characters 'a' and 'b'.
Output
Output exactly |s| integers. If IA should reverse the i-th prefix (that is, the substring from 1 to i), the i-th integer should be equal to 1, and it should be equal to 0 otherwise.
If there are multiple possible sequences leading to the optimal answer, print any of them.
Examples
Input
bbab
Output
0 1 1 0
Input
aaaaa
Output
1 0 0 0 1
Note
In the first example, IA can reverse the second and the third prefix and get a string "abbb". She cannot get better result, since it is also lexicographically smallest string obtainable by permuting characters of the initial string.
In the second example, she can reverse any subset of prefixes — all letters are 'a'. | instruction | 0 | 58,836 | 6 | 117,672 |
Tags: constructive algorithms, greedy, implementation
Correct Solution:
```
from itertools import groupby
s = "#"+input()
l = len(s)
# ans = [0]*l
# for i in range(1,l):
# if s[i] == "a":
# ans[i-1] ^= 1
# ans[i] = 1
# print(*ans[1:])
ss = s[1:]
ans1 = [0]*(l-1)
gb = [list(g) for k,g in groupby(ss)]
ll,first_a,turn_a = 0,False,False
for i in gb:
if i[0] == "a":
if not first_a:
ll += len(i)
ans1[ll-1] = 1
first_a = True
turn_a = True
else:
ll += len(i)
ans1[ll-1] = 1
turn_a = True
else:
ll += len(i)
ans1[ll-1] = 1
turn_a = False
if not turn_a:
ans1[ll-1] = 0
print(*ans1)
``` | output | 1 | 58,836 | 6 | 117,673 |
Provide tags and a correct Python 3 solution for this coding contest problem.
IA has so many colorful magnets on her fridge! Exactly one letter is written on each magnet, 'a' or 'b'. She loves to play with them, placing all magnets in a row. However, the girl is quickly bored and usually thinks how to make her entertainment more interesting.
Today, when IA looked at the fridge, she noticed that the word formed by magnets is really messy. "It would look much better when I'll swap some of them!" — thought the girl — "but how to do it?". After a while, she got an idea. IA will look at all prefixes with lengths from 1 to the length of the word and for each prefix she will either reverse this prefix or leave it as it is. She will consider the prefixes in the fixed order: from the shortest to the largest. She wants to get the lexicographically smallest possible word after she considers all prefixes. Can you help her, telling which prefixes should be chosen for reversing?
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 ≠ 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.
Input
The first and the only line contains a string s (1 ≤ |s| ≤ 1000), describing the initial string formed by magnets. The string s consists only of characters 'a' and 'b'.
Output
Output exactly |s| integers. If IA should reverse the i-th prefix (that is, the substring from 1 to i), the i-th integer should be equal to 1, and it should be equal to 0 otherwise.
If there are multiple possible sequences leading to the optimal answer, print any of them.
Examples
Input
bbab
Output
0 1 1 0
Input
aaaaa
Output
1 0 0 0 1
Note
In the first example, IA can reverse the second and the third prefix and get a string "abbb". She cannot get better result, since it is also lexicographically smallest string obtainable by permuting characters of the initial string.
In the second example, she can reverse any subset of prefixes — all letters are 'a'. | instruction | 0 | 58,837 | 6 | 117,674 |
Tags: constructive algorithms, greedy, implementation
Correct Solution:
```
s=input()
n=len(s)
a=s.count('a')
s1=''
count=[]
i=0
while i<n:
count.append(0)
s1=s1+s[i]
while i+count[-1]<n and s[i+count[-1]]==s[i]:
count[-1]+=1
i+=count[-1]
#print(s1)
#print(count)
nc=len(count)
if s1[nc-1]=='a':
for i in range(nc):
for j in range(1,count[i]):
print(0,end=' ')
print(1,end=' ')
else:
bs=0
while bs+1<=n and s[-bs-1]=='b':
bs+=1
for i in range(nc-1):
for j in range(1,count[i]):
print(0,end=' ')
print(1,end=' ')
for i in range(bs):
print(0,end=' ')
``` | output | 1 | 58,837 | 6 | 117,675 |
Provide tags and a correct Python 3 solution for this coding contest problem.
IA has so many colorful magnets on her fridge! Exactly one letter is written on each magnet, 'a' or 'b'. She loves to play with them, placing all magnets in a row. However, the girl is quickly bored and usually thinks how to make her entertainment more interesting.
Today, when IA looked at the fridge, she noticed that the word formed by magnets is really messy. "It would look much better when I'll swap some of them!" — thought the girl — "but how to do it?". After a while, she got an idea. IA will look at all prefixes with lengths from 1 to the length of the word and for each prefix she will either reverse this prefix or leave it as it is. She will consider the prefixes in the fixed order: from the shortest to the largest. She wants to get the lexicographically smallest possible word after she considers all prefixes. Can you help her, telling which prefixes should be chosen for reversing?
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 ≠ 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.
Input
The first and the only line contains a string s (1 ≤ |s| ≤ 1000), describing the initial string formed by magnets. The string s consists only of characters 'a' and 'b'.
Output
Output exactly |s| integers. If IA should reverse the i-th prefix (that is, the substring from 1 to i), the i-th integer should be equal to 1, and it should be equal to 0 otherwise.
If there are multiple possible sequences leading to the optimal answer, print any of them.
Examples
Input
bbab
Output
0 1 1 0
Input
aaaaa
Output
1 0 0 0 1
Note
In the first example, IA can reverse the second and the third prefix and get a string "abbb". She cannot get better result, since it is also lexicographically smallest string obtainable by permuting characters of the initial string.
In the second example, she can reverse any subset of prefixes — all letters are 'a'. | instruction | 0 | 58,838 | 6 | 117,676 |
Tags: constructive algorithms, greedy, implementation
Correct Solution:
```
s = input()
def swap(i):
return s[:i:-1] + s[i + 1:]
for e in range(len(s) - 1):
if s[e] != s[e + 1]:
print(1, end=' ')
# s = swap(e)
# print(s)
else:
print(0, end=' ')
if s[-1] == 'a':
print(1, end=' ')
else:
print(0)
``` | output | 1 | 58,838 | 6 | 117,677 |
Provide tags and a correct Python 3 solution for this coding contest problem.
IA has so many colorful magnets on her fridge! Exactly one letter is written on each magnet, 'a' or 'b'. She loves to play with them, placing all magnets in a row. However, the girl is quickly bored and usually thinks how to make her entertainment more interesting.
Today, when IA looked at the fridge, she noticed that the word formed by magnets is really messy. "It would look much better when I'll swap some of them!" — thought the girl — "but how to do it?". After a while, she got an idea. IA will look at all prefixes with lengths from 1 to the length of the word and for each prefix she will either reverse this prefix or leave it as it is. She will consider the prefixes in the fixed order: from the shortest to the largest. She wants to get the lexicographically smallest possible word after she considers all prefixes. Can you help her, telling which prefixes should be chosen for reversing?
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 ≠ 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.
Input
The first and the only line contains a string s (1 ≤ |s| ≤ 1000), describing the initial string formed by magnets. The string s consists only of characters 'a' and 'b'.
Output
Output exactly |s| integers. If IA should reverse the i-th prefix (that is, the substring from 1 to i), the i-th integer should be equal to 1, and it should be equal to 0 otherwise.
If there are multiple possible sequences leading to the optimal answer, print any of them.
Examples
Input
bbab
Output
0 1 1 0
Input
aaaaa
Output
1 0 0 0 1
Note
In the first example, IA can reverse the second and the third prefix and get a string "abbb". She cannot get better result, since it is also lexicographically smallest string obtainable by permuting characters of the initial string.
In the second example, she can reverse any subset of prefixes — all letters are 'a'. | instruction | 0 | 58,839 | 6 | 117,678 |
Tags: constructive algorithms, greedy, implementation
Correct Solution:
```
def not_last(c):
if c == 'a':
return 'b'
return 'a'
s = input()
last = 'b'
res = []
for i in range(len(s) - 1):
if s[i + 1] == last:
res += [0]
else:
last = not_last(last)
res += [1]
if last == 'a':
res += [1]
else:
res += [0]
print(' '.join(list(map(str, res))))
``` | output | 1 | 58,839 | 6 | 117,679 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
IA has so many colorful magnets on her fridge! Exactly one letter is written on each magnet, 'a' or 'b'. She loves to play with them, placing all magnets in a row. However, the girl is quickly bored and usually thinks how to make her entertainment more interesting.
Today, when IA looked at the fridge, she noticed that the word formed by magnets is really messy. "It would look much better when I'll swap some of them!" — thought the girl — "but how to do it?". After a while, she got an idea. IA will look at all prefixes with lengths from 1 to the length of the word and for each prefix she will either reverse this prefix or leave it as it is. She will consider the prefixes in the fixed order: from the shortest to the largest. She wants to get the lexicographically smallest possible word after she considers all prefixes. Can you help her, telling which prefixes should be chosen for reversing?
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 ≠ 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.
Input
The first and the only line contains a string s (1 ≤ |s| ≤ 1000), describing the initial string formed by magnets. The string s consists only of characters 'a' and 'b'.
Output
Output exactly |s| integers. If IA should reverse the i-th prefix (that is, the substring from 1 to i), the i-th integer should be equal to 1, and it should be equal to 0 otherwise.
If there are multiple possible sequences leading to the optimal answer, print any of them.
Examples
Input
bbab
Output
0 1 1 0
Input
aaaaa
Output
1 0 0 0 1
Note
In the first example, IA can reverse the second and the third prefix and get a string "abbb". She cannot get better result, since it is also lexicographically smallest string obtainable by permuting characters of the initial string.
In the second example, she can reverse any subset of prefixes — all letters are 'a'.
Submitted Solution:
```
from sys import stdin,stdout
a = list(input())
b = sorted(a)
l = len(a)
ans = ['0' for i in range(l)]
for i in range(1,l):
if a == b:
break
if a[0] == 'a':
if a[i] == 'a':
a[0:i-1] = a[i-1:0:-1]
ans[i-1] = '1'
else:
if a[i] == 'b':
a[0:i-1] = a[i-1:0:-1]
ans[i-1] = '1'
if a[0] == 'b':
ans[l-1] = '1'
stdout.write(' '.join(ans))
``` | instruction | 0 | 58,840 | 6 | 117,680 |
Yes | output | 1 | 58,840 | 6 | 117,681 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
IA has so many colorful magnets on her fridge! Exactly one letter is written on each magnet, 'a' or 'b'. She loves to play with them, placing all magnets in a row. However, the girl is quickly bored and usually thinks how to make her entertainment more interesting.
Today, when IA looked at the fridge, she noticed that the word formed by magnets is really messy. "It would look much better when I'll swap some of them!" — thought the girl — "but how to do it?". After a while, she got an idea. IA will look at all prefixes with lengths from 1 to the length of the word and for each prefix she will either reverse this prefix or leave it as it is. She will consider the prefixes in the fixed order: from the shortest to the largest. She wants to get the lexicographically smallest possible word after she considers all prefixes. Can you help her, telling which prefixes should be chosen for reversing?
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 ≠ 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.
Input
The first and the only line contains a string s (1 ≤ |s| ≤ 1000), describing the initial string formed by magnets. The string s consists only of characters 'a' and 'b'.
Output
Output exactly |s| integers. If IA should reverse the i-th prefix (that is, the substring from 1 to i), the i-th integer should be equal to 1, and it should be equal to 0 otherwise.
If there are multiple possible sequences leading to the optimal answer, print any of them.
Examples
Input
bbab
Output
0 1 1 0
Input
aaaaa
Output
1 0 0 0 1
Note
In the first example, IA can reverse the second and the third prefix and get a string "abbb". She cannot get better result, since it is also lexicographically smallest string obtainable by permuting characters of the initial string.
In the second example, she can reverse any subset of prefixes — all letters are 'a'.
Submitted Solution:
```
a=list(input())
b=[]
c=0
for i in range(len(a)-1):
if a[i]!=a[i+1]:
b.append(i)
d=[0]*len(a)
for i in range(1,len(b)):
d[b[i]]=1
if a[-1]=="a":
d[-1]=1
print(*d)
``` | instruction | 0 | 58,841 | 6 | 117,682 |
Yes | output | 1 | 58,841 | 6 | 117,683 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
IA has so many colorful magnets on her fridge! Exactly one letter is written on each magnet, 'a' or 'b'. She loves to play with them, placing all magnets in a row. However, the girl is quickly bored and usually thinks how to make her entertainment more interesting.
Today, when IA looked at the fridge, she noticed that the word formed by magnets is really messy. "It would look much better when I'll swap some of them!" — thought the girl — "but how to do it?". After a while, she got an idea. IA will look at all prefixes with lengths from 1 to the length of the word and for each prefix she will either reverse this prefix or leave it as it is. She will consider the prefixes in the fixed order: from the shortest to the largest. She wants to get the lexicographically smallest possible word after she considers all prefixes. Can you help her, telling which prefixes should be chosen for reversing?
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 ≠ 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.
Input
The first and the only line contains a string s (1 ≤ |s| ≤ 1000), describing the initial string formed by magnets. The string s consists only of characters 'a' and 'b'.
Output
Output exactly |s| integers. If IA should reverse the i-th prefix (that is, the substring from 1 to i), the i-th integer should be equal to 1, and it should be equal to 0 otherwise.
If there are multiple possible sequences leading to the optimal answer, print any of them.
Examples
Input
bbab
Output
0 1 1 0
Input
aaaaa
Output
1 0 0 0 1
Note
In the first example, IA can reverse the second and the third prefix and get a string "abbb". She cannot get better result, since it is also lexicographically smallest string obtainable by permuting characters of the initial string.
In the second example, she can reverse any subset of prefixes — all letters are 'a'.
Submitted Solution:
```
s=input()
n=len(s)
ans=[]
for i in range(n-1):
if s[i]!=s[i+1]:
ans.append(1)
else:
ans.append(0)
if s[-1]=="a":
ans.append(1)
else:
ans.append(0)
for t in ans:
print(t,end=" ")
``` | instruction | 0 | 58,842 | 6 | 117,684 |
Yes | output | 1 | 58,842 | 6 | 117,685 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
IA has so many colorful magnets on her fridge! Exactly one letter is written on each magnet, 'a' or 'b'. She loves to play with them, placing all magnets in a row. However, the girl is quickly bored and usually thinks how to make her entertainment more interesting.
Today, when IA looked at the fridge, she noticed that the word formed by magnets is really messy. "It would look much better when I'll swap some of them!" — thought the girl — "but how to do it?". After a while, she got an idea. IA will look at all prefixes with lengths from 1 to the length of the word and for each prefix she will either reverse this prefix or leave it as it is. She will consider the prefixes in the fixed order: from the shortest to the largest. She wants to get the lexicographically smallest possible word after she considers all prefixes. Can you help her, telling which prefixes should be chosen for reversing?
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 ≠ 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.
Input
The first and the only line contains a string s (1 ≤ |s| ≤ 1000), describing the initial string formed by magnets. The string s consists only of characters 'a' and 'b'.
Output
Output exactly |s| integers. If IA should reverse the i-th prefix (that is, the substring from 1 to i), the i-th integer should be equal to 1, and it should be equal to 0 otherwise.
If there are multiple possible sequences leading to the optimal answer, print any of them.
Examples
Input
bbab
Output
0 1 1 0
Input
aaaaa
Output
1 0 0 0 1
Note
In the first example, IA can reverse the second and the third prefix and get a string "abbb". She cannot get better result, since it is also lexicographically smallest string obtainable by permuting characters of the initial string.
In the second example, she can reverse any subset of prefixes — all letters are 'a'.
Submitted Solution:
```
#codeforces_1043C
gi = lambda : list(map(int,input().split()))
s = input()
n = len(s)
ans = [0 for e in range(n)]
k = 1
while k < n:
if s[k] == 'a':
temp = k
while k < n and s[k] == 'a':k+=1
ans[temp-1] = ans[k-1] = 1
k += 1
print(*ans)
``` | instruction | 0 | 58,843 | 6 | 117,686 |
Yes | output | 1 | 58,843 | 6 | 117,687 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
IA has so many colorful magnets on her fridge! Exactly one letter is written on each magnet, 'a' or 'b'. She loves to play with them, placing all magnets in a row. However, the girl is quickly bored and usually thinks how to make her entertainment more interesting.
Today, when IA looked at the fridge, she noticed that the word formed by magnets is really messy. "It would look much better when I'll swap some of them!" — thought the girl — "but how to do it?". After a while, she got an idea. IA will look at all prefixes with lengths from 1 to the length of the word and for each prefix she will either reverse this prefix or leave it as it is. She will consider the prefixes in the fixed order: from the shortest to the largest. She wants to get the lexicographically smallest possible word after she considers all prefixes. Can you help her, telling which prefixes should be chosen for reversing?
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 ≠ 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.
Input
The first and the only line contains a string s (1 ≤ |s| ≤ 1000), describing the initial string formed by magnets. The string s consists only of characters 'a' and 'b'.
Output
Output exactly |s| integers. If IA should reverse the i-th prefix (that is, the substring from 1 to i), the i-th integer should be equal to 1, and it should be equal to 0 otherwise.
If there are multiple possible sequences leading to the optimal answer, print any of them.
Examples
Input
bbab
Output
0 1 1 0
Input
aaaaa
Output
1 0 0 0 1
Note
In the first example, IA can reverse the second and the third prefix and get a string "abbb". She cannot get better result, since it is also lexicographically smallest string obtainable by permuting characters of the initial string.
In the second example, she can reverse any subset of prefixes — all letters are 'a'.
Submitted Solution:
```
from sys import maxsize, stdout, stdin,stderr
mod = int(1e9 + 7)
def I(): return int(stdin.readline())
def lint(): return [int(x) for x in stdin.readline().split()]
def S(): return input().strip()
def grid(r, c): return [lint() for i in range(r)]
from collections import defaultdict, Counter
import math
import heapq
from heapq import heappop , heappush
import bisect
from itertools import groupby
def gcd(a,b):
while b:
a %= b
tmp = a
a = b
b = tmp
return a
def lcm(a,b):
return a / gcd(a, b) * b
def check_prime(n):
for i in range(2, int(n ** (1 / 2)) + 1):
if not n % i:
return False
return True
def Bs(a, x):
i=0
j=0
left = 0
right = len(a)
flag=False
while left<right:
mi = (left+right)//2
#print(smi,a[mi],x)
if a[mi]<=x:
left = mi+1
i+=1
else:
right = mi
j+=1
#print(left,right,"----")
#print(i-1,j)
if left>0 and a[left-1]==x:
return i-1, j
else:
return -1, -1
s = input()
n = len(s)
ar=[0]*n
a=0
for i in range(1,n):
if s[i]=='a':
if i==n-1:
ar[i]=1
elif s[i+1]!='a':
if a==0:
ar[i-1]=1
ar[i]=1
a=0
else:
a+=1
print(*ar)
``` | instruction | 0 | 58,844 | 6 | 117,688 |
No | output | 1 | 58,844 | 6 | 117,689 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
IA has so many colorful magnets on her fridge! Exactly one letter is written on each magnet, 'a' or 'b'. She loves to play with them, placing all magnets in a row. However, the girl is quickly bored and usually thinks how to make her entertainment more interesting.
Today, when IA looked at the fridge, she noticed that the word formed by magnets is really messy. "It would look much better when I'll swap some of them!" — thought the girl — "but how to do it?". After a while, she got an idea. IA will look at all prefixes with lengths from 1 to the length of the word and for each prefix she will either reverse this prefix or leave it as it is. She will consider the prefixes in the fixed order: from the shortest to the largest. She wants to get the lexicographically smallest possible word after she considers all prefixes. Can you help her, telling which prefixes should be chosen for reversing?
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 ≠ 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.
Input
The first and the only line contains a string s (1 ≤ |s| ≤ 1000), describing the initial string formed by magnets. The string s consists only of characters 'a' and 'b'.
Output
Output exactly |s| integers. If IA should reverse the i-th prefix (that is, the substring from 1 to i), the i-th integer should be equal to 1, and it should be equal to 0 otherwise.
If there are multiple possible sequences leading to the optimal answer, print any of them.
Examples
Input
bbab
Output
0 1 1 0
Input
aaaaa
Output
1 0 0 0 1
Note
In the first example, IA can reverse the second and the third prefix and get a string "abbb". She cannot get better result, since it is also lexicographically smallest string obtainable by permuting characters of the initial string.
In the second example, she can reverse any subset of prefixes — all letters are 'a'.
Submitted Solution:
```
#########################################################################################################\
#########################################################################################################
###################################The_Apurv_Rathore#####################################################
#########################################################################################################
#########################################################################################################
import sys,os,io
from sys import stdin
from math import log, gcd, ceil
from collections import defaultdict, deque, Counter
from heapq import heappush, heappop, heapify
from bisect import bisect_left , bisect_right
import math
alphabets = list('abcdefghijklmnopqrstuvwxyz')
#for deep recursion__________________________________________-
from types import GeneratorType
def bootstrap(f, stack=[]):
def wrappedfunc(*args, **kwargs):
if stack:
return f(*args, **kwargs)
else:
to = f(*args, **kwargs)
while True:
if type(to) is GeneratorType:
stack.append(to)
to = next(to)
else:
stack.pop()
if not stack:
break
to = stack[-1].send(to)
return to
return wrappedfunc
def ncr(n, r, p):
num = den = 1
for i in range(r):
num = (num * (n - i)) % p
den = (den * (i + 1)) % p
return (num * pow(den,p - 2, p)) % p
def primeFactors(n):
l = []
while n % 2 == 0:
l.append(2)
n = n / 2
for i in range(3,int(math.sqrt(n))+1,2):
while n % i== 0:
l.append(int(i))
n = n / i
if n > 2:
l.append(n)
c = dict(Counter(l))
return list(set(l))
# return c
def power(x, y, p) :
res = 1
x = x % p
if (x == 0) :
return 0
while (y > 0) :
if ((y & 1) == 1) :
res = (res * x) % p
y = y >> 1 # y = y/2
x = (x * x) % p
return res
#____________________GetPrimeFactors in log(n)________________________________________
def sieveForSmallestPrimeFactor():
MAXN = 100001
spf = [0 for i in range(MAXN)]
spf[1] = 1
for i in range(2, MAXN):
spf[i] = i
for i in range(4, MAXN, 2):
spf[i] = 2
for i in range(3, math.ceil(math.sqrt(MAXN))):
if (spf[i] == i):
for j in range(i * i, MAXN, i):
if (spf[j] == j):
spf[j] = i
return spf
def getPrimeFactorizationLOGN(x):
spf = sieveForSmallestPrimeFactor()
ret = list()
while (x != 1):
ret.append(spf[x])
x = x // spf[x]
return ret
#____________________________________________________________
def SieveOfEratosthenes(n):
#time complexity = nlog(log(n))
prime = [True for i in range(n+1)]
p = 2
while (p * p <= n):
if (prime[p] == True):
for i in range(p * p, n+1, p):
prime[i] = False
p += 1
return prime
def si():
return input()
def divideCeil(n,x):
if (n%x==0):
return n//x
return n//x+1
def ii():
return int(input())
def li():
return list(map(int,input().split()))
#__________________________TEMPLATE__________________OVER_______________________________________________________
if(os.path.exists('input.txt')):
sys.stdin = open("input.txt","r") ; sys.stdout = open("output.txt","w")
# else:
# input = io.BytesIO(os.read(0, os.fstat(0).st_size)).readline
def solve():
s = list(si())
ans = [0]*(len(s))
for i in range(len(s)):
s1 = s[:i][::-1]+s[i+1:]
if s1<s:
s = s1[:]
ans[i]=1
print(*ans)
t = 1
# t = ii()
for _ in range(t):
solve()
``` | instruction | 0 | 58,845 | 6 | 117,690 |
No | output | 1 | 58,845 | 6 | 117,691 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
IA has so many colorful magnets on her fridge! Exactly one letter is written on each magnet, 'a' or 'b'. She loves to play with them, placing all magnets in a row. However, the girl is quickly bored and usually thinks how to make her entertainment more interesting.
Today, when IA looked at the fridge, she noticed that the word formed by magnets is really messy. "It would look much better when I'll swap some of them!" — thought the girl — "but how to do it?". After a while, she got an idea. IA will look at all prefixes with lengths from 1 to the length of the word and for each prefix she will either reverse this prefix or leave it as it is. She will consider the prefixes in the fixed order: from the shortest to the largest. She wants to get the lexicographically smallest possible word after she considers all prefixes. Can you help her, telling which prefixes should be chosen for reversing?
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 ≠ 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.
Input
The first and the only line contains a string s (1 ≤ |s| ≤ 1000), describing the initial string formed by magnets. The string s consists only of characters 'a' and 'b'.
Output
Output exactly |s| integers. If IA should reverse the i-th prefix (that is, the substring from 1 to i), the i-th integer should be equal to 1, and it should be equal to 0 otherwise.
If there are multiple possible sequences leading to the optimal answer, print any of them.
Examples
Input
bbab
Output
0 1 1 0
Input
aaaaa
Output
1 0 0 0 1
Note
In the first example, IA can reverse the second and the third prefix and get a string "abbb". She cannot get better result, since it is also lexicographically smallest string obtainable by permuting characters of the initial string.
In the second example, she can reverse any subset of prefixes — all letters are 'a'.
Submitted Solution:
```
s = input()
arr = [0 for _ in range(len(s))]
for i in range(len(s)):
# act = s[:i+1]
# k = len(act)
# a_left = act[:k//2].count('a')
# a_right = act[k//2:].count('a')
# print(act, act[:k//2], act[k//2 + 1:])
if ''.join(reversed(s[:i + 1])) + s[i+1:] <= s:
s = ''.join(reversed(s[:i + 1])) + s[i+1:]
arr[i] = 1
print(*arr)
``` | instruction | 0 | 58,846 | 6 | 117,692 |
No | output | 1 | 58,846 | 6 | 117,693 |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.