description stringlengths 171 4k | code stringlengths 94 3.98k | normalized_code stringlengths 57 4.99k |
|---|---|---|
For a given non-negative integer N, find the next smallest Happy Number. A number is called happy if it leads to 1 after a sequence of steps. Wherein at each step the number is replaced by the sum of squares of its digits that is if we start with Happy Number and keep replacing it with sum of squares of its digits, we reach 1 at some point.
Example 1:
Input:
N = 8
Output:
10
Explanation:
Next happy number after 8 is 10 since
1*1 + 0*0 = 1
Example 2:
Input:
N = 10
Output
13
Explanation:
After 10, 13 is the smallest happy number because
1*1 + 3*3 = 10, so we replace 13 by 10 and 1*1 + 0*0 = 1.
Your Task:
You don't need to read input or print anything. Your task is to complete the function nextHappy() which takes an integer N as input parameters and returns an integer, next Happy number after N.
Expected Time Complexity: O(Nlog_{10}N)
Expected Space Complexity: O(1)
Constraints:
1<=N<=10^{5} | class Solution:
def isHappy(self, n):
seen = set()
while n not in seen:
seen.add(n)
n = sum(int(i) ** 2 for i in str(n))
return n == 1
def nextHappy(self, N):
N += 1
while not self.isHappy(N):
N += 1
return N | CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR WHILE VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER VAR FUNC_CALL VAR VAR RETURN VAR NUMBER FUNC_DEF VAR NUMBER WHILE FUNC_CALL VAR VAR VAR NUMBER RETURN VAR |
For a given non-negative integer N, find the next smallest Happy Number. A number is called happy if it leads to 1 after a sequence of steps. Wherein at each step the number is replaced by the sum of squares of its digits that is if we start with Happy Number and keep replacing it with sum of squares of its digits, we reach 1 at some point.
Example 1:
Input:
N = 8
Output:
10
Explanation:
Next happy number after 8 is 10 since
1*1 + 0*0 = 1
Example 2:
Input:
N = 10
Output
13
Explanation:
After 10, 13 is the smallest happy number because
1*1 + 3*3 = 10, so we replace 13 by 10 and 1*1 + 0*0 = 1.
Your Task:
You don't need to read input or print anything. Your task is to complete the function nextHappy() which takes an integer N as input parameters and returns an integer, next Happy number after N.
Expected Time Complexity: O(Nlog_{10}N)
Expected Space Complexity: O(1)
Constraints:
1<=N<=10^{5} | def happy(n):
temp = sum([pow(int(x), 2) for x in str(n)])
d = {}
d[temp] = 1
if temp == 1:
return True
while True:
temp = sum([pow(int(x), 2) for x in str(temp)])
if d.get(temp) == None:
d[temp] = 1
else:
return False
if temp == 1:
return True
return False
class Solution:
def nextHappy(self, N):
N += 1
while happy(N) != True:
N += 1
return N | FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER VAR FUNC_CALL VAR VAR ASSIGN VAR DICT ASSIGN VAR VAR NUMBER IF VAR NUMBER RETURN NUMBER WHILE NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER VAR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR NONE ASSIGN VAR VAR NUMBER RETURN NUMBER IF VAR NUMBER RETURN NUMBER RETURN NUMBER CLASS_DEF FUNC_DEF VAR NUMBER WHILE FUNC_CALL VAR VAR NUMBER VAR NUMBER RETURN VAR |
For a given non-negative integer N, find the next smallest Happy Number. A number is called happy if it leads to 1 after a sequence of steps. Wherein at each step the number is replaced by the sum of squares of its digits that is if we start with Happy Number and keep replacing it with sum of squares of its digits, we reach 1 at some point.
Example 1:
Input:
N = 8
Output:
10
Explanation:
Next happy number after 8 is 10 since
1*1 + 0*0 = 1
Example 2:
Input:
N = 10
Output
13
Explanation:
After 10, 13 is the smallest happy number because
1*1 + 3*3 = 10, so we replace 13 by 10 and 1*1 + 0*0 = 1.
Your Task:
You don't need to read input or print anything. Your task is to complete the function nextHappy() which takes an integer N as input parameters and returns an integer, next Happy number after N.
Expected Time Complexity: O(Nlog_{10}N)
Expected Space Complexity: O(1)
Constraints:
1<=N<=10^{5} | class Solution:
def nextHappy(self, N):
def ha(n, l):
l.append(n)
if n == 1:
return True
s = 0
while n:
t = n % 10
s += t * t
n = int(n / 10)
if s in l:
return False
return ha(s, l)
N += 1
while True:
l = []
if ha(N, l):
return N
break
N += 1 | CLASS_DEF FUNC_DEF FUNC_DEF EXPR FUNC_CALL VAR VAR IF VAR NUMBER RETURN NUMBER ASSIGN VAR NUMBER WHILE VAR ASSIGN VAR BIN_OP VAR NUMBER VAR BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER IF VAR VAR RETURN NUMBER RETURN FUNC_CALL VAR VAR VAR VAR NUMBER WHILE NUMBER ASSIGN VAR LIST IF FUNC_CALL VAR VAR VAR RETURN VAR VAR NUMBER |
For a given non-negative integer N, find the next smallest Happy Number. A number is called happy if it leads to 1 after a sequence of steps. Wherein at each step the number is replaced by the sum of squares of its digits that is if we start with Happy Number and keep replacing it with sum of squares of its digits, we reach 1 at some point.
Example 1:
Input:
N = 8
Output:
10
Explanation:
Next happy number after 8 is 10 since
1*1 + 0*0 = 1
Example 2:
Input:
N = 10
Output
13
Explanation:
After 10, 13 is the smallest happy number because
1*1 + 3*3 = 10, so we replace 13 by 10 and 1*1 + 0*0 = 1.
Your Task:
You don't need to read input or print anything. Your task is to complete the function nextHappy() which takes an integer N as input parameters and returns an integer, next Happy number after N.
Expected Time Complexity: O(Nlog_{10}N)
Expected Space Complexity: O(1)
Constraints:
1<=N<=10^{5} | class Solution:
def nextHappy(self, N):
hno = 0
while hno != 1:
N = N + 1
l = list(str(N))
hno = 0
while hno >= 10 or hno == 0:
sum1 = 0
for i in l:
sum1 = sum1 + int(i) ** 2
hno = sum1
l = list(str(sum1))
return N | CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER WHILE VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER WHILE VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR ASSIGN VAR BIN_OP VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR RETURN VAR |
For a given non-negative integer N, find the next smallest Happy Number. A number is called happy if it leads to 1 after a sequence of steps. Wherein at each step the number is replaced by the sum of squares of its digits that is if we start with Happy Number and keep replacing it with sum of squares of its digits, we reach 1 at some point.
Example 1:
Input:
N = 8
Output:
10
Explanation:
Next happy number after 8 is 10 since
1*1 + 0*0 = 1
Example 2:
Input:
N = 10
Output
13
Explanation:
After 10, 13 is the smallest happy number because
1*1 + 3*3 = 10, so we replace 13 by 10 and 1*1 + 0*0 = 1.
Your Task:
You don't need to read input or print anything. Your task is to complete the function nextHappy() which takes an integer N as input parameters and returns an integer, next Happy number after N.
Expected Time Complexity: O(Nlog_{10}N)
Expected Space Complexity: O(1)
Constraints:
1<=N<=10^{5} | class Solution:
def nextHappy(self, N):
memo = {}
d = {}
for i in range(10):
d[i] = i * i
def sol(cur, d):
if cur == 1:
return True
temp = 0
memo[cur] = False
for i in str(cur):
temp += d[int(i)]
if temp not in memo:
return sol(temp, d)
else:
return memo[temp]
while True:
N += 1
if sol(N, d):
return N | CLASS_DEF FUNC_DEF ASSIGN VAR DICT ASSIGN VAR DICT FOR VAR FUNC_CALL VAR NUMBER ASSIGN VAR VAR BIN_OP VAR VAR FUNC_DEF IF VAR NUMBER RETURN NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR VAR FUNC_CALL VAR VAR IF VAR VAR RETURN FUNC_CALL VAR VAR VAR RETURN VAR VAR WHILE NUMBER VAR NUMBER IF FUNC_CALL VAR VAR VAR RETURN VAR |
For a given non-negative integer N, find the next smallest Happy Number. A number is called happy if it leads to 1 after a sequence of steps. Wherein at each step the number is replaced by the sum of squares of its digits that is if we start with Happy Number and keep replacing it with sum of squares of its digits, we reach 1 at some point.
Example 1:
Input:
N = 8
Output:
10
Explanation:
Next happy number after 8 is 10 since
1*1 + 0*0 = 1
Example 2:
Input:
N = 10
Output
13
Explanation:
After 10, 13 is the smallest happy number because
1*1 + 3*3 = 10, so we replace 13 by 10 and 1*1 + 0*0 = 1.
Your Task:
You don't need to read input or print anything. Your task is to complete the function nextHappy() which takes an integer N as input parameters and returns an integer, next Happy number after N.
Expected Time Complexity: O(Nlog_{10}N)
Expected Space Complexity: O(1)
Constraints:
1<=N<=10^{5} | class Solution:
def nextHappy(self, N):
def ishappy(n):
temp = n
a = 0
while temp:
r = temp % 10
temp = temp // 10
a += r * r
if a > 9:
return ishappy(a)
if a == 1:
return True
return False
i = N + 1
while i:
if ishappy(i):
return i
i += 1 | CLASS_DEF FUNC_DEF FUNC_DEF ASSIGN VAR VAR ASSIGN VAR NUMBER WHILE VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER VAR BIN_OP VAR VAR IF VAR NUMBER RETURN FUNC_CALL VAR VAR IF VAR NUMBER RETURN NUMBER RETURN NUMBER ASSIGN VAR BIN_OP VAR NUMBER WHILE VAR IF FUNC_CALL VAR VAR RETURN VAR VAR NUMBER |
Kolya got string s for his birthday, the string consists of small English letters. He immediately added k more characters to the right of the string.
Then Borya came and said that the new string contained a tandem repeat of length l as a substring. How large could l be?
See notes for definition of a tandem repeat.
-----Input-----
The first line contains s (1 ≤ |s| ≤ 200). This string contains only small English letters. The second line contains number k (1 ≤ k ≤ 200) — the number of the added characters.
-----Output-----
Print a single number — the maximum length of the tandem repeat that could have occurred in the new string.
-----Examples-----
Input
aaba
2
Output
6
Input
aaabbbb
2
Output
6
Input
abracadabra
10
Output
20
-----Note-----
A tandem repeat of length 2n is string s, where for any position i (1 ≤ i ≤ n) the following condition fulfills: s_{i} = s_{i} + n.
In the first sample Kolya could obtain a string aabaab, in the second — aaabbbbbb, in the third — abracadabrabracadabra. | def main():
s = input()
k = int(input())
s += "?" * k
N = len(s)
result = 0
for l in range(1, N):
count = 0
for i in range(N - l):
if s[i] == s[i + l] or s[i + l] == "?":
count += 1
if count == l:
result = l
break
else:
count = 0
print(result * 2)
main() | FUNC_DEF ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR BIN_OP STRING VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR VAR IF VAR VAR VAR BIN_OP VAR VAR VAR BIN_OP VAR VAR STRING VAR NUMBER IF VAR VAR ASSIGN VAR VAR ASSIGN VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR |
Kolya got string s for his birthday, the string consists of small English letters. He immediately added k more characters to the right of the string.
Then Borya came and said that the new string contained a tandem repeat of length l as a substring. How large could l be?
See notes for definition of a tandem repeat.
-----Input-----
The first line contains s (1 ≤ |s| ≤ 200). This string contains only small English letters. The second line contains number k (1 ≤ k ≤ 200) — the number of the added characters.
-----Output-----
Print a single number — the maximum length of the tandem repeat that could have occurred in the new string.
-----Examples-----
Input
aaba
2
Output
6
Input
aaabbbb
2
Output
6
Input
abracadabra
10
Output
20
-----Note-----
A tandem repeat of length 2n is string s, where for any position i (1 ≤ i ≤ n) the following condition fulfills: s_{i} = s_{i} + n.
In the first sample Kolya could obtain a string aabaab, in the second — aaabbbbbb, in the third — abracadabrabracadabra. | s = input() + "X" * int(input())
for v in range(len(s) - len(s) % 2, 0, -2):
for i in range(len(s) - v + 1):
if all(s[j + v // 2] in ("X", s[j]) for j in range(i, i + v // 2)):
print(v), exit() | ASSIGN VAR BIN_OP FUNC_CALL VAR BIN_OP STRING FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER NUMBER FOR VAR FUNC_CALL VAR BIN_OP BIN_OP FUNC_CALL VAR VAR VAR NUMBER IF FUNC_CALL VAR VAR BIN_OP VAR BIN_OP VAR NUMBER STRING VAR VAR VAR FUNC_CALL VAR VAR BIN_OP VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR FUNC_CALL VAR |
Kolya got string s for his birthday, the string consists of small English letters. He immediately added k more characters to the right of the string.
Then Borya came and said that the new string contained a tandem repeat of length l as a substring. How large could l be?
See notes for definition of a tandem repeat.
-----Input-----
The first line contains s (1 ≤ |s| ≤ 200). This string contains only small English letters. The second line contains number k (1 ≤ k ≤ 200) — the number of the added characters.
-----Output-----
Print a single number — the maximum length of the tandem repeat that could have occurred in the new string.
-----Examples-----
Input
aaba
2
Output
6
Input
aaabbbb
2
Output
6
Input
abracadabra
10
Output
20
-----Note-----
A tandem repeat of length 2n is string s, where for any position i (1 ≤ i ≤ n) the following condition fulfills: s_{i} = s_{i} + n.
In the first sample Kolya could obtain a string aabaab, in the second — aaabbbbbb, in the third — abracadabrabracadabra. | s = input()
n = int(input())
def is_tandem(string, half):
if len(string) <= half:
return True
second = string[half:]
first = string[0 : len(second)]
return first == second
min_len = 0
if n >= len(s):
min_len = (len(s) + n) // 2 * 2
for i in range(n, len(s) + 1, 2):
total_len = i + n
half = total_len // 2
sub_str = s[len(s) - i :]
if is_tandem(sub_str, half):
min_len = i + n
if min_len < len(s):
for i in range(len(s)):
for j in range(i + 2, len(s) + 1, 2):
sub_str = s[i:j]
half = (j - i) // 2
if is_tandem(sub_str, half) and len(sub_str) > min_len:
min_len = len(sub_str)
print(min_len) | ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_DEF IF FUNC_CALL VAR VAR VAR RETURN NUMBER ASSIGN VAR VAR VAR ASSIGN VAR VAR NUMBER FUNC_CALL VAR VAR RETURN VAR VAR ASSIGN VAR NUMBER IF VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP FUNC_CALL VAR VAR VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR VAR BIN_OP FUNC_CALL VAR VAR VAR IF FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR IF VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER ASSIGN VAR VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR |
Kolya got string s for his birthday, the string consists of small English letters. He immediately added k more characters to the right of the string.
Then Borya came and said that the new string contained a tandem repeat of length l as a substring. How large could l be?
See notes for definition of a tandem repeat.
-----Input-----
The first line contains s (1 ≤ |s| ≤ 200). This string contains only small English letters. The second line contains number k (1 ≤ k ≤ 200) — the number of the added characters.
-----Output-----
Print a single number — the maximum length of the tandem repeat that could have occurred in the new string.
-----Examples-----
Input
aaba
2
Output
6
Input
aaabbbb
2
Output
6
Input
abracadabra
10
Output
20
-----Note-----
A tandem repeat of length 2n is string s, where for any position i (1 ≤ i ≤ n) the following condition fulfills: s_{i} = s_{i} + n.
In the first sample Kolya could obtain a string aabaab, in the second — aaabbbbbb, in the third — abracadabrabracadabra. | def tandemRepeat(i, j):
a = s[i : i + j - 1]
b = s[i - j : i - 1]
for k in range(len(a)):
if a[k] == b[k] or a[k] == "*" or b[k] == "*":
continue
else:
return False
return True
s = input()
n = int(input())
s = s + "*" * n
ans = -1
for i in range(len(s)):
for j in range(1, len(s)):
if i + j - 1 < len(s) and i - j >= 0 and tandemRepeat(i, j):
if ans < j:
ans = j
print(ans * 2) | FUNC_DEF ASSIGN VAR VAR VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR VAR BIN_OP VAR VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR VAR VAR STRING VAR VAR STRING RETURN NUMBER RETURN NUMBER ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR BIN_OP VAR BIN_OP STRING VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR IF BIN_OP BIN_OP VAR VAR NUMBER FUNC_CALL VAR VAR BIN_OP VAR VAR NUMBER FUNC_CALL VAR VAR VAR IF VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER |
Kolya got string s for his birthday, the string consists of small English letters. He immediately added k more characters to the right of the string.
Then Borya came and said that the new string contained a tandem repeat of length l as a substring. How large could l be?
See notes for definition of a tandem repeat.
-----Input-----
The first line contains s (1 ≤ |s| ≤ 200). This string contains only small English letters. The second line contains number k (1 ≤ k ≤ 200) — the number of the added characters.
-----Output-----
Print a single number — the maximum length of the tandem repeat that could have occurred in the new string.
-----Examples-----
Input
aaba
2
Output
6
Input
aaabbbb
2
Output
6
Input
abracadabra
10
Output
20
-----Note-----
A tandem repeat of length 2n is string s, where for any position i (1 ≤ i ≤ n) the following condition fulfills: s_{i} = s_{i} + n.
In the first sample Kolya could obtain a string aabaab, in the second — aaabbbbbb, in the third — abracadabrabracadabra. | s = input()
k = int(input())
m = len(s) + k
ans = 0
if k >= len(s):
ans = m - m % 2
for x in range(len(s) // 2 + 1, 0, -1):
for y in range(len(s) - 2 * x):
if s[y : y + x] == s[y + x : y + 2 * x]:
ans = max(ans, 2 * x)
for l in range(m // 2, 0, -1):
c = s[m - 2 * l : m - l]
if c[: l - k] == s[m - l :]:
ans = max(ans, 2 * l)
break
print(ans) | ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR VAR ASSIGN VAR NUMBER IF VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER NUMBER NUMBER FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR BIN_OP NUMBER VAR IF VAR VAR BIN_OP VAR VAR VAR BIN_OP VAR VAR BIN_OP VAR BIN_OP NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP NUMBER VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR VAR BIN_OP VAR BIN_OP NUMBER VAR BIN_OP VAR VAR IF VAR BIN_OP VAR VAR VAR BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP NUMBER VAR EXPR FUNC_CALL VAR VAR |
Kolya got string s for his birthday, the string consists of small English letters. He immediately added k more characters to the right of the string.
Then Borya came and said that the new string contained a tandem repeat of length l as a substring. How large could l be?
See notes for definition of a tandem repeat.
-----Input-----
The first line contains s (1 ≤ |s| ≤ 200). This string contains only small English letters. The second line contains number k (1 ≤ k ≤ 200) — the number of the added characters.
-----Output-----
Print a single number — the maximum length of the tandem repeat that could have occurred in the new string.
-----Examples-----
Input
aaba
2
Output
6
Input
aaabbbb
2
Output
6
Input
abracadabra
10
Output
20
-----Note-----
A tandem repeat of length 2n is string s, where for any position i (1 ≤ i ≤ n) the following condition fulfills: s_{i} = s_{i} + n.
In the first sample Kolya could obtain a string aabaab, in the second — aaabbbbbb, in the third — abracadabrabracadabra. | s = input()
n = len(s)
k = int(input())
best = 0
if k >= n:
best = (k + n) // 2 * 2
for st in range(n):
for ln in range(2, n - st):
if s[st : st + ln // 2] == s[st + ln // 2 : st + ln]:
best = max(best, ln)
for st1 in range(n):
if (n + k - st1) % 2 == 0:
lens = (n + k - st1) // 2
nlen = lens - k
st2 = st1 + lens
if s[st1 : st1 + nlen] == s[st2:]:
best = max(best, lens * 2)
print(best) | ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR NUMBER IF VAR VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR VAR IF VAR VAR BIN_OP VAR BIN_OP VAR NUMBER VAR BIN_OP VAR BIN_OP VAR NUMBER BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FOR VAR FUNC_CALL VAR VAR IF BIN_OP BIN_OP BIN_OP VAR VAR VAR NUMBER NUMBER ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR VAR IF VAR VAR BIN_OP VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR |
Kolya got string s for his birthday, the string consists of small English letters. He immediately added k more characters to the right of the string.
Then Borya came and said that the new string contained a tandem repeat of length l as a substring. How large could l be?
See notes for definition of a tandem repeat.
-----Input-----
The first line contains s (1 ≤ |s| ≤ 200). This string contains only small English letters. The second line contains number k (1 ≤ k ≤ 200) — the number of the added characters.
-----Output-----
Print a single number — the maximum length of the tandem repeat that could have occurred in the new string.
-----Examples-----
Input
aaba
2
Output
6
Input
aaabbbb
2
Output
6
Input
abracadabra
10
Output
20
-----Note-----
A tandem repeat of length 2n is string s, where for any position i (1 ≤ i ≤ n) the following condition fulfills: s_{i} = s_{i} + n.
In the first sample Kolya could obtain a string aabaab, in the second — aaabbbbbb, in the third — abracadabrabracadabra. | def tandem(s):
n = len(s) // 2
for i in range(n):
if s[i] != s[i + n] and s[i] != "#" and s[i + n] != "#":
return False
return True
def main():
s = input()
k = int(input())
s = s + "#" * k
n = len(s)
ans = 0
for i in range(n):
for j in range(i + 2, n + 1, 2):
if tandem(s[i:j]):
ans = max(ans, j - i)
print(ans)
main() | FUNC_DEF ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR BIN_OP VAR VAR VAR VAR STRING VAR BIN_OP VAR VAR STRING RETURN NUMBER RETURN NUMBER FUNC_DEF ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR BIN_OP VAR BIN_OP STRING VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER NUMBER IF FUNC_CALL VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR |
Kolya got string s for his birthday, the string consists of small English letters. He immediately added k more characters to the right of the string.
Then Borya came and said that the new string contained a tandem repeat of length l as a substring. How large could l be?
See notes for definition of a tandem repeat.
-----Input-----
The first line contains s (1 ≤ |s| ≤ 200). This string contains only small English letters. The second line contains number k (1 ≤ k ≤ 200) — the number of the added characters.
-----Output-----
Print a single number — the maximum length of the tandem repeat that could have occurred in the new string.
-----Examples-----
Input
aaba
2
Output
6
Input
aaabbbb
2
Output
6
Input
abracadabra
10
Output
20
-----Note-----
A tandem repeat of length 2n is string s, where for any position i (1 ≤ i ≤ n) the following condition fulfills: s_{i} = s_{i} + n.
In the first sample Kolya could obtain a string aabaab, in the second — aaabbbbbb, in the third — abracadabrabracadabra. | import sys
str = input()
k = int(input())
ans = 0
if k >= len(str):
print((k + len(str)) // 2 * 2)
return
for i in range(len(str)):
for j in range(0, i + 1):
len1 = len(str[j : i + 1])
len2 = len(str[i + 1 :])
minn = min(len1, len2)
if str[j : j + minn] == str[i + 1 : i + 1 + minn]:
if minn == len1:
ans = max(ans, len1 * 2)
elif k + len2 >= len1:
ans = max(ans, len1 * 2)
print(ans) | IMPORT ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR NUMBER IF VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP BIN_OP BIN_OP VAR FUNC_CALL VAR VAR NUMBER NUMBER RETURN FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR IF VAR VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER BIN_OP BIN_OP VAR NUMBER VAR IF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER IF BIN_OP VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR |
Kolya got string s for his birthday, the string consists of small English letters. He immediately added k more characters to the right of the string.
Then Borya came and said that the new string contained a tandem repeat of length l as a substring. How large could l be?
See notes for definition of a tandem repeat.
-----Input-----
The first line contains s (1 ≤ |s| ≤ 200). This string contains only small English letters. The second line contains number k (1 ≤ k ≤ 200) — the number of the added characters.
-----Output-----
Print a single number — the maximum length of the tandem repeat that could have occurred in the new string.
-----Examples-----
Input
aaba
2
Output
6
Input
aaabbbb
2
Output
6
Input
abracadabra
10
Output
20
-----Note-----
A tandem repeat of length 2n is string s, where for any position i (1 ≤ i ≤ n) the following condition fulfills: s_{i} = s_{i} + n.
In the first sample Kolya could obtain a string aabaab, in the second — aaabbbbbb, in the third — abracadabrabracadabra. | from sys import stdin, stdout
nmbr = lambda: int(stdin.readline())
lst = lambda: list(map(int, stdin.readline().split()))
def fn(l):
for i in range(n - l + 1):
f = 1
for j in range(i, i + l // 2):
if a[j] == a[j + l // 2] or a[j + l // 2] == " ":
continue
f = 0
break
if f == 1:
return True
return False
for m in range(1):
s = list(input())
n = len(s)
k = nmbr()
n = n + k
a = s + [" "] * k
ans = 0
for l in range(2, n + 1, 2):
if fn(l):
ans = max(ans, l)
print(ans) | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FUNC_DEF FOR VAR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR BIN_OP VAR BIN_OP VAR NUMBER IF VAR VAR VAR BIN_OP VAR BIN_OP VAR NUMBER VAR BIN_OP VAR BIN_OP VAR NUMBER STRING ASSIGN VAR NUMBER IF VAR NUMBER RETURN NUMBER RETURN NUMBER FOR VAR FUNC_CALL VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR BIN_OP LIST STRING VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER NUMBER IF FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR |
Kolya got string s for his birthday, the string consists of small English letters. He immediately added k more characters to the right of the string.
Then Borya came and said that the new string contained a tandem repeat of length l as a substring. How large could l be?
See notes for definition of a tandem repeat.
-----Input-----
The first line contains s (1 ≤ |s| ≤ 200). This string contains only small English letters. The second line contains number k (1 ≤ k ≤ 200) — the number of the added characters.
-----Output-----
Print a single number — the maximum length of the tandem repeat that could have occurred in the new string.
-----Examples-----
Input
aaba
2
Output
6
Input
aaabbbb
2
Output
6
Input
abracadabra
10
Output
20
-----Note-----
A tandem repeat of length 2n is string s, where for any position i (1 ≤ i ≤ n) the following condition fulfills: s_{i} = s_{i} + n.
In the first sample Kolya could obtain a string aabaab, in the second — aaabbbbbb, in the third — abracadabrabracadabra. | t, k = input(), int(input())
n = len(t)
s = min(k, (n + k) // 2)
for i in range(n - k):
for j in range(i + s + 1, min(n, (n + i + k) // 2 + 1)):
d = j - i
if n - j > d:
if t[i:j] == t[j : j + d]:
s = d
elif t[i : n - d] == t[j:n]:
s = d
print(2 * s) | ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR VAR FOR VAR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR NUMBER FUNC_CALL VAR VAR BIN_OP BIN_OP BIN_OP BIN_OP VAR VAR VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR VAR IF BIN_OP VAR VAR VAR IF VAR VAR VAR VAR VAR BIN_OP VAR VAR ASSIGN VAR VAR IF VAR VAR BIN_OP VAR VAR VAR VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR BIN_OP NUMBER VAR |
Kolya got string s for his birthday, the string consists of small English letters. He immediately added k more characters to the right of the string.
Then Borya came and said that the new string contained a tandem repeat of length l as a substring. How large could l be?
See notes for definition of a tandem repeat.
-----Input-----
The first line contains s (1 ≤ |s| ≤ 200). This string contains only small English letters. The second line contains number k (1 ≤ k ≤ 200) — the number of the added characters.
-----Output-----
Print a single number — the maximum length of the tandem repeat that could have occurred in the new string.
-----Examples-----
Input
aaba
2
Output
6
Input
aaabbbb
2
Output
6
Input
abracadabra
10
Output
20
-----Note-----
A tandem repeat of length 2n is string s, where for any position i (1 ≤ i ≤ n) the following condition fulfills: s_{i} = s_{i} + n.
In the first sample Kolya could obtain a string aabaab, in the second — aaabbbbbb, in the third — abracadabrabracadabra. | s = input()
k = int(input())
n = len(s)
ans = min(2 * k, 2 * n)
start = min(k + 1, n + 1)
end = (n + k) // 2 + 1
for i in range(start, end):
flag = True
for j in range(n - 1, max(-1, n - (i - k) - 1), -1):
if s[j] != s[j - i]:
flag = False
break
if flag:
ans = 2 * i
for l in range(n):
for d in range(n - l):
if l + 2 * d <= n and s[l : l + d] == s[l + d : l + 2 * d]:
ans = max(ans, d * 2)
print(ans) | ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP NUMBER VAR BIN_OP NUMBER VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER FUNC_CALL VAR NUMBER BIN_OP BIN_OP VAR BIN_OP VAR VAR NUMBER NUMBER IF VAR VAR VAR BIN_OP VAR VAR ASSIGN VAR NUMBER IF VAR ASSIGN VAR BIN_OP NUMBER VAR FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR VAR IF BIN_OP VAR BIN_OP NUMBER VAR VAR VAR VAR BIN_OP VAR VAR VAR BIN_OP VAR VAR BIN_OP VAR BIN_OP NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR |
Kolya got string s for his birthday, the string consists of small English letters. He immediately added k more characters to the right of the string.
Then Borya came and said that the new string contained a tandem repeat of length l as a substring. How large could l be?
See notes for definition of a tandem repeat.
-----Input-----
The first line contains s (1 ≤ |s| ≤ 200). This string contains only small English letters. The second line contains number k (1 ≤ k ≤ 200) — the number of the added characters.
-----Output-----
Print a single number — the maximum length of the tandem repeat that could have occurred in the new string.
-----Examples-----
Input
aaba
2
Output
6
Input
aaabbbb
2
Output
6
Input
abracadabra
10
Output
20
-----Note-----
A tandem repeat of length 2n is string s, where for any position i (1 ≤ i ≤ n) the following condition fulfills: s_{i} = s_{i} + n.
In the first sample Kolya could obtain a string aabaab, in the second — aaabbbbbb, in the third — abracadabrabracadabra. | s = input()
k = int(input())
s += "?" * k
n = len(s)
res = 0
for i in range(n):
for j in range(i + 1, n):
d = j - i + 1
if d % 2 != 0:
continue
valid = True
for e in range(i, j + 1):
if e + d // 2 > j:
break
if s[e] == "?" or s[e + d // 2] == "?" or s[e] == s[e + d // 2]:
continue
valid = False
if valid:
res = max(res, d)
print(res) | ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR BIN_OP STRING VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF BIN_OP VAR NUMBER NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER IF BIN_OP VAR BIN_OP VAR NUMBER VAR IF VAR VAR STRING VAR BIN_OP VAR BIN_OP VAR NUMBER STRING VAR VAR VAR BIN_OP VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER IF VAR ASSIGN VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR |
Kolya got string s for his birthday, the string consists of small English letters. He immediately added k more characters to the right of the string.
Then Borya came and said that the new string contained a tandem repeat of length l as a substring. How large could l be?
See notes for definition of a tandem repeat.
-----Input-----
The first line contains s (1 ≤ |s| ≤ 200). This string contains only small English letters. The second line contains number k (1 ≤ k ≤ 200) — the number of the added characters.
-----Output-----
Print a single number — the maximum length of the tandem repeat that could have occurred in the new string.
-----Examples-----
Input
aaba
2
Output
6
Input
aaabbbb
2
Output
6
Input
abracadabra
10
Output
20
-----Note-----
A tandem repeat of length 2n is string s, where for any position i (1 ≤ i ≤ n) the following condition fulfills: s_{i} = s_{i} + n.
In the first sample Kolya could obtain a string aabaab, in the second — aaabbbbbb, in the third — abracadabrabracadabra. | s = input().rstrip()
k = int(input())
if k > len(s):
print(len(s) + k - ((len(s) + k) % 2 == 1))
else:
aux = []
for j in range(1, len(s)):
i = len(s) - 1
cnt = 0
while i - j >= 0 and s[i] == s[i - j]:
i -= 1
cnt += 1
if cnt == j:
break
diff = i - (len(s) - j - 1)
if diff <= k:
aux.append(2 * j)
aux.append(2 * k)
for i in range(len(s)):
for j in range((len(s) - i) // 2):
if s[i : i + j] == s[i + j : i + 2 * j]:
aux.append(2 * j)
print(max(aux)) | ASSIGN VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR IF VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP BIN_OP FUNC_CALL VAR VAR VAR BIN_OP BIN_OP FUNC_CALL VAR VAR VAR NUMBER NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER WHILE BIN_OP VAR VAR NUMBER VAR VAR VAR BIN_OP VAR VAR VAR NUMBER VAR NUMBER IF VAR VAR ASSIGN VAR BIN_OP VAR BIN_OP BIN_OP FUNC_CALL VAR VAR VAR NUMBER IF VAR VAR EXPR FUNC_CALL VAR BIN_OP NUMBER VAR EXPR FUNC_CALL VAR BIN_OP NUMBER VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP BIN_OP FUNC_CALL VAR VAR VAR NUMBER IF VAR VAR BIN_OP VAR VAR VAR BIN_OP VAR VAR BIN_OP VAR BIN_OP NUMBER VAR EXPR FUNC_CALL VAR BIN_OP NUMBER VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR |
Kolya got string s for his birthday, the string consists of small English letters. He immediately added k more characters to the right of the string.
Then Borya came and said that the new string contained a tandem repeat of length l as a substring. How large could l be?
See notes for definition of a tandem repeat.
-----Input-----
The first line contains s (1 ≤ |s| ≤ 200). This string contains only small English letters. The second line contains number k (1 ≤ k ≤ 200) — the number of the added characters.
-----Output-----
Print a single number — the maximum length of the tandem repeat that could have occurred in the new string.
-----Examples-----
Input
aaba
2
Output
6
Input
aaabbbb
2
Output
6
Input
abracadabra
10
Output
20
-----Note-----
A tandem repeat of length 2n is string s, where for any position i (1 ≤ i ≤ n) the following condition fulfills: s_{i} = s_{i} + n.
In the first sample Kolya could obtain a string aabaab, in the second — aaabbbbbb, in the third — abracadabrabracadabra. | s = [*input()]
k = int(input())
s.extend(["?"] * k)
m = 0
for i in range(1, len(s) // 2 + 1):
for j in range(len(s) - 2 * i + 1):
q = 0
for k in range(i):
if s[j + k] == "?" or s[j + k + i] == "?":
continue
if s[j + k] != s[j + k + i]:
q = 1
break
if q:
continue
else:
m = i
break
print(2 * m) | ASSIGN VAR LIST FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR EXPR FUNC_CALL VAR BIN_OP LIST STRING VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR BIN_OP BIN_OP FUNC_CALL VAR VAR BIN_OP NUMBER VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR BIN_OP VAR VAR STRING VAR BIN_OP BIN_OP VAR VAR VAR STRING IF VAR BIN_OP VAR VAR VAR BIN_OP BIN_OP VAR VAR VAR ASSIGN VAR NUMBER IF VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR BIN_OP NUMBER VAR |
Kolya got string s for his birthday, the string consists of small English letters. He immediately added k more characters to the right of the string.
Then Borya came and said that the new string contained a tandem repeat of length l as a substring. How large could l be?
See notes for definition of a tandem repeat.
-----Input-----
The first line contains s (1 ≤ |s| ≤ 200). This string contains only small English letters. The second line contains number k (1 ≤ k ≤ 200) — the number of the added characters.
-----Output-----
Print a single number — the maximum length of the tandem repeat that could have occurred in the new string.
-----Examples-----
Input
aaba
2
Output
6
Input
aaabbbb
2
Output
6
Input
abracadabra
10
Output
20
-----Note-----
A tandem repeat of length 2n is string s, where for any position i (1 ≤ i ≤ n) the following condition fulfills: s_{i} = s_{i} + n.
In the first sample Kolya could obtain a string aabaab, in the second — aaabbbbbb, in the third — abracadabrabracadabra. | s = input()
k = int(input())
le = len(s) + k
m = 0
for i in range(len(s)):
j = 1
while i + 2 * j <= le:
mi = i + j
la = i + 2 * j
if mi >= len(s):
m = max(m, j)
if la <= len(s) and s[i:mi] == s[mi:la]:
m = max(m, j)
if mi < len(s):
xl = len(s) - mi
if s[i : i + xl] == s[mi : len(s)]:
m = max(m, j)
j += 1
print(2 * m) | ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER WHILE BIN_OP VAR BIN_OP NUMBER VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR BIN_OP NUMBER VAR IF VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR IF VAR FUNC_CALL VAR VAR VAR VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR IF VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR VAR IF VAR VAR BIN_OP VAR VAR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP NUMBER VAR |
Kolya got string s for his birthday, the string consists of small English letters. He immediately added k more characters to the right of the string.
Then Borya came and said that the new string contained a tandem repeat of length l as a substring. How large could l be?
See notes for definition of a tandem repeat.
-----Input-----
The first line contains s (1 ≤ |s| ≤ 200). This string contains only small English letters. The second line contains number k (1 ≤ k ≤ 200) — the number of the added characters.
-----Output-----
Print a single number — the maximum length of the tandem repeat that could have occurred in the new string.
-----Examples-----
Input
aaba
2
Output
6
Input
aaabbbb
2
Output
6
Input
abracadabra
10
Output
20
-----Note-----
A tandem repeat of length 2n is string s, where for any position i (1 ≤ i ≤ n) the following condition fulfills: s_{i} = s_{i} + n.
In the first sample Kolya could obtain a string aabaab, in the second — aaabbbbbb, in the third — abracadabrabracadabra. | def solve():
str1 = input()
num = int(input())
str2 = str1 + "0" * num
ans = num / 2
length = len(str2)
for i in range(length):
for j in range(2, length + 1, 2):
if i + j <= length:
lstr = str2[i : i + j // 2]
rstr = str2[i + j // 2 : i + j]
ok = True
for k in range(j // 2):
if lstr[k] == rstr[k] or rstr[k] == "0":
pass
else:
ok = False
if ok:
ans = max(ans, j)
print(ans)
solve() | FUNC_DEF ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR BIN_OP VAR BIN_OP STRING VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER NUMBER IF BIN_OP VAR VAR VAR ASSIGN VAR VAR VAR BIN_OP VAR BIN_OP VAR NUMBER ASSIGN VAR VAR BIN_OP VAR BIN_OP VAR NUMBER BIN_OP VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER IF VAR VAR VAR VAR VAR VAR STRING ASSIGN VAR NUMBER IF VAR ASSIGN VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR |
Kolya got string s for his birthday, the string consists of small English letters. He immediately added k more characters to the right of the string.
Then Borya came and said that the new string contained a tandem repeat of length l as a substring. How large could l be?
See notes for definition of a tandem repeat.
-----Input-----
The first line contains s (1 ≤ |s| ≤ 200). This string contains only small English letters. The second line contains number k (1 ≤ k ≤ 200) — the number of the added characters.
-----Output-----
Print a single number — the maximum length of the tandem repeat that could have occurred in the new string.
-----Examples-----
Input
aaba
2
Output
6
Input
aaabbbb
2
Output
6
Input
abracadabra
10
Output
20
-----Note-----
A tandem repeat of length 2n is string s, where for any position i (1 ≤ i ≤ n) the following condition fulfills: s_{i} = s_{i} + n.
In the first sample Kolya could obtain a string aabaab, in the second — aaabbbbbb, in the third — abracadabrabracadabra. | s = input()
k = int(input())
ans = 0
def get_max_tandem_repeat(s, n):
while n > 0:
for i in range(len(s) - 2):
if i + 2 * n > len(s):
n -= 1
break
flag = True
for j in range(i, i + n):
if s[j] != s[j + n] and s[j + n] != "_":
flag = False
break
if flag:
return n * 2
if k >= len(s):
ans = (k + len(s)) // 2 * 2
else:
s = s + "_" * k
n = len(s) // 2
ans = get_max_tandem_repeat(s, n)
print(ans) | ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR NUMBER FUNC_DEF WHILE VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER IF BIN_OP VAR BIN_OP NUMBER VAR FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR BIN_OP VAR VAR IF VAR VAR VAR BIN_OP VAR VAR VAR BIN_OP VAR VAR STRING ASSIGN VAR NUMBER IF VAR RETURN BIN_OP VAR NUMBER IF VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR FUNC_CALL VAR VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR BIN_OP STRING VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR |
Kolya got string s for his birthday, the string consists of small English letters. He immediately added k more characters to the right of the string.
Then Borya came and said that the new string contained a tandem repeat of length l as a substring. How large could l be?
See notes for definition of a tandem repeat.
-----Input-----
The first line contains s (1 ≤ |s| ≤ 200). This string contains only small English letters. The second line contains number k (1 ≤ k ≤ 200) — the number of the added characters.
-----Output-----
Print a single number — the maximum length of the tandem repeat that could have occurred in the new string.
-----Examples-----
Input
aaba
2
Output
6
Input
aaabbbb
2
Output
6
Input
abracadabra
10
Output
20
-----Note-----
A tandem repeat of length 2n is string s, where for any position i (1 ≤ i ≤ n) the following condition fulfills: s_{i} = s_{i} + n.
In the first sample Kolya could obtain a string aabaab, in the second — aaabbbbbb, in the third — abracadabrabracadabra. | def match(str1, str2):
for t in range(len(str1)):
if str1[t] != str2[t] and str2[t] != "_":
return False
return True
string = input()
k = int(input())
length = len(string)
mark = False
if k >= length:
print(2 * int((length + k) / 2))
else:
s = string + k * "_"
n = int(len(s) / 2)
for i in range(n, 0, -1):
for j in range(len(s) - 2 * i + 1):
if match(s[j : i + j], s[i + j : i + i + j]) and mark == False:
print(2 * i)
mark = True | FUNC_DEF FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR VAR VAR STRING RETURN NUMBER RETURN NUMBER ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER IF VAR VAR EXPR FUNC_CALL VAR BIN_OP NUMBER FUNC_CALL VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP VAR BIN_OP VAR STRING ASSIGN VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR BIN_OP BIN_OP FUNC_CALL VAR VAR BIN_OP NUMBER VAR NUMBER IF FUNC_CALL VAR VAR VAR BIN_OP VAR VAR VAR BIN_OP VAR VAR BIN_OP BIN_OP VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP NUMBER VAR ASSIGN VAR NUMBER |
Kolya got string s for his birthday, the string consists of small English letters. He immediately added k more characters to the right of the string.
Then Borya came and said that the new string contained a tandem repeat of length l as a substring. How large could l be?
See notes for definition of a tandem repeat.
-----Input-----
The first line contains s (1 ≤ |s| ≤ 200). This string contains only small English letters. The second line contains number k (1 ≤ k ≤ 200) — the number of the added characters.
-----Output-----
Print a single number — the maximum length of the tandem repeat that could have occurred in the new string.
-----Examples-----
Input
aaba
2
Output
6
Input
aaabbbb
2
Output
6
Input
abracadabra
10
Output
20
-----Note-----
A tandem repeat of length 2n is string s, where for any position i (1 ≤ i ≤ n) the following condition fulfills: s_{i} = s_{i} + n.
In the first sample Kolya could obtain a string aabaab, in the second — aaabbbbbb, in the third — abracadabrabracadabra. | def tandem(n, a, i):
for k in range(i, i + n):
if a[k] != a[k + n] and a[k + n] != "??":
return 0
return 1
s = list(input().strip())
k = int(input())
s += ["??" for i in range(k)]
res = 0
for i in range(len(s)):
for n in range(1, (len(s) - i) // 2 + 1):
if tandem(n, s, i):
res = max(res, n * 2)
print(res) | FUNC_DEF FOR VAR FUNC_CALL VAR VAR BIN_OP VAR VAR IF VAR VAR VAR BIN_OP VAR VAR VAR BIN_OP VAR VAR STRING RETURN NUMBER RETURN NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR STRING VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP BIN_OP BIN_OP FUNC_CALL VAR VAR VAR NUMBER NUMBER IF FUNC_CALL VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR |
Kolya got string s for his birthday, the string consists of small English letters. He immediately added k more characters to the right of the string.
Then Borya came and said that the new string contained a tandem repeat of length l as a substring. How large could l be?
See notes for definition of a tandem repeat.
-----Input-----
The first line contains s (1 ≤ |s| ≤ 200). This string contains only small English letters. The second line contains number k (1 ≤ k ≤ 200) — the number of the added characters.
-----Output-----
Print a single number — the maximum length of the tandem repeat that could have occurred in the new string.
-----Examples-----
Input
aaba
2
Output
6
Input
aaabbbb
2
Output
6
Input
abracadabra
10
Output
20
-----Note-----
A tandem repeat of length 2n is string s, where for any position i (1 ≤ i ≤ n) the following condition fulfills: s_{i} = s_{i} + n.
In the first sample Kolya could obtain a string aabaab, in the second — aaabbbbbb, in the third — abracadabrabracadabra. | def solve():
s = input()
k = int(input())
len_s = len(s)
len_f = len_s + k
max_n = int(len_f / 2)
for i in range(max_n, 0, -1):
for j in range(len_f):
if len_f - j >= 2 * i:
ok = True
for k in range(j, j + i):
if k + i < len_s and s[k] != s[k + i]:
ok = False
if ok:
print(i * 2)
return
solve() | FUNC_DEF ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR IF BIN_OP VAR VAR BIN_OP NUMBER VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR BIN_OP VAR VAR IF BIN_OP VAR VAR VAR VAR VAR VAR BIN_OP VAR VAR ASSIGN VAR NUMBER IF VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER RETURN EXPR FUNC_CALL VAR |
Kolya got string s for his birthday, the string consists of small English letters. He immediately added k more characters to the right of the string.
Then Borya came and said that the new string contained a tandem repeat of length l as a substring. How large could l be?
See notes for definition of a tandem repeat.
-----Input-----
The first line contains s (1 ≤ |s| ≤ 200). This string contains only small English letters. The second line contains number k (1 ≤ k ≤ 200) — the number of the added characters.
-----Output-----
Print a single number — the maximum length of the tandem repeat that could have occurred in the new string.
-----Examples-----
Input
aaba
2
Output
6
Input
aaabbbb
2
Output
6
Input
abracadabra
10
Output
20
-----Note-----
A tandem repeat of length 2n is string s, where for any position i (1 ≤ i ≤ n) the following condition fulfills: s_{i} = s_{i} + n.
In the first sample Kolya could obtain a string aabaab, in the second — aaabbbbbb, in the third — abracadabrabracadabra. | def tandem(s, start, l, max_len, n):
if start >= n:
if max_len - start >= 2 * l:
return True
return False
if max_len - start < 2 * l:
return False
for i in range(start, start + l):
si = s[start]
if i + l > max_len:
return False
if i + l < len(s) and s[i] != s[i + l]:
return False
return True
def found(s, l, max_len):
if l == 0:
return True
n = len(s)
l = l // 2
for i in range(1, n):
start = i
if tandem(s, start, l, max_len, n):
return True
while len(s) != n:
s.pop()
return False
def solve(s, k):
max_len = 2 * ((len(s) + k) // 2)
s1 = [0]
s1.extend(s)
s = s1[:]
while True:
if found(s, max_len, len(s) + k):
print(max_len)
return
max_len -= 2
def main():
s = list(input())
k = int(input())
solve(s, k)
main() | FUNC_DEF IF VAR VAR IF BIN_OP VAR VAR BIN_OP NUMBER VAR RETURN NUMBER RETURN NUMBER IF BIN_OP VAR VAR BIN_OP NUMBER VAR RETURN NUMBER FOR VAR FUNC_CALL VAR VAR BIN_OP VAR VAR ASSIGN VAR VAR VAR IF BIN_OP VAR VAR VAR RETURN NUMBER IF BIN_OP VAR VAR FUNC_CALL VAR VAR VAR VAR VAR BIN_OP VAR VAR RETURN NUMBER RETURN NUMBER FUNC_DEF IF VAR NUMBER RETURN NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR IF FUNC_CALL VAR VAR VAR VAR VAR VAR RETURN NUMBER WHILE FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR RETURN NUMBER FUNC_DEF ASSIGN VAR BIN_OP NUMBER BIN_OP BIN_OP FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR LIST NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR WHILE NUMBER IF FUNC_CALL VAR VAR VAR BIN_OP FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR RETURN VAR NUMBER FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR |
Kolya got string s for his birthday, the string consists of small English letters. He immediately added k more characters to the right of the string.
Then Borya came and said that the new string contained a tandem repeat of length l as a substring. How large could l be?
See notes for definition of a tandem repeat.
-----Input-----
The first line contains s (1 ≤ |s| ≤ 200). This string contains only small English letters. The second line contains number k (1 ≤ k ≤ 200) — the number of the added characters.
-----Output-----
Print a single number — the maximum length of the tandem repeat that could have occurred in the new string.
-----Examples-----
Input
aaba
2
Output
6
Input
aaabbbb
2
Output
6
Input
abracadabra
10
Output
20
-----Note-----
A tandem repeat of length 2n is string s, where for any position i (1 ≤ i ≤ n) the following condition fulfills: s_{i} = s_{i} + n.
In the first sample Kolya could obtain a string aabaab, in the second — aaabbbbbb, in the third — abracadabrabracadabra. | s, r = input(), int(input())
def compare(a, b):
return a[: len(b)] == b
def f(s, n, N):
if not s or n < 2 * N:
return N
for i in range(n // 2, 0, -1):
if compare(s[:i], s[i : 2 * i]):
return f(s[1:], n - 1, max(i, N))
return f(s[1:], n - 1, N)
if len(s) < r:
print((len(s) + r) // 2 * 2)
else:
print(2 * f(s, len(s) + r, 0)) | ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_DEF RETURN VAR FUNC_CALL VAR VAR VAR FUNC_DEF IF VAR VAR BIN_OP NUMBER VAR RETURN VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER IF FUNC_CALL VAR VAR VAR VAR VAR BIN_OP NUMBER VAR RETURN FUNC_CALL VAR VAR NUMBER BIN_OP VAR NUMBER FUNC_CALL VAR VAR VAR RETURN FUNC_CALL VAR VAR NUMBER BIN_OP VAR NUMBER VAR IF FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP BIN_OP BIN_OP FUNC_CALL VAR VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR BIN_OP NUMBER FUNC_CALL VAR VAR BIN_OP FUNC_CALL VAR VAR VAR NUMBER |
Kolya got string s for his birthday, the string consists of small English letters. He immediately added k more characters to the right of the string.
Then Borya came and said that the new string contained a tandem repeat of length l as a substring. How large could l be?
See notes for definition of a tandem repeat.
-----Input-----
The first line contains s (1 ≤ |s| ≤ 200). This string contains only small English letters. The second line contains number k (1 ≤ k ≤ 200) — the number of the added characters.
-----Output-----
Print a single number — the maximum length of the tandem repeat that could have occurred in the new string.
-----Examples-----
Input
aaba
2
Output
6
Input
aaabbbb
2
Output
6
Input
abracadabra
10
Output
20
-----Note-----
A tandem repeat of length 2n is string s, where for any position i (1 ≤ i ≤ n) the following condition fulfills: s_{i} = s_{i} + n.
In the first sample Kolya could obtain a string aabaab, in the second — aaabbbbbb, in the third — abracadabrabracadabra. | s = input()
k = int(input())
n = len(s)
if k >= n:
print(int(2 * ((n + k) // 2)))
raise SystemExit
ll = 0
for i in range(k + 1):
for l in range((n + i) // 2, i - 1, -1):
if s[n - (l - i) : n] == s[n + i - 2 * l : n - l]:
if l > ll:
ll = l
break
j = ll
while 2 * j <= n:
j = j + 1
for i in range(n - 2 * j):
if s[i : i + j] == s[i + j : i + 2 * j]:
ll = j
break
print(int(2 * ll)) | ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR IF VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR BIN_OP NUMBER BIN_OP BIN_OP VAR VAR NUMBER VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR NUMBER BIN_OP VAR NUMBER NUMBER IF VAR BIN_OP VAR BIN_OP VAR VAR VAR VAR BIN_OP BIN_OP VAR VAR BIN_OP NUMBER VAR BIN_OP VAR VAR IF VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR WHILE BIN_OP NUMBER VAR VAR ASSIGN VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR BIN_OP NUMBER VAR IF VAR VAR BIN_OP VAR VAR VAR BIN_OP VAR VAR BIN_OP VAR BIN_OP NUMBER VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR BIN_OP NUMBER VAR |
Kolya got string s for his birthday, the string consists of small English letters. He immediately added k more characters to the right of the string.
Then Borya came and said that the new string contained a tandem repeat of length l as a substring. How large could l be?
See notes for definition of a tandem repeat.
-----Input-----
The first line contains s (1 ≤ |s| ≤ 200). This string contains only small English letters. The second line contains number k (1 ≤ k ≤ 200) — the number of the added characters.
-----Output-----
Print a single number — the maximum length of the tandem repeat that could have occurred in the new string.
-----Examples-----
Input
aaba
2
Output
6
Input
aaabbbb
2
Output
6
Input
abracadabra
10
Output
20
-----Note-----
A tandem repeat of length 2n is string s, where for any position i (1 ≤ i ≤ n) the following condition fulfills: s_{i} = s_{i} + n.
In the first sample Kolya could obtain a string aabaab, in the second — aaabbbbbb, in the third — abracadabrabracadabra. | def max_len(s):
N = len(s)
for L in range(N // 2, 0, -1):
for i in range(N - 2 * L + 1):
if all(
ch2 in ch1 + "_" for ch1, ch2 in zip(s[i : i + L], s[i + L : i + 2 * L])
):
return 2 * L
print(max_len(input() + "_" * int(input()))) | FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER FOR VAR FUNC_CALL VAR BIN_OP BIN_OP VAR BIN_OP NUMBER VAR NUMBER IF FUNC_CALL VAR VAR BIN_OP VAR STRING VAR VAR FUNC_CALL VAR VAR VAR BIN_OP VAR VAR VAR BIN_OP VAR VAR BIN_OP VAR BIN_OP NUMBER VAR RETURN BIN_OP NUMBER VAR EXPR FUNC_CALL VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR BIN_OP STRING FUNC_CALL VAR FUNC_CALL VAR |
Kolya got string s for his birthday, the string consists of small English letters. He immediately added k more characters to the right of the string.
Then Borya came and said that the new string contained a tandem repeat of length l as a substring. How large could l be?
See notes for definition of a tandem repeat.
-----Input-----
The first line contains s (1 ≤ |s| ≤ 200). This string contains only small English letters. The second line contains number k (1 ≤ k ≤ 200) — the number of the added characters.
-----Output-----
Print a single number — the maximum length of the tandem repeat that could have occurred in the new string.
-----Examples-----
Input
aaba
2
Output
6
Input
aaabbbb
2
Output
6
Input
abracadabra
10
Output
20
-----Note-----
A tandem repeat of length 2n is string s, where for any position i (1 ≤ i ≤ n) the following condition fulfills: s_{i} = s_{i} + n.
In the first sample Kolya could obtain a string aabaab, in the second — aaabbbbbb, in the third — abracadabrabracadabra. | st = input()
num = int(input())
maxlen = 0
for start in range(len(st) + num):
for end in range(start + 1, len(st) + num, 2):
middle = (end + start + 1) // 2
ans = True
for it in range(min(len(st) - middle, end - middle)):
if st[start + it] != st[middle + it]:
ans = False
break
if ans:
maxlen = max(maxlen, end - start + 1)
print(maxlen) | ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR VAR NUMBER NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR VAR BIN_OP VAR VAR IF VAR BIN_OP VAR VAR VAR BIN_OP VAR VAR ASSIGN VAR NUMBER IF VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR VAR |
Kolya got string s for his birthday, the string consists of small English letters. He immediately added k more characters to the right of the string.
Then Borya came and said that the new string contained a tandem repeat of length l as a substring. How large could l be?
See notes for definition of a tandem repeat.
-----Input-----
The first line contains s (1 ≤ |s| ≤ 200). This string contains only small English letters. The second line contains number k (1 ≤ k ≤ 200) — the number of the added characters.
-----Output-----
Print a single number — the maximum length of the tandem repeat that could have occurred in the new string.
-----Examples-----
Input
aaba
2
Output
6
Input
aaabbbb
2
Output
6
Input
abracadabra
10
Output
20
-----Note-----
A tandem repeat of length 2n is string s, where for any position i (1 ≤ i ≤ n) the following condition fulfills: s_{i} = s_{i} + n.
In the first sample Kolya could obtain a string aabaab, in the second — aaabbbbbb, in the third — abracadabrabracadabra. | s = input()
k = int(input())
ans = 0
for l in range(1, (len(s) + k) // 2 + 1):
cur = s + "a" * k
cur = "".join(
cur[i] if i < max(l, len(s)) else cur[i - l] for i in range(len(s) + k)
)
check = "".join("01"[cur[i] == cur[i + l]] for i in range(len(s) + k - l))
if "1" * l in check:
ans = max(ans, l)
print(2 * ans) | ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP BIN_OP BIN_OP FUNC_CALL VAR VAR VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR BIN_OP STRING VAR ASSIGN VAR FUNC_CALL STRING VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR VAR VAR BIN_OP VAR VAR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL STRING STRING VAR VAR VAR BIN_OP VAR VAR VAR FUNC_CALL VAR BIN_OP BIN_OP FUNC_CALL VAR VAR VAR VAR IF BIN_OP STRING VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP NUMBER VAR |
Kolya got string s for his birthday, the string consists of small English letters. He immediately added k more characters to the right of the string.
Then Borya came and said that the new string contained a tandem repeat of length l as a substring. How large could l be?
See notes for definition of a tandem repeat.
-----Input-----
The first line contains s (1 ≤ |s| ≤ 200). This string contains only small English letters. The second line contains number k (1 ≤ k ≤ 200) — the number of the added characters.
-----Output-----
Print a single number — the maximum length of the tandem repeat that could have occurred in the new string.
-----Examples-----
Input
aaba
2
Output
6
Input
aaabbbb
2
Output
6
Input
abracadabra
10
Output
20
-----Note-----
A tandem repeat of length 2n is string s, where for any position i (1 ≤ i ≤ n) the following condition fulfills: s_{i} = s_{i} + n.
In the first sample Kolya could obtain a string aabaab, in the second — aaabbbbbb, in the third — abracadabrabracadabra. | def equal(x, y):
for i in range(len(x)):
if x[i] != y[i] and "?" not in x[i] + y[i]:
return False
return True
s = input()
r = 1
for i in range(len(s) - 1):
j = (len(s) - i) // 2
while j >= 0:
if s[i : i + j] == s[i + j : i + 2 * j]:
r = max(r, j)
break
j -= 1
n = int(input())
if len(s) + n & 1 == 1:
s = s[1:]
s += "?" * n
n = len(s)
for i in range(0, len(s), 2):
if equal(s[i : n // 2 + i // 2], s[n // 2 + i // 2 :]):
print(max(n - i, r * 2))
exit() | FUNC_DEF FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR STRING BIN_OP VAR VAR VAR VAR RETURN NUMBER RETURN NUMBER ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP FUNC_CALL VAR VAR VAR NUMBER WHILE VAR NUMBER IF VAR VAR BIN_OP VAR VAR VAR BIN_OP VAR VAR BIN_OP VAR BIN_OP NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR IF BIN_OP BIN_OP FUNC_CALL VAR VAR VAR NUMBER NUMBER ASSIGN VAR VAR NUMBER VAR BIN_OP STRING VAR ASSIGN VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR NUMBER IF FUNC_CALL VAR VAR VAR BIN_OP BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR BIN_OP BIN_OP VAR NUMBER BIN_OP VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR |
Kolya got string s for his birthday, the string consists of small English letters. He immediately added k more characters to the right of the string.
Then Borya came and said that the new string contained a tandem repeat of length l as a substring. How large could l be?
See notes for definition of a tandem repeat.
-----Input-----
The first line contains s (1 ≤ |s| ≤ 200). This string contains only small English letters. The second line contains number k (1 ≤ k ≤ 200) — the number of the added characters.
-----Output-----
Print a single number — the maximum length of the tandem repeat that could have occurred in the new string.
-----Examples-----
Input
aaba
2
Output
6
Input
aaabbbb
2
Output
6
Input
abracadabra
10
Output
20
-----Note-----
A tandem repeat of length 2n is string s, where for any position i (1 ≤ i ≤ n) the following condition fulfills: s_{i} = s_{i} + n.
In the first sample Kolya could obtain a string aabaab, in the second — aaabbbbbb, in the third — abracadabrabracadabra. | s = input()
k = int(input())
s = s + "?" * k
for i in range(len(s) // 2 + 1):
a = [0] * len(s)
for j in range(len(s) - i):
if s[j] == s[j + i] or s[j + i] == "?":
a[j] = 1
c = 0
mx = 0
for j in a:
if j == 1:
c += 1
else:
mx = max(mx, c)
c = 0
if mx >= i:
ans = i * 2
print(ans) | ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR BIN_OP VAR BIN_OP STRING VAR FOR VAR FUNC_CALL VAR BIN_OP BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER ASSIGN VAR BIN_OP LIST NUMBER FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR VAR IF VAR VAR VAR BIN_OP VAR VAR VAR BIN_OP VAR VAR STRING ASSIGN VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR IF VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR NUMBER IF VAR VAR ASSIGN VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR |
Kolya got string s for his birthday, the string consists of small English letters. He immediately added k more characters to the right of the string.
Then Borya came and said that the new string contained a tandem repeat of length l as a substring. How large could l be?
See notes for definition of a tandem repeat.
-----Input-----
The first line contains s (1 ≤ |s| ≤ 200). This string contains only small English letters. The second line contains number k (1 ≤ k ≤ 200) — the number of the added characters.
-----Output-----
Print a single number — the maximum length of the tandem repeat that could have occurred in the new string.
-----Examples-----
Input
aaba
2
Output
6
Input
aaabbbb
2
Output
6
Input
abracadabra
10
Output
20
-----Note-----
A tandem repeat of length 2n is string s, where for any position i (1 ≤ i ≤ n) the following condition fulfills: s_{i} = s_{i} + n.
In the first sample Kolya could obtain a string aabaab, in the second — aaabbbbbb, in the third — abracadabrabracadabra. | s = str(input())
k = int(input())
a = 0
def tandem(string):
if len(string) % 2 == 1:
return False
elif string[: len(string) // 2] == string[len(string) // 2 :]:
return True
else:
return False
if len(s) <= k:
a = 2 * ((len(s) + k) // 2)
else:
a = 2 * k
for i in range(0, len(s) - 1):
for j in range(i + 1, len(s)):
if tandem(s[i : j + 1]):
a = max(a, j - i + 1)
for i in range(len(s) - 1):
if s[i] == s[-1]:
x = 0
while i - x >= 0:
if s[i - x] == s[-1 - x] and i + x < len(s):
x += 1
else:
break
if len(s) - i - x - 1 <= k:
a = max(a, 2 * (len(s) - i - 1))
print(a) | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR NUMBER FUNC_DEF IF BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER RETURN NUMBER IF VAR BIN_OP FUNC_CALL VAR VAR NUMBER VAR BIN_OP FUNC_CALL VAR VAR NUMBER RETURN NUMBER RETURN NUMBER IF FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP NUMBER BIN_OP BIN_OP FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR BIN_OP NUMBER VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER IF VAR VAR VAR NUMBER ASSIGN VAR NUMBER WHILE BIN_OP VAR VAR NUMBER IF VAR BIN_OP VAR VAR VAR BIN_OP NUMBER VAR BIN_OP VAR VAR FUNC_CALL VAR VAR VAR NUMBER IF BIN_OP BIN_OP BIN_OP FUNC_CALL VAR VAR VAR VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP NUMBER BIN_OP BIN_OP FUNC_CALL VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR |
Kolya got string s for his birthday, the string consists of small English letters. He immediately added k more characters to the right of the string.
Then Borya came and said that the new string contained a tandem repeat of length l as a substring. How large could l be?
See notes for definition of a tandem repeat.
-----Input-----
The first line contains s (1 ≤ |s| ≤ 200). This string contains only small English letters. The second line contains number k (1 ≤ k ≤ 200) — the number of the added characters.
-----Output-----
Print a single number — the maximum length of the tandem repeat that could have occurred in the new string.
-----Examples-----
Input
aaba
2
Output
6
Input
aaabbbb
2
Output
6
Input
abracadabra
10
Output
20
-----Note-----
A tandem repeat of length 2n is string s, where for any position i (1 ≤ i ≤ n) the following condition fulfills: s_{i} = s_{i} + n.
In the first sample Kolya could obtain a string aabaab, in the second — aaabbbbbb, in the third — abracadabrabracadabra. | a = input()
b = int(input())
j = 0
l = 0
for start in range(len(a) + b):
for end in range(start + 1, len(a) + b):
if (end - start + 1) % 2 == 0:
middle = (start + end) // 2 + 1
k = 0
for traverse in range(start, len(a)):
if k + middle >= len(a) or k + middle > end:
j = end - start + 1
if j > l:
l = j
elif k + middle < len(a) and a[traverse] != a[middle + k]:
break
elif k + middle < len(a) and a[traverse] == a[middle + k]:
k += 1
print(l) | ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP FUNC_CALL VAR VAR VAR IF BIN_OP BIN_OP BIN_OP VAR VAR NUMBER NUMBER NUMBER ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR VAR NUMBER NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR IF BIN_OP VAR VAR FUNC_CALL VAR VAR BIN_OP VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF VAR VAR ASSIGN VAR VAR IF BIN_OP VAR VAR FUNC_CALL VAR VAR VAR VAR VAR BIN_OP VAR VAR IF BIN_OP VAR VAR FUNC_CALL VAR VAR VAR VAR VAR BIN_OP VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR |
Kolya got string s for his birthday, the string consists of small English letters. He immediately added k more characters to the right of the string.
Then Borya came and said that the new string contained a tandem repeat of length l as a substring. How large could l be?
See notes for definition of a tandem repeat.
-----Input-----
The first line contains s (1 ≤ |s| ≤ 200). This string contains only small English letters. The second line contains number k (1 ≤ k ≤ 200) — the number of the added characters.
-----Output-----
Print a single number — the maximum length of the tandem repeat that could have occurred in the new string.
-----Examples-----
Input
aaba
2
Output
6
Input
aaabbbb
2
Output
6
Input
abracadabra
10
Output
20
-----Note-----
A tandem repeat of length 2n is string s, where for any position i (1 ≤ i ≤ n) the following condition fulfills: s_{i} = s_{i} + n.
In the first sample Kolya could obtain a string aabaab, in the second — aaabbbbbb, in the third — abracadabrabracadabra. | import sys
def readln():
return tuple(map(int, input().split()))
s = input()
(k,) = readln()
s += "?" * k
ans = len(s) // 2 * 2
while ans:
for i in range(len(s) - ans + 1):
flag = True
for j in range(i, i + ans // 2):
flag = flag and (s[j] == s[j + ans // 2] or s[j + ans // 2] == "?")
if flag:
print(ans)
return
ans -= 2 | IMPORT FUNC_DEF RETURN FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP STRING VAR ASSIGN VAR BIN_OP BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER WHILE VAR FOR VAR FUNC_CALL VAR BIN_OP BIN_OP FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR BIN_OP VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR VAR VAR BIN_OP VAR BIN_OP VAR NUMBER VAR BIN_OP VAR BIN_OP VAR NUMBER STRING IF VAR EXPR FUNC_CALL VAR VAR RETURN VAR NUMBER |
Kolya got string s for his birthday, the string consists of small English letters. He immediately added k more characters to the right of the string.
Then Borya came and said that the new string contained a tandem repeat of length l as a substring. How large could l be?
See notes for definition of a tandem repeat.
-----Input-----
The first line contains s (1 ≤ |s| ≤ 200). This string contains only small English letters. The second line contains number k (1 ≤ k ≤ 200) — the number of the added characters.
-----Output-----
Print a single number — the maximum length of the tandem repeat that could have occurred in the new string.
-----Examples-----
Input
aaba
2
Output
6
Input
aaabbbb
2
Output
6
Input
abracadabra
10
Output
20
-----Note-----
A tandem repeat of length 2n is string s, where for any position i (1 ≤ i ≤ n) the following condition fulfills: s_{i} = s_{i} + n.
In the first sample Kolya could obtain a string aabaab, in the second — aaabbbbbb, in the third — abracadabrabracadabra. | import sys
3
s = sys.stdin.readline().strip()
k = int(sys.stdin.readline())
s += "*" * k
def is_tandem(s):
n = len(s) // 2
a, b = s[:n], s[n:]
for i in range(n):
if a[i] == "*" or b[i] == "*":
continue
if a[i] != b[i]:
return False
return True
l = 0
for i in range(len(s)):
for n in range(2, len(s) - i + 1, 2):
if is_tandem(s[i : i + n]):
l = max(l, n)
print(l) | IMPORT EXPR NUMBER ASSIGN VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR BIN_OP STRING VAR FUNC_DEF ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR VAR VAR VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR STRING VAR VAR STRING IF VAR VAR VAR VAR RETURN NUMBER RETURN NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP BIN_OP FUNC_CALL VAR VAR VAR NUMBER NUMBER IF FUNC_CALL VAR VAR VAR BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR |
Kolya got string s for his birthday, the string consists of small English letters. He immediately added k more characters to the right of the string.
Then Borya came and said that the new string contained a tandem repeat of length l as a substring. How large could l be?
See notes for definition of a tandem repeat.
-----Input-----
The first line contains s (1 ≤ |s| ≤ 200). This string contains only small English letters. The second line contains number k (1 ≤ k ≤ 200) — the number of the added characters.
-----Output-----
Print a single number — the maximum length of the tandem repeat that could have occurred in the new string.
-----Examples-----
Input
aaba
2
Output
6
Input
aaabbbb
2
Output
6
Input
abracadabra
10
Output
20
-----Note-----
A tandem repeat of length 2n is string s, where for any position i (1 ≤ i ≤ n) the following condition fulfills: s_{i} = s_{i} + n.
In the first sample Kolya could obtain a string aabaab, in the second — aaabbbbbb, in the third — abracadabrabracadabra. | def equal(a, b):
for i in range(len(a)):
if a[i] != b[i] and a[i] != "*" and b[i] != "*":
return False
return True
string, k, best = input().strip(), int(input()), 0
string += "*" * k
for i in range(len(string) + 1):
for j in range(i + 1, len(string) + 1):
if not (j - i) % 2:
if equal(string[i : (i + j) // 2], string[(i + j) // 2 : j]):
best = max(best, j - i)
print(best) | FUNC_DEF FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR VAR VAR STRING VAR VAR STRING RETURN NUMBER RETURN NUMBER ASSIGN VAR VAR VAR FUNC_CALL FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR NUMBER VAR BIN_OP STRING VAR FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER IF BIN_OP BIN_OP VAR VAR NUMBER IF FUNC_CALL VAR VAR VAR BIN_OP BIN_OP VAR VAR NUMBER VAR BIN_OP BIN_OP VAR VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR |
An integer has sequential digits if and only if each digit in the number is one more than the previous digit.
Return a sorted list of all the integers in the range [low, high] inclusive that have sequential digits.
Example 1:
Input: low = 100, high = 300
Output: [123,234]
Example 2:
Input: low = 1000, high = 13000
Output: [1234,2345,3456,4567,5678,6789,12345]
Constraints:
10 <= low <= high <= 10^9 | class Solution:
def sequentialDigits(self, low: int, high: int) -> List[int]:
res = []
def dfs(curr_num):
if curr_num >= low and curr_num <= high:
res.append(curr_num)
elif curr_num > high:
return
if int(str(curr_num)[-1]) == 9:
return
dfs(curr_num * 10 + int(str(curr_num)[-1]) + 1)
dfs(1)
dfs(2)
dfs(3)
dfs(4)
dfs(5)
dfs(6)
dfs(7)
dfs(8)
dfs(9)
return sorted(res) | CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR LIST FUNC_DEF IF VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR IF VAR VAR RETURN IF FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER NUMBER RETURN EXPR FUNC_CALL VAR BIN_OP BIN_OP BIN_OP VAR NUMBER FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR NUMBER RETURN FUNC_CALL VAR VAR VAR VAR |
An integer has sequential digits if and only if each digit in the number is one more than the previous digit.
Return a sorted list of all the integers in the range [low, high] inclusive that have sequential digits.
Example 1:
Input: low = 100, high = 300
Output: [123,234]
Example 2:
Input: low = 1000, high = 13000
Output: [1234,2345,3456,4567,5678,6789,12345]
Constraints:
10 <= low <= high <= 10^9 | class Solution0:
def sequentialDigits(self, low: int, high: int) -> List[int]:
nl, nh = len(str(low)), len(str(high))
ans = []
sequential = "123456789"
for n in range(nl, min(nh + 1, 10)):
start, delta = int(sequential[:n]), int("1" * n)
k_min = 0 if n > nl else max(-(-(low - start) // delta), 0)
k_max = (
9 - n if n < nh else (min(high, int(sequential[-n:])) - start) // delta
)
ans += [(start + k * delta) for k in range(k_min, k_max + 1)]
return ans
class Solution:
def sequentialDigits(self, low: int, high: int) -> List[int]:
nl, nh = len(str(low)), len(str(high))
ans = []
sequential = "123456789"
for n in range(nl, min(nh + 1, 10)):
for k in range(10 - n):
if low <= int(sequential[k : k + n]) <= high:
ans.append(int(sequential[k : k + n]))
return ans | CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR LIST ASSIGN VAR STRING FOR VAR FUNC_CALL VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR BIN_OP STRING VAR ASSIGN VAR VAR VAR NUMBER FUNC_CALL VAR BIN_OP BIN_OP VAR VAR VAR NUMBER ASSIGN VAR VAR VAR BIN_OP NUMBER VAR BIN_OP BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR VAR VAR VAR BIN_OP VAR BIN_OP VAR VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER RETURN VAR VAR VAR CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR LIST ASSIGN VAR STRING FOR VAR FUNC_CALL VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR BIN_OP NUMBER VAR IF VAR FUNC_CALL VAR VAR VAR BIN_OP VAR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR BIN_OP VAR VAR RETURN VAR VAR VAR |
An integer has sequential digits if and only if each digit in the number is one more than the previous digit.
Return a sorted list of all the integers in the range [low, high] inclusive that have sequential digits.
Example 1:
Input: low = 100, high = 300
Output: [123,234]
Example 2:
Input: low = 1000, high = 13000
Output: [1234,2345,3456,4567,5678,6789,12345]
Constraints:
10 <= low <= high <= 10^9 | class Solution:
def sequentialDigits(self, low: int, high: int) -> List[int]:
l = len(str(low))
f = len(str(high))
s = len(str(low)[0])
a = []
for i in range(l, f + 1):
while True:
t = ""
if i + s > 10:
break
for j in range(s, i + s):
t += str(j)
if int(t) > high:
break
if int(t) < low:
s += 1
continue
s += 1
a.append(t)
s = 1
return a | CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER WHILE NUMBER ASSIGN VAR STRING IF BIN_OP VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR BIN_OP VAR VAR VAR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR VAR IF FUNC_CALL VAR VAR VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER RETURN VAR VAR VAR |
An integer has sequential digits if and only if each digit in the number is one more than the previous digit.
Return a sorted list of all the integers in the range [low, high] inclusive that have sequential digits.
Example 1:
Input: low = 100, high = 300
Output: [123,234]
Example 2:
Input: low = 1000, high = 13000
Output: [1234,2345,3456,4567,5678,6789,12345]
Constraints:
10 <= low <= high <= 10^9 | class Solution:
def SequentialDigits(self, low: int, high: int) -> List[int]:
res = []
for i in range(1, 10):
for j in range(i + 1, 10):
i = i * 10 + j
if i > high:
break
if i >= low:
res.append(i)
return sorted(res)
def sequentialDigits(self, low, high):
out = []
queue = deque(range(1, 10))
while queue and queue[0] <= high:
elem = queue.popleft()
if low <= elem:
out.append(elem)
last = elem % 10
if last < 9:
queue.append(elem * 10 + last + 1)
return out | CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP BIN_OP VAR NUMBER VAR IF VAR VAR IF VAR VAR EXPR FUNC_CALL VAR VAR RETURN FUNC_CALL VAR VAR VAR VAR FUNC_DEF ASSIGN VAR LIST ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR NUMBER NUMBER WHILE VAR VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR IF VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR BIN_OP BIN_OP BIN_OP VAR NUMBER VAR NUMBER RETURN VAR |
An integer has sequential digits if and only if each digit in the number is one more than the previous digit.
Return a sorted list of all the integers in the range [low, high] inclusive that have sequential digits.
Example 1:
Input: low = 100, high = 300
Output: [123,234]
Example 2:
Input: low = 1000, high = 13000
Output: [1234,2345,3456,4567,5678,6789,12345]
Constraints:
10 <= low <= high <= 10^9 | class Solution:
def sequentialDigits(self, low: int, high: int) -> List[int]:
minRange = len(str(low))
maxRange = len(str(high))
digits = ["1", "2", "3", "4", "5", "6", "7", "8", "9"]
result = []
for n in range(minRange, maxRange + 1):
for i in range(10 - n):
num = int("".join(digits[i : i + n]))
if num >= low and num <= high:
result.append(num)
if num > high:
break
return result | CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR LIST STRING STRING STRING STRING STRING STRING STRING STRING STRING ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP NUMBER VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL STRING VAR VAR BIN_OP VAR VAR IF VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR IF VAR VAR RETURN VAR VAR VAR |
An integer has sequential digits if and only if each digit in the number is one more than the previous digit.
Return a sorted list of all the integers in the range [low, high] inclusive that have sequential digits.
Example 1:
Input: low = 100, high = 300
Output: [123,234]
Example 2:
Input: low = 1000, high = 13000
Output: [1234,2345,3456,4567,5678,6789,12345]
Constraints:
10 <= low <= high <= 10^9 | class Solution:
def sequentialDigits(self, low: int, high: int) -> List[int]:
nums = []
max_digits = 9
for depth in range(2, max_digits + 1):
nums += self.genNum(max_digits, depth)
return [n for n in nums if n >= low and n <= high]
def genNum(self, max_digits, depth):
result = []
for startDigit in range(1, max_digits - depth + 2):
num = startDigit
for lvl in range(depth - 1):
num = num * 10 + num % 10 + 1
result.append(num)
return result | CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR LIST ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER VAR FUNC_CALL VAR VAR VAR RETURN VAR VAR VAR VAR VAR VAR VAR VAR VAR FUNC_DEF ASSIGN VAR LIST FOR VAR FUNC_CALL VAR NUMBER BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR NUMBER BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR RETURN VAR |
An integer has sequential digits if and only if each digit in the number is one more than the previous digit.
Return a sorted list of all the integers in the range [low, high] inclusive that have sequential digits.
Example 1:
Input: low = 100, high = 300
Output: [123,234]
Example 2:
Input: low = 1000, high = 13000
Output: [1234,2345,3456,4567,5678,6789,12345]
Constraints:
10 <= low <= high <= 10^9 | class Solution:
def sequentialDigits(self, low: int, high: int) -> List[int]:
ans = []
for ndigit in range(1, 10):
for i in range(1, 10 - ndigit + 1):
x = i
for j in range(ndigit - 1):
x = x * 10 + (i + j + 1)
if x > high:
break
if x >= low:
ans.append(x)
return ans | CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP BIN_OP NUMBER VAR NUMBER ASSIGN VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR NUMBER BIN_OP BIN_OP VAR VAR NUMBER IF VAR VAR IF VAR VAR EXPR FUNC_CALL VAR VAR RETURN VAR VAR VAR |
Igor has been into chess for a long time and now he is sick of the game by the ordinary rules. He is going to think of new rules of the game and become world famous.
Igor's chessboard is a square of size n × n cells. Igor decided that simple rules guarantee success, that's why his game will have only one type of pieces. Besides, all pieces in his game are of the same color. The possible moves of a piece are described by a set of shift vectors. The next passage contains a formal description of available moves.
Let the rows of the board be numbered from top to bottom and the columns be numbered from left to right from 1 to n. Let's assign to each square a pair of integers (x, y) — the number of the corresponding column and row. Each of the possible moves of the piece is defined by a pair of integers (dx, dy); using this move, the piece moves from the field (x, y) to the field (x + dx, y + dy). You can perform the move if the cell (x + dx, y + dy) is within the boundaries of the board and doesn't contain another piece. Pieces that stand on the cells other than (x, y) and (x + dx, y + dy) are not important when considering the possibility of making the given move (for example, like when a knight moves in usual chess).
Igor offers you to find out what moves his chess piece can make. He placed several pieces on the board and for each unoccupied square he told you whether it is attacked by any present piece (i.e. whether some of the pieces on the field can move to that cell). Restore a possible set of shift vectors of the piece, or else determine that Igor has made a mistake and such situation is impossible for any set of shift vectors.
-----Input-----
The first line contains a single integer n (1 ≤ n ≤ 50).
The next n lines contain n characters each describing the position offered by Igor. The j-th character of the i-th string can have the following values: o — in this case the field (i, j) is occupied by a piece and the field may or may not be attacked by some other piece; x — in this case field (i, j) is attacked by some piece; . — in this case field (i, j) isn't attacked by any piece.
It is guaranteed that there is at least one piece on the board.
-----Output-----
If there is a valid set of moves, in the first line print a single word 'YES' (without the quotes). Next, print the description of the set of moves of a piece in the form of a (2n - 1) × (2n - 1) board, the center of the board has a piece and symbols 'x' mark cells that are attacked by it, in a format similar to the input. See examples of the output for a full understanding of the format. If there are several possible answers, print any of them.
If a valid set of moves does not exist, print a single word 'NO'.
-----Examples-----
Input
5
oxxxx
x...x
x...x
x...x
xxxxo
Output
YES
....x....
....x....
....x....
....x....
xxxxoxxxx
....x....
....x....
....x....
....x....
Input
6
.x.x..
x.x.x.
.xo..x
x..ox.
.x.x.x
..x.x.
Output
YES
...........
...........
...........
....x.x....
...x...x...
.....o.....
...x...x...
....x.x....
...........
...........
...........
Input
3
o.x
oxx
o.x
Output
NO
-----Note-----
In the first sample test the piece is a usual chess rook, and in the second sample test the piece is a usual chess knight. | n = int(input())
b = [input() for i in range(n)]
h = [([False] * n) for i in range(n)]
p = [[i, j] for i in range(n) for j in range(n) if b[i][j] == "o"]
v = [(["."] * (2 * n - 1)) for i in range(2 * n - 1)]
def on(x, y):
return x in range(n) and y in range(n)
def proc(dx, dy):
for pi in p:
if on(pi[0] + dx, pi[1] + dy) and b[pi[0] + dx][pi[1] + dy] == ".":
return
v[dx + n - 1][dy + n - 1] = "x"
for pi in p:
if on(pi[0] + dx, pi[1] + dy):
h[pi[0] + dx][pi[1] + dy] = True
for i in range(-(n - 1), n):
for j in range(-(n - 1), n):
proc(i, j)
if any(b[i][j] == "x" and not h[i][j] for i in range(n) for j in range(n)):
print("NO")
else:
print("YES")
v[n - 1][n - 1] = "o"
for vi in v:
print("".join(vi)) | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER VAR VAR FUNC_CALL VAR VAR ASSIGN VAR LIST VAR VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR VAR VAR STRING ASSIGN VAR BIN_OP LIST STRING BIN_OP BIN_OP NUMBER VAR NUMBER VAR FUNC_CALL VAR BIN_OP BIN_OP NUMBER VAR NUMBER FUNC_DEF RETURN VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR FUNC_DEF FOR VAR VAR IF FUNC_CALL VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR STRING RETURN ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER BIN_OP BIN_OP VAR VAR NUMBER STRING FOR VAR VAR IF FUNC_CALL VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR ASSIGN VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR VAR VAR IF FUNC_CALL VAR VAR VAR VAR STRING VAR VAR VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING ASSIGN VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER STRING FOR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL STRING VAR |
Igor has been into chess for a long time and now he is sick of the game by the ordinary rules. He is going to think of new rules of the game and become world famous.
Igor's chessboard is a square of size n × n cells. Igor decided that simple rules guarantee success, that's why his game will have only one type of pieces. Besides, all pieces in his game are of the same color. The possible moves of a piece are described by a set of shift vectors. The next passage contains a formal description of available moves.
Let the rows of the board be numbered from top to bottom and the columns be numbered from left to right from 1 to n. Let's assign to each square a pair of integers (x, y) — the number of the corresponding column and row. Each of the possible moves of the piece is defined by a pair of integers (dx, dy); using this move, the piece moves from the field (x, y) to the field (x + dx, y + dy). You can perform the move if the cell (x + dx, y + dy) is within the boundaries of the board and doesn't contain another piece. Pieces that stand on the cells other than (x, y) and (x + dx, y + dy) are not important when considering the possibility of making the given move (for example, like when a knight moves in usual chess).
Igor offers you to find out what moves his chess piece can make. He placed several pieces on the board and for each unoccupied square he told you whether it is attacked by any present piece (i.e. whether some of the pieces on the field can move to that cell). Restore a possible set of shift vectors of the piece, or else determine that Igor has made a mistake and such situation is impossible for any set of shift vectors.
-----Input-----
The first line contains a single integer n (1 ≤ n ≤ 50).
The next n lines contain n characters each describing the position offered by Igor. The j-th character of the i-th string can have the following values: o — in this case the field (i, j) is occupied by a piece and the field may or may not be attacked by some other piece; x — in this case field (i, j) is attacked by some piece; . — in this case field (i, j) isn't attacked by any piece.
It is guaranteed that there is at least one piece on the board.
-----Output-----
If there is a valid set of moves, in the first line print a single word 'YES' (without the quotes). Next, print the description of the set of moves of a piece in the form of a (2n - 1) × (2n - 1) board, the center of the board has a piece and symbols 'x' mark cells that are attacked by it, in a format similar to the input. See examples of the output for a full understanding of the format. If there are several possible answers, print any of them.
If a valid set of moves does not exist, print a single word 'NO'.
-----Examples-----
Input
5
oxxxx
x...x
x...x
x...x
xxxxo
Output
YES
....x....
....x....
....x....
....x....
xxxxoxxxx
....x....
....x....
....x....
....x....
Input
6
.x.x..
x.x.x.
.xo..x
x..ox.
.x.x.x
..x.x.
Output
YES
...........
...........
...........
....x.x....
...x...x...
.....o.....
...x...x...
....x.x....
...........
...........
...........
Input
3
o.x
oxx
o.x
Output
NO
-----Note-----
In the first sample test the piece is a usual chess rook, and in the second sample test the piece is a usual chess knight. | import sys
fin = sys.stdin
n = int(fin.readline())
ma = [[None]] * n
for i in range(0, n):
aux = fin.readline()
aux = aux[:-1]
ma[i] = list(aux)
r = []
for i in range(0, 2 * n - 1):
r.append(None)
r[i] = []
for j in range(0, 2 * n - 1):
r[i].append("x")
for i in range(0, n):
for j in range(0, n):
if ma[i][j] == "o":
for ii in range(0, n):
for jj in range(0, n):
if ma[ii][jj] == ".":
r[n - 1 + ii - i][n - 1 + jj - j] = "."
g = 1
r[n - 1][n - 1] = "o"
for i in range(0, n):
for j in range(0, n):
if ma[i][j] == "x":
cg = 0
for ii in range(0, n):
for jj in range(0, n):
if ma[ii][jj] == "o" and r[n - 1 - ii + i][n - 1 - jj + j] == "x":
cg = 1
if cg != 1:
g = 0
if g != 1:
print("NO")
else:
print("YES")
for line in r:
print("".join(line)) | IMPORT ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR BIN_OP LIST LIST NONE VAR FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR NUMBER BIN_OP BIN_OP NUMBER VAR NUMBER EXPR FUNC_CALL VAR NONE ASSIGN VAR VAR LIST FOR VAR FUNC_CALL VAR NUMBER BIN_OP BIN_OP NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR STRING FOR VAR FUNC_CALL VAR NUMBER VAR FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR VAR VAR STRING FOR VAR FUNC_CALL VAR NUMBER VAR FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR VAR VAR STRING ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR NUMBER VAR VAR BIN_OP BIN_OP BIN_OP VAR NUMBER VAR VAR STRING ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER STRING FOR VAR FUNC_CALL VAR NUMBER VAR FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR VAR VAR STRING ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR VAR VAR STRING VAR BIN_OP BIN_OP BIN_OP VAR NUMBER VAR VAR BIN_OP BIN_OP BIN_OP VAR NUMBER VAR VAR STRING ASSIGN VAR NUMBER IF VAR NUMBER ASSIGN VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING FOR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL STRING VAR |
Igor has been into chess for a long time and now he is sick of the game by the ordinary rules. He is going to think of new rules of the game and become world famous.
Igor's chessboard is a square of size n × n cells. Igor decided that simple rules guarantee success, that's why his game will have only one type of pieces. Besides, all pieces in his game are of the same color. The possible moves of a piece are described by a set of shift vectors. The next passage contains a formal description of available moves.
Let the rows of the board be numbered from top to bottom and the columns be numbered from left to right from 1 to n. Let's assign to each square a pair of integers (x, y) — the number of the corresponding column and row. Each of the possible moves of the piece is defined by a pair of integers (dx, dy); using this move, the piece moves from the field (x, y) to the field (x + dx, y + dy). You can perform the move if the cell (x + dx, y + dy) is within the boundaries of the board and doesn't contain another piece. Pieces that stand on the cells other than (x, y) and (x + dx, y + dy) are not important when considering the possibility of making the given move (for example, like when a knight moves in usual chess).
Igor offers you to find out what moves his chess piece can make. He placed several pieces on the board and for each unoccupied square he told you whether it is attacked by any present piece (i.e. whether some of the pieces on the field can move to that cell). Restore a possible set of shift vectors of the piece, or else determine that Igor has made a mistake and such situation is impossible for any set of shift vectors.
-----Input-----
The first line contains a single integer n (1 ≤ n ≤ 50).
The next n lines contain n characters each describing the position offered by Igor. The j-th character of the i-th string can have the following values: o — in this case the field (i, j) is occupied by a piece and the field may or may not be attacked by some other piece; x — in this case field (i, j) is attacked by some piece; . — in this case field (i, j) isn't attacked by any piece.
It is guaranteed that there is at least one piece on the board.
-----Output-----
If there is a valid set of moves, in the first line print a single word 'YES' (without the quotes). Next, print the description of the set of moves of a piece in the form of a (2n - 1) × (2n - 1) board, the center of the board has a piece and symbols 'x' mark cells that are attacked by it, in a format similar to the input. See examples of the output for a full understanding of the format. If there are several possible answers, print any of them.
If a valid set of moves does not exist, print a single word 'NO'.
-----Examples-----
Input
5
oxxxx
x...x
x...x
x...x
xxxxo
Output
YES
....x....
....x....
....x....
....x....
xxxxoxxxx
....x....
....x....
....x....
....x....
Input
6
.x.x..
x.x.x.
.xo..x
x..ox.
.x.x.x
..x.x.
Output
YES
...........
...........
...........
....x.x....
...x...x...
.....o.....
...x...x...
....x.x....
...........
...........
...........
Input
3
o.x
oxx
o.x
Output
NO
-----Note-----
In the first sample test the piece is a usual chess rook, and in the second sample test the piece is a usual chess knight. | import sys
f = sys.stdin
n = int(f.readline().strip())
fig = []
place = []
for i in range(n):
place.append([-1] * n)
for i in range(n):
s = f.readline().strip()
for k in range(len(s)):
if s[k] == "o":
fig.append((k, i))
place[k][i] = -1
elif s[k] == "x":
place[k][i] = 1
elif s[k] == ".":
place[k][i] = 0
hh = []
for i in range(2 * n - 1):
hh.append([1] * (2 * n - 1))
res = True
for fi in range(len(fig)):
x = fig[fi][0]
y = fig[fi][1]
for i in range(n):
for k in range(n):
r = place[k][i]
if r == 0:
dx = k - x + n - 1
dy = i - y + n - 1
hh[dx][dy] = 0
for i in range(n):
for k in range(n):
r = place[k][i]
if r == 1:
beat = False
for fi in range(len(fig)):
dx = k - fig[fi][0] + n - 1
dy = i - fig[fi][1] + n - 1
if hh[dx][dy] != 0:
beat = True
if not beat:
res = False
hh[n - 1][n - 1] = -1
if res:
print("YES")
for i in range(2 * n - 1):
s = ""
for k in range(2 * n - 1):
if hh[k][i] == 0:
s += "."
elif hh[k][i] == -1:
s += "o"
else:
s += "x"
print(s)
else:
print("NO") | IMPORT ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP LIST NUMBER VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR STRING EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR VAR NUMBER IF VAR VAR STRING ASSIGN VAR VAR VAR NUMBER IF VAR VAR STRING ASSIGN VAR VAR VAR NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR BIN_OP BIN_OP NUMBER VAR NUMBER EXPR FUNC_CALL VAR BIN_OP LIST NUMBER BIN_OP BIN_OP NUMBER VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR IF VAR NUMBER ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR IF VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR VAR VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR VAR VAR NUMBER VAR NUMBER IF VAR VAR VAR NUMBER ASSIGN VAR NUMBER IF VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER NUMBER IF VAR EXPR FUNC_CALL VAR STRING FOR VAR FUNC_CALL VAR BIN_OP BIN_OP NUMBER VAR NUMBER ASSIGN VAR STRING FOR VAR FUNC_CALL VAR BIN_OP BIN_OP NUMBER VAR NUMBER IF VAR VAR VAR NUMBER VAR STRING IF VAR VAR VAR NUMBER VAR STRING VAR STRING EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR STRING |
Igor has been into chess for a long time and now he is sick of the game by the ordinary rules. He is going to think of new rules of the game and become world famous.
Igor's chessboard is a square of size n × n cells. Igor decided that simple rules guarantee success, that's why his game will have only one type of pieces. Besides, all pieces in his game are of the same color. The possible moves of a piece are described by a set of shift vectors. The next passage contains a formal description of available moves.
Let the rows of the board be numbered from top to bottom and the columns be numbered from left to right from 1 to n. Let's assign to each square a pair of integers (x, y) — the number of the corresponding column and row. Each of the possible moves of the piece is defined by a pair of integers (dx, dy); using this move, the piece moves from the field (x, y) to the field (x + dx, y + dy). You can perform the move if the cell (x + dx, y + dy) is within the boundaries of the board and doesn't contain another piece. Pieces that stand on the cells other than (x, y) and (x + dx, y + dy) are not important when considering the possibility of making the given move (for example, like when a knight moves in usual chess).
Igor offers you to find out what moves his chess piece can make. He placed several pieces on the board and for each unoccupied square he told you whether it is attacked by any present piece (i.e. whether some of the pieces on the field can move to that cell). Restore a possible set of shift vectors of the piece, or else determine that Igor has made a mistake and such situation is impossible for any set of shift vectors.
-----Input-----
The first line contains a single integer n (1 ≤ n ≤ 50).
The next n lines contain n characters each describing the position offered by Igor. The j-th character of the i-th string can have the following values: o — in this case the field (i, j) is occupied by a piece and the field may or may not be attacked by some other piece; x — in this case field (i, j) is attacked by some piece; . — in this case field (i, j) isn't attacked by any piece.
It is guaranteed that there is at least one piece on the board.
-----Output-----
If there is a valid set of moves, in the first line print a single word 'YES' (without the quotes). Next, print the description of the set of moves of a piece in the form of a (2n - 1) × (2n - 1) board, the center of the board has a piece and symbols 'x' mark cells that are attacked by it, in a format similar to the input. See examples of the output for a full understanding of the format. If there are several possible answers, print any of them.
If a valid set of moves does not exist, print a single word 'NO'.
-----Examples-----
Input
5
oxxxx
x...x
x...x
x...x
xxxxo
Output
YES
....x....
....x....
....x....
....x....
xxxxoxxxx
....x....
....x....
....x....
....x....
Input
6
.x.x..
x.x.x.
.xo..x
x..ox.
.x.x.x
..x.x.
Output
YES
...........
...........
...........
....x.x....
...x...x...
.....o.....
...x...x...
....x.x....
...........
...........
...........
Input
3
o.x
oxx
o.x
Output
NO
-----Note-----
In the first sample test the piece is a usual chess rook, and in the second sample test the piece is a usual chess knight. | s = int(input())
opos = []
xpos = []
dpos = []
for i in range(s):
x = input()
for j in range(s):
if x[j] == "o":
opos.append([i, j])
elif x[j] == "x":
xpos.append([i, j])
else:
dpos.append([i, j])
ddifs = {}
for i in dpos:
a = i[0]
b = i[1]
for j in opos:
ddifs.update({(j[0] - a, j[1] - b): 0})
xdifs = []
for i in xpos:
a = i[0]
b = i[1]
lis = []
for j in opos:
lis.append((j[0] - a, j[1] - b))
xdifs.append(lis)
vectors = {}
filled = [(0) for i in range(len(xpos))]
for i in range(len(xdifs)):
for j in xdifs[i]:
if j not in ddifs:
filled[i] = 1
vectors.update({j: 0})
if len(filled) != 0 and min(filled) == 0:
print("NO")
else:
print("YES")
for i in range(2 * s - 1):
string = ""
for j in range(2 * s - 1):
if (s - 1 - i, s - 1 - j) in vectors:
string += "x"
elif i == s - 1 and j == s - 1:
string += "o"
else:
string += "."
print(string) | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR LIST ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR STRING EXPR FUNC_CALL VAR LIST VAR VAR IF VAR VAR STRING EXPR FUNC_CALL VAR LIST VAR VAR EXPR FUNC_CALL VAR LIST VAR VAR ASSIGN VAR DICT FOR VAR VAR ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER FOR VAR VAR EXPR FUNC_CALL VAR DICT BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR LIST FOR VAR VAR ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR LIST FOR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR DICT ASSIGN VAR NUMBER VAR FUNC_CALL VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FOR VAR VAR VAR IF VAR VAR ASSIGN VAR VAR NUMBER EXPR FUNC_CALL VAR DICT VAR NUMBER IF FUNC_CALL VAR VAR NUMBER FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING FOR VAR FUNC_CALL VAR BIN_OP BIN_OP NUMBER VAR NUMBER ASSIGN VAR STRING FOR VAR FUNC_CALL VAR BIN_OP BIN_OP NUMBER VAR NUMBER IF BIN_OP BIN_OP VAR NUMBER VAR BIN_OP BIN_OP VAR NUMBER VAR VAR VAR STRING IF VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR STRING VAR STRING EXPR FUNC_CALL VAR VAR |
Igor has been into chess for a long time and now he is sick of the game by the ordinary rules. He is going to think of new rules of the game and become world famous.
Igor's chessboard is a square of size n × n cells. Igor decided that simple rules guarantee success, that's why his game will have only one type of pieces. Besides, all pieces in his game are of the same color. The possible moves of a piece are described by a set of shift vectors. The next passage contains a formal description of available moves.
Let the rows of the board be numbered from top to bottom and the columns be numbered from left to right from 1 to n. Let's assign to each square a pair of integers (x, y) — the number of the corresponding column and row. Each of the possible moves of the piece is defined by a pair of integers (dx, dy); using this move, the piece moves from the field (x, y) to the field (x + dx, y + dy). You can perform the move if the cell (x + dx, y + dy) is within the boundaries of the board and doesn't contain another piece. Pieces that stand on the cells other than (x, y) and (x + dx, y + dy) are not important when considering the possibility of making the given move (for example, like when a knight moves in usual chess).
Igor offers you to find out what moves his chess piece can make. He placed several pieces on the board and for each unoccupied square he told you whether it is attacked by any present piece (i.e. whether some of the pieces on the field can move to that cell). Restore a possible set of shift vectors of the piece, or else determine that Igor has made a mistake and such situation is impossible for any set of shift vectors.
-----Input-----
The first line contains a single integer n (1 ≤ n ≤ 50).
The next n lines contain n characters each describing the position offered by Igor. The j-th character of the i-th string can have the following values: o — in this case the field (i, j) is occupied by a piece and the field may or may not be attacked by some other piece; x — in this case field (i, j) is attacked by some piece; . — in this case field (i, j) isn't attacked by any piece.
It is guaranteed that there is at least one piece on the board.
-----Output-----
If there is a valid set of moves, in the first line print a single word 'YES' (without the quotes). Next, print the description of the set of moves of a piece in the form of a (2n - 1) × (2n - 1) board, the center of the board has a piece and symbols 'x' mark cells that are attacked by it, in a format similar to the input. See examples of the output for a full understanding of the format. If there are several possible answers, print any of them.
If a valid set of moves does not exist, print a single word 'NO'.
-----Examples-----
Input
5
oxxxx
x...x
x...x
x...x
xxxxo
Output
YES
....x....
....x....
....x....
....x....
xxxxoxxxx
....x....
....x....
....x....
....x....
Input
6
.x.x..
x.x.x.
.xo..x
x..ox.
.x.x.x
..x.x.
Output
YES
...........
...........
...........
....x.x....
...x...x...
.....o.....
...x...x...
....x.x....
...........
...........
...........
Input
3
o.x
oxx
o.x
Output
NO
-----Note-----
In the first sample test the piece is a usual chess rook, and in the second sample test the piece is a usual chess knight. | def main():
n = int(input())
d = [input() for i in range(n)]
figures = []
for i in range(n):
for j in range(n):
if d[i][j] == "o":
figures.append((i, j))
result = [["x" for i in range(2 * n - 1)] for j in range(2 * n - 1)]
result[n - 1][n - 1] = "o"
for i in range(n):
for j in range(n):
if d[i][j] == ".":
for x, y in figures:
dx, dy = x - i, y - j
result[n - 1 - dx][n - 1 - dy] = "."
for i in range(n):
for j in range(n):
if d[i][j] == "x":
for x, y in figures:
dx, dy = x - i, y - j
if result[n - 1 - dx][n - 1 - dy] == "x":
break
else:
print("NO")
return
print("YES")
for i in result:
print("".join(i))
main() | FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR STRING EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR STRING VAR FUNC_CALL VAR BIN_OP BIN_OP NUMBER VAR NUMBER VAR FUNC_CALL VAR BIN_OP BIN_OP NUMBER VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER STRING FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR STRING FOR VAR VAR VAR ASSIGN VAR VAR BIN_OP VAR VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR NUMBER VAR BIN_OP BIN_OP VAR NUMBER VAR STRING FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR STRING FOR VAR VAR VAR ASSIGN VAR VAR BIN_OP VAR VAR BIN_OP VAR VAR IF VAR BIN_OP BIN_OP VAR NUMBER VAR BIN_OP BIN_OP VAR NUMBER VAR STRING EXPR FUNC_CALL VAR STRING RETURN EXPR FUNC_CALL VAR STRING FOR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL STRING VAR EXPR FUNC_CALL VAR |
Igor has been into chess for a long time and now he is sick of the game by the ordinary rules. He is going to think of new rules of the game and become world famous.
Igor's chessboard is a square of size n × n cells. Igor decided that simple rules guarantee success, that's why his game will have only one type of pieces. Besides, all pieces in his game are of the same color. The possible moves of a piece are described by a set of shift vectors. The next passage contains a formal description of available moves.
Let the rows of the board be numbered from top to bottom and the columns be numbered from left to right from 1 to n. Let's assign to each square a pair of integers (x, y) — the number of the corresponding column and row. Each of the possible moves of the piece is defined by a pair of integers (dx, dy); using this move, the piece moves from the field (x, y) to the field (x + dx, y + dy). You can perform the move if the cell (x + dx, y + dy) is within the boundaries of the board and doesn't contain another piece. Pieces that stand on the cells other than (x, y) and (x + dx, y + dy) are not important when considering the possibility of making the given move (for example, like when a knight moves in usual chess).
Igor offers you to find out what moves his chess piece can make. He placed several pieces on the board and for each unoccupied square he told you whether it is attacked by any present piece (i.e. whether some of the pieces on the field can move to that cell). Restore a possible set of shift vectors of the piece, or else determine that Igor has made a mistake and such situation is impossible for any set of shift vectors.
-----Input-----
The first line contains a single integer n (1 ≤ n ≤ 50).
The next n lines contain n characters each describing the position offered by Igor. The j-th character of the i-th string can have the following values: o — in this case the field (i, j) is occupied by a piece and the field may or may not be attacked by some other piece; x — in this case field (i, j) is attacked by some piece; . — in this case field (i, j) isn't attacked by any piece.
It is guaranteed that there is at least one piece on the board.
-----Output-----
If there is a valid set of moves, in the first line print a single word 'YES' (without the quotes). Next, print the description of the set of moves of a piece in the form of a (2n - 1) × (2n - 1) board, the center of the board has a piece and symbols 'x' mark cells that are attacked by it, in a format similar to the input. See examples of the output for a full understanding of the format. If there are several possible answers, print any of them.
If a valid set of moves does not exist, print a single word 'NO'.
-----Examples-----
Input
5
oxxxx
x...x
x...x
x...x
xxxxo
Output
YES
....x....
....x....
....x....
....x....
xxxxoxxxx
....x....
....x....
....x....
....x....
Input
6
.x.x..
x.x.x.
.xo..x
x..ox.
.x.x.x
..x.x.
Output
YES
...........
...........
...........
....x.x....
...x...x...
.....o.....
...x...x...
....x.x....
...........
...........
...........
Input
3
o.x
oxx
o.x
Output
NO
-----Note-----
In the first sample test the piece is a usual chess rook, and in the second sample test the piece is a usual chess knight. | import sys
n = int(input())
s = [input() for i in range(n)]
a = [([1] * (2 * n)) for i in range(2 * n)]
for i in range(n):
for j in range(n):
if s[i][j] != "o":
continue
for x in range(n):
for y in range(n):
if s[x][y] == ".":
a[n + x - i][n + y - j] = 0
for i in range(n):
for j in range(n):
if s[i][j] != "x":
continue
c = 0
for x in range(n):
for y in range(n):
if s[x][y] == "o" and a[n + i - x][n + j - y] == 1:
c = 1
break
if c == 1:
break
if c == 0:
print("NO")
return
print("YES")
for i in range(1, 2 * n):
for j in range(1, 2 * n):
if i == n and j == n:
print("o", end="")
elif a[i][j] == 1:
print("x", end="")
else:
print(".", end="")
print("") | IMPORT ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP NUMBER VAR VAR FUNC_CALL VAR BIN_OP NUMBER VAR FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR STRING FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR STRING ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR BIN_OP BIN_OP VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR STRING ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR STRING VAR BIN_OP BIN_OP VAR VAR VAR BIN_OP BIN_OP VAR VAR VAR NUMBER ASSIGN VAR NUMBER IF VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR STRING RETURN EXPR FUNC_CALL VAR STRING FOR VAR FUNC_CALL VAR NUMBER BIN_OP NUMBER VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP NUMBER VAR IF VAR VAR VAR VAR EXPR FUNC_CALL VAR STRING STRING IF VAR VAR VAR NUMBER EXPR FUNC_CALL VAR STRING STRING EXPR FUNC_CALL VAR STRING STRING EXPR FUNC_CALL VAR STRING |
Igor has been into chess for a long time and now he is sick of the game by the ordinary rules. He is going to think of new rules of the game and become world famous.
Igor's chessboard is a square of size n × n cells. Igor decided that simple rules guarantee success, that's why his game will have only one type of pieces. Besides, all pieces in his game are of the same color. The possible moves of a piece are described by a set of shift vectors. The next passage contains a formal description of available moves.
Let the rows of the board be numbered from top to bottom and the columns be numbered from left to right from 1 to n. Let's assign to each square a pair of integers (x, y) — the number of the corresponding column and row. Each of the possible moves of the piece is defined by a pair of integers (dx, dy); using this move, the piece moves from the field (x, y) to the field (x + dx, y + dy). You can perform the move if the cell (x + dx, y + dy) is within the boundaries of the board and doesn't contain another piece. Pieces that stand on the cells other than (x, y) and (x + dx, y + dy) are not important when considering the possibility of making the given move (for example, like when a knight moves in usual chess).
Igor offers you to find out what moves his chess piece can make. He placed several pieces on the board and for each unoccupied square he told you whether it is attacked by any present piece (i.e. whether some of the pieces on the field can move to that cell). Restore a possible set of shift vectors of the piece, or else determine that Igor has made a mistake and such situation is impossible for any set of shift vectors.
-----Input-----
The first line contains a single integer n (1 ≤ n ≤ 50).
The next n lines contain n characters each describing the position offered by Igor. The j-th character of the i-th string can have the following values: o — in this case the field (i, j) is occupied by a piece and the field may or may not be attacked by some other piece; x — in this case field (i, j) is attacked by some piece; . — in this case field (i, j) isn't attacked by any piece.
It is guaranteed that there is at least one piece on the board.
-----Output-----
If there is a valid set of moves, in the first line print a single word 'YES' (without the quotes). Next, print the description of the set of moves of a piece in the form of a (2n - 1) × (2n - 1) board, the center of the board has a piece and symbols 'x' mark cells that are attacked by it, in a format similar to the input. See examples of the output for a full understanding of the format. If there are several possible answers, print any of them.
If a valid set of moves does not exist, print a single word 'NO'.
-----Examples-----
Input
5
oxxxx
x...x
x...x
x...x
xxxxo
Output
YES
....x....
....x....
....x....
....x....
xxxxoxxxx
....x....
....x....
....x....
....x....
Input
6
.x.x..
x.x.x.
.xo..x
x..ox.
.x.x.x
..x.x.
Output
YES
...........
...........
...........
....x.x....
...x...x...
.....o.....
...x...x...
....x.x....
...........
...........
...........
Input
3
o.x
oxx
o.x
Output
NO
-----Note-----
In the first sample test the piece is a usual chess rook, and in the second sample test the piece is a usual chess knight. | from sys import stdin
n = int(stdin.readline())
board = []
for x in range(n):
board.append(list(stdin.readline().strip()))
moves = {y: {x: (True) for x in range(-n + 1, n)} for y in range(-n + 1, n)}
for x in range(n):
for y in range(n):
if board[x][y] == "o":
for a in range(n):
for b in range(n):
if board[a][b] == ".":
moves[a - x][b - y] = False
recreate = [["." for x in range(n)] for y in range(n)]
for x in range(n):
for y in range(n):
if board[x][y] == "o":
recreate[x][y] = "o"
for a in moves:
for b in moves:
if moves[a][b]:
if 0 <= x + a < n and 0 <= y + b < n:
if recreate[x + a][y + b] != "o":
recreate[x + a][y + b] = "x"
if board == recreate:
print("YES")
for x in range(-n + 1, n):
l = []
for y in range(-n + 1, n):
if moves[x][y]:
l.append("x")
else:
l.append(".")
if x == 0:
l[n - 1] = "o"
print("".join(l))
else:
print("NO") | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR STRING FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR STRING ASSIGN VAR BIN_OP VAR VAR BIN_OP VAR VAR NUMBER ASSIGN VAR STRING VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR STRING ASSIGN VAR VAR VAR STRING FOR VAR VAR FOR VAR VAR IF VAR VAR VAR IF NUMBER BIN_OP VAR VAR VAR NUMBER BIN_OP VAR VAR VAR IF VAR BIN_OP VAR VAR BIN_OP VAR VAR STRING ASSIGN VAR BIN_OP VAR VAR BIN_OP VAR VAR STRING IF VAR VAR EXPR FUNC_CALL VAR STRING FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR IF VAR VAR VAR EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING IF VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER STRING EXPR FUNC_CALL VAR FUNC_CALL STRING VAR EXPR FUNC_CALL VAR STRING |
The Zoo in the Grid Kingdom is represented by an infinite grid. The Zoo has n observation binoculars located at the OX axis. For each i between 1 and n, inclusive, there exists a single binocular located at the point with coordinates (i, 0). There are m flamingos in the Zoo, located at points with positive coordinates. The flamingos are currently sleeping and you can assume that they don't move.
In order to get a good view over the flamingos, each of the binoculars can be independently rotated to face any angle (not necessarily integer). Then, the binocular can be used to observe all flamingos that is located at the straight line passing through the binocular at the angle it is set. In other words, you can assign each binocular a direction corresponding to any straight line passing through the binocular, and the binocular will be able to see all flamingos located on that line.
Today, some kids from the prestigious Codeforces kindergarten went on a Field Study to the Zoo. Their teacher would like to set each binocular an angle to maximize the number of flamingos that can be seen by the binocular. The teacher is very interested in the sum of these values over all binoculars. Please help him find this sum.
Input
The first line contains two space-separated integers n and m (1 ≤ n ≤ 106, 1 ≤ m ≤ 250), denoting the number of binoculars and the number of flamingos, respectively.
Then m lines follow, the i-th line will contain two space-separated integers xi and yi (1 ≤ xi, yi ≤ 109), which means that the i-th flamingo is located at point (xi, yi).
All flamingos will be located at distinct points.
Output
Print a single integer denoting the maximum total number of flamingos that can be seen by all the binoculars.
Examples
Input
5 5
2 1
4 1
3 2
4 3
4 4
Output
11
Note
This picture shows the answer to the example test case.
<image> | from sys import *
def gcd(a, b):
if b == 0:
return a
return gcd(b, a % b)
def normalize_rational(num, den):
if num ^ den < 0 and num > 0 or num ^ den > 0 and num < 0:
num = -num
den = -den
g = gcd(abs(num), abs(den))
num //= g
den //= g
return num, den
l = stdin.readline()
n, m = (int(tkn) for tkn in l.split())
xs = [0] * m
ys = [0] * m
maxhits = [1] * (n + 1)
maxhits[0] = 0
for i in range(m):
l = stdin.readline()
x, y = (int(tkn) for tkn in l.split())
xs[i] = x
ys[i] = y
line_to_points = {}
for i in range(m):
for j in range(m):
dy = ys[i] - ys[j]
dx = xs[i] - xs[j]
if dy == 0:
continue
else:
slope = normalize_rational(dy, dx)
c_prime = ys[i] * dx - xs[i] * dy
x_intercept = -c_prime / dy
line = slope, x_intercept
if line in line_to_points:
points = line_to_points[line]
points.add(i)
points.add(j)
continue
if int(x_intercept) == x_intercept and x_intercept <= n and x_intercept > 0:
points = set([i, j])
line_to_points[line] = points
for line, points in line_to_points.items():
x_intercept = int(line[1])
maxhits[x_intercept] = max(maxhits[x_intercept], len(points))
print(sum(maxhits)) | FUNC_DEF IF VAR NUMBER RETURN VAR RETURN FUNC_CALL VAR VAR BIN_OP VAR VAR FUNC_DEF IF BIN_OP VAR VAR NUMBER VAR NUMBER BIN_OP VAR VAR NUMBER VAR NUMBER ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR VAR VAR VAR RETURN VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER ASSIGN VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR DICT FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR VAR IF VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR BIN_OP VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR VAR VAR IF VAR VAR ASSIGN VAR VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR VAR VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR LIST VAR VAR ASSIGN VAR VAR VAR FOR VAR VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR |
The Zoo in the Grid Kingdom is represented by an infinite grid. The Zoo has n observation binoculars located at the OX axis. For each i between 1 and n, inclusive, there exists a single binocular located at the point with coordinates (i, 0). There are m flamingos in the Zoo, located at points with positive coordinates. The flamingos are currently sleeping and you can assume that they don't move.
In order to get a good view over the flamingos, each of the binoculars can be independently rotated to face any angle (not necessarily integer). Then, the binocular can be used to observe all flamingos that is located at the straight line passing through the binocular at the angle it is set. In other words, you can assign each binocular a direction corresponding to any straight line passing through the binocular, and the binocular will be able to see all flamingos located on that line.
Today, some kids from the prestigious Codeforces kindergarten went on a Field Study to the Zoo. Their teacher would like to set each binocular an angle to maximize the number of flamingos that can be seen by the binocular. The teacher is very interested in the sum of these values over all binoculars. Please help him find this sum.
Input
The first line contains two space-separated integers n and m (1 ≤ n ≤ 106, 1 ≤ m ≤ 250), denoting the number of binoculars and the number of flamingos, respectively.
Then m lines follow, the i-th line will contain two space-separated integers xi and yi (1 ≤ xi, yi ≤ 109), which means that the i-th flamingo is located at point (xi, yi).
All flamingos will be located at distinct points.
Output
Print a single integer denoting the maximum total number of flamingos that can be seen by all the binoculars.
Examples
Input
5 5
2 1
4 1
3 2
4 3
4 4
Output
11
Note
This picture shows the answer to the example test case.
<image> | n, m = map(int, input().split())
l = []
anss = [1] * (n + 1)
for i in range(m):
l.append(list(map(int, input().split())))
for i in range(m - 1):
for j in range(i + 1, m):
sx = l[i][0] - l[j][0]
sy = l[i][1] - l[j][1]
ans = 2
if sx:
if sy:
x = (l[i][0] * sy - l[i][1] * sx) / sy
if 1 <= x <= n and int(x) == x:
for k in range(j + 1, m):
if l[k][1] * sx == sy * (l[k][0] - l[i][0]) + l[i][1] * sx:
ans += 1
anss[int(x)] = max(ans, anss[int(x)])
elif 1 <= l[i][0] <= n:
for k in range(j + 1, m):
if l[k][0] == l[i][0]:
ans += 1
anss[l[i][0]] = max(anss[l[i][0]], ans)
print(sum(anss[1:])) | ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR ASSIGN VAR BIN_OP VAR VAR NUMBER VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR NUMBER VAR VAR NUMBER ASSIGN VAR NUMBER IF VAR IF VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR VAR NUMBER VAR BIN_OP VAR VAR NUMBER VAR VAR IF NUMBER VAR VAR FUNC_CALL VAR VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR IF BIN_OP VAR VAR NUMBER VAR BIN_OP BIN_OP VAR BIN_OP VAR VAR NUMBER VAR VAR NUMBER BIN_OP VAR VAR NUMBER VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR IF NUMBER VAR VAR NUMBER VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR IF VAR VAR NUMBER VAR VAR NUMBER VAR NUMBER ASSIGN VAR VAR VAR NUMBER FUNC_CALL VAR VAR VAR VAR NUMBER VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER |
Given two positive integers n and k, the binary string Sn is formed as follows:
S1 = "0"
Si = Si-1 + "1" + reverse(invert(Si-1)) for i > 1
Where + denotes the concatenation operation, reverse(x) returns the reversed string x, and invert(x) inverts all the bits in x (0 changes to 1 and 1 changes to 0).
For example, the first 4 strings in the above sequence are:
S1 = "0"
S2 = "011"
S3 = "0111001"
S4 = "011100110110001"
Return the kth bit in Sn. It is guaranteed that k is valid for the given n.
Example 1:
Input: n = 3, k = 1
Output: "0"
Explanation: S3 is "0111001". The first bit is "0".
Example 2:
Input: n = 4, k = 11
Output: "1"
Explanation: S4 is "011100110110001". The 11th bit is "1".
Example 3:
Input: n = 1, k = 1
Output: "0"
Example 4:
Input: n = 2, k = 3
Output: "1"
Constraints:
1 <= n <= 20
1 <= k <= 2n - 1 | class Solution:
def findKthBit(self, n: int, k: int) -> str:
def revert(s):
ans = ""
for x in s:
if x == "1":
ans += "0"
else:
ans += "1"
return ans
def helper(n):
if n == 1:
return "0"
res = helper(n - 1)
return res + "1" + revert(res)[::-1]
return helper(n)[k - 1] | CLASS_DEF FUNC_DEF VAR VAR FUNC_DEF ASSIGN VAR STRING FOR VAR VAR IF VAR STRING VAR STRING VAR STRING RETURN VAR FUNC_DEF IF VAR NUMBER RETURN STRING ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER RETURN BIN_OP BIN_OP VAR STRING FUNC_CALL VAR VAR NUMBER RETURN FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR |
Given two positive integers n and k, the binary string Sn is formed as follows:
S1 = "0"
Si = Si-1 + "1" + reverse(invert(Si-1)) for i > 1
Where + denotes the concatenation operation, reverse(x) returns the reversed string x, and invert(x) inverts all the bits in x (0 changes to 1 and 1 changes to 0).
For example, the first 4 strings in the above sequence are:
S1 = "0"
S2 = "011"
S3 = "0111001"
S4 = "011100110110001"
Return the kth bit in Sn. It is guaranteed that k is valid for the given n.
Example 1:
Input: n = 3, k = 1
Output: "0"
Explanation: S3 is "0111001". The first bit is "0".
Example 2:
Input: n = 4, k = 11
Output: "1"
Explanation: S4 is "011100110110001". The 11th bit is "1".
Example 3:
Input: n = 1, k = 1
Output: "0"
Example 4:
Input: n = 2, k = 3
Output: "1"
Constraints:
1 <= n <= 20
1 <= k <= 2n - 1 | class Solution:
s = [0]
def findKthBit(self, n: int, k: int) -> str:
while len(self.s) < k:
self.s = self.s + [1] + [(n ^ 1) for n in self.s[::-1]]
return str(self.s[k - 1]) | CLASS_DEF ASSIGN VAR LIST NUMBER FUNC_DEF VAR VAR WHILE FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR LIST NUMBER BIN_OP VAR NUMBER VAR VAR NUMBER RETURN FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR |
Given two positive integers n and k, the binary string Sn is formed as follows:
S1 = "0"
Si = Si-1 + "1" + reverse(invert(Si-1)) for i > 1
Where + denotes the concatenation operation, reverse(x) returns the reversed string x, and invert(x) inverts all the bits in x (0 changes to 1 and 1 changes to 0).
For example, the first 4 strings in the above sequence are:
S1 = "0"
S2 = "011"
S3 = "0111001"
S4 = "011100110110001"
Return the kth bit in Sn. It is guaranteed that k is valid for the given n.
Example 1:
Input: n = 3, k = 1
Output: "0"
Explanation: S3 is "0111001". The first bit is "0".
Example 2:
Input: n = 4, k = 11
Output: "1"
Explanation: S4 is "011100110110001". The 11th bit is "1".
Example 3:
Input: n = 1, k = 1
Output: "0"
Example 4:
Input: n = 2, k = 3
Output: "1"
Constraints:
1 <= n <= 20
1 <= k <= 2n - 1 | class Solution:
def findKthBit(self, n: int, k: int) -> str:
return self.solv(n, k)
def solv(sef, n, k):
i = 1
lst = ["0"]
ans = "0"
ki = 1
if k == 1:
return "0"
while i < n + 1:
ans = "1"
lst.append(ans)
ki += 1
if ki == k:
return ans
for j in range(ki - 2, -1, -1):
ans = "1" if lst[j] == "0" else "0"
lst.append(ans)
ki += 1
if ki == k:
return ans
i += 1
return None | CLASS_DEF FUNC_DEF VAR VAR RETURN FUNC_CALL VAR VAR VAR VAR FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR LIST STRING ASSIGN VAR STRING ASSIGN VAR NUMBER IF VAR NUMBER RETURN STRING WHILE VAR BIN_OP VAR NUMBER ASSIGN VAR STRING EXPR FUNC_CALL VAR VAR VAR NUMBER IF VAR VAR RETURN VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR VAR VAR STRING STRING STRING EXPR FUNC_CALL VAR VAR VAR NUMBER IF VAR VAR RETURN VAR VAR NUMBER RETURN NONE |
Given two positive integers n and k, the binary string Sn is formed as follows:
S1 = "0"
Si = Si-1 + "1" + reverse(invert(Si-1)) for i > 1
Where + denotes the concatenation operation, reverse(x) returns the reversed string x, and invert(x) inverts all the bits in x (0 changes to 1 and 1 changes to 0).
For example, the first 4 strings in the above sequence are:
S1 = "0"
S2 = "011"
S3 = "0111001"
S4 = "011100110110001"
Return the kth bit in Sn. It is guaranteed that k is valid for the given n.
Example 1:
Input: n = 3, k = 1
Output: "0"
Explanation: S3 is "0111001". The first bit is "0".
Example 2:
Input: n = 4, k = 11
Output: "1"
Explanation: S4 is "011100110110001". The 11th bit is "1".
Example 3:
Input: n = 1, k = 1
Output: "0"
Example 4:
Input: n = 2, k = 3
Output: "1"
Constraints:
1 <= n <= 20
1 <= k <= 2n - 1 | class Solution:
def findKthBit(self, n: int, k: int) -> str:
def invert(bin):
bin = list(bin)
for i in range(len(bin)):
if bin[i] == "1":
bin[i] = "0"
else:
bin[i] = "1"
return "".join(bin)
bin_n = bin(n)[2:]
dp = ["0"] * n
for i in range(1, n):
dp[i] = dp[i - 1] + "1" + invert(dp[i - 1])[::-1]
return dp[-1][k - 1] | CLASS_DEF FUNC_DEF VAR VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR STRING ASSIGN VAR VAR STRING ASSIGN VAR VAR STRING RETURN FUNC_CALL STRING VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR BIN_OP LIST STRING VAR FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER STRING FUNC_CALL VAR VAR BIN_OP VAR NUMBER NUMBER RETURN VAR NUMBER BIN_OP VAR NUMBER VAR |
Given two positive integers n and k, the binary string Sn is formed as follows:
S1 = "0"
Si = Si-1 + "1" + reverse(invert(Si-1)) for i > 1
Where + denotes the concatenation operation, reverse(x) returns the reversed string x, and invert(x) inverts all the bits in x (0 changes to 1 and 1 changes to 0).
For example, the first 4 strings in the above sequence are:
S1 = "0"
S2 = "011"
S3 = "0111001"
S4 = "011100110110001"
Return the kth bit in Sn. It is guaranteed that k is valid for the given n.
Example 1:
Input: n = 3, k = 1
Output: "0"
Explanation: S3 is "0111001". The first bit is "0".
Example 2:
Input: n = 4, k = 11
Output: "1"
Explanation: S4 is "011100110110001". The 11th bit is "1".
Example 3:
Input: n = 1, k = 1
Output: "0"
Example 4:
Input: n = 2, k = 3
Output: "1"
Constraints:
1 <= n <= 20
1 <= k <= 2n - 1 | class Solution:
def findKthBit(self, n: int, k: int) -> str:
d = dict()
d[0] = [0]
for i in range(1, n + 1):
d[i] = d[i - 1] + [1] + [(1 - x) for x in d[i - 1]][::-1]
return str(d[n][k - 1]) | CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER LIST NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER LIST NUMBER BIN_OP NUMBER VAR VAR VAR BIN_OP VAR NUMBER NUMBER RETURN FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER VAR |
Given two positive integers n and k, the binary string Sn is formed as follows:
S1 = "0"
Si = Si-1 + "1" + reverse(invert(Si-1)) for i > 1
Where + denotes the concatenation operation, reverse(x) returns the reversed string x, and invert(x) inverts all the bits in x (0 changes to 1 and 1 changes to 0).
For example, the first 4 strings in the above sequence are:
S1 = "0"
S2 = "011"
S3 = "0111001"
S4 = "011100110110001"
Return the kth bit in Sn. It is guaranteed that k is valid for the given n.
Example 1:
Input: n = 3, k = 1
Output: "0"
Explanation: S3 is "0111001". The first bit is "0".
Example 2:
Input: n = 4, k = 11
Output: "1"
Explanation: S4 is "011100110110001". The 11th bit is "1".
Example 3:
Input: n = 1, k = 1
Output: "0"
Example 4:
Input: n = 2, k = 3
Output: "1"
Constraints:
1 <= n <= 20
1 <= k <= 2n - 1 | class Solution:
def findKthBit(self, n: int, k: int) -> str:
s = "0"
if n == 1:
return "0"
for i in range(1, n):
s_i = s[::-1]
s = s + "1" + "".join([("1" if c == "0" else "0") for c in s_i])
return s[k - 1] | CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR STRING IF VAR NUMBER RETURN STRING FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR STRING FUNC_CALL STRING VAR STRING STRING STRING VAR VAR RETURN VAR BIN_OP VAR NUMBER VAR |
Given two positive integers n and k, the binary string Sn is formed as follows:
S1 = "0"
Si = Si-1 + "1" + reverse(invert(Si-1)) for i > 1
Where + denotes the concatenation operation, reverse(x) returns the reversed string x, and invert(x) inverts all the bits in x (0 changes to 1 and 1 changes to 0).
For example, the first 4 strings in the above sequence are:
S1 = "0"
S2 = "011"
S3 = "0111001"
S4 = "011100110110001"
Return the kth bit in Sn. It is guaranteed that k is valid for the given n.
Example 1:
Input: n = 3, k = 1
Output: "0"
Explanation: S3 is "0111001". The first bit is "0".
Example 2:
Input: n = 4, k = 11
Output: "1"
Explanation: S4 is "011100110110001". The 11th bit is "1".
Example 3:
Input: n = 1, k = 1
Output: "0"
Example 4:
Input: n = 2, k = 3
Output: "1"
Constraints:
1 <= n <= 20
1 <= k <= 2n - 1 | class Solution:
def findKthBit(self, n: int, k: int) -> str:
full = [0]
for _ in range(n - 1):
full = full + [1] + [((x + 1) % 2) for x in full][::-1]
return str(full[k - 1]) | CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR LIST NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR LIST NUMBER BIN_OP BIN_OP VAR NUMBER NUMBER VAR VAR NUMBER RETURN FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR |
Given two positive integers n and k, the binary string Sn is formed as follows:
S1 = "0"
Si = Si-1 + "1" + reverse(invert(Si-1)) for i > 1
Where + denotes the concatenation operation, reverse(x) returns the reversed string x, and invert(x) inverts all the bits in x (0 changes to 1 and 1 changes to 0).
For example, the first 4 strings in the above sequence are:
S1 = "0"
S2 = "011"
S3 = "0111001"
S4 = "011100110110001"
Return the kth bit in Sn. It is guaranteed that k is valid for the given n.
Example 1:
Input: n = 3, k = 1
Output: "0"
Explanation: S3 is "0111001". The first bit is "0".
Example 2:
Input: n = 4, k = 11
Output: "1"
Explanation: S4 is "011100110110001". The 11th bit is "1".
Example 3:
Input: n = 1, k = 1
Output: "0"
Example 4:
Input: n = 2, k = 3
Output: "1"
Constraints:
1 <= n <= 20
1 <= k <= 2n - 1 | class Solution:
def findKthBit(self, n: int, k: int) -> str:
def invert(s):
temp = ""
for i in range(len(s)):
if s[i] == "1":
temp = temp + "0"
else:
temp = temp + "1"
return temp
s = "0"
for i in range(1, n):
inv = invert(s)
rev = inv[::-1]
s = s + "1" + rev
return s[k - 1] | CLASS_DEF FUNC_DEF VAR VAR FUNC_DEF ASSIGN VAR STRING FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR STRING ASSIGN VAR BIN_OP VAR STRING ASSIGN VAR BIN_OP VAR STRING RETURN VAR ASSIGN VAR STRING FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR STRING VAR RETURN VAR BIN_OP VAR NUMBER VAR |
Given two positive integers n and k, the binary string Sn is formed as follows:
S1 = "0"
Si = Si-1 + "1" + reverse(invert(Si-1)) for i > 1
Where + denotes the concatenation operation, reverse(x) returns the reversed string x, and invert(x) inverts all the bits in x (0 changes to 1 and 1 changes to 0).
For example, the first 4 strings in the above sequence are:
S1 = "0"
S2 = "011"
S3 = "0111001"
S4 = "011100110110001"
Return the kth bit in Sn. It is guaranteed that k is valid for the given n.
Example 1:
Input: n = 3, k = 1
Output: "0"
Explanation: S3 is "0111001". The first bit is "0".
Example 2:
Input: n = 4, k = 11
Output: "1"
Explanation: S4 is "011100110110001". The 11th bit is "1".
Example 3:
Input: n = 1, k = 1
Output: "0"
Example 4:
Input: n = 2, k = 3
Output: "1"
Constraints:
1 <= n <= 20
1 <= k <= 2n - 1 | bins = [[0]]
for _ in range(20):
bins.append(bins[-1] + [1] + [(1 - x) for x in reversed(bins[-1])])
class Solution:
def findKthBit(self, n: int, k: int) -> str:
return str(bins[n - 1][k - 1]) | ASSIGN VAR LIST LIST NUMBER FOR VAR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR BIN_OP BIN_OP VAR NUMBER LIST NUMBER BIN_OP NUMBER VAR VAR FUNC_CALL VAR VAR NUMBER CLASS_DEF FUNC_DEF VAR VAR RETURN FUNC_CALL VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR |
Given two positive integers n and k, the binary string Sn is formed as follows:
S1 = "0"
Si = Si-1 + "1" + reverse(invert(Si-1)) for i > 1
Where + denotes the concatenation operation, reverse(x) returns the reversed string x, and invert(x) inverts all the bits in x (0 changes to 1 and 1 changes to 0).
For example, the first 4 strings in the above sequence are:
S1 = "0"
S2 = "011"
S3 = "0111001"
S4 = "011100110110001"
Return the kth bit in Sn. It is guaranteed that k is valid for the given n.
Example 1:
Input: n = 3, k = 1
Output: "0"
Explanation: S3 is "0111001". The first bit is "0".
Example 2:
Input: n = 4, k = 11
Output: "1"
Explanation: S4 is "011100110110001". The 11th bit is "1".
Example 3:
Input: n = 1, k = 1
Output: "0"
Example 4:
Input: n = 2, k = 3
Output: "1"
Constraints:
1 <= n <= 20
1 <= k <= 2n - 1 | class Solution:
def invert(self, t):
ret = ""
for i in t:
if i == "0":
ret += "1"
else:
ret += "0"
return ret
def findKthBit(self, n: int, k: int) -> str:
i = 1
l = list()
l.append("0")
while i <= n:
t = l[-1]
ans = t + "1"
ans += self.invert(t[::-1])
l.append(ans)
i += 1
return l[-1][k - 1] | CLASS_DEF FUNC_DEF ASSIGN VAR STRING FOR VAR VAR IF VAR STRING VAR STRING VAR STRING RETURN VAR FUNC_DEF VAR VAR ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR EXPR FUNC_CALL VAR STRING WHILE VAR VAR ASSIGN VAR VAR NUMBER ASSIGN VAR BIN_OP VAR STRING VAR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR NUMBER RETURN VAR NUMBER BIN_OP VAR NUMBER VAR |
Given two positive integers n and k, the binary string Sn is formed as follows:
S1 = "0"
Si = Si-1 + "1" + reverse(invert(Si-1)) for i > 1
Where + denotes the concatenation operation, reverse(x) returns the reversed string x, and invert(x) inverts all the bits in x (0 changes to 1 and 1 changes to 0).
For example, the first 4 strings in the above sequence are:
S1 = "0"
S2 = "011"
S3 = "0111001"
S4 = "011100110110001"
Return the kth bit in Sn. It is guaranteed that k is valid for the given n.
Example 1:
Input: n = 3, k = 1
Output: "0"
Explanation: S3 is "0111001". The first bit is "0".
Example 2:
Input: n = 4, k = 11
Output: "1"
Explanation: S4 is "011100110110001". The 11th bit is "1".
Example 3:
Input: n = 1, k = 1
Output: "0"
Example 4:
Input: n = 2, k = 3
Output: "1"
Constraints:
1 <= n <= 20
1 <= k <= 2n - 1 | class Solution:
def findKthBit(self, n: int, k: int) -> str:
s = ""
def recurse(n):
nonlocal s
if n <= 1:
return "0"
else:
_str = recurse(n - 1)
s = _str + "1" + self.reverse(self.invert(_str))
return s
return recurse(n)[k - 1]
def invert(self, string):
return "".join([("0" if char == "1" else "1") for char in string])
def reverse(self, string):
return string[::-1] | CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR STRING FUNC_DEF IF VAR NUMBER RETURN STRING ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR STRING FUNC_CALL VAR FUNC_CALL VAR VAR RETURN VAR RETURN FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR FUNC_DEF RETURN FUNC_CALL STRING VAR STRING STRING STRING VAR VAR FUNC_DEF RETURN VAR NUMBER |
Given two positive integers n and k, the binary string Sn is formed as follows:
S1 = "0"
Si = Si-1 + "1" + reverse(invert(Si-1)) for i > 1
Where + denotes the concatenation operation, reverse(x) returns the reversed string x, and invert(x) inverts all the bits in x (0 changes to 1 and 1 changes to 0).
For example, the first 4 strings in the above sequence are:
S1 = "0"
S2 = "011"
S3 = "0111001"
S4 = "011100110110001"
Return the kth bit in Sn. It is guaranteed that k is valid for the given n.
Example 1:
Input: n = 3, k = 1
Output: "0"
Explanation: S3 is "0111001". The first bit is "0".
Example 2:
Input: n = 4, k = 11
Output: "1"
Explanation: S4 is "011100110110001". The 11th bit is "1".
Example 3:
Input: n = 1, k = 1
Output: "0"
Example 4:
Input: n = 2, k = 3
Output: "1"
Constraints:
1 <= n <= 20
1 <= k <= 2n - 1 | class Solution:
def findKthBit(self, n: int, k: int) -> str:
def helper(res, k):
if k == 0:
return res
invert = [("1" if i == "0" else "0") for i in res]
invert = "".join(invert)
res = res + "1" + invert[::-1]
return helper(res, k - 1)
res = helper("0", n - 1)
return res[k - 1] | CLASS_DEF FUNC_DEF VAR VAR FUNC_DEF IF VAR NUMBER RETURN VAR ASSIGN VAR VAR STRING STRING STRING VAR VAR ASSIGN VAR FUNC_CALL STRING VAR ASSIGN VAR BIN_OP BIN_OP VAR STRING VAR NUMBER RETURN FUNC_CALL VAR VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR STRING BIN_OP VAR NUMBER RETURN VAR BIN_OP VAR NUMBER VAR |
Given two positive integers n and k, the binary string Sn is formed as follows:
S1 = "0"
Si = Si-1 + "1" + reverse(invert(Si-1)) for i > 1
Where + denotes the concatenation operation, reverse(x) returns the reversed string x, and invert(x) inverts all the bits in x (0 changes to 1 and 1 changes to 0).
For example, the first 4 strings in the above sequence are:
S1 = "0"
S2 = "011"
S3 = "0111001"
S4 = "011100110110001"
Return the kth bit in Sn. It is guaranteed that k is valid for the given n.
Example 1:
Input: n = 3, k = 1
Output: "0"
Explanation: S3 is "0111001". The first bit is "0".
Example 2:
Input: n = 4, k = 11
Output: "1"
Explanation: S4 is "011100110110001". The 11th bit is "1".
Example 3:
Input: n = 1, k = 1
Output: "0"
Example 4:
Input: n = 2, k = 3
Output: "1"
Constraints:
1 <= n <= 20
1 <= k <= 2n - 1 | class Solution:
def findKthBit(self, n: int, k: int) -> str:
s = "011"
if k < 4:
return s[k - 1]
d = {"0": "1", "1": "0"}
for i in range(n):
s += "1"
if len(s) == k:
return "1"
else:
for x in s[:-1][::-1]:
s += d[x]
if len(s) == k:
return d[x] | CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR STRING IF VAR NUMBER RETURN VAR BIN_OP VAR NUMBER ASSIGN VAR DICT STRING STRING STRING STRING FOR VAR FUNC_CALL VAR VAR VAR STRING IF FUNC_CALL VAR VAR VAR RETURN STRING FOR VAR VAR NUMBER NUMBER VAR VAR VAR IF FUNC_CALL VAR VAR VAR RETURN VAR VAR VAR |
Given two positive integers n and k, the binary string Sn is formed as follows:
S1 = "0"
Si = Si-1 + "1" + reverse(invert(Si-1)) for i > 1
Where + denotes the concatenation operation, reverse(x) returns the reversed string x, and invert(x) inverts all the bits in x (0 changes to 1 and 1 changes to 0).
For example, the first 4 strings in the above sequence are:
S1 = "0"
S2 = "011"
S3 = "0111001"
S4 = "011100110110001"
Return the kth bit in Sn. It is guaranteed that k is valid for the given n.
Example 1:
Input: n = 3, k = 1
Output: "0"
Explanation: S3 is "0111001". The first bit is "0".
Example 2:
Input: n = 4, k = 11
Output: "1"
Explanation: S4 is "011100110110001". The 11th bit is "1".
Example 3:
Input: n = 1, k = 1
Output: "0"
Example 4:
Input: n = 2, k = 3
Output: "1"
Constraints:
1 <= n <= 20
1 <= k <= 2n - 1 | class Solution:
def findKthBit(self, n: int, k: int) -> str:
def operator(s):
t = reversed([("0" if i == "1" else "1") for i in s])
return s + "1" + "".join(t)
s = "0"
while len(s) < k:
s = operator(s)
return s[k - 1] | CLASS_DEF FUNC_DEF VAR VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR STRING STRING STRING VAR VAR RETURN BIN_OP BIN_OP VAR STRING FUNC_CALL STRING VAR ASSIGN VAR STRING WHILE FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR RETURN VAR BIN_OP VAR NUMBER VAR |
Given two positive integers n and k, the binary string Sn is formed as follows:
S1 = "0"
Si = Si-1 + "1" + reverse(invert(Si-1)) for i > 1
Where + denotes the concatenation operation, reverse(x) returns the reversed string x, and invert(x) inverts all the bits in x (0 changes to 1 and 1 changes to 0).
For example, the first 4 strings in the above sequence are:
S1 = "0"
S2 = "011"
S3 = "0111001"
S4 = "011100110110001"
Return the kth bit in Sn. It is guaranteed that k is valid for the given n.
Example 1:
Input: n = 3, k = 1
Output: "0"
Explanation: S3 is "0111001". The first bit is "0".
Example 2:
Input: n = 4, k = 11
Output: "1"
Explanation: S4 is "011100110110001". The 11th bit is "1".
Example 3:
Input: n = 1, k = 1
Output: "0"
Example 4:
Input: n = 2, k = 3
Output: "1"
Constraints:
1 <= n <= 20
1 <= k <= 2n - 1 | class Solution:
def findKthBit(self, n: int, k: int) -> str:
def reverse_invert(array: List[str]) -> List[str]:
res = []
for ind in range(len(array) - 1, -1, -1):
res.append("1" if array[ind] == "0" else "0")
return res
bits = ["0"]
for curr in range(2, n + 1):
bits += ["1"] + reverse_invert(bits)
return bits[k - 1] | CLASS_DEF FUNC_DEF VAR VAR FUNC_DEF VAR VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER NUMBER EXPR FUNC_CALL VAR VAR VAR STRING STRING STRING RETURN VAR VAR VAR ASSIGN VAR LIST STRING FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER VAR BIN_OP LIST STRING FUNC_CALL VAR VAR RETURN VAR BIN_OP VAR NUMBER VAR |
Given two positive integers n and k, the binary string Sn is formed as follows:
S1 = "0"
Si = Si-1 + "1" + reverse(invert(Si-1)) for i > 1
Where + denotes the concatenation operation, reverse(x) returns the reversed string x, and invert(x) inverts all the bits in x (0 changes to 1 and 1 changes to 0).
For example, the first 4 strings in the above sequence are:
S1 = "0"
S2 = "011"
S3 = "0111001"
S4 = "011100110110001"
Return the kth bit in Sn. It is guaranteed that k is valid for the given n.
Example 1:
Input: n = 3, k = 1
Output: "0"
Explanation: S3 is "0111001". The first bit is "0".
Example 2:
Input: n = 4, k = 11
Output: "1"
Explanation: S4 is "011100110110001". The 11th bit is "1".
Example 3:
Input: n = 1, k = 1
Output: "0"
Example 4:
Input: n = 2, k = 3
Output: "1"
Constraints:
1 <= n <= 20
1 <= k <= 2n - 1 | class Solution:
def findKthBit(self, n: int, k: int) -> str:
curr = "0"
for x in range(2, n + 1):
nxt = curr + "1" + self.invert(curr)[::-1]
curr = nxt
return curr[k - 1]
def invert(self, s):
return "".join([("1" if x == "0" else "0") for x in s]) | CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR STRING FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR STRING FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR RETURN VAR BIN_OP VAR NUMBER VAR FUNC_DEF RETURN FUNC_CALL STRING VAR STRING STRING STRING VAR VAR |
Given two positive integers n and k, the binary string Sn is formed as follows:
S1 = "0"
Si = Si-1 + "1" + reverse(invert(Si-1)) for i > 1
Where + denotes the concatenation operation, reverse(x) returns the reversed string x, and invert(x) inverts all the bits in x (0 changes to 1 and 1 changes to 0).
For example, the first 4 strings in the above sequence are:
S1 = "0"
S2 = "011"
S3 = "0111001"
S4 = "011100110110001"
Return the kth bit in Sn. It is guaranteed that k is valid for the given n.
Example 1:
Input: n = 3, k = 1
Output: "0"
Explanation: S3 is "0111001". The first bit is "0".
Example 2:
Input: n = 4, k = 11
Output: "1"
Explanation: S4 is "011100110110001". The 11th bit is "1".
Example 3:
Input: n = 1, k = 1
Output: "0"
Example 4:
Input: n = 2, k = 3
Output: "1"
Constraints:
1 <= n <= 20
1 <= k <= 2n - 1 | class Solution:
def findKthBit(self, n: int, k: int) -> str:
cur = "0"
while len(cur) < k:
cur = cur + "1" + "".join("1" if b == "0" else "0" for b in reversed(cur))
return cur[k - 1] | CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR STRING WHILE FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR STRING FUNC_CALL STRING VAR STRING STRING STRING VAR FUNC_CALL VAR VAR RETURN VAR BIN_OP VAR NUMBER VAR |
Given two positive integers n and k, the binary string Sn is formed as follows:
S1 = "0"
Si = Si-1 + "1" + reverse(invert(Si-1)) for i > 1
Where + denotes the concatenation operation, reverse(x) returns the reversed string x, and invert(x) inverts all the bits in x (0 changes to 1 and 1 changes to 0).
For example, the first 4 strings in the above sequence are:
S1 = "0"
S2 = "011"
S3 = "0111001"
S4 = "011100110110001"
Return the kth bit in Sn. It is guaranteed that k is valid for the given n.
Example 1:
Input: n = 3, k = 1
Output: "0"
Explanation: S3 is "0111001". The first bit is "0".
Example 2:
Input: n = 4, k = 11
Output: "1"
Explanation: S4 is "011100110110001". The 11th bit is "1".
Example 3:
Input: n = 1, k = 1
Output: "0"
Example 4:
Input: n = 2, k = 3
Output: "1"
Constraints:
1 <= n <= 20
1 <= k <= 2n - 1 | class Solution:
def findKthBit(self, n: int, k: int) -> str:
def inner(n, k):
if n == 1:
re = 0
elif k == 2 ** (n - 1):
re = 1
elif k < 2 ** (n - 1):
re = inner(n - 1, k)
else:
re = inner(n - 1, 2**n - k) ^ 1
return re
return str(inner(n, k)) | CLASS_DEF FUNC_DEF VAR VAR FUNC_DEF IF VAR NUMBER ASSIGN VAR NUMBER IF VAR BIN_OP NUMBER BIN_OP VAR NUMBER ASSIGN VAR NUMBER IF VAR BIN_OP NUMBER BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR ASSIGN VAR BIN_OP FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP BIN_OP NUMBER VAR VAR NUMBER RETURN VAR RETURN FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR |
Given two positive integers n and k, the binary string Sn is formed as follows:
S1 = "0"
Si = Si-1 + "1" + reverse(invert(Si-1)) for i > 1
Where + denotes the concatenation operation, reverse(x) returns the reversed string x, and invert(x) inverts all the bits in x (0 changes to 1 and 1 changes to 0).
For example, the first 4 strings in the above sequence are:
S1 = "0"
S2 = "011"
S3 = "0111001"
S4 = "011100110110001"
Return the kth bit in Sn. It is guaranteed that k is valid for the given n.
Example 1:
Input: n = 3, k = 1
Output: "0"
Explanation: S3 is "0111001". The first bit is "0".
Example 2:
Input: n = 4, k = 11
Output: "1"
Explanation: S4 is "011100110110001". The 11th bit is "1".
Example 3:
Input: n = 1, k = 1
Output: "0"
Example 4:
Input: n = 2, k = 3
Output: "1"
Constraints:
1 <= n <= 20
1 <= k <= 2n - 1 | class Solution:
def findKthBit(self, n: int, k: int) -> str:
def negVal(val):
lenVal = len(val)
mask = int("1" * lenVal, 2)
val = int(val, 2)
neg = val ^ mask
lenNeg = len(bin(neg)) - 2
neg = "0" * (lenVal - lenNeg) + bin(neg)[2:]
return neg[::-1]
strVal = "0"
for i in range(n - 1):
strVal += "1" + negVal(strVal)
if k > len(strVal):
return "0"
else:
return strVal[k - 1] | CLASS_DEF FUNC_DEF VAR VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP STRING VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP STRING BIN_OP VAR VAR FUNC_CALL VAR VAR NUMBER RETURN VAR NUMBER ASSIGN VAR STRING FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR BIN_OP STRING FUNC_CALL VAR VAR IF VAR FUNC_CALL VAR VAR RETURN STRING RETURN VAR BIN_OP VAR NUMBER VAR |
Given two positive integers n and k, the binary string Sn is formed as follows:
S1 = "0"
Si = Si-1 + "1" + reverse(invert(Si-1)) for i > 1
Where + denotes the concatenation operation, reverse(x) returns the reversed string x, and invert(x) inverts all the bits in x (0 changes to 1 and 1 changes to 0).
For example, the first 4 strings in the above sequence are:
S1 = "0"
S2 = "011"
S3 = "0111001"
S4 = "011100110110001"
Return the kth bit in Sn. It is guaranteed that k is valid for the given n.
Example 1:
Input: n = 3, k = 1
Output: "0"
Explanation: S3 is "0111001". The first bit is "0".
Example 2:
Input: n = 4, k = 11
Output: "1"
Explanation: S4 is "011100110110001". The 11th bit is "1".
Example 3:
Input: n = 1, k = 1
Output: "0"
Example 4:
Input: n = 2, k = 3
Output: "1"
Constraints:
1 <= n <= 20
1 <= k <= 2n - 1 | class Solution:
def findKthBit(self, n: int, k: int) -> str:
def helper(memo):
ans = ""
for c in memo:
if c == "0":
ans += "1"
else:
ans += "0"
return ans
memo = "0"
for i in range(1, n + 1):
memo = memo + "1" + helper(memo)[::-1]
return memo[k - 1] | CLASS_DEF FUNC_DEF VAR VAR FUNC_DEF ASSIGN VAR STRING FOR VAR VAR IF VAR STRING VAR STRING VAR STRING RETURN VAR ASSIGN VAR STRING FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR STRING FUNC_CALL VAR VAR NUMBER RETURN VAR BIN_OP VAR NUMBER VAR |
Given two positive integers n and k, the binary string Sn is formed as follows:
S1 = "0"
Si = Si-1 + "1" + reverse(invert(Si-1)) for i > 1
Where + denotes the concatenation operation, reverse(x) returns the reversed string x, and invert(x) inverts all the bits in x (0 changes to 1 and 1 changes to 0).
For example, the first 4 strings in the above sequence are:
S1 = "0"
S2 = "011"
S3 = "0111001"
S4 = "011100110110001"
Return the kth bit in Sn. It is guaranteed that k is valid for the given n.
Example 1:
Input: n = 3, k = 1
Output: "0"
Explanation: S3 is "0111001". The first bit is "0".
Example 2:
Input: n = 4, k = 11
Output: "1"
Explanation: S4 is "011100110110001". The 11th bit is "1".
Example 3:
Input: n = 1, k = 1
Output: "0"
Example 4:
Input: n = 2, k = 3
Output: "1"
Constraints:
1 <= n <= 20
1 <= k <= 2n - 1 | class Solution:
def findKthBit(self, n: int, k: int) -> str:
def revinv(x):
ans = []
for c in x:
if c == "0":
ans.append("1")
else:
ans.append("0")
return "".join(reversed(ans))
s = "0"
while k > len(s):
s = s + "1" + revinv(s)
return s[k - 1] | CLASS_DEF FUNC_DEF VAR VAR FUNC_DEF ASSIGN VAR LIST FOR VAR VAR IF VAR STRING EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING RETURN FUNC_CALL STRING FUNC_CALL VAR VAR ASSIGN VAR STRING WHILE VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR STRING FUNC_CALL VAR VAR RETURN VAR BIN_OP VAR NUMBER VAR |
Given two positive integers n and k, the binary string Sn is formed as follows:
S1 = "0"
Si = Si-1 + "1" + reverse(invert(Si-1)) for i > 1
Where + denotes the concatenation operation, reverse(x) returns the reversed string x, and invert(x) inverts all the bits in x (0 changes to 1 and 1 changes to 0).
For example, the first 4 strings in the above sequence are:
S1 = "0"
S2 = "011"
S3 = "0111001"
S4 = "011100110110001"
Return the kth bit in Sn. It is guaranteed that k is valid for the given n.
Example 1:
Input: n = 3, k = 1
Output: "0"
Explanation: S3 is "0111001". The first bit is "0".
Example 2:
Input: n = 4, k = 11
Output: "1"
Explanation: S4 is "011100110110001". The 11th bit is "1".
Example 3:
Input: n = 1, k = 1
Output: "0"
Example 4:
Input: n = 2, k = 3
Output: "1"
Constraints:
1 <= n <= 20
1 <= k <= 2n - 1 | class Solution:
def invert(self, s):
out = ""
for c in s:
if c == "0":
out += "1"
if c == "1":
out += "0"
return out
def reverse(self, s):
return s[::-1]
def findKthBit(self, n: int, k: int) -> str:
l = []
for i in range(n):
if i == 0:
l.append("0")
else:
last_s = l[-1]
s = last_s + "1" + self.reverse(self.invert(last_s))
l.append(s)
s = l[-1]
return s[k - 1] | CLASS_DEF FUNC_DEF ASSIGN VAR STRING FOR VAR VAR IF VAR STRING VAR STRING IF VAR STRING VAR STRING RETURN VAR FUNC_DEF RETURN VAR NUMBER FUNC_DEF VAR VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR STRING ASSIGN VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR STRING FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR NUMBER RETURN VAR BIN_OP VAR NUMBER VAR |
Given two positive integers n and k, the binary string Sn is formed as follows:
S1 = "0"
Si = Si-1 + "1" + reverse(invert(Si-1)) for i > 1
Where + denotes the concatenation operation, reverse(x) returns the reversed string x, and invert(x) inverts all the bits in x (0 changes to 1 and 1 changes to 0).
For example, the first 4 strings in the above sequence are:
S1 = "0"
S2 = "011"
S3 = "0111001"
S4 = "011100110110001"
Return the kth bit in Sn. It is guaranteed that k is valid for the given n.
Example 1:
Input: n = 3, k = 1
Output: "0"
Explanation: S3 is "0111001". The first bit is "0".
Example 2:
Input: n = 4, k = 11
Output: "1"
Explanation: S4 is "011100110110001". The 11th bit is "1".
Example 3:
Input: n = 1, k = 1
Output: "0"
Example 4:
Input: n = 2, k = 3
Output: "1"
Constraints:
1 <= n <= 20
1 <= k <= 2n - 1 | class Solution:
def findKthBit(self, n: int, k: int) -> str:
def invert(s):
return "".join([("1" if c == "0" else "0") for c in s])
def revStr(s):
return "".join(list(reversed(s)))
counter = 1
x = "0"
while counter != n:
x = x + "1" + revStr(invert(x))
counter += 1
return x[k - 1] | CLASS_DEF FUNC_DEF VAR VAR FUNC_DEF RETURN FUNC_CALL STRING VAR STRING STRING STRING VAR VAR FUNC_DEF RETURN FUNC_CALL STRING FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR STRING WHILE VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR STRING FUNC_CALL VAR FUNC_CALL VAR VAR VAR NUMBER RETURN VAR BIN_OP VAR NUMBER VAR |
Given two positive integers n and k, the binary string Sn is formed as follows:
S1 = "0"
Si = Si-1 + "1" + reverse(invert(Si-1)) for i > 1
Where + denotes the concatenation operation, reverse(x) returns the reversed string x, and invert(x) inverts all the bits in x (0 changes to 1 and 1 changes to 0).
For example, the first 4 strings in the above sequence are:
S1 = "0"
S2 = "011"
S3 = "0111001"
S4 = "011100110110001"
Return the kth bit in Sn. It is guaranteed that k is valid for the given n.
Example 1:
Input: n = 3, k = 1
Output: "0"
Explanation: S3 is "0111001". The first bit is "0".
Example 2:
Input: n = 4, k = 11
Output: "1"
Explanation: S4 is "011100110110001". The 11th bit is "1".
Example 3:
Input: n = 1, k = 1
Output: "0"
Example 4:
Input: n = 2, k = 3
Output: "1"
Constraints:
1 <= n <= 20
1 <= k <= 2n - 1 | def invert(s):
return bin(int(s, 2) ^ int("1" * len(s), 2))[2:]
def reverse(s):
return s[::-1]
class Solution:
def findKthBit(self, n: int, k: int) -> str:
S = [None] * n
S[0] = "0"
for i in range(1, n):
S[i] = S[i - 1] + "1" + reverse(invert(S[i - 1]))
return S[n - 1][k - 1] | FUNC_DEF RETURN FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER FUNC_CALL VAR BIN_OP STRING FUNC_CALL VAR VAR NUMBER NUMBER FUNC_DEF RETURN VAR NUMBER CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR BIN_OP LIST NONE VAR ASSIGN VAR NUMBER STRING FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER STRING FUNC_CALL VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER RETURN VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR |
Given two positive integers n and k, the binary string Sn is formed as follows:
S1 = "0"
Si = Si-1 + "1" + reverse(invert(Si-1)) for i > 1
Where + denotes the concatenation operation, reverse(x) returns the reversed string x, and invert(x) inverts all the bits in x (0 changes to 1 and 1 changes to 0).
For example, the first 4 strings in the above sequence are:
S1 = "0"
S2 = "011"
S3 = "0111001"
S4 = "011100110110001"
Return the kth bit in Sn. It is guaranteed that k is valid for the given n.
Example 1:
Input: n = 3, k = 1
Output: "0"
Explanation: S3 is "0111001". The first bit is "0".
Example 2:
Input: n = 4, k = 11
Output: "1"
Explanation: S4 is "011100110110001". The 11th bit is "1".
Example 3:
Input: n = 1, k = 1
Output: "0"
Example 4:
Input: n = 2, k = 3
Output: "1"
Constraints:
1 <= n <= 20
1 <= k <= 2n - 1 | class Solution:
def findKthBit(self, n: int, k: int) -> str:
l = [0] * (n + 1)
for i in range(1, n + 1):
l[i] = 2 * l[i - 1] + 1
def helper(n, k, f):
m = l[n]
if n == 1:
return 0 ^ f
if k == m // 2 + 1:
return 1 ^ f
if k <= m // 2:
return helper(n - 1, k, f)
else:
return helper(n - 1, m - k + 1, 1 ^ f)
return str(helper(n, k, 0)) | CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR BIN_OP BIN_OP NUMBER VAR BIN_OP VAR NUMBER NUMBER FUNC_DEF ASSIGN VAR VAR VAR IF VAR NUMBER RETURN BIN_OP NUMBER VAR IF VAR BIN_OP BIN_OP VAR NUMBER NUMBER RETURN BIN_OP NUMBER VAR IF VAR BIN_OP VAR NUMBER RETURN FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR RETURN FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP BIN_OP VAR VAR NUMBER BIN_OP NUMBER VAR RETURN FUNC_CALL VAR FUNC_CALL VAR VAR VAR NUMBER VAR |
Given two positive integers n and k, the binary string Sn is formed as follows:
S1 = "0"
Si = Si-1 + "1" + reverse(invert(Si-1)) for i > 1
Where + denotes the concatenation operation, reverse(x) returns the reversed string x, and invert(x) inverts all the bits in x (0 changes to 1 and 1 changes to 0).
For example, the first 4 strings in the above sequence are:
S1 = "0"
S2 = "011"
S3 = "0111001"
S4 = "011100110110001"
Return the kth bit in Sn. It is guaranteed that k is valid for the given n.
Example 1:
Input: n = 3, k = 1
Output: "0"
Explanation: S3 is "0111001". The first bit is "0".
Example 2:
Input: n = 4, k = 11
Output: "1"
Explanation: S4 is "011100110110001". The 11th bit is "1".
Example 3:
Input: n = 1, k = 1
Output: "0"
Example 4:
Input: n = 2, k = 3
Output: "1"
Constraints:
1 <= n <= 20
1 <= k <= 2n - 1 | class Solution:
def findKthBit(self, n: int, k: int) -> str:
def reverseInvert(string: str) -> str:
output = []
for char in string:
if char == "0":
output.append("1")
else:
output.append("0")
output.reverse()
return "".join(output)
def getNthSequence(n: int) -> str:
if n == 1:
return "0"
prev = getNthSequence(n - 1)
return prev + "1" + reverseInvert(prev)
return getNthSequence(int(ceil(log2(k)) + 1))[k - 1] | CLASS_DEF FUNC_DEF VAR VAR FUNC_DEF VAR ASSIGN VAR LIST FOR VAR VAR IF VAR STRING EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR RETURN FUNC_CALL STRING VAR VAR FUNC_DEF VAR IF VAR NUMBER RETURN STRING ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER RETURN BIN_OP BIN_OP VAR STRING FUNC_CALL VAR VAR VAR RETURN FUNC_CALL VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER BIN_OP VAR NUMBER VAR |
Given two positive integers n and k, the binary string Sn is formed as follows:
S1 = "0"
Si = Si-1 + "1" + reverse(invert(Si-1)) for i > 1
Where + denotes the concatenation operation, reverse(x) returns the reversed string x, and invert(x) inverts all the bits in x (0 changes to 1 and 1 changes to 0).
For example, the first 4 strings in the above sequence are:
S1 = "0"
S2 = "011"
S3 = "0111001"
S4 = "011100110110001"
Return the kth bit in Sn. It is guaranteed that k is valid for the given n.
Example 1:
Input: n = 3, k = 1
Output: "0"
Explanation: S3 is "0111001". The first bit is "0".
Example 2:
Input: n = 4, k = 11
Output: "1"
Explanation: S4 is "011100110110001". The 11th bit is "1".
Example 3:
Input: n = 1, k = 1
Output: "0"
Example 4:
Input: n = 2, k = 3
Output: "1"
Constraints:
1 <= n <= 20
1 <= k <= 2n - 1 | class Solution:
def findKthBit(self, n: int, k: int) -> str:
return str(k // (k & -k) >> 1 & 1 ^ (k & 1 ^ 1))
def findKthBit_logn(self, n: int, k: int) -> str:
cur, flip = 1 << n - 1, 0
while k > 1:
if k & k - 1 == 0:
return str(flip ^ 1)
if k > cur:
k = 2 * cur - k
flip ^= 1
cur >>= 1
return str(flip) | CLASS_DEF FUNC_DEF VAR VAR RETURN FUNC_CALL VAR BIN_OP BIN_OP BIN_OP BIN_OP VAR BIN_OP VAR VAR NUMBER NUMBER BIN_OP BIN_OP VAR NUMBER NUMBER VAR FUNC_DEF VAR VAR ASSIGN VAR VAR BIN_OP NUMBER BIN_OP VAR NUMBER NUMBER WHILE VAR NUMBER IF BIN_OP VAR BIN_OP VAR NUMBER NUMBER RETURN FUNC_CALL VAR BIN_OP VAR NUMBER IF VAR VAR ASSIGN VAR BIN_OP BIN_OP NUMBER VAR VAR VAR NUMBER VAR NUMBER RETURN FUNC_CALL VAR VAR VAR |
Given two positive integers n and k, the binary string Sn is formed as follows:
S1 = "0"
Si = Si-1 + "1" + reverse(invert(Si-1)) for i > 1
Where + denotes the concatenation operation, reverse(x) returns the reversed string x, and invert(x) inverts all the bits in x (0 changes to 1 and 1 changes to 0).
For example, the first 4 strings in the above sequence are:
S1 = "0"
S2 = "011"
S3 = "0111001"
S4 = "011100110110001"
Return the kth bit in Sn. It is guaranteed that k is valid for the given n.
Example 1:
Input: n = 3, k = 1
Output: "0"
Explanation: S3 is "0111001". The first bit is "0".
Example 2:
Input: n = 4, k = 11
Output: "1"
Explanation: S4 is "011100110110001". The 11th bit is "1".
Example 3:
Input: n = 1, k = 1
Output: "0"
Example 4:
Input: n = 2, k = 3
Output: "1"
Constraints:
1 <= n <= 20
1 <= k <= 2n - 1 | class Solution:
def findKthBit(self, n: int, k: int) -> str:
dp = ["0"] * n
for i in range(1, n):
dp[i] = (
dp[i - 1] + "1" + "".join([str(1 - int(n)) for n in dp[i - 1]])[::-1]
)
return dp[-1][k - 1] | CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR BIN_OP LIST STRING VAR FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER STRING FUNC_CALL STRING FUNC_CALL VAR BIN_OP NUMBER FUNC_CALL VAR VAR VAR VAR BIN_OP VAR NUMBER NUMBER RETURN VAR NUMBER BIN_OP VAR NUMBER VAR |
Given two positive integers n and k, the binary string Sn is formed as follows:
S1 = "0"
Si = Si-1 + "1" + reverse(invert(Si-1)) for i > 1
Where + denotes the concatenation operation, reverse(x) returns the reversed string x, and invert(x) inverts all the bits in x (0 changes to 1 and 1 changes to 0).
For example, the first 4 strings in the above sequence are:
S1 = "0"
S2 = "011"
S3 = "0111001"
S4 = "011100110110001"
Return the kth bit in Sn. It is guaranteed that k is valid for the given n.
Example 1:
Input: n = 3, k = 1
Output: "0"
Explanation: S3 is "0111001". The first bit is "0".
Example 2:
Input: n = 4, k = 11
Output: "1"
Explanation: S4 is "011100110110001". The 11th bit is "1".
Example 3:
Input: n = 1, k = 1
Output: "0"
Example 4:
Input: n = 2, k = 3
Output: "1"
Constraints:
1 <= n <= 20
1 <= k <= 2n - 1 | class Solution:
def findKthBit(self, n: int, k: int) -> str:
s = []
for i in range(n):
if i == 0:
s.append(["0"])
else:
temp = s[i - 1].copy()
for j in range(len(temp)):
if temp[j] == "0":
temp[j] = "1"
else:
temp[j] = "0"
temp = temp[::-1]
s.append([])
for zz in s[i - 1]:
s[i].append(zz)
s[i].append("1")
for zz in temp:
s[i].append(zz)
return s[-1][k - 1] | CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR LIST STRING ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR STRING ASSIGN VAR VAR STRING ASSIGN VAR VAR STRING ASSIGN VAR VAR NUMBER EXPR FUNC_CALL VAR LIST FOR VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR STRING FOR VAR VAR EXPR FUNC_CALL VAR VAR VAR RETURN VAR NUMBER BIN_OP VAR NUMBER VAR |
Given two positive integers n and k, the binary string Sn is formed as follows:
S1 = "0"
Si = Si-1 + "1" + reverse(invert(Si-1)) for i > 1
Where + denotes the concatenation operation, reverse(x) returns the reversed string x, and invert(x) inverts all the bits in x (0 changes to 1 and 1 changes to 0).
For example, the first 4 strings in the above sequence are:
S1 = "0"
S2 = "011"
S3 = "0111001"
S4 = "011100110110001"
Return the kth bit in Sn. It is guaranteed that k is valid for the given n.
Example 1:
Input: n = 3, k = 1
Output: "0"
Explanation: S3 is "0111001". The first bit is "0".
Example 2:
Input: n = 4, k = 11
Output: "1"
Explanation: S4 is "011100110110001". The 11th bit is "1".
Example 3:
Input: n = 1, k = 1
Output: "0"
Example 4:
Input: n = 2, k = 3
Output: "1"
Constraints:
1 <= n <= 20
1 <= k <= 2n - 1 | class Solution:
def findKthBit(self, n: int, k: int) -> str:
def invert(x):
return [(0 if i == 1 else 1) for i in x]
S = [0]
i = 1
while i < n and len(S) < k:
S.append(1)
S.extend(invert(S[:-1])[::-1])
i += 1
return str(S[k - 1]) | CLASS_DEF FUNC_DEF VAR VAR FUNC_DEF RETURN VAR NUMBER NUMBER NUMBER VAR VAR ASSIGN VAR LIST NUMBER ASSIGN VAR NUMBER WHILE VAR VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER NUMBER VAR NUMBER RETURN FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR |
Given two positive integers n and k, the binary string Sn is formed as follows:
S1 = "0"
Si = Si-1 + "1" + reverse(invert(Si-1)) for i > 1
Where + denotes the concatenation operation, reverse(x) returns the reversed string x, and invert(x) inverts all the bits in x (0 changes to 1 and 1 changes to 0).
For example, the first 4 strings in the above sequence are:
S1 = "0"
S2 = "011"
S3 = "0111001"
S4 = "011100110110001"
Return the kth bit in Sn. It is guaranteed that k is valid for the given n.
Example 1:
Input: n = 3, k = 1
Output: "0"
Explanation: S3 is "0111001". The first bit is "0".
Example 2:
Input: n = 4, k = 11
Output: "1"
Explanation: S4 is "011100110110001". The 11th bit is "1".
Example 3:
Input: n = 1, k = 1
Output: "0"
Example 4:
Input: n = 2, k = 3
Output: "1"
Constraints:
1 <= n <= 20
1 <= k <= 2n - 1 | class Solution:
def findKthBit(self, n: int, k: int) -> str:
memo = {}
def dp(s):
if s in memo:
return memo[s]
if s == 1:
return "0"
temp = dp(s - 1)
res = temp + "1" + "".join([("0" if b == "1" else "1") for b in temp])[::-1]
memo[s] = res
return res
return dp(n)[k - 1] | CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR DICT FUNC_DEF IF VAR VAR RETURN VAR VAR IF VAR NUMBER RETURN STRING ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR STRING FUNC_CALL STRING VAR STRING STRING STRING VAR VAR NUMBER ASSIGN VAR VAR VAR RETURN VAR RETURN FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR |
Given two positive integers n and k, the binary string Sn is formed as follows:
S1 = "0"
Si = Si-1 + "1" + reverse(invert(Si-1)) for i > 1
Where + denotes the concatenation operation, reverse(x) returns the reversed string x, and invert(x) inverts all the bits in x (0 changes to 1 and 1 changes to 0).
For example, the first 4 strings in the above sequence are:
S1 = "0"
S2 = "011"
S3 = "0111001"
S4 = "011100110110001"
Return the kth bit in Sn. It is guaranteed that k is valid for the given n.
Example 1:
Input: n = 3, k = 1
Output: "0"
Explanation: S3 is "0111001". The first bit is "0".
Example 2:
Input: n = 4, k = 11
Output: "1"
Explanation: S4 is "011100110110001". The 11th bit is "1".
Example 3:
Input: n = 1, k = 1
Output: "0"
Example 4:
Input: n = 2, k = 3
Output: "1"
Constraints:
1 <= n <= 20
1 <= k <= 2n - 1 | class Solution:
def findKthBit(self, n: int, k: int) -> str:
sn = ["0"]
for i in range(1, n):
nx = sn[i - 1] + "1" + self.reverse(self.invert(sn[i - 1]))
sn.append(nx)
return sn[-1][k - 1]
def invert(self, s):
return "".join("0" if _ == "1" else "1" for _ in s)
def reverse(self, s):
return "".join(reversed(s)) | CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR LIST STRING FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER STRING FUNC_CALL VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR RETURN VAR NUMBER BIN_OP VAR NUMBER VAR FUNC_DEF RETURN FUNC_CALL STRING VAR STRING STRING STRING VAR VAR FUNC_DEF RETURN FUNC_CALL STRING FUNC_CALL VAR VAR |
Given two positive integers n and k, the binary string Sn is formed as follows:
S1 = "0"
Si = Si-1 + "1" + reverse(invert(Si-1)) for i > 1
Where + denotes the concatenation operation, reverse(x) returns the reversed string x, and invert(x) inverts all the bits in x (0 changes to 1 and 1 changes to 0).
For example, the first 4 strings in the above sequence are:
S1 = "0"
S2 = "011"
S3 = "0111001"
S4 = "011100110110001"
Return the kth bit in Sn. It is guaranteed that k is valid for the given n.
Example 1:
Input: n = 3, k = 1
Output: "0"
Explanation: S3 is "0111001". The first bit is "0".
Example 2:
Input: n = 4, k = 11
Output: "1"
Explanation: S4 is "011100110110001". The 11th bit is "1".
Example 3:
Input: n = 1, k = 1
Output: "0"
Example 4:
Input: n = 2, k = 3
Output: "1"
Constraints:
1 <= n <= 20
1 <= k <= 2n - 1 | class Solution:
def findKthBit(self, n: int, k: int) -> str:
s = "0"
while n > 1:
p = ""
for l in s[::-1]:
p += "1" if l == "0" else "0"
s = s + "1" + p
n -= 1
return s[k - 1] | CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR STRING WHILE VAR NUMBER ASSIGN VAR STRING FOR VAR VAR NUMBER VAR VAR STRING STRING STRING ASSIGN VAR BIN_OP BIN_OP VAR STRING VAR VAR NUMBER RETURN VAR BIN_OP VAR NUMBER VAR |
Given two positive integers n and k, the binary string Sn is formed as follows:
S1 = "0"
Si = Si-1 + "1" + reverse(invert(Si-1)) for i > 1
Where + denotes the concatenation operation, reverse(x) returns the reversed string x, and invert(x) inverts all the bits in x (0 changes to 1 and 1 changes to 0).
For example, the first 4 strings in the above sequence are:
S1 = "0"
S2 = "011"
S3 = "0111001"
S4 = "011100110110001"
Return the kth bit in Sn. It is guaranteed that k is valid for the given n.
Example 1:
Input: n = 3, k = 1
Output: "0"
Explanation: S3 is "0111001". The first bit is "0".
Example 2:
Input: n = 4, k = 11
Output: "1"
Explanation: S4 is "011100110110001". The 11th bit is "1".
Example 3:
Input: n = 1, k = 1
Output: "0"
Example 4:
Input: n = 2, k = 3
Output: "1"
Constraints:
1 <= n <= 20
1 <= k <= 2n - 1 | class Solution:
def findKthBit(self, n: int, k: int) -> str:
s2 = "011"
if k <= 3:
return s2[k - 1]
if k == 2 ** (n - 1):
return "1"
elif k < 2 ** (n - 1):
return self.findKthBit(n - 1, k)
else:
ans = self.findKthBit(n - 1, 2**n - k)
return "1" if ans == "0" else "0" | CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR STRING IF VAR NUMBER RETURN VAR BIN_OP VAR NUMBER IF VAR BIN_OP NUMBER BIN_OP VAR NUMBER RETURN STRING IF VAR BIN_OP NUMBER BIN_OP VAR NUMBER RETURN FUNC_CALL VAR BIN_OP VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP BIN_OP NUMBER VAR VAR RETURN VAR STRING STRING STRING VAR |
Given two positive integers n and k, the binary string Sn is formed as follows:
S1 = "0"
Si = Si-1 + "1" + reverse(invert(Si-1)) for i > 1
Where + denotes the concatenation operation, reverse(x) returns the reversed string x, and invert(x) inverts all the bits in x (0 changes to 1 and 1 changes to 0).
For example, the first 4 strings in the above sequence are:
S1 = "0"
S2 = "011"
S3 = "0111001"
S4 = "011100110110001"
Return the kth bit in Sn. It is guaranteed that k is valid for the given n.
Example 1:
Input: n = 3, k = 1
Output: "0"
Explanation: S3 is "0111001". The first bit is "0".
Example 2:
Input: n = 4, k = 11
Output: "1"
Explanation: S4 is "011100110110001". The 11th bit is "1".
Example 3:
Input: n = 1, k = 1
Output: "0"
Example 4:
Input: n = 2, k = 3
Output: "1"
Constraints:
1 <= n <= 20
1 <= k <= 2n - 1 | class Solution:
def findKthBit(self, n: int, k: int) -> str:
m = 2**n - 1
if k == 1 or 0 < m - k < 4:
return "0"
if k == m or k < 5:
return "1"
s = [0]
for _ in range(n - 1):
s += [1] + [(x ^ 1) for x in reversed(s)]
return str(s[k - 1]) | CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR BIN_OP BIN_OP NUMBER VAR NUMBER IF VAR NUMBER NUMBER BIN_OP VAR VAR NUMBER RETURN STRING IF VAR VAR VAR NUMBER RETURN STRING ASSIGN VAR LIST NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER VAR FUNC_CALL VAR VAR RETURN FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR |
Given two positive integers n and k, the binary string Sn is formed as follows:
S1 = "0"
Si = Si-1 + "1" + reverse(invert(Si-1)) for i > 1
Where + denotes the concatenation operation, reverse(x) returns the reversed string x, and invert(x) inverts all the bits in x (0 changes to 1 and 1 changes to 0).
For example, the first 4 strings in the above sequence are:
S1 = "0"
S2 = "011"
S3 = "0111001"
S4 = "011100110110001"
Return the kth bit in Sn. It is guaranteed that k is valid for the given n.
Example 1:
Input: n = 3, k = 1
Output: "0"
Explanation: S3 is "0111001". The first bit is "0".
Example 2:
Input: n = 4, k = 11
Output: "1"
Explanation: S4 is "011100110110001". The 11th bit is "1".
Example 3:
Input: n = 1, k = 1
Output: "0"
Example 4:
Input: n = 2, k = 3
Output: "1"
Constraints:
1 <= n <= 20
1 <= k <= 2n - 1 | class Solution:
def findKthBit(self, n: int, k: int) -> str:
dp = [[]] * n
dp[0] = [0]
for i in range(1, n):
dp[i] = dp[i - 1] + [1] + list(reversed([(v ^ 1) for v in dp[i - 1]]))
return str(dp[-1][k - 1]) | CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR BIN_OP LIST LIST VAR ASSIGN VAR NUMBER LIST NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER LIST NUMBER FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR BIN_OP VAR NUMBER RETURN FUNC_CALL VAR VAR NUMBER BIN_OP VAR NUMBER VAR |
Given two positive integers n and k, the binary string Sn is formed as follows:
S1 = "0"
Si = Si-1 + "1" + reverse(invert(Si-1)) for i > 1
Where + denotes the concatenation operation, reverse(x) returns the reversed string x, and invert(x) inverts all the bits in x (0 changes to 1 and 1 changes to 0).
For example, the first 4 strings in the above sequence are:
S1 = "0"
S2 = "011"
S3 = "0111001"
S4 = "011100110110001"
Return the kth bit in Sn. It is guaranteed that k is valid for the given n.
Example 1:
Input: n = 3, k = 1
Output: "0"
Explanation: S3 is "0111001". The first bit is "0".
Example 2:
Input: n = 4, k = 11
Output: "1"
Explanation: S4 is "011100110110001". The 11th bit is "1".
Example 3:
Input: n = 1, k = 1
Output: "0"
Example 4:
Input: n = 2, k = 3
Output: "1"
Constraints:
1 <= n <= 20
1 <= k <= 2n - 1 | class Solution:
def findKthBit(self, n: int, k: int) -> str:
i = n - 1
invert = False
while i > 0:
half_len = (2 ** (i + 1) - 1) // 2
if k == half_len + 1:
return "1" if not invert else "0"
if k > half_len:
k = half_len - (k - half_len - 1) + 1
invert = not invert
i -= 1
return "1" if invert else "0" | CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER WHILE VAR NUMBER ASSIGN VAR BIN_OP BIN_OP BIN_OP NUMBER BIN_OP VAR NUMBER NUMBER NUMBER IF VAR BIN_OP VAR NUMBER RETURN VAR STRING STRING IF VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR BIN_OP BIN_OP VAR VAR NUMBER NUMBER ASSIGN VAR VAR VAR NUMBER RETURN VAR STRING STRING VAR |
Given two positive integers n and k, the binary string Sn is formed as follows:
S1 = "0"
Si = Si-1 + "1" + reverse(invert(Si-1)) for i > 1
Where + denotes the concatenation operation, reverse(x) returns the reversed string x, and invert(x) inverts all the bits in x (0 changes to 1 and 1 changes to 0).
For example, the first 4 strings in the above sequence are:
S1 = "0"
S2 = "011"
S3 = "0111001"
S4 = "011100110110001"
Return the kth bit in Sn. It is guaranteed that k is valid for the given n.
Example 1:
Input: n = 3, k = 1
Output: "0"
Explanation: S3 is "0111001". The first bit is "0".
Example 2:
Input: n = 4, k = 11
Output: "1"
Explanation: S4 is "011100110110001". The 11th bit is "1".
Example 3:
Input: n = 1, k = 1
Output: "0"
Example 4:
Input: n = 2, k = 3
Output: "1"
Constraints:
1 <= n <= 20
1 <= k <= 2n - 1 | class Solution:
def findKthBit(self, n: int, k: int) -> str:
x = "0"
for i in range(1, n):
x = (
x
+ "1"
+ "".join(list(map(lambda x: "1" if x == "0" else "0", x))[::-1])
)
return x[k - 1] | CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR STRING FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR BIN_OP BIN_OP VAR STRING FUNC_CALL STRING FUNC_CALL VAR FUNC_CALL VAR VAR STRING STRING STRING VAR NUMBER RETURN VAR BIN_OP VAR NUMBER VAR |
Given two positive integers n and k, the binary string Sn is formed as follows:
S1 = "0"
Si = Si-1 + "1" + reverse(invert(Si-1)) for i > 1
Where + denotes the concatenation operation, reverse(x) returns the reversed string x, and invert(x) inverts all the bits in x (0 changes to 1 and 1 changes to 0).
For example, the first 4 strings in the above sequence are:
S1 = "0"
S2 = "011"
S3 = "0111001"
S4 = "011100110110001"
Return the kth bit in Sn. It is guaranteed that k is valid for the given n.
Example 1:
Input: n = 3, k = 1
Output: "0"
Explanation: S3 is "0111001". The first bit is "0".
Example 2:
Input: n = 4, k = 11
Output: "1"
Explanation: S4 is "011100110110001". The 11th bit is "1".
Example 3:
Input: n = 1, k = 1
Output: "0"
Example 4:
Input: n = 2, k = 3
Output: "1"
Constraints:
1 <= n <= 20
1 <= k <= 2n - 1 | class Solution:
def findKthBit(self, n: int, k: int) -> str:
if n == 1:
return "0"
if n == 2:
return ["0", "1", "1"][k - 1]
record = [0, 1, 1]
for i in range(n - 2):
record = record + [1] + record
l = len(record)
record[l // 2 + l // 2 // 2 + 1] = 0
return str(record[k - 1]) | CLASS_DEF FUNC_DEF VAR VAR IF VAR NUMBER RETURN STRING IF VAR NUMBER RETURN LIST STRING STRING STRING BIN_OP VAR NUMBER ASSIGN VAR LIST NUMBER NUMBER NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR LIST NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR NUMBER BIN_OP BIN_OP VAR NUMBER NUMBER NUMBER NUMBER RETURN FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR |
Given two positive integers n and k, the binary string Sn is formed as follows:
S1 = "0"
Si = Si-1 + "1" + reverse(invert(Si-1)) for i > 1
Where + denotes the concatenation operation, reverse(x) returns the reversed string x, and invert(x) inverts all the bits in x (0 changes to 1 and 1 changes to 0).
For example, the first 4 strings in the above sequence are:
S1 = "0"
S2 = "011"
S3 = "0111001"
S4 = "011100110110001"
Return the kth bit in Sn. It is guaranteed that k is valid for the given n.
Example 1:
Input: n = 3, k = 1
Output: "0"
Explanation: S3 is "0111001". The first bit is "0".
Example 2:
Input: n = 4, k = 11
Output: "1"
Explanation: S4 is "011100110110001". The 11th bit is "1".
Example 3:
Input: n = 1, k = 1
Output: "0"
Example 4:
Input: n = 2, k = 3
Output: "1"
Constraints:
1 <= n <= 20
1 <= k <= 2n - 1 | class Solution:
def findKthBit(self, n: int, k: int) -> str:
def invert(data):
ans = ""
for val in data:
if val == "1":
ans += "0"
else:
ans += "1"
return ans
res = [""] * (n + 1)
res[1] = "0"
for i in range(2, n + 1):
res[i] = res[i - 1] + "1" + invert(res[i - 1])[::-1]
return res[n][k - 1] | CLASS_DEF FUNC_DEF VAR VAR FUNC_DEF ASSIGN VAR STRING FOR VAR VAR IF VAR STRING VAR STRING VAR STRING RETURN VAR ASSIGN VAR BIN_OP LIST STRING BIN_OP VAR NUMBER ASSIGN VAR NUMBER STRING FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER STRING FUNC_CALL VAR VAR BIN_OP VAR NUMBER NUMBER RETURN VAR VAR BIN_OP VAR NUMBER VAR |
Given two positive integers n and k, the binary string Sn is formed as follows:
S1 = "0"
Si = Si-1 + "1" + reverse(invert(Si-1)) for i > 1
Where + denotes the concatenation operation, reverse(x) returns the reversed string x, and invert(x) inverts all the bits in x (0 changes to 1 and 1 changes to 0).
For example, the first 4 strings in the above sequence are:
S1 = "0"
S2 = "011"
S3 = "0111001"
S4 = "011100110110001"
Return the kth bit in Sn. It is guaranteed that k is valid for the given n.
Example 1:
Input: n = 3, k = 1
Output: "0"
Explanation: S3 is "0111001". The first bit is "0".
Example 2:
Input: n = 4, k = 11
Output: "1"
Explanation: S4 is "011100110110001". The 11th bit is "1".
Example 3:
Input: n = 1, k = 1
Output: "0"
Example 4:
Input: n = 2, k = 3
Output: "1"
Constraints:
1 <= n <= 20
1 <= k <= 2n - 1 | class Solution:
def findKthBit(self, n: int, k: int) -> str:
A = [0]
while k > len(A):
A.append(1)
for i in range(len(A) - 2, -1, -1):
A.append(A[i] ^ 1)
return str(A[k - 1]) | CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR LIST NUMBER WHILE VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER NUMBER EXPR FUNC_CALL VAR BIN_OP VAR VAR NUMBER RETURN FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR |
Given two positive integers n and k, the binary string Sn is formed as follows:
S1 = "0"
Si = Si-1 + "1" + reverse(invert(Si-1)) for i > 1
Where + denotes the concatenation operation, reverse(x) returns the reversed string x, and invert(x) inverts all the bits in x (0 changes to 1 and 1 changes to 0).
For example, the first 4 strings in the above sequence are:
S1 = "0"
S2 = "011"
S3 = "0111001"
S4 = "011100110110001"
Return the kth bit in Sn. It is guaranteed that k is valid for the given n.
Example 1:
Input: n = 3, k = 1
Output: "0"
Explanation: S3 is "0111001". The first bit is "0".
Example 2:
Input: n = 4, k = 11
Output: "1"
Explanation: S4 is "011100110110001". The 11th bit is "1".
Example 3:
Input: n = 1, k = 1
Output: "0"
Example 4:
Input: n = 2, k = 3
Output: "1"
Constraints:
1 <= n <= 20
1 <= k <= 2n - 1 | class Solution:
def findKthBit(self, n: int, k: int) -> str:
def reverse(s):
return "".join([s[len(s) - 1 - i] for i in range(len(s))])
def invert(s):
return "".join([("1" if s[i] == "0" else "0") for i in range(len(s))])
def findNthStr(n1):
if n1 == 1:
return "0"
else:
prev = findNthStr(n1 - 1)
return prev + "1" + reverse(invert(prev))
return findNthStr(n)[k - 1] | CLASS_DEF FUNC_DEF VAR VAR FUNC_DEF RETURN FUNC_CALL STRING VAR BIN_OP BIN_OP FUNC_CALL VAR VAR NUMBER VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_DEF RETURN FUNC_CALL STRING VAR VAR STRING STRING STRING VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_DEF IF VAR NUMBER RETURN STRING ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER RETURN BIN_OP BIN_OP VAR STRING FUNC_CALL VAR FUNC_CALL VAR VAR RETURN FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR |
Given two positive integers n and k, the binary string Sn is formed as follows:
S1 = "0"
Si = Si-1 + "1" + reverse(invert(Si-1)) for i > 1
Where + denotes the concatenation operation, reverse(x) returns the reversed string x, and invert(x) inverts all the bits in x (0 changes to 1 and 1 changes to 0).
For example, the first 4 strings in the above sequence are:
S1 = "0"
S2 = "011"
S3 = "0111001"
S4 = "011100110110001"
Return the kth bit in Sn. It is guaranteed that k is valid for the given n.
Example 1:
Input: n = 3, k = 1
Output: "0"
Explanation: S3 is "0111001". The first bit is "0".
Example 2:
Input: n = 4, k = 11
Output: "1"
Explanation: S4 is "011100110110001". The 11th bit is "1".
Example 3:
Input: n = 1, k = 1
Output: "0"
Example 4:
Input: n = 2, k = 3
Output: "1"
Constraints:
1 <= n <= 20
1 <= k <= 2n - 1 | class Solution:
def findKthBit(self, n: int, k: int) -> str:
def invert(l):
result = []
for item in l:
if item == "0":
result.append("1")
else:
result.append("0")
return result
string = "0"
while len(string) < k:
string = string + "1"
addition = invert(string[0 : len(string) - 1])
addition.reverse()
addition = "".join(addition)
string = string + addition
return string[k - 1] | CLASS_DEF FUNC_DEF VAR VAR FUNC_DEF ASSIGN VAR LIST FOR VAR VAR IF VAR STRING EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING RETURN VAR ASSIGN VAR STRING WHILE FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP VAR STRING ASSIGN VAR FUNC_CALL VAR VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR ASSIGN VAR FUNC_CALL STRING VAR ASSIGN VAR BIN_OP VAR VAR RETURN VAR BIN_OP VAR NUMBER VAR |
Given two positive integers n and k, the binary string Sn is formed as follows:
S1 = "0"
Si = Si-1 + "1" + reverse(invert(Si-1)) for i > 1
Where + denotes the concatenation operation, reverse(x) returns the reversed string x, and invert(x) inverts all the bits in x (0 changes to 1 and 1 changes to 0).
For example, the first 4 strings in the above sequence are:
S1 = "0"
S2 = "011"
S3 = "0111001"
S4 = "011100110110001"
Return the kth bit in Sn. It is guaranteed that k is valid for the given n.
Example 1:
Input: n = 3, k = 1
Output: "0"
Explanation: S3 is "0111001". The first bit is "0".
Example 2:
Input: n = 4, k = 11
Output: "1"
Explanation: S4 is "011100110110001". The 11th bit is "1".
Example 3:
Input: n = 1, k = 1
Output: "0"
Example 4:
Input: n = 2, k = 3
Output: "1"
Constraints:
1 <= n <= 20
1 <= k <= 2n - 1 | class Solution:
def findKthBit(self, n: int, k: int) -> str:
def invert(n):
s = ""
for char in n:
if char == "0":
s += "1"
else:
s += "0"
return s
def generater(n):
dp = [-1] * n
dp[0] = "0"
for i in range(1, len(dp)):
dp[i] = dp[i - 1] + "1" + invert(dp[i - 1])[::-1]
return dp[-1]
return generater(n)[k - 1] | CLASS_DEF FUNC_DEF VAR VAR FUNC_DEF ASSIGN VAR STRING FOR VAR VAR IF VAR STRING VAR STRING VAR STRING RETURN VAR FUNC_DEF ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR NUMBER STRING FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR ASSIGN VAR VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER STRING FUNC_CALL VAR VAR BIN_OP VAR NUMBER NUMBER RETURN VAR NUMBER RETURN FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR |
Given two positive integers n and k, the binary string Sn is formed as follows:
S1 = "0"
Si = Si-1 + "1" + reverse(invert(Si-1)) for i > 1
Where + denotes the concatenation operation, reverse(x) returns the reversed string x, and invert(x) inverts all the bits in x (0 changes to 1 and 1 changes to 0).
For example, the first 4 strings in the above sequence are:
S1 = "0"
S2 = "011"
S3 = "0111001"
S4 = "011100110110001"
Return the kth bit in Sn. It is guaranteed that k is valid for the given n.
Example 1:
Input: n = 3, k = 1
Output: "0"
Explanation: S3 is "0111001". The first bit is "0".
Example 2:
Input: n = 4, k = 11
Output: "1"
Explanation: S4 is "011100110110001". The 11th bit is "1".
Example 3:
Input: n = 1, k = 1
Output: "0"
Example 4:
Input: n = 2, k = 3
Output: "1"
Constraints:
1 <= n <= 20
1 <= k <= 2n - 1 | class Solution:
def findKthBit(self, n: int, k: int) -> str:
s = []
s.append("0")
for i in range(1, n):
ans = s[i - 1] + "1"
temp = ""
for j in s[i - 1]:
if j == "0":
temp += "1"
else:
temp += "0"
ans = ans + temp[::-1]
s.append(ans)
return s[-1][k - 1] | CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR LIST EXPR FUNC_CALL VAR STRING FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR BIN_OP VAR BIN_OP VAR NUMBER STRING ASSIGN VAR STRING FOR VAR VAR BIN_OP VAR NUMBER IF VAR STRING VAR STRING VAR STRING ASSIGN VAR BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR VAR RETURN VAR NUMBER BIN_OP VAR NUMBER VAR |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.