description
stringlengths 171
4k
| code
stringlengths 94
3.98k
| normalized_code
stringlengths 57
4.99k
|
|---|---|---|
You are given an array a with n distinct integers. Construct an array b by permuting a such that for every non-empty subset of indices S = {x_1, x_2, ..., x_{k}} (1 ≤ x_{i} ≤ n, 0 < k < n) the sums of elements on that positions in a and b are different, i. e. $\sum_{i = 1}^{k} a_{x_{i}} \neq \sum_{i = 1}^{k} b_{x_{i}}$
-----Input-----
The first line contains one integer n (1 ≤ n ≤ 22) — the size of the array.
The second line contains n space-separated distinct integers a_1, a_2, ..., a_{n} (0 ≤ a_{i} ≤ 10^9) — the elements of the array.
-----Output-----
If there is no such array b, print -1.
Otherwise in the only line print n space-separated integers b_1, b_2, ..., b_{n}. Note that b must be a permutation of a.
If there are multiple answers, print any of them.
-----Examples-----
Input
2
1 2
Output
2 1
Input
4
1000 100 10 1
Output
100 1 1000 10
-----Note-----
An array x is a permutation of y, if we can shuffle elements of y such that it will coincide with x.
Note that the empty subset and the subset containing all indices are not counted.
|
n = int(input())
j = [int(x) for x in input().split(" ")]
if n == 1:
print(j[0])
else:
k = [[j[i], i] for i in range(n)]
k.sort()
o = [(0) for i in range(len(k))]
for i in range(len(k)):
o[k[i][1]] = str(k[(i + 1) % n][0])
print(" ".join(o))
|
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR STRING IF VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER ASSIGN VAR LIST VAR VAR VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR NUMBER FUNC_CALL VAR VAR BIN_OP BIN_OP VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL STRING VAR
|
You are given an array a with n distinct integers. Construct an array b by permuting a such that for every non-empty subset of indices S = {x_1, x_2, ..., x_{k}} (1 ≤ x_{i} ≤ n, 0 < k < n) the sums of elements on that positions in a and b are different, i. e. $\sum_{i = 1}^{k} a_{x_{i}} \neq \sum_{i = 1}^{k} b_{x_{i}}$
-----Input-----
The first line contains one integer n (1 ≤ n ≤ 22) — the size of the array.
The second line contains n space-separated distinct integers a_1, a_2, ..., a_{n} (0 ≤ a_{i} ≤ 10^9) — the elements of the array.
-----Output-----
If there is no such array b, print -1.
Otherwise in the only line print n space-separated integers b_1, b_2, ..., b_{n}. Note that b must be a permutation of a.
If there are multiple answers, print any of them.
-----Examples-----
Input
2
1 2
Output
2 1
Input
4
1000 100 10 1
Output
100 1 1000 10
-----Note-----
An array x is a permutation of y, if we can shuffle elements of y such that it will coincide with x.
Note that the empty subset and the subset containing all indices are not counted.
|
n = int(input())
a = list(map(int, input().split()))
def solve(n, a):
if n == 1:
return [str(a[0])]
arr = [[x, i] for i, x in enumerate(a)]
arr = sorted(arr, key=lambda x: x[0])
ans = [-1] * n
for [x, i], [y, j] in zip(arr[1:], arr[:-1]):
ans[j] = str(x)
ans[arr[-1][1]] = str(arr[0][0])
return ans
ans = solve(n, a)
print(" ".join(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 IF VAR NUMBER RETURN LIST FUNC_CALL VAR VAR NUMBER ASSIGN VAR LIST VAR VAR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER VAR FOR LIST VAR VAR LIST VAR VAR FUNC_CALL VAR VAR NUMBER VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR NUMBER NUMBER FUNC_CALL VAR VAR NUMBER NUMBER RETURN VAR ASSIGN VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL STRING VAR
|
You are given an array a with n distinct integers. Construct an array b by permuting a such that for every non-empty subset of indices S = {x_1, x_2, ..., x_{k}} (1 ≤ x_{i} ≤ n, 0 < k < n) the sums of elements on that positions in a and b are different, i. e. $\sum_{i = 1}^{k} a_{x_{i}} \neq \sum_{i = 1}^{k} b_{x_{i}}$
-----Input-----
The first line contains one integer n (1 ≤ n ≤ 22) — the size of the array.
The second line contains n space-separated distinct integers a_1, a_2, ..., a_{n} (0 ≤ a_{i} ≤ 10^9) — the elements of the array.
-----Output-----
If there is no such array b, print -1.
Otherwise in the only line print n space-separated integers b_1, b_2, ..., b_{n}. Note that b must be a permutation of a.
If there are multiple answers, print any of them.
-----Examples-----
Input
2
1 2
Output
2 1
Input
4
1000 100 10 1
Output
100 1 1000 10
-----Note-----
An array x is a permutation of y, if we can shuffle elements of y such that it will coincide with x.
Note that the empty subset and the subset containing all indices are not counted.
|
n = int(input())
L = list(map(int, input().split()))
S = sorted(L)
for i in L:
print(S[(S.index(i) + 1) % n], end=" ")
|
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR FOR VAR VAR EXPR FUNC_CALL VAR VAR BIN_OP BIN_OP FUNC_CALL VAR VAR NUMBER VAR STRING
|
You are given an array a with n distinct integers. Construct an array b by permuting a such that for every non-empty subset of indices S = {x_1, x_2, ..., x_{k}} (1 ≤ x_{i} ≤ n, 0 < k < n) the sums of elements on that positions in a and b are different, i. e. $\sum_{i = 1}^{k} a_{x_{i}} \neq \sum_{i = 1}^{k} b_{x_{i}}$
-----Input-----
The first line contains one integer n (1 ≤ n ≤ 22) — the size of the array.
The second line contains n space-separated distinct integers a_1, a_2, ..., a_{n} (0 ≤ a_{i} ≤ 10^9) — the elements of the array.
-----Output-----
If there is no such array b, print -1.
Otherwise in the only line print n space-separated integers b_1, b_2, ..., b_{n}. Note that b must be a permutation of a.
If there are multiple answers, print any of them.
-----Examples-----
Input
2
1 2
Output
2 1
Input
4
1000 100 10 1
Output
100 1 1000 10
-----Note-----
An array x is a permutation of y, if we can shuffle elements of y such that it will coincide with x.
Note that the empty subset and the subset containing all indices are not counted.
|
import sys
input = sys.stdin.readline
n = int(input())
a = list(map(int, input().split()))
b = sorted(a)
ans = []
for x in a:
ans.append(b[b.index(x) - 1])
print(*ans)
|
IMPORT ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR LIST FOR VAR VAR EXPR FUNC_CALL VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR VAR
|
You are given an array a with n distinct integers. Construct an array b by permuting a such that for every non-empty subset of indices S = {x_1, x_2, ..., x_{k}} (1 ≤ x_{i} ≤ n, 0 < k < n) the sums of elements on that positions in a and b are different, i. e. $\sum_{i = 1}^{k} a_{x_{i}} \neq \sum_{i = 1}^{k} b_{x_{i}}$
-----Input-----
The first line contains one integer n (1 ≤ n ≤ 22) — the size of the array.
The second line contains n space-separated distinct integers a_1, a_2, ..., a_{n} (0 ≤ a_{i} ≤ 10^9) — the elements of the array.
-----Output-----
If there is no such array b, print -1.
Otherwise in the only line print n space-separated integers b_1, b_2, ..., b_{n}. Note that b must be a permutation of a.
If there are multiple answers, print any of them.
-----Examples-----
Input
2
1 2
Output
2 1
Input
4
1000 100 10 1
Output
100 1 1000 10
-----Note-----
An array x is a permutation of y, if we can shuffle elements of y such that it will coincide with x.
Note that the empty subset and the subset containing all indices are not counted.
|
n = int(input())
a = list(map(int, input().split()))
ans = sorted(a) + [min(a)]
print(" ".join([str(ans[ans.index(a[i]) + 1]) for i in range(n)]))
|
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR LIST FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL STRING FUNC_CALL VAR VAR BIN_OP FUNC_CALL VAR VAR VAR NUMBER VAR FUNC_CALL VAR VAR
|
You are given an array a with n distinct integers. Construct an array b by permuting a such that for every non-empty subset of indices S = {x_1, x_2, ..., x_{k}} (1 ≤ x_{i} ≤ n, 0 < k < n) the sums of elements on that positions in a and b are different, i. e. $\sum_{i = 1}^{k} a_{x_{i}} \neq \sum_{i = 1}^{k} b_{x_{i}}$
-----Input-----
The first line contains one integer n (1 ≤ n ≤ 22) — the size of the array.
The second line contains n space-separated distinct integers a_1, a_2, ..., a_{n} (0 ≤ a_{i} ≤ 10^9) — the elements of the array.
-----Output-----
If there is no such array b, print -1.
Otherwise in the only line print n space-separated integers b_1, b_2, ..., b_{n}. Note that b must be a permutation of a.
If there are multiple answers, print any of them.
-----Examples-----
Input
2
1 2
Output
2 1
Input
4
1000 100 10 1
Output
100 1 1000 10
-----Note-----
An array x is a permutation of y, if we can shuffle elements of y such that it will coincide with x.
Note that the empty subset and the subset containing all indices are not counted.
|
n = int(input())
a = list(map(int, input().split()))
ans = list()
for i in range(n):
ans.append(0)
position = dict()
for i in range(n):
position[a[i]] = i
a = sorted(a)
ans[position[a[n - 1]]] = a[0]
for i in range(n - 1):
ans[position[a[i]]] = a[i + 1]
for i in range(n):
print(ans[i], end=" ")
|
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR NUMBER ASSIGN VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR BIN_OP VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR VAR VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR STRING
|
You are given an array a with n distinct integers. Construct an array b by permuting a such that for every non-empty subset of indices S = {x_1, x_2, ..., x_{k}} (1 ≤ x_{i} ≤ n, 0 < k < n) the sums of elements on that positions in a and b are different, i. e. $\sum_{i = 1}^{k} a_{x_{i}} \neq \sum_{i = 1}^{k} b_{x_{i}}$
-----Input-----
The first line contains one integer n (1 ≤ n ≤ 22) — the size of the array.
The second line contains n space-separated distinct integers a_1, a_2, ..., a_{n} (0 ≤ a_{i} ≤ 10^9) — the elements of the array.
-----Output-----
If there is no such array b, print -1.
Otherwise in the only line print n space-separated integers b_1, b_2, ..., b_{n}. Note that b must be a permutation of a.
If there are multiple answers, print any of them.
-----Examples-----
Input
2
1 2
Output
2 1
Input
4
1000 100 10 1
Output
100 1 1000 10
-----Note-----
An array x is a permutation of y, if we can shuffle elements of y such that it will coincide with x.
Note that the empty subset and the subset containing all indices are not counted.
|
n = int(input())
A = list(map(int, input().split()))
C = []
for i, a in enumerate(A):
C.append((a, i))
C.sort()
B = [-1] * n
for i in range(n - 1):
B[C[i + 1][1]] = C[i][0]
B[C[0][1]] = C[-1][0]
print(*B)
|
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST FOR VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER NUMBER VAR VAR NUMBER ASSIGN VAR VAR NUMBER NUMBER VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR
|
You are given an array [A_{1}, \ldots, A_{N}]. You want to split it into several (≥ 2) [subarrays] so that the following condition holds:
Calculate the sum of elements in each subarray. Then, the AND of all these sums is nonzero, where AND denotes the bitwise AND operation.
Determine if it's possible. If it's possible, find one such split. You don't have to minimize the number of subarrays.
------ Input Format ------
- The first line of the input contains a single integer T - the number of test cases. The description of the test cases follows.
- The first line of each test case contains a single integer N - the length of the array.
- The second line of each test case contains N space-separated integers A_{1}, A_{2}, \ldots, A_{N}.
------ Output Format ------
For each test case, if it's not possible to split the array so that the condition from the statement holds, output NO.
Otherwise, output YES. In the next line, output K (2 ≤ K ≤ N) - the number of subarrays you are splitting into.
In the i-th of the next K lines, output two integers L_{i}, R_{i} (1 ≤ L_{i} ≤ R_{i} ≤ N), where [L_{i}, R_{i}] denotes the i-th subarray.
Each number from 1 to N has to appear in exactly one of the segments [L_{i}, R_{i}].
You can output each letter in either uppercase or lowercase. You can output any solution. You don't have to minimize K.
------ Constraints ------
$1 ≤ T ≤ 10^{5}$
$2 ≤ N ≤ 10^{5}$
$0 ≤ A_{i} < 2^{30}$
- Sum of $N$ over all test cases doesn't exceed $2\cdot 10^{5}$
----- Sample Input 1 ------
5
4
1 9 8 4
3
0 0 0
2
16 15
5
1 2 6 24 120
7
8 6 0 16 24 0 8
----- Sample Output 1 ------
YES
2
2 4
1 1
NO
NO
YES
2
1 2
3 5
YES
3
1 1
6 7
2 5
----- explanation 1 ------
Test case $1$: You can split the array into two subarrays $[2, 4]$ and $[1, 1]$. They have sums $21$ and $1$ correspondingly, whose AND is $1$.
Test case $4$: You can split the array into two subarrays $[1, 2]$ and $[3, 5]$. They have sums $3$ and $150$ correspondingly, whose AND is $2$.
Test case $5$: You can split the array into three subarrays $[1, 1]$, $[6, 7]$, $[2, 5]$. They have sums $8$, $8$, $46$, whose AND is $8$.
It's possible to show that it's impossible to split in the desired way in the second and the third test cases.
|
t = int(input())
while t > 0:
t -= 1
n = int(input())
ar = [int(x) for x in input().split()]
f = 0
b = [[(0) for x in range(31)] for x in range(n)]
for i in range(n):
x = ar[i]
j = 0
while x > 0:
d = x % 2
x //= 2
b[i][j] = d
j += 1
bit = 0
while bit < 31:
a = []
for i in range(n):
if b[i][bit] == 1:
a.append(i + 1)
bit += 1
if len(a) < 2:
continue
print("YES")
f = 1
print(len(a))
i = 0
print(1, a[i])
a[len(a) - 1] = n
while i < len(a) - 1:
print(a[i] + 1, a[i + 1])
i += 1
break
if f == 0:
print("NO")
|
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR WHILE VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR NUMBER VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR NUMBER WHILE VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR VAR VAR VAR VAR NUMBER ASSIGN VAR NUMBER WHILE VAR NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR NUMBER IF FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR STRING ASSIGN VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER EXPR FUNC_CALL VAR NUMBER VAR VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER VAR WHILE VAR BIN_OP FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR VAR NUMBER VAR BIN_OP VAR NUMBER VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR STRING
|
You are given an array [A_{1}, \ldots, A_{N}]. You want to split it into several (≥ 2) [subarrays] so that the following condition holds:
Calculate the sum of elements in each subarray. Then, the AND of all these sums is nonzero, where AND denotes the bitwise AND operation.
Determine if it's possible. If it's possible, find one such split. You don't have to minimize the number of subarrays.
------ Input Format ------
- The first line of the input contains a single integer T - the number of test cases. The description of the test cases follows.
- The first line of each test case contains a single integer N - the length of the array.
- The second line of each test case contains N space-separated integers A_{1}, A_{2}, \ldots, A_{N}.
------ Output Format ------
For each test case, if it's not possible to split the array so that the condition from the statement holds, output NO.
Otherwise, output YES. In the next line, output K (2 ≤ K ≤ N) - the number of subarrays you are splitting into.
In the i-th of the next K lines, output two integers L_{i}, R_{i} (1 ≤ L_{i} ≤ R_{i} ≤ N), where [L_{i}, R_{i}] denotes the i-th subarray.
Each number from 1 to N has to appear in exactly one of the segments [L_{i}, R_{i}].
You can output each letter in either uppercase or lowercase. You can output any solution. You don't have to minimize K.
------ Constraints ------
$1 ≤ T ≤ 10^{5}$
$2 ≤ N ≤ 10^{5}$
$0 ≤ A_{i} < 2^{30}$
- Sum of $N$ over all test cases doesn't exceed $2\cdot 10^{5}$
----- Sample Input 1 ------
5
4
1 9 8 4
3
0 0 0
2
16 15
5
1 2 6 24 120
7
8 6 0 16 24 0 8
----- Sample Output 1 ------
YES
2
2 4
1 1
NO
NO
YES
2
1 2
3 5
YES
3
1 1
6 7
2 5
----- explanation 1 ------
Test case $1$: You can split the array into two subarrays $[2, 4]$ and $[1, 1]$. They have sums $21$ and $1$ correspondingly, whose AND is $1$.
Test case $4$: You can split the array into two subarrays $[1, 2]$ and $[3, 5]$. They have sums $3$ and $150$ correspondingly, whose AND is $2$.
Test case $5$: You can split the array into three subarrays $[1, 1]$, $[6, 7]$, $[2, 5]$. They have sums $8$, $8$, $46$, whose AND is $8$.
It's possible to show that it's impossible to split in the desired way in the second and the third test cases.
|
for iota in range(int(input())):
n = int(input())
a = list(map(int, input().split()))
odd = []
while True:
if len(odd) >= 2:
break
if a == [0] * n:
break
odd = []
for i in range(n):
if a[i] % 2 == 1:
odd.append(i)
a[i] >>= 1
if len(odd) < 2:
print("NO")
else:
print("YES")
print(len(odd))
for i in range(len(odd)):
if i == 0:
print(1, odd[i] + 1)
continue
if i == len(odd) - 1:
print(odd[i - 1] + 2, n)
continue
print(odd[i - 1] + 2, odd[i] + 1)
|
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST WHILE NUMBER IF FUNC_CALL VAR VAR NUMBER IF VAR BIN_OP LIST NUMBER VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR IF BIN_OP VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR VAR VAR NUMBER IF FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER BIN_OP VAR VAR NUMBER IF VAR BIN_OP FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR BIN_OP VAR NUMBER NUMBER VAR EXPR FUNC_CALL VAR BIN_OP VAR BIN_OP VAR NUMBER NUMBER BIN_OP VAR VAR NUMBER
|
You are given an array [A_{1}, \ldots, A_{N}]. You want to split it into several (≥ 2) [subarrays] so that the following condition holds:
Calculate the sum of elements in each subarray. Then, the AND of all these sums is nonzero, where AND denotes the bitwise AND operation.
Determine if it's possible. If it's possible, find one such split. You don't have to minimize the number of subarrays.
------ Input Format ------
- The first line of the input contains a single integer T - the number of test cases. The description of the test cases follows.
- The first line of each test case contains a single integer N - the length of the array.
- The second line of each test case contains N space-separated integers A_{1}, A_{2}, \ldots, A_{N}.
------ Output Format ------
For each test case, if it's not possible to split the array so that the condition from the statement holds, output NO.
Otherwise, output YES. In the next line, output K (2 ≤ K ≤ N) - the number of subarrays you are splitting into.
In the i-th of the next K lines, output two integers L_{i}, R_{i} (1 ≤ L_{i} ≤ R_{i} ≤ N), where [L_{i}, R_{i}] denotes the i-th subarray.
Each number from 1 to N has to appear in exactly one of the segments [L_{i}, R_{i}].
You can output each letter in either uppercase or lowercase. You can output any solution. You don't have to minimize K.
------ Constraints ------
$1 ≤ T ≤ 10^{5}$
$2 ≤ N ≤ 10^{5}$
$0 ≤ A_{i} < 2^{30}$
- Sum of $N$ over all test cases doesn't exceed $2\cdot 10^{5}$
----- Sample Input 1 ------
5
4
1 9 8 4
3
0 0 0
2
16 15
5
1 2 6 24 120
7
8 6 0 16 24 0 8
----- Sample Output 1 ------
YES
2
2 4
1 1
NO
NO
YES
2
1 2
3 5
YES
3
1 1
6 7
2 5
----- explanation 1 ------
Test case $1$: You can split the array into two subarrays $[2, 4]$ and $[1, 1]$. They have sums $21$ and $1$ correspondingly, whose AND is $1$.
Test case $4$: You can split the array into two subarrays $[1, 2]$ and $[3, 5]$. They have sums $3$ and $150$ correspondingly, whose AND is $2$.
Test case $5$: You can split the array into three subarrays $[1, 1]$, $[6, 7]$, $[2, 5]$. They have sums $8$, $8$, $46$, whose AND is $8$.
It's possible to show that it's impossible to split in the desired way in the second and the third test cases.
|
for _ in range(int(input())):
n = int(input())
l = list(map(int, input().split()))
if n == 2:
if l[0] & l[1] > 0:
print("YES")
print(2)
print(1, 1)
print(2, 2)
continue
else:
print("NO")
continue
c0 = 0
c1 = 0
pos = []
a = l[0]
for j in range(n):
i = l[j]
if i == 0:
c0 += 1
elif i % 2 == 1:
c1 += 1
pos.append(j + 1)
a &= i
if c0 >= n - 1:
print("NO")
elif c1 >= 2:
print("YES")
print(c1)
print(1, pos[0])
for i in range(len(pos) - 2):
print(pos[i] + 1, pos[i + 1])
print(pos[-2] + 1, n)
elif a > 0:
print("YES")
print(n)
for i in range(0, n):
print(i + 1, i + 1)
else:
pos.clear()
pos = {}
for i in range(31):
pos[i] = []
for k in range(n):
i = l[k]
if i == 0:
continue
i = bin(i)
for j in range(1, len(i) - 1):
if i[-j] == "1":
pos[j - 1].append(k + 1)
for i in pos:
if len(pos[i]) > 1:
print("YES")
print(len(pos[i]))
pos = pos[i]
print(1, pos[0])
for i in range(len(pos) - 2):
print(pos[i] + 1, pos[i + 1])
print(pos[-2] + 1, n)
break
else:
print("NO")
|
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR IF VAR NUMBER IF BIN_OP VAR NUMBER VAR NUMBER NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR NUMBER NUMBER EXPR FUNC_CALL VAR NUMBER NUMBER EXPR FUNC_CALL VAR STRING ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR LIST ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR IF VAR NUMBER VAR NUMBER IF BIN_OP VAR NUMBER NUMBER VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR IF VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR STRING IF VAR NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR VAR NUMBER VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER VAR IF VAR NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR NUMBER VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER EXPR FUNC_CALL VAR ASSIGN VAR DICT FOR VAR FUNC_CALL VAR NUMBER ASSIGN VAR VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR IF VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER IF VAR VAR STRING EXPR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER FOR VAR VAR IF FUNC_CALL VAR VAR VAR NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR VAR EXPR FUNC_CALL VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR VAR NUMBER VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER VAR EXPR FUNC_CALL VAR STRING
|
You are given an array [A_{1}, \ldots, A_{N}]. You want to split it into several (≥ 2) [subarrays] so that the following condition holds:
Calculate the sum of elements in each subarray. Then, the AND of all these sums is nonzero, where AND denotes the bitwise AND operation.
Determine if it's possible. If it's possible, find one such split. You don't have to minimize the number of subarrays.
------ Input Format ------
- The first line of the input contains a single integer T - the number of test cases. The description of the test cases follows.
- The first line of each test case contains a single integer N - the length of the array.
- The second line of each test case contains N space-separated integers A_{1}, A_{2}, \ldots, A_{N}.
------ Output Format ------
For each test case, if it's not possible to split the array so that the condition from the statement holds, output NO.
Otherwise, output YES. In the next line, output K (2 ≤ K ≤ N) - the number of subarrays you are splitting into.
In the i-th of the next K lines, output two integers L_{i}, R_{i} (1 ≤ L_{i} ≤ R_{i} ≤ N), where [L_{i}, R_{i}] denotes the i-th subarray.
Each number from 1 to N has to appear in exactly one of the segments [L_{i}, R_{i}].
You can output each letter in either uppercase or lowercase. You can output any solution. You don't have to minimize K.
------ Constraints ------
$1 ≤ T ≤ 10^{5}$
$2 ≤ N ≤ 10^{5}$
$0 ≤ A_{i} < 2^{30}$
- Sum of $N$ over all test cases doesn't exceed $2\cdot 10^{5}$
----- Sample Input 1 ------
5
4
1 9 8 4
3
0 0 0
2
16 15
5
1 2 6 24 120
7
8 6 0 16 24 0 8
----- Sample Output 1 ------
YES
2
2 4
1 1
NO
NO
YES
2
1 2
3 5
YES
3
1 1
6 7
2 5
----- explanation 1 ------
Test case $1$: You can split the array into two subarrays $[2, 4]$ and $[1, 1]$. They have sums $21$ and $1$ correspondingly, whose AND is $1$.
Test case $4$: You can split the array into two subarrays $[1, 2]$ and $[3, 5]$. They have sums $3$ and $150$ correspondingly, whose AND is $2$.
Test case $5$: You can split the array into three subarrays $[1, 1]$, $[6, 7]$, $[2, 5]$. They have sums $8$, $8$, $46$, whose AND is $8$.
It's possible to show that it's impossible to split in the desired way in the second and the third test cases.
|
for i in range(int(input())):
n = int(input())
a = list(map(int, input().split()))
bit = [0] * 32
ok = False
for i in a:
for j in range(32):
if i & 1 << j > 0:
bit[j] += 1
if bit[j] > 1:
ok = True
if ok:
print("YES")
ans = -1
for i in range(32):
if bit[i] > 1:
print(bit[i])
ans = i
break
s = 0
st = 1
c = 1
for i in range(n):
s += a[i]
if s & 1 << ans > 0:
if c != bit[ans]:
print(st, i + 1)
st = i + 2
s = 0
else:
print(st, n)
break
c += 1
else:
print("NO")
|
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER NUMBER ASSIGN VAR NUMBER FOR VAR VAR FOR VAR FUNC_CALL VAR NUMBER IF BIN_OP VAR BIN_OP NUMBER VAR NUMBER VAR VAR NUMBER IF VAR VAR NUMBER ASSIGN VAR NUMBER IF VAR EXPR FUNC_CALL VAR STRING ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER IF VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR VAR VAR IF BIN_OP VAR BIN_OP NUMBER VAR NUMBER IF VAR VAR VAR EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR STRING
|
You are given an array [A_{1}, \ldots, A_{N}]. You want to split it into several (≥ 2) [subarrays] so that the following condition holds:
Calculate the sum of elements in each subarray. Then, the AND of all these sums is nonzero, where AND denotes the bitwise AND operation.
Determine if it's possible. If it's possible, find one such split. You don't have to minimize the number of subarrays.
------ Input Format ------
- The first line of the input contains a single integer T - the number of test cases. The description of the test cases follows.
- The first line of each test case contains a single integer N - the length of the array.
- The second line of each test case contains N space-separated integers A_{1}, A_{2}, \ldots, A_{N}.
------ Output Format ------
For each test case, if it's not possible to split the array so that the condition from the statement holds, output NO.
Otherwise, output YES. In the next line, output K (2 ≤ K ≤ N) - the number of subarrays you are splitting into.
In the i-th of the next K lines, output two integers L_{i}, R_{i} (1 ≤ L_{i} ≤ R_{i} ≤ N), where [L_{i}, R_{i}] denotes the i-th subarray.
Each number from 1 to N has to appear in exactly one of the segments [L_{i}, R_{i}].
You can output each letter in either uppercase or lowercase. You can output any solution. You don't have to minimize K.
------ Constraints ------
$1 ≤ T ≤ 10^{5}$
$2 ≤ N ≤ 10^{5}$
$0 ≤ A_{i} < 2^{30}$
- Sum of $N$ over all test cases doesn't exceed $2\cdot 10^{5}$
----- Sample Input 1 ------
5
4
1 9 8 4
3
0 0 0
2
16 15
5
1 2 6 24 120
7
8 6 0 16 24 0 8
----- Sample Output 1 ------
YES
2
2 4
1 1
NO
NO
YES
2
1 2
3 5
YES
3
1 1
6 7
2 5
----- explanation 1 ------
Test case $1$: You can split the array into two subarrays $[2, 4]$ and $[1, 1]$. They have sums $21$ and $1$ correspondingly, whose AND is $1$.
Test case $4$: You can split the array into two subarrays $[1, 2]$ and $[3, 5]$. They have sums $3$ and $150$ correspondingly, whose AND is $2$.
Test case $5$: You can split the array into three subarrays $[1, 1]$, $[6, 7]$, $[2, 5]$. They have sums $8$, $8$, $46$, whose AND is $8$.
It's possible to show that it's impossible to split in the desired way in the second and the third test cases.
|
test = int(input())
while test:
n = int(input())
a = [bin(int(x)) for x in input().split()]
ans = []
for i in range(-1, -31, -1):
for j in range(n):
if len(a[j]) - 2 >= -i and a[j][i] == "1":
ans.append(j + 1)
if len(ans) > 1:
break
else:
ans.clear()
if ans:
print("YES")
print(len(ans))
ans.insert(0, 0)
ans[-1] = n
for i in range(1, len(ans)):
print(ans[i - 1] + 1, ans[i])
else:
print("NO")
test -= 1
|
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR WHILE VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR NUMBER NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR IF BIN_OP FUNC_CALL VAR VAR VAR NUMBER VAR VAR VAR VAR STRING EXPR FUNC_CALL VAR BIN_OP VAR NUMBER IF FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR IF VAR EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR NUMBER NUMBER ASSIGN VAR NUMBER VAR FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR BIN_OP VAR NUMBER NUMBER VAR VAR EXPR FUNC_CALL VAR STRING VAR NUMBER
|
You are given an array [A_{1}, \ldots, A_{N}]. You want to split it into several (≥ 2) [subarrays] so that the following condition holds:
Calculate the sum of elements in each subarray. Then, the AND of all these sums is nonzero, where AND denotes the bitwise AND operation.
Determine if it's possible. If it's possible, find one such split. You don't have to minimize the number of subarrays.
------ Input Format ------
- The first line of the input contains a single integer T - the number of test cases. The description of the test cases follows.
- The first line of each test case contains a single integer N - the length of the array.
- The second line of each test case contains N space-separated integers A_{1}, A_{2}, \ldots, A_{N}.
------ Output Format ------
For each test case, if it's not possible to split the array so that the condition from the statement holds, output NO.
Otherwise, output YES. In the next line, output K (2 ≤ K ≤ N) - the number of subarrays you are splitting into.
In the i-th of the next K lines, output two integers L_{i}, R_{i} (1 ≤ L_{i} ≤ R_{i} ≤ N), where [L_{i}, R_{i}] denotes the i-th subarray.
Each number from 1 to N has to appear in exactly one of the segments [L_{i}, R_{i}].
You can output each letter in either uppercase or lowercase. You can output any solution. You don't have to minimize K.
------ Constraints ------
$1 ≤ T ≤ 10^{5}$
$2 ≤ N ≤ 10^{5}$
$0 ≤ A_{i} < 2^{30}$
- Sum of $N$ over all test cases doesn't exceed $2\cdot 10^{5}$
----- Sample Input 1 ------
5
4
1 9 8 4
3
0 0 0
2
16 15
5
1 2 6 24 120
7
8 6 0 16 24 0 8
----- Sample Output 1 ------
YES
2
2 4
1 1
NO
NO
YES
2
1 2
3 5
YES
3
1 1
6 7
2 5
----- explanation 1 ------
Test case $1$: You can split the array into two subarrays $[2, 4]$ and $[1, 1]$. They have sums $21$ and $1$ correspondingly, whose AND is $1$.
Test case $4$: You can split the array into two subarrays $[1, 2]$ and $[3, 5]$. They have sums $3$ and $150$ correspondingly, whose AND is $2$.
Test case $5$: You can split the array into three subarrays $[1, 1]$, $[6, 7]$, $[2, 5]$. They have sums $8$, $8$, $46$, whose AND is $8$.
It's possible to show that it's impossible to split in the desired way in the second and the third test cases.
|
import sys
input = lambda: sys.stdin.readline()
T = int(input())
for _ in range(T):
n = int(input())
a = list(map(int, input().split()))
L = [(0) for i in range(32)]
for i in a:
for j in range(32):
if i & 1 << j:
L[j] += 1
f = False
for i in range(32):
if L[i] > 1:
f = True
print("YES")
t = L[i]
print(L[i])
break
if not f:
print("NO")
continue
j = 1
for value in range(n):
if a[value] & 1 << i:
if t > 1:
print(j, value + 1)
t -= 1
else:
print(j, n)
j = value + 2
|
IMPORT ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR NUMBER FOR VAR VAR FOR VAR FUNC_CALL VAR NUMBER IF BIN_OP VAR BIN_OP NUMBER VAR VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER IF VAR VAR NUMBER ASSIGN VAR NUMBER EXPR FUNC_CALL VAR STRING ASSIGN VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR IF VAR EXPR FUNC_CALL VAR STRING ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF BIN_OP VAR VAR BIN_OP NUMBER VAR IF VAR NUMBER EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER
|
You are given an array [A_{1}, \ldots, A_{N}]. You want to split it into several (≥ 2) [subarrays] so that the following condition holds:
Calculate the sum of elements in each subarray. Then, the AND of all these sums is nonzero, where AND denotes the bitwise AND operation.
Determine if it's possible. If it's possible, find one such split. You don't have to minimize the number of subarrays.
------ Input Format ------
- The first line of the input contains a single integer T - the number of test cases. The description of the test cases follows.
- The first line of each test case contains a single integer N - the length of the array.
- The second line of each test case contains N space-separated integers A_{1}, A_{2}, \ldots, A_{N}.
------ Output Format ------
For each test case, if it's not possible to split the array so that the condition from the statement holds, output NO.
Otherwise, output YES. In the next line, output K (2 ≤ K ≤ N) - the number of subarrays you are splitting into.
In the i-th of the next K lines, output two integers L_{i}, R_{i} (1 ≤ L_{i} ≤ R_{i} ≤ N), where [L_{i}, R_{i}] denotes the i-th subarray.
Each number from 1 to N has to appear in exactly one of the segments [L_{i}, R_{i}].
You can output each letter in either uppercase or lowercase. You can output any solution. You don't have to minimize K.
------ Constraints ------
$1 ≤ T ≤ 10^{5}$
$2 ≤ N ≤ 10^{5}$
$0 ≤ A_{i} < 2^{30}$
- Sum of $N$ over all test cases doesn't exceed $2\cdot 10^{5}$
----- Sample Input 1 ------
5
4
1 9 8 4
3
0 0 0
2
16 15
5
1 2 6 24 120
7
8 6 0 16 24 0 8
----- Sample Output 1 ------
YES
2
2 4
1 1
NO
NO
YES
2
1 2
3 5
YES
3
1 1
6 7
2 5
----- explanation 1 ------
Test case $1$: You can split the array into two subarrays $[2, 4]$ and $[1, 1]$. They have sums $21$ and $1$ correspondingly, whose AND is $1$.
Test case $4$: You can split the array into two subarrays $[1, 2]$ and $[3, 5]$. They have sums $3$ and $150$ correspondingly, whose AND is $2$.
Test case $5$: You can split the array into three subarrays $[1, 1]$, $[6, 7]$, $[2, 5]$. They have sums $8$, $8$, $46$, whose AND is $8$.
It's possible to show that it's impossible to split in the desired way in the second and the third test cases.
|
def fun(n, a):
if a == [0] * n:
print("NO")
return
l = []
for i in range(n):
if a[i] % 2 == 1:
l.append(i)
if len(l) > 1:
print("YES")
print(len(l))
x = 1
for i in range(len(l) - 1):
print(x, l[i] + 1)
x = l[i] + 2
print(x, n)
return
L = [(a[i] // 2) for i in range(n)]
return fun(n, L)
for _ in range(int(input())):
n = int(input())
a = list(map(int, input().split()))
fun(n, a)
|
FUNC_DEF IF VAR BIN_OP LIST NUMBER VAR EXPR FUNC_CALL VAR STRING RETURN ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR IF BIN_OP VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR VAR BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR RETURN ASSIGN VAR BIN_OP VAR VAR NUMBER VAR FUNC_CALL VAR VAR RETURN FUNC_CALL VAR VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR VAR VAR
|
You are given an array [A_{1}, \ldots, A_{N}]. You want to split it into several (≥ 2) [subarrays] so that the following condition holds:
Calculate the sum of elements in each subarray. Then, the AND of all these sums is nonzero, where AND denotes the bitwise AND operation.
Determine if it's possible. If it's possible, find one such split. You don't have to minimize the number of subarrays.
------ Input Format ------
- The first line of the input contains a single integer T - the number of test cases. The description of the test cases follows.
- The first line of each test case contains a single integer N - the length of the array.
- The second line of each test case contains N space-separated integers A_{1}, A_{2}, \ldots, A_{N}.
------ Output Format ------
For each test case, if it's not possible to split the array so that the condition from the statement holds, output NO.
Otherwise, output YES. In the next line, output K (2 ≤ K ≤ N) - the number of subarrays you are splitting into.
In the i-th of the next K lines, output two integers L_{i}, R_{i} (1 ≤ L_{i} ≤ R_{i} ≤ N), where [L_{i}, R_{i}] denotes the i-th subarray.
Each number from 1 to N has to appear in exactly one of the segments [L_{i}, R_{i}].
You can output each letter in either uppercase or lowercase. You can output any solution. You don't have to minimize K.
------ Constraints ------
$1 ≤ T ≤ 10^{5}$
$2 ≤ N ≤ 10^{5}$
$0 ≤ A_{i} < 2^{30}$
- Sum of $N$ over all test cases doesn't exceed $2\cdot 10^{5}$
----- Sample Input 1 ------
5
4
1 9 8 4
3
0 0 0
2
16 15
5
1 2 6 24 120
7
8 6 0 16 24 0 8
----- Sample Output 1 ------
YES
2
2 4
1 1
NO
NO
YES
2
1 2
3 5
YES
3
1 1
6 7
2 5
----- explanation 1 ------
Test case $1$: You can split the array into two subarrays $[2, 4]$ and $[1, 1]$. They have sums $21$ and $1$ correspondingly, whose AND is $1$.
Test case $4$: You can split the array into two subarrays $[1, 2]$ and $[3, 5]$. They have sums $3$ and $150$ correspondingly, whose AND is $2$.
Test case $5$: You can split the array into three subarrays $[1, 1]$, $[6, 7]$, $[2, 5]$. They have sums $8$, $8$, $46$, whose AND is $8$.
It's possible to show that it's impossible to split in the desired way in the second and the third test cases.
|
for _ in range(int(input())):
n = int(input())
arr = list(map(int, input().split()))
output_arr = None
for i in range(n):
z = format(arr[i], "032b")
arr[i] = z[::-1]
res = False
for j in range(32):
result = []
for i in range(len(arr)):
if arr[i][j] == "1":
result.append(i)
if len(result) > 1:
output_arr = result.copy()
res = True
break
if res == False:
print("NO")
else:
print("YES")
if len(output_arr) % 2 == 0:
print(2)
print(1, output_arr[0] + 1)
print(output_arr[0] + 2, n)
else:
print(3)
print(1, output_arr[0] + 1)
print(output_arr[0] + 2, output_arr[1] + 1)
print(output_arr[1] + 2, n)
|
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NONE FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR STRING ASSIGN VAR VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR VAR STRING EXPR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING IF BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER VAR EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER VAR
|
You are given an array [A_{1}, \ldots, A_{N}]. You want to split it into several (≥ 2) [subarrays] so that the following condition holds:
Calculate the sum of elements in each subarray. Then, the AND of all these sums is nonzero, where AND denotes the bitwise AND operation.
Determine if it's possible. If it's possible, find one such split. You don't have to minimize the number of subarrays.
------ Input Format ------
- The first line of the input contains a single integer T - the number of test cases. The description of the test cases follows.
- The first line of each test case contains a single integer N - the length of the array.
- The second line of each test case contains N space-separated integers A_{1}, A_{2}, \ldots, A_{N}.
------ Output Format ------
For each test case, if it's not possible to split the array so that the condition from the statement holds, output NO.
Otherwise, output YES. In the next line, output K (2 ≤ K ≤ N) - the number of subarrays you are splitting into.
In the i-th of the next K lines, output two integers L_{i}, R_{i} (1 ≤ L_{i} ≤ R_{i} ≤ N), where [L_{i}, R_{i}] denotes the i-th subarray.
Each number from 1 to N has to appear in exactly one of the segments [L_{i}, R_{i}].
You can output each letter in either uppercase or lowercase. You can output any solution. You don't have to minimize K.
------ Constraints ------
$1 ≤ T ≤ 10^{5}$
$2 ≤ N ≤ 10^{5}$
$0 ≤ A_{i} < 2^{30}$
- Sum of $N$ over all test cases doesn't exceed $2\cdot 10^{5}$
----- Sample Input 1 ------
5
4
1 9 8 4
3
0 0 0
2
16 15
5
1 2 6 24 120
7
8 6 0 16 24 0 8
----- Sample Output 1 ------
YES
2
2 4
1 1
NO
NO
YES
2
1 2
3 5
YES
3
1 1
6 7
2 5
----- explanation 1 ------
Test case $1$: You can split the array into two subarrays $[2, 4]$ and $[1, 1]$. They have sums $21$ and $1$ correspondingly, whose AND is $1$.
Test case $4$: You can split the array into two subarrays $[1, 2]$ and $[3, 5]$. They have sums $3$ and $150$ correspondingly, whose AND is $2$.
Test case $5$: You can split the array into three subarrays $[1, 1]$, $[6, 7]$, $[2, 5]$. They have sums $8$, $8$, $46$, whose AND is $8$.
It's possible to show that it's impossible to split in the desired way in the second and the third test cases.
|
for _ in range(int(input())):
try:
n = int(input())
l = list(map(int, input().strip().split()))
ind, c = -1, 0
for i in range(32):
c1 = 0
for j in l:
if j & 1 << i:
c1 += 1
if c1 > 1:
c = c1
ind = i
break
if ind == -1:
print("NO")
continue
print("YES")
print(c)
x = 1
c -= 1
for i in range(n):
if c == 0:
break
if l[i] & 1 << ind:
print(x, end=" ")
print(i + 1)
x = i + 2
c -= 1
print(x, end=" ")
print(n)
finally:
pass
|
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR IF BIN_OP VAR BIN_OP NUMBER VAR VAR NUMBER IF VAR NUMBER ASSIGN VAR VAR ASSIGN VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR NUMBER IF BIN_OP VAR VAR BIN_OP NUMBER VAR EXPR FUNC_CALL VAR VAR STRING EXPR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR STRING EXPR FUNC_CALL VAR VAR
|
You are given an array [A_{1}, \ldots, A_{N}]. You want to split it into several (≥ 2) [subarrays] so that the following condition holds:
Calculate the sum of elements in each subarray. Then, the AND of all these sums is nonzero, where AND denotes the bitwise AND operation.
Determine if it's possible. If it's possible, find one such split. You don't have to minimize the number of subarrays.
------ Input Format ------
- The first line of the input contains a single integer T - the number of test cases. The description of the test cases follows.
- The first line of each test case contains a single integer N - the length of the array.
- The second line of each test case contains N space-separated integers A_{1}, A_{2}, \ldots, A_{N}.
------ Output Format ------
For each test case, if it's not possible to split the array so that the condition from the statement holds, output NO.
Otherwise, output YES. In the next line, output K (2 ≤ K ≤ N) - the number of subarrays you are splitting into.
In the i-th of the next K lines, output two integers L_{i}, R_{i} (1 ≤ L_{i} ≤ R_{i} ≤ N), where [L_{i}, R_{i}] denotes the i-th subarray.
Each number from 1 to N has to appear in exactly one of the segments [L_{i}, R_{i}].
You can output each letter in either uppercase or lowercase. You can output any solution. You don't have to minimize K.
------ Constraints ------
$1 ≤ T ≤ 10^{5}$
$2 ≤ N ≤ 10^{5}$
$0 ≤ A_{i} < 2^{30}$
- Sum of $N$ over all test cases doesn't exceed $2\cdot 10^{5}$
----- Sample Input 1 ------
5
4
1 9 8 4
3
0 0 0
2
16 15
5
1 2 6 24 120
7
8 6 0 16 24 0 8
----- Sample Output 1 ------
YES
2
2 4
1 1
NO
NO
YES
2
1 2
3 5
YES
3
1 1
6 7
2 5
----- explanation 1 ------
Test case $1$: You can split the array into two subarrays $[2, 4]$ and $[1, 1]$. They have sums $21$ and $1$ correspondingly, whose AND is $1$.
Test case $4$: You can split the array into two subarrays $[1, 2]$ and $[3, 5]$. They have sums $3$ and $150$ correspondingly, whose AND is $2$.
Test case $5$: You can split the array into three subarrays $[1, 1]$, $[6, 7]$, $[2, 5]$. They have sums $8$, $8$, $46$, whose AND is $8$.
It's possible to show that it's impossible to split in the desired way in the second and the third test cases.
|
for _ in range(int(input())):
n = int(input())
a = list(map(int, input().split()))
i = 1
flag = True
while i < 1000000000.0:
t = 0
for j in range(n):
if a[j] & i == i:
t += 1
if t >= 2:
flag = False
print("YES")
print(t)
f = 1
s = -1
for j in range(n):
if a[j] & i == i:
s = j + 1
print(f, s)
t -= 1
f = j + 2
if t == 1:
break
print(f, n)
break
i *= 2
if flag:
print("NO")
|
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF BIN_OP VAR VAR VAR VAR VAR NUMBER IF VAR NUMBER ASSIGN VAR NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF BIN_OP VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR NUMBER IF VAR EXPR FUNC_CALL VAR STRING
|
You are given an array [A_{1}, \ldots, A_{N}]. You want to split it into several (≥ 2) [subarrays] so that the following condition holds:
Calculate the sum of elements in each subarray. Then, the AND of all these sums is nonzero, where AND denotes the bitwise AND operation.
Determine if it's possible. If it's possible, find one such split. You don't have to minimize the number of subarrays.
------ Input Format ------
- The first line of the input contains a single integer T - the number of test cases. The description of the test cases follows.
- The first line of each test case contains a single integer N - the length of the array.
- The second line of each test case contains N space-separated integers A_{1}, A_{2}, \ldots, A_{N}.
------ Output Format ------
For each test case, if it's not possible to split the array so that the condition from the statement holds, output NO.
Otherwise, output YES. In the next line, output K (2 ≤ K ≤ N) - the number of subarrays you are splitting into.
In the i-th of the next K lines, output two integers L_{i}, R_{i} (1 ≤ L_{i} ≤ R_{i} ≤ N), where [L_{i}, R_{i}] denotes the i-th subarray.
Each number from 1 to N has to appear in exactly one of the segments [L_{i}, R_{i}].
You can output each letter in either uppercase or lowercase. You can output any solution. You don't have to minimize K.
------ Constraints ------
$1 ≤ T ≤ 10^{5}$
$2 ≤ N ≤ 10^{5}$
$0 ≤ A_{i} < 2^{30}$
- Sum of $N$ over all test cases doesn't exceed $2\cdot 10^{5}$
----- Sample Input 1 ------
5
4
1 9 8 4
3
0 0 0
2
16 15
5
1 2 6 24 120
7
8 6 0 16 24 0 8
----- Sample Output 1 ------
YES
2
2 4
1 1
NO
NO
YES
2
1 2
3 5
YES
3
1 1
6 7
2 5
----- explanation 1 ------
Test case $1$: You can split the array into two subarrays $[2, 4]$ and $[1, 1]$. They have sums $21$ and $1$ correspondingly, whose AND is $1$.
Test case $4$: You can split the array into two subarrays $[1, 2]$ and $[3, 5]$. They have sums $3$ and $150$ correspondingly, whose AND is $2$.
Test case $5$: You can split the array into three subarrays $[1, 1]$, $[6, 7]$, $[2, 5]$. They have sums $8$, $8$, $46$, whose AND is $8$.
It's possible to show that it's impossible to split in the desired way in the second and the third test cases.
|
import sys
sys.setrecursionlimit(1000000)
def mi():
return map(int, input().split())
def li():
return list(mi())
def si():
return str(input())
def ni():
return int(input())
def printyes():
print("YES")
def printno():
print("NO")
def find(a, j):
count = 0
loc = [0]
for i in range(len(a)):
if a[i] & 1 << j:
count += 1
loc.append(i + 1)
if count < 2:
return False
ans = []
for i in range(1, len(loc) - 1):
ans.append((loc[i - 1] + 1, loc[i]))
ans.append((loc[-2] + 1, len(a)))
return ans
for t in range(int(input())):
n = ni()
a = li()
final = "NO"
ans = []
for j in range(32):
temp = find(a, j)
if temp:
final = "YES"
ans = temp
break
print(final)
if ans:
print(len(ans))
for i in ans:
print(*i)
|
IMPORT EXPR FUNC_CALL VAR NUMBER FUNC_DEF RETURN FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FUNC_DEF RETURN FUNC_CALL VAR FUNC_CALL VAR FUNC_DEF RETURN FUNC_CALL VAR FUNC_CALL VAR FUNC_DEF RETURN FUNC_CALL VAR FUNC_CALL VAR FUNC_DEF EXPR FUNC_CALL VAR STRING FUNC_DEF EXPR FUNC_CALL VAR STRING FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR LIST NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF BIN_OP VAR VAR BIN_OP NUMBER VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER IF VAR NUMBER RETURN NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR BIN_OP VAR NUMBER NUMBER VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER FUNC_CALL VAR VAR RETURN VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR STRING ASSIGN VAR LIST FOR VAR FUNC_CALL VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR IF VAR ASSIGN VAR STRING ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR IF VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR FOR VAR VAR EXPR FUNC_CALL VAR VAR
|
You are given an array [A_{1}, \ldots, A_{N}]. You want to split it into several (≥ 2) [subarrays] so that the following condition holds:
Calculate the sum of elements in each subarray. Then, the AND of all these sums is nonzero, where AND denotes the bitwise AND operation.
Determine if it's possible. If it's possible, find one such split. You don't have to minimize the number of subarrays.
------ Input Format ------
- The first line of the input contains a single integer T - the number of test cases. The description of the test cases follows.
- The first line of each test case contains a single integer N - the length of the array.
- The second line of each test case contains N space-separated integers A_{1}, A_{2}, \ldots, A_{N}.
------ Output Format ------
For each test case, if it's not possible to split the array so that the condition from the statement holds, output NO.
Otherwise, output YES. In the next line, output K (2 ≤ K ≤ N) - the number of subarrays you are splitting into.
In the i-th of the next K lines, output two integers L_{i}, R_{i} (1 ≤ L_{i} ≤ R_{i} ≤ N), where [L_{i}, R_{i}] denotes the i-th subarray.
Each number from 1 to N has to appear in exactly one of the segments [L_{i}, R_{i}].
You can output each letter in either uppercase or lowercase. You can output any solution. You don't have to minimize K.
------ Constraints ------
$1 ≤ T ≤ 10^{5}$
$2 ≤ N ≤ 10^{5}$
$0 ≤ A_{i} < 2^{30}$
- Sum of $N$ over all test cases doesn't exceed $2\cdot 10^{5}$
----- Sample Input 1 ------
5
4
1 9 8 4
3
0 0 0
2
16 15
5
1 2 6 24 120
7
8 6 0 16 24 0 8
----- Sample Output 1 ------
YES
2
2 4
1 1
NO
NO
YES
2
1 2
3 5
YES
3
1 1
6 7
2 5
----- explanation 1 ------
Test case $1$: You can split the array into two subarrays $[2, 4]$ and $[1, 1]$. They have sums $21$ and $1$ correspondingly, whose AND is $1$.
Test case $4$: You can split the array into two subarrays $[1, 2]$ and $[3, 5]$. They have sums $3$ and $150$ correspondingly, whose AND is $2$.
Test case $5$: You can split the array into three subarrays $[1, 1]$, $[6, 7]$, $[2, 5]$. They have sums $8$, $8$, $46$, whose AND is $8$.
It's possible to show that it's impossible to split in the desired way in the second and the third test cases.
|
for s in [*open(0)][2::2]:
l = [*map(int, s.split())]
f = 1
for i in range(30):
b = 1 << i
ind = []
for j in range(len(l)):
if l[j] & b:
ind += [j]
if len(ind) > 1:
print("YES")
print(len(ind))
ind = sorted(ind)
p = 0
for x in ind[:-1]:
print(p + 1, x + 1)
p = x + 1
print(p + 1, len(l))
f = 0
break
if f == 1:
print("NO")
|
FOR VAR LIST FUNC_CALL VAR NUMBER NUMBER NUMBER ASSIGN VAR LIST FUNC_CALL VAR VAR FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER ASSIGN VAR BIN_OP NUMBER VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF BIN_OP VAR VAR VAR VAR LIST VAR IF FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR ASSIGN VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR STRING
|
You are given an array [A_{1}, \ldots, A_{N}]. You want to split it into several (≥ 2) [subarrays] so that the following condition holds:
Calculate the sum of elements in each subarray. Then, the AND of all these sums is nonzero, where AND denotes the bitwise AND operation.
Determine if it's possible. If it's possible, find one such split. You don't have to minimize the number of subarrays.
------ Input Format ------
- The first line of the input contains a single integer T - the number of test cases. The description of the test cases follows.
- The first line of each test case contains a single integer N - the length of the array.
- The second line of each test case contains N space-separated integers A_{1}, A_{2}, \ldots, A_{N}.
------ Output Format ------
For each test case, if it's not possible to split the array so that the condition from the statement holds, output NO.
Otherwise, output YES. In the next line, output K (2 ≤ K ≤ N) - the number of subarrays you are splitting into.
In the i-th of the next K lines, output two integers L_{i}, R_{i} (1 ≤ L_{i} ≤ R_{i} ≤ N), where [L_{i}, R_{i}] denotes the i-th subarray.
Each number from 1 to N has to appear in exactly one of the segments [L_{i}, R_{i}].
You can output each letter in either uppercase or lowercase. You can output any solution. You don't have to minimize K.
------ Constraints ------
$1 ≤ T ≤ 10^{5}$
$2 ≤ N ≤ 10^{5}$
$0 ≤ A_{i} < 2^{30}$
- Sum of $N$ over all test cases doesn't exceed $2\cdot 10^{5}$
----- Sample Input 1 ------
5
4
1 9 8 4
3
0 0 0
2
16 15
5
1 2 6 24 120
7
8 6 0 16 24 0 8
----- Sample Output 1 ------
YES
2
2 4
1 1
NO
NO
YES
2
1 2
3 5
YES
3
1 1
6 7
2 5
----- explanation 1 ------
Test case $1$: You can split the array into two subarrays $[2, 4]$ and $[1, 1]$. They have sums $21$ and $1$ correspondingly, whose AND is $1$.
Test case $4$: You can split the array into two subarrays $[1, 2]$ and $[3, 5]$. They have sums $3$ and $150$ correspondingly, whose AND is $2$.
Test case $5$: You can split the array into three subarrays $[1, 1]$, $[6, 7]$, $[2, 5]$. They have sums $8$, $8$, $46$, whose AND is $8$.
It's possible to show that it's impossible to split in the desired way in the second and the third test cases.
|
resultAns = ""
for _ in range(int(input())):
number = int(input())
arrayList = list(map(int, input().split()))
inclusive = -1
countNumber = 0
for i in range(32):
counter = 0
for j in range(number):
if arrayList[j] & 1 << i != 0:
counter += 1
if counter > 1:
inclusive = i
countNumber = counter
break
if inclusive == -1:
resultAns += "NO\n"
else:
resultAns += "YES\n"
resultAns += str(countNumber) + "\n"
lastDigit = 1
for i in range(number):
if arrayList[i] & 1 << inclusive != 0 and countNumber > 1:
resultAns += str(lastDigit) + " " + str(i + 1) + "\n"
lastDigit = i + 2
countNumber -= 1
resultAns += str(lastDigit) + " " + str(number) + "\n"
print(resultAns)
|
ASSIGN VAR STRING FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF BIN_OP VAR VAR BIN_OP NUMBER VAR NUMBER VAR NUMBER IF VAR NUMBER ASSIGN VAR VAR ASSIGN VAR VAR IF VAR NUMBER VAR STRING VAR STRING VAR BIN_OP FUNC_CALL VAR VAR STRING ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF BIN_OP VAR VAR BIN_OP NUMBER VAR NUMBER VAR NUMBER VAR BIN_OP BIN_OP BIN_OP FUNC_CALL VAR VAR STRING FUNC_CALL VAR BIN_OP VAR NUMBER STRING ASSIGN VAR BIN_OP VAR NUMBER VAR NUMBER VAR BIN_OP BIN_OP BIN_OP FUNC_CALL VAR VAR STRING FUNC_CALL VAR VAR STRING EXPR FUNC_CALL VAR VAR
|
You are given an array [A_{1}, \ldots, A_{N}]. You want to split it into several (≥ 2) [subarrays] so that the following condition holds:
Calculate the sum of elements in each subarray. Then, the AND of all these sums is nonzero, where AND denotes the bitwise AND operation.
Determine if it's possible. If it's possible, find one such split. You don't have to minimize the number of subarrays.
------ Input Format ------
- The first line of the input contains a single integer T - the number of test cases. The description of the test cases follows.
- The first line of each test case contains a single integer N - the length of the array.
- The second line of each test case contains N space-separated integers A_{1}, A_{2}, \ldots, A_{N}.
------ Output Format ------
For each test case, if it's not possible to split the array so that the condition from the statement holds, output NO.
Otherwise, output YES. In the next line, output K (2 ≤ K ≤ N) - the number of subarrays you are splitting into.
In the i-th of the next K lines, output two integers L_{i}, R_{i} (1 ≤ L_{i} ≤ R_{i} ≤ N), where [L_{i}, R_{i}] denotes the i-th subarray.
Each number from 1 to N has to appear in exactly one of the segments [L_{i}, R_{i}].
You can output each letter in either uppercase or lowercase. You can output any solution. You don't have to minimize K.
------ Constraints ------
$1 ≤ T ≤ 10^{5}$
$2 ≤ N ≤ 10^{5}$
$0 ≤ A_{i} < 2^{30}$
- Sum of $N$ over all test cases doesn't exceed $2\cdot 10^{5}$
----- Sample Input 1 ------
5
4
1 9 8 4
3
0 0 0
2
16 15
5
1 2 6 24 120
7
8 6 0 16 24 0 8
----- Sample Output 1 ------
YES
2
2 4
1 1
NO
NO
YES
2
1 2
3 5
YES
3
1 1
6 7
2 5
----- explanation 1 ------
Test case $1$: You can split the array into two subarrays $[2, 4]$ and $[1, 1]$. They have sums $21$ and $1$ correspondingly, whose AND is $1$.
Test case $4$: You can split the array into two subarrays $[1, 2]$ and $[3, 5]$. They have sums $3$ and $150$ correspondingly, whose AND is $2$.
Test case $5$: You can split the array into three subarrays $[1, 1]$, $[6, 7]$, $[2, 5]$. They have sums $8$, $8$, $46$, whose AND is $8$.
It's possible to show that it's impossible to split in the desired way in the second and the third test cases.
|
def solve(n, arr):
for i in range(31):
left = 0
intervals = []
for j in range(n):
if arr[j] & 1 << i > 0:
intervals.append([left + 1, j + 1])
left = j + 1
if len(intervals) <= 1:
continue
intervals[-1][1] = n
break
if not intervals:
return "NO"
res = f"YES\n{len(intervals)}\n"
for left, right in intervals:
res += f"{left} {right}\n"
return res[:-1]
for _ in range(int(input())):
n = int(input())
arr = list(map(int, input().split()))
print(solve(n, arr))
|
FUNC_DEF FOR VAR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR IF BIN_OP VAR VAR BIN_OP NUMBER VAR NUMBER EXPR FUNC_CALL VAR LIST BIN_OP VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER IF FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER NUMBER VAR IF VAR RETURN STRING ASSIGN VAR STRING FUNC_CALL VAR VAR STRING FOR VAR VAR VAR VAR VAR STRING VAR STRING RETURN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR
|
You are given an array [A_{1}, \ldots, A_{N}]. You want to split it into several (≥ 2) [subarrays] so that the following condition holds:
Calculate the sum of elements in each subarray. Then, the AND of all these sums is nonzero, where AND denotes the bitwise AND operation.
Determine if it's possible. If it's possible, find one such split. You don't have to minimize the number of subarrays.
------ Input Format ------
- The first line of the input contains a single integer T - the number of test cases. The description of the test cases follows.
- The first line of each test case contains a single integer N - the length of the array.
- The second line of each test case contains N space-separated integers A_{1}, A_{2}, \ldots, A_{N}.
------ Output Format ------
For each test case, if it's not possible to split the array so that the condition from the statement holds, output NO.
Otherwise, output YES. In the next line, output K (2 ≤ K ≤ N) - the number of subarrays you are splitting into.
In the i-th of the next K lines, output two integers L_{i}, R_{i} (1 ≤ L_{i} ≤ R_{i} ≤ N), where [L_{i}, R_{i}] denotes the i-th subarray.
Each number from 1 to N has to appear in exactly one of the segments [L_{i}, R_{i}].
You can output each letter in either uppercase or lowercase. You can output any solution. You don't have to minimize K.
------ Constraints ------
$1 ≤ T ≤ 10^{5}$
$2 ≤ N ≤ 10^{5}$
$0 ≤ A_{i} < 2^{30}$
- Sum of $N$ over all test cases doesn't exceed $2\cdot 10^{5}$
----- Sample Input 1 ------
5
4
1 9 8 4
3
0 0 0
2
16 15
5
1 2 6 24 120
7
8 6 0 16 24 0 8
----- Sample Output 1 ------
YES
2
2 4
1 1
NO
NO
YES
2
1 2
3 5
YES
3
1 1
6 7
2 5
----- explanation 1 ------
Test case $1$: You can split the array into two subarrays $[2, 4]$ and $[1, 1]$. They have sums $21$ and $1$ correspondingly, whose AND is $1$.
Test case $4$: You can split the array into two subarrays $[1, 2]$ and $[3, 5]$. They have sums $3$ and $150$ correspondingly, whose AND is $2$.
Test case $5$: You can split the array into three subarrays $[1, 1]$, $[6, 7]$, $[2, 5]$. They have sums $8$, $8$, $46$, whose AND is $8$.
It's possible to show that it's impossible to split in the desired way in the second and the third test cases.
|
from sys import stdin
input = stdin.readline
for _ in range(int(input())):
n = int(input())
a = list(map(int, input().split()))
for i in range(31):
cnt, target = 0, 1 << i
v = []
for j in range(n):
if a[j] & target:
v.append(j + 1)
if len(v) > 1:
break
if len(v) > 1:
print("YES")
print(len(v))
print(f"1 {v[0]}")
for i in range(1, len(v)):
if i + 1 < len(v):
print(f"{v[i - 1] + 1} {v[i]}")
else:
print(f"{v[i - 1] + 1} {n}")
else:
print("NO")
|
ASSIGN VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FOR VAR FUNC_CALL VAR NUMBER ASSIGN VAR VAR NUMBER BIN_OP NUMBER VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR IF BIN_OP VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER IF FUNC_CALL VAR VAR NUMBER IF FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR STRING VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR IF BIN_OP VAR NUMBER FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR BIN_OP VAR NUMBER NUMBER STRING VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR BIN_OP VAR NUMBER NUMBER STRING VAR EXPR FUNC_CALL VAR STRING
|
You are given an array [A_{1}, \ldots, A_{N}]. You want to split it into several (≥ 2) [subarrays] so that the following condition holds:
Calculate the sum of elements in each subarray. Then, the AND of all these sums is nonzero, where AND denotes the bitwise AND operation.
Determine if it's possible. If it's possible, find one such split. You don't have to minimize the number of subarrays.
------ Input Format ------
- The first line of the input contains a single integer T - the number of test cases. The description of the test cases follows.
- The first line of each test case contains a single integer N - the length of the array.
- The second line of each test case contains N space-separated integers A_{1}, A_{2}, \ldots, A_{N}.
------ Output Format ------
For each test case, if it's not possible to split the array so that the condition from the statement holds, output NO.
Otherwise, output YES. In the next line, output K (2 ≤ K ≤ N) - the number of subarrays you are splitting into.
In the i-th of the next K lines, output two integers L_{i}, R_{i} (1 ≤ L_{i} ≤ R_{i} ≤ N), where [L_{i}, R_{i}] denotes the i-th subarray.
Each number from 1 to N has to appear in exactly one of the segments [L_{i}, R_{i}].
You can output each letter in either uppercase or lowercase. You can output any solution. You don't have to minimize K.
------ Constraints ------
$1 ≤ T ≤ 10^{5}$
$2 ≤ N ≤ 10^{5}$
$0 ≤ A_{i} < 2^{30}$
- Sum of $N$ over all test cases doesn't exceed $2\cdot 10^{5}$
----- Sample Input 1 ------
5
4
1 9 8 4
3
0 0 0
2
16 15
5
1 2 6 24 120
7
8 6 0 16 24 0 8
----- Sample Output 1 ------
YES
2
2 4
1 1
NO
NO
YES
2
1 2
3 5
YES
3
1 1
6 7
2 5
----- explanation 1 ------
Test case $1$: You can split the array into two subarrays $[2, 4]$ and $[1, 1]$. They have sums $21$ and $1$ correspondingly, whose AND is $1$.
Test case $4$: You can split the array into two subarrays $[1, 2]$ and $[3, 5]$. They have sums $3$ and $150$ correspondingly, whose AND is $2$.
Test case $5$: You can split the array into three subarrays $[1, 1]$, $[6, 7]$, $[2, 5]$. They have sums $8$, $8$, $46$, whose AND is $8$.
It's possible to show that it's impossible to split in the desired way in the second and the third test cases.
|
t = int(input())
while t > 0:
n = int(input())
ar = [int(i) for i in input().split(" ")]
ind = [1]
i = 0
while i < 30 and len(ind) < 2:
ind.clear()
x = 1 << i
for j in range(n):
if ar[j] & x > 0:
ind.append(j)
i += 1
length = len(ind)
if length >= 2:
print("YES")
print(len(ind))
prev = 0
for i in ind[:-1]:
print(prev + 1, i + 1)
prev = i + 1
print(prev + 1, n)
else:
print("NO")
t -= 1
|
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR WHILE VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR LIST NUMBER ASSIGN VAR NUMBER WHILE VAR NUMBER FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR ASSIGN VAR BIN_OP NUMBER VAR FOR VAR FUNC_CALL VAR VAR IF BIN_OP VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR STRING VAR NUMBER
|
Word $s$ of length $n$ is called $k$-complete if $s$ is a palindrome, i.e. $s_i=s_{n+1-i}$ for all $1 \le i \le n$; $s$ has a period of $k$, i.e. $s_i=s_{k+i}$ for all $1 \le i \le n-k$.
For example, "abaaba" is a $3$-complete word, while "abccba" is not.
Bob is given a word $s$ of length $n$ consisting of only lowercase Latin letters and an integer $k$, such that $n$ is divisible by $k$. He wants to convert $s$ to any $k$-complete word.
To do this Bob can choose some $i$ ($1 \le i \le n$) and replace the letter at position $i$ with some other lowercase Latin letter.
So now Bob wants to know the minimum number of letters he has to replace to convert $s$ to any $k$-complete word.
Note that Bob can do zero changes if the word $s$ is already $k$-complete.
You are required to answer $t$ test cases independently.
-----Input-----
The first line contains a single integer $t$ ($1 \le t\le 10^5$) — the number of test cases.
The first line of each test case contains two integers $n$ and $k$ ($1 \le k < n \le 2 \cdot 10^5$, $n$ is divisible by $k$).
The second line of each test case contains a word $s$ of length $n$.
It is guaranteed that word $s$ only contains lowercase Latin letters. And it is guaranteed that the sum of $n$ over all test cases will not exceed $2 \cdot 10^5$.
-----Output-----
For each test case, output one integer, representing the minimum number of characters he has to replace to convert $s$ to any $k$-complete word.
-----Example-----
Input
4
6 2
abaaba
6 3
abaaba
36 9
hippopotomonstrosesquippedaliophobia
21 7
wudixiaoxingxingheclp
Output
2
0
23
16
-----Note-----
In the first test case, one optimal solution is aaaaaa.
In the second test case, the given word itself is $k$-complete.
|
import sys
input = lambda: sys.stdin.readline().rstrip()
for _ in range(int(input())):
n, k = map(int, input().split())
a = [(ord(i) - 97) for i in input()]
d = [([0] * 26) for i in range((k + 1) // 2)]
ans = 0
for i in range(len(a)):
d[min(i % k, k - i % k - 1)][a[i]] += 1
for i in d:
ans += sum(i) - max(i)
print(ans)
|
IMPORT ASSIGN VAR FUNC_CALL FUNC_CALL VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER VAR FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER NUMBER VAR FUNC_CALL VAR BIN_OP BIN_OP VAR NUMBER NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR BIN_OP VAR VAR BIN_OP BIN_OP VAR BIN_OP VAR VAR NUMBER VAR VAR NUMBER FOR VAR VAR VAR BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR
|
Word $s$ of length $n$ is called $k$-complete if $s$ is a palindrome, i.e. $s_i=s_{n+1-i}$ for all $1 \le i \le n$; $s$ has a period of $k$, i.e. $s_i=s_{k+i}$ for all $1 \le i \le n-k$.
For example, "abaaba" is a $3$-complete word, while "abccba" is not.
Bob is given a word $s$ of length $n$ consisting of only lowercase Latin letters and an integer $k$, such that $n$ is divisible by $k$. He wants to convert $s$ to any $k$-complete word.
To do this Bob can choose some $i$ ($1 \le i \le n$) and replace the letter at position $i$ with some other lowercase Latin letter.
So now Bob wants to know the minimum number of letters he has to replace to convert $s$ to any $k$-complete word.
Note that Bob can do zero changes if the word $s$ is already $k$-complete.
You are required to answer $t$ test cases independently.
-----Input-----
The first line contains a single integer $t$ ($1 \le t\le 10^5$) — the number of test cases.
The first line of each test case contains two integers $n$ and $k$ ($1 \le k < n \le 2 \cdot 10^5$, $n$ is divisible by $k$).
The second line of each test case contains a word $s$ of length $n$.
It is guaranteed that word $s$ only contains lowercase Latin letters. And it is guaranteed that the sum of $n$ over all test cases will not exceed $2 \cdot 10^5$.
-----Output-----
For each test case, output one integer, representing the minimum number of characters he has to replace to convert $s$ to any $k$-complete word.
-----Example-----
Input
4
6 2
abaaba
6 3
abaaba
36 9
hippopotomonstrosesquippedaliophobia
21 7
wudixiaoxingxingheclp
Output
2
0
23
16
-----Note-----
In the first test case, one optimal solution is aaaaaa.
In the second test case, the given word itself is $k$-complete.
|
t = int(input())
def most_f(List):
return max(set(List), key=List.count)
for test in range(t):
n, k = [int(x) for x in input().split()]
arr = list(input())
allsame = True
count = 0
end = k
ev = 0
if n % 2 == 0:
ev = 1
for i in range(0, k // 2 + ev):
valk = []
end -= 1
valk = [arr[x] for x in range(i, n, k)]
valm = [arr[x] for x in range(end, n, k)]
val = valk[:] + valm[:]
most = most_f(val)
endwr = end
for j in range(i, n, k):
if arr[j] != most:
arr[j] = most
count += 1
if arr[endwr] != most:
arr[endwr] = most
count += 1
endwr += k
if n % 2 == 1:
val = [arr[x] for x in range(k // 2, n, k)]
most = most_f(val)
for i in range(k // 2, n, k):
if arr[i] != most:
arr[i] = most
count += 1
print(count)
|
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_DEF RETURN FUNC_CALL VAR FUNC_CALL VAR VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR ASSIGN VAR NUMBER IF BIN_OP VAR NUMBER NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP BIN_OP VAR NUMBER VAR ASSIGN VAR LIST VAR NUMBER ASSIGN VAR VAR VAR VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FOR VAR FUNC_CALL VAR VAR VAR VAR IF VAR VAR VAR ASSIGN VAR VAR VAR VAR NUMBER IF VAR VAR VAR ASSIGN VAR VAR VAR VAR NUMBER VAR VAR IF BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR IF VAR VAR VAR ASSIGN VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR
|
Word $s$ of length $n$ is called $k$-complete if $s$ is a palindrome, i.e. $s_i=s_{n+1-i}$ for all $1 \le i \le n$; $s$ has a period of $k$, i.e. $s_i=s_{k+i}$ for all $1 \le i \le n-k$.
For example, "abaaba" is a $3$-complete word, while "abccba" is not.
Bob is given a word $s$ of length $n$ consisting of only lowercase Latin letters and an integer $k$, such that $n$ is divisible by $k$. He wants to convert $s$ to any $k$-complete word.
To do this Bob can choose some $i$ ($1 \le i \le n$) and replace the letter at position $i$ with some other lowercase Latin letter.
So now Bob wants to know the minimum number of letters he has to replace to convert $s$ to any $k$-complete word.
Note that Bob can do zero changes if the word $s$ is already $k$-complete.
You are required to answer $t$ test cases independently.
-----Input-----
The first line contains a single integer $t$ ($1 \le t\le 10^5$) — the number of test cases.
The first line of each test case contains two integers $n$ and $k$ ($1 \le k < n \le 2 \cdot 10^5$, $n$ is divisible by $k$).
The second line of each test case contains a word $s$ of length $n$.
It is guaranteed that word $s$ only contains lowercase Latin letters. And it is guaranteed that the sum of $n$ over all test cases will not exceed $2 \cdot 10^5$.
-----Output-----
For each test case, output one integer, representing the minimum number of characters he has to replace to convert $s$ to any $k$-complete word.
-----Example-----
Input
4
6 2
abaaba
6 3
abaaba
36 9
hippopotomonstrosesquippedaliophobia
21 7
wudixiaoxingxingheclp
Output
2
0
23
16
-----Note-----
In the first test case, one optimal solution is aaaaaa.
In the second test case, the given word itself is $k$-complete.
|
def max_counter(a):
d = {}
for i in a:
d[i] = d.get(i, 0) + 1
return max(d.values())
for _ in range(int(input())):
n, k = map(int, input().split())
s = list(input())
ans = 0
for i in range(k):
ans += (
len(s[i::k])
+ len(s[n - i - 1 :: -k])
- max_counter(s[i::k] + s[n - i - 1 :: -k])
)
print(ans // 2)
|
FUNC_DEF ASSIGN VAR DICT FOR VAR VAR ASSIGN VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER RETURN FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR BIN_OP BIN_OP FUNC_CALL VAR VAR VAR VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR BIN_OP BIN_OP VAR VAR NUMBER VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER
|
Word $s$ of length $n$ is called $k$-complete if $s$ is a palindrome, i.e. $s_i=s_{n+1-i}$ for all $1 \le i \le n$; $s$ has a period of $k$, i.e. $s_i=s_{k+i}$ for all $1 \le i \le n-k$.
For example, "abaaba" is a $3$-complete word, while "abccba" is not.
Bob is given a word $s$ of length $n$ consisting of only lowercase Latin letters and an integer $k$, such that $n$ is divisible by $k$. He wants to convert $s$ to any $k$-complete word.
To do this Bob can choose some $i$ ($1 \le i \le n$) and replace the letter at position $i$ with some other lowercase Latin letter.
So now Bob wants to know the minimum number of letters he has to replace to convert $s$ to any $k$-complete word.
Note that Bob can do zero changes if the word $s$ is already $k$-complete.
You are required to answer $t$ test cases independently.
-----Input-----
The first line contains a single integer $t$ ($1 \le t\le 10^5$) — the number of test cases.
The first line of each test case contains two integers $n$ and $k$ ($1 \le k < n \le 2 \cdot 10^5$, $n$ is divisible by $k$).
The second line of each test case contains a word $s$ of length $n$.
It is guaranteed that word $s$ only contains lowercase Latin letters. And it is guaranteed that the sum of $n$ over all test cases will not exceed $2 \cdot 10^5$.
-----Output-----
For each test case, output one integer, representing the minimum number of characters he has to replace to convert $s$ to any $k$-complete word.
-----Example-----
Input
4
6 2
abaaba
6 3
abaaba
36 9
hippopotomonstrosesquippedaliophobia
21 7
wudixiaoxingxingheclp
Output
2
0
23
16
-----Note-----
In the first test case, one optimal solution is aaaaaa.
In the second test case, the given word itself is $k$-complete.
|
for xyz in range(0, int(input())):
n, k = map(int, input().split())
s = input()
c = 0
for i in range(0, k):
ac = [0] * 26
r = i
while r < n:
ac[ord(s[r]) - 97] += 1
r += k
r = k - i - 1
while r < n:
ac[ord(s[r]) - 97] += 1
r += k
c += sum(ac) - max(ac)
print(c // 2)
|
FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR BIN_OP LIST NUMBER NUMBER ASSIGN VAR VAR WHILE VAR VAR VAR BIN_OP FUNC_CALL VAR VAR VAR NUMBER NUMBER VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER WHILE VAR VAR VAR BIN_OP FUNC_CALL VAR VAR VAR NUMBER NUMBER VAR VAR VAR BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER
|
Word $s$ of length $n$ is called $k$-complete if $s$ is a palindrome, i.e. $s_i=s_{n+1-i}$ for all $1 \le i \le n$; $s$ has a period of $k$, i.e. $s_i=s_{k+i}$ for all $1 \le i \le n-k$.
For example, "abaaba" is a $3$-complete word, while "abccba" is not.
Bob is given a word $s$ of length $n$ consisting of only lowercase Latin letters and an integer $k$, such that $n$ is divisible by $k$. He wants to convert $s$ to any $k$-complete word.
To do this Bob can choose some $i$ ($1 \le i \le n$) and replace the letter at position $i$ with some other lowercase Latin letter.
So now Bob wants to know the minimum number of letters he has to replace to convert $s$ to any $k$-complete word.
Note that Bob can do zero changes if the word $s$ is already $k$-complete.
You are required to answer $t$ test cases independently.
-----Input-----
The first line contains a single integer $t$ ($1 \le t\le 10^5$) — the number of test cases.
The first line of each test case contains two integers $n$ and $k$ ($1 \le k < n \le 2 \cdot 10^5$, $n$ is divisible by $k$).
The second line of each test case contains a word $s$ of length $n$.
It is guaranteed that word $s$ only contains lowercase Latin letters. And it is guaranteed that the sum of $n$ over all test cases will not exceed $2 \cdot 10^5$.
-----Output-----
For each test case, output one integer, representing the minimum number of characters he has to replace to convert $s$ to any $k$-complete word.
-----Example-----
Input
4
6 2
abaaba
6 3
abaaba
36 9
hippopotomonstrosesquippedaliophobia
21 7
wudixiaoxingxingheclp
Output
2
0
23
16
-----Note-----
In the first test case, one optimal solution is aaaaaa.
In the second test case, the given word itself is $k$-complete.
|
printn = lambda x: print(x, end="")
inn = lambda: int(input())
inl = lambda: list(map(int, input().split()))
inm = lambda: map(int, input().split())
ins = lambda: input().strip()
DBG = True
BIG = 10**18
R = 10**9 + 7
def ddprint(x):
if DBG:
print(x)
def foo(s, n, k):
ccnt = [([0] * 26) for i in range((k + 1) // 2)]
for i in range(n):
r = i % k
j = r if r < (k + 1) // 2 else k - 1 - r
ccnt[j][ord(s[i]) - ord("a")] += 1
sm = 0
for j in range((k + 1) // 2):
sm += max(ccnt[j])
return n - sm
t = inn()
for tt in range(t):
n, k = inm()
s = ins()
print(foo(s, n, k))
|
ASSIGN VAR FUNC_CALL VAR VAR STRING ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP NUMBER NUMBER ASSIGN VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER FUNC_DEF IF VAR EXPR FUNC_CALL VAR VAR FUNC_DEF ASSIGN VAR BIN_OP LIST NUMBER NUMBER VAR FUNC_CALL VAR BIN_OP BIN_OP VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR VAR BIN_OP BIN_OP VAR NUMBER NUMBER VAR BIN_OP BIN_OP VAR NUMBER VAR VAR VAR BIN_OP FUNC_CALL VAR VAR VAR FUNC_CALL VAR STRING NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP BIN_OP VAR NUMBER NUMBER VAR FUNC_CALL VAR VAR VAR RETURN BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR
|
Word $s$ of length $n$ is called $k$-complete if $s$ is a palindrome, i.e. $s_i=s_{n+1-i}$ for all $1 \le i \le n$; $s$ has a period of $k$, i.e. $s_i=s_{k+i}$ for all $1 \le i \le n-k$.
For example, "abaaba" is a $3$-complete word, while "abccba" is not.
Bob is given a word $s$ of length $n$ consisting of only lowercase Latin letters and an integer $k$, such that $n$ is divisible by $k$. He wants to convert $s$ to any $k$-complete word.
To do this Bob can choose some $i$ ($1 \le i \le n$) and replace the letter at position $i$ with some other lowercase Latin letter.
So now Bob wants to know the minimum number of letters he has to replace to convert $s$ to any $k$-complete word.
Note that Bob can do zero changes if the word $s$ is already $k$-complete.
You are required to answer $t$ test cases independently.
-----Input-----
The first line contains a single integer $t$ ($1 \le t\le 10^5$) — the number of test cases.
The first line of each test case contains two integers $n$ and $k$ ($1 \le k < n \le 2 \cdot 10^5$, $n$ is divisible by $k$).
The second line of each test case contains a word $s$ of length $n$.
It is guaranteed that word $s$ only contains lowercase Latin letters. And it is guaranteed that the sum of $n$ over all test cases will not exceed $2 \cdot 10^5$.
-----Output-----
For each test case, output one integer, representing the minimum number of characters he has to replace to convert $s$ to any $k$-complete word.
-----Example-----
Input
4
6 2
abaaba
6 3
abaaba
36 9
hippopotomonstrosesquippedaliophobia
21 7
wudixiaoxingxingheclp
Output
2
0
23
16
-----Note-----
In the first test case, one optimal solution is aaaaaa.
In the second test case, the given word itself is $k$-complete.
|
for _ in range(int(input())):
a, b = map(int, input().split(" "))
c = input()
rat = a // b
cost = 0
for i in range(b):
f = []
g = []
for j in range(rat):
f.append(c[i + j * b])
f.append(c[a - 1 - i - j * b])
h = set(f)
for k in h:
g.append(f.count(k))
cost += len(f) - max(g)
print(cost // 2)
|
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR LIST ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR BIN_OP VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR BIN_OP BIN_OP BIN_OP VAR NUMBER VAR BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FOR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER
|
Word $s$ of length $n$ is called $k$-complete if $s$ is a palindrome, i.e. $s_i=s_{n+1-i}$ for all $1 \le i \le n$; $s$ has a period of $k$, i.e. $s_i=s_{k+i}$ for all $1 \le i \le n-k$.
For example, "abaaba" is a $3$-complete word, while "abccba" is not.
Bob is given a word $s$ of length $n$ consisting of only lowercase Latin letters and an integer $k$, such that $n$ is divisible by $k$. He wants to convert $s$ to any $k$-complete word.
To do this Bob can choose some $i$ ($1 \le i \le n$) and replace the letter at position $i$ with some other lowercase Latin letter.
So now Bob wants to know the minimum number of letters he has to replace to convert $s$ to any $k$-complete word.
Note that Bob can do zero changes if the word $s$ is already $k$-complete.
You are required to answer $t$ test cases independently.
-----Input-----
The first line contains a single integer $t$ ($1 \le t\le 10^5$) — the number of test cases.
The first line of each test case contains two integers $n$ and $k$ ($1 \le k < n \le 2 \cdot 10^5$, $n$ is divisible by $k$).
The second line of each test case contains a word $s$ of length $n$.
It is guaranteed that word $s$ only contains lowercase Latin letters. And it is guaranteed that the sum of $n$ over all test cases will not exceed $2 \cdot 10^5$.
-----Output-----
For each test case, output one integer, representing the minimum number of characters he has to replace to convert $s$ to any $k$-complete word.
-----Example-----
Input
4
6 2
abaaba
6 3
abaaba
36 9
hippopotomonstrosesquippedaliophobia
21 7
wudixiaoxingxingheclp
Output
2
0
23
16
-----Note-----
In the first test case, one optimal solution is aaaaaa.
In the second test case, the given word itself is $k$-complete.
|
for _ in range(int(input())):
n, k = map(int, input().split())
s = input()
ans = []
if k % 2 == 1:
ran = k // 2 + 1
else:
ran = k // 2
for i in range(ran):
temp = []
start = i
end = k - i - 1
for j in range(n // k):
if i != k // 2:
temp.append(s[start + (j - 1) * k])
temp.append(s[end + (j - 1) * k])
ans.append(temp)
res = 0
for i in range(len(ans)):
some = {}
for j in range(len(ans[i])):
if some.get(ans[i][j]) == None:
some[ans[i][j]] = 1
else:
some[ans[i][j]] += 1
res += len(ans[i]) - max(some.values())
print(res)
|
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR LIST IF BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR LIST ASSIGN VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR VAR IF VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR BIN_OP VAR BIN_OP BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR VAR BIN_OP VAR BIN_OP BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR DICT FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR IF FUNC_CALL VAR VAR VAR VAR NONE ASSIGN VAR VAR VAR VAR NUMBER VAR VAR VAR VAR NUMBER VAR BIN_OP FUNC_CALL VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR
|
Word $s$ of length $n$ is called $k$-complete if $s$ is a palindrome, i.e. $s_i=s_{n+1-i}$ for all $1 \le i \le n$; $s$ has a period of $k$, i.e. $s_i=s_{k+i}$ for all $1 \le i \le n-k$.
For example, "abaaba" is a $3$-complete word, while "abccba" is not.
Bob is given a word $s$ of length $n$ consisting of only lowercase Latin letters and an integer $k$, such that $n$ is divisible by $k$. He wants to convert $s$ to any $k$-complete word.
To do this Bob can choose some $i$ ($1 \le i \le n$) and replace the letter at position $i$ with some other lowercase Latin letter.
So now Bob wants to know the minimum number of letters he has to replace to convert $s$ to any $k$-complete word.
Note that Bob can do zero changes if the word $s$ is already $k$-complete.
You are required to answer $t$ test cases independently.
-----Input-----
The first line contains a single integer $t$ ($1 \le t\le 10^5$) — the number of test cases.
The first line of each test case contains two integers $n$ and $k$ ($1 \le k < n \le 2 \cdot 10^5$, $n$ is divisible by $k$).
The second line of each test case contains a word $s$ of length $n$.
It is guaranteed that word $s$ only contains lowercase Latin letters. And it is guaranteed that the sum of $n$ over all test cases will not exceed $2 \cdot 10^5$.
-----Output-----
For each test case, output one integer, representing the minimum number of characters he has to replace to convert $s$ to any $k$-complete word.
-----Example-----
Input
4
6 2
abaaba
6 3
abaaba
36 9
hippopotomonstrosesquippedaliophobia
21 7
wudixiaoxingxingheclp
Output
2
0
23
16
-----Note-----
In the first test case, one optimal solution is aaaaaa.
In the second test case, the given word itself is $k$-complete.
|
I = lambda: list(map(int, input().split()))
for _ in range(int(input())):
n, k = I()
s = input()
x = 0
for i in range(k // 2):
al = [0] * 26
for j in range(i, n, k):
al[ord(s[j]) - 97] += 1
al[ord(s[(j // k + 1) * k - j % k - 1]) - 97] += 1
x += sum(al) - max(al)
if k % 2:
al = [0] * 26
for j in range(k // 2, n, k):
al[ord(s[j]) - 97] += 1
x += sum(al) - max(al)
print(x)
|
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR VAR VAR VAR BIN_OP FUNC_CALL VAR VAR VAR NUMBER NUMBER VAR BIN_OP FUNC_CALL VAR VAR BIN_OP BIN_OP BIN_OP BIN_OP BIN_OP VAR VAR NUMBER VAR BIN_OP VAR VAR NUMBER NUMBER NUMBER VAR BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR VAR IF BIN_OP VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR VAR BIN_OP FUNC_CALL VAR VAR VAR NUMBER NUMBER VAR BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR
|
Word $s$ of length $n$ is called $k$-complete if $s$ is a palindrome, i.e. $s_i=s_{n+1-i}$ for all $1 \le i \le n$; $s$ has a period of $k$, i.e. $s_i=s_{k+i}$ for all $1 \le i \le n-k$.
For example, "abaaba" is a $3$-complete word, while "abccba" is not.
Bob is given a word $s$ of length $n$ consisting of only lowercase Latin letters and an integer $k$, such that $n$ is divisible by $k$. He wants to convert $s$ to any $k$-complete word.
To do this Bob can choose some $i$ ($1 \le i \le n$) and replace the letter at position $i$ with some other lowercase Latin letter.
So now Bob wants to know the minimum number of letters he has to replace to convert $s$ to any $k$-complete word.
Note that Bob can do zero changes if the word $s$ is already $k$-complete.
You are required to answer $t$ test cases independently.
-----Input-----
The first line contains a single integer $t$ ($1 \le t\le 10^5$) — the number of test cases.
The first line of each test case contains two integers $n$ and $k$ ($1 \le k < n \le 2 \cdot 10^5$, $n$ is divisible by $k$).
The second line of each test case contains a word $s$ of length $n$.
It is guaranteed that word $s$ only contains lowercase Latin letters. And it is guaranteed that the sum of $n$ over all test cases will not exceed $2 \cdot 10^5$.
-----Output-----
For each test case, output one integer, representing the minimum number of characters he has to replace to convert $s$ to any $k$-complete word.
-----Example-----
Input
4
6 2
abaaba
6 3
abaaba
36 9
hippopotomonstrosesquippedaliophobia
21 7
wudixiaoxingxingheclp
Output
2
0
23
16
-----Note-----
In the first test case, one optimal solution is aaaaaa.
In the second test case, the given word itself is $k$-complete.
|
T = int(input())
while T > 0:
T -= 1
n, k = map(int, input().split())
s = input()
length = n / k
ans = 0
halfk = k // 2 if k % 2 == 0 else k // 2 + 1
for i in range(halfk):
d = {}
for ch in s[i::k]:
ch = ord(ch)
if ch in d:
d[ch] += 1
else:
d[ch] = 1
if i * 2 + 1 != k:
for ch in s[k - 1 - i :: k]:
ch = ord(ch)
if ch in d:
d[ch] += 1
else:
d[ch] = 1
if i * 2 + 1 != k:
ans += max(d.values())
else:
ans += max(d.values())
print(n - ans)
|
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR WHILE VAR NUMBER VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER NUMBER BIN_OP VAR NUMBER BIN_OP BIN_OP VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR DICT FOR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR NUMBER ASSIGN VAR VAR NUMBER IF BIN_OP BIN_OP VAR NUMBER NUMBER VAR FOR VAR VAR BIN_OP BIN_OP VAR NUMBER VAR VAR ASSIGN VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR NUMBER ASSIGN VAR VAR NUMBER IF BIN_OP BIN_OP VAR NUMBER NUMBER VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL VAR FUNC_CALL VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR
|
Word $s$ of length $n$ is called $k$-complete if $s$ is a palindrome, i.e. $s_i=s_{n+1-i}$ for all $1 \le i \le n$; $s$ has a period of $k$, i.e. $s_i=s_{k+i}$ for all $1 \le i \le n-k$.
For example, "abaaba" is a $3$-complete word, while "abccba" is not.
Bob is given a word $s$ of length $n$ consisting of only lowercase Latin letters and an integer $k$, such that $n$ is divisible by $k$. He wants to convert $s$ to any $k$-complete word.
To do this Bob can choose some $i$ ($1 \le i \le n$) and replace the letter at position $i$ with some other lowercase Latin letter.
So now Bob wants to know the minimum number of letters he has to replace to convert $s$ to any $k$-complete word.
Note that Bob can do zero changes if the word $s$ is already $k$-complete.
You are required to answer $t$ test cases independently.
-----Input-----
The first line contains a single integer $t$ ($1 \le t\le 10^5$) — the number of test cases.
The first line of each test case contains two integers $n$ and $k$ ($1 \le k < n \le 2 \cdot 10^5$, $n$ is divisible by $k$).
The second line of each test case contains a word $s$ of length $n$.
It is guaranteed that word $s$ only contains lowercase Latin letters. And it is guaranteed that the sum of $n$ over all test cases will not exceed $2 \cdot 10^5$.
-----Output-----
For each test case, output one integer, representing the minimum number of characters he has to replace to convert $s$ to any $k$-complete word.
-----Example-----
Input
4
6 2
abaaba
6 3
abaaba
36 9
hippopotomonstrosesquippedaliophobia
21 7
wudixiaoxingxingheclp
Output
2
0
23
16
-----Note-----
In the first test case, one optimal solution is aaaaaa.
In the second test case, the given word itself is $k$-complete.
|
import sys
t = int(sys.stdin.readline())
for _ in range(t):
n, k = map(int, sys.stdin.readline().split())
s = sys.stdin.readline()[:-1]
x = n // k
ans = 0
left, right = 0, k - 1
while left < right:
arr = [0] * 26
for j in range(x):
arr[ord(s[left + k * j]) - 97] += 1
arr[ord(s[right + k * j]) - 97] += 1
m = max(arr)
ans += 2 * x - m
left += 1
right -= 1
if left == right:
arr = [0] * 26
for j in range(x):
arr[ord(s[left + k * j]) - 97] += 1
m = max(arr)
ans += x - m
print(ans)
|
IMPORT ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR NUMBER ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR NUMBER ASSIGN VAR VAR NUMBER BIN_OP VAR NUMBER WHILE VAR VAR ASSIGN VAR BIN_OP LIST NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR VAR BIN_OP FUNC_CALL VAR VAR BIN_OP VAR BIN_OP VAR VAR NUMBER NUMBER VAR BIN_OP FUNC_CALL VAR VAR BIN_OP VAR BIN_OP VAR VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR BIN_OP BIN_OP NUMBER VAR VAR VAR NUMBER VAR NUMBER IF VAR VAR ASSIGN VAR BIN_OP LIST NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR VAR BIN_OP FUNC_CALL VAR VAR BIN_OP VAR BIN_OP VAR VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR
|
Word $s$ of length $n$ is called $k$-complete if $s$ is a palindrome, i.e. $s_i=s_{n+1-i}$ for all $1 \le i \le n$; $s$ has a period of $k$, i.e. $s_i=s_{k+i}$ for all $1 \le i \le n-k$.
For example, "abaaba" is a $3$-complete word, while "abccba" is not.
Bob is given a word $s$ of length $n$ consisting of only lowercase Latin letters and an integer $k$, such that $n$ is divisible by $k$. He wants to convert $s$ to any $k$-complete word.
To do this Bob can choose some $i$ ($1 \le i \le n$) and replace the letter at position $i$ with some other lowercase Latin letter.
So now Bob wants to know the minimum number of letters he has to replace to convert $s$ to any $k$-complete word.
Note that Bob can do zero changes if the word $s$ is already $k$-complete.
You are required to answer $t$ test cases independently.
-----Input-----
The first line contains a single integer $t$ ($1 \le t\le 10^5$) — the number of test cases.
The first line of each test case contains two integers $n$ and $k$ ($1 \le k < n \le 2 \cdot 10^5$, $n$ is divisible by $k$).
The second line of each test case contains a word $s$ of length $n$.
It is guaranteed that word $s$ only contains lowercase Latin letters. And it is guaranteed that the sum of $n$ over all test cases will not exceed $2 \cdot 10^5$.
-----Output-----
For each test case, output one integer, representing the minimum number of characters he has to replace to convert $s$ to any $k$-complete word.
-----Example-----
Input
4
6 2
abaaba
6 3
abaaba
36 9
hippopotomonstrosesquippedaliophobia
21 7
wudixiaoxingxingheclp
Output
2
0
23
16
-----Note-----
In the first test case, one optimal solution is aaaaaa.
In the second test case, the given word itself is $k$-complete.
|
import sys
input = sys.stdin.readline
def main():
testNum = int(input())
for testcase in range(testNum):
n, k = map(int, input().split())
s = list(input())[:n]
res = 0
used = [True] * n
empty = [True] * k
for i in range(n):
if used[i]:
tank = [i]
c = [0] * 27
while tank:
p = tank.pop()
if not used[p]:
continue
c[ord(s[p]) - 97] += 1
used[p] = False
kk = p % k
if empty[kk]:
empty[kk] = False
for j in range(n // k):
if used[j * k + kk]:
tank.append(j * k + kk)
if used[n - 1 - p]:
tank.append(n - 1 - p)
res += sum(c) - max(c)
print(res)
main()
|
IMPORT ASSIGN VAR VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR ASSIGN VAR LIST VAR ASSIGN VAR BIN_OP LIST NUMBER NUMBER WHILE VAR ASSIGN VAR FUNC_CALL VAR IF VAR VAR VAR BIN_OP FUNC_CALL VAR VAR VAR NUMBER NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR IF VAR VAR ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR VAR IF VAR BIN_OP BIN_OP VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR VAR IF VAR BIN_OP BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR BIN_OP BIN_OP VAR NUMBER VAR VAR BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR
|
Word $s$ of length $n$ is called $k$-complete if $s$ is a palindrome, i.e. $s_i=s_{n+1-i}$ for all $1 \le i \le n$; $s$ has a period of $k$, i.e. $s_i=s_{k+i}$ for all $1 \le i \le n-k$.
For example, "abaaba" is a $3$-complete word, while "abccba" is not.
Bob is given a word $s$ of length $n$ consisting of only lowercase Latin letters and an integer $k$, such that $n$ is divisible by $k$. He wants to convert $s$ to any $k$-complete word.
To do this Bob can choose some $i$ ($1 \le i \le n$) and replace the letter at position $i$ with some other lowercase Latin letter.
So now Bob wants to know the minimum number of letters he has to replace to convert $s$ to any $k$-complete word.
Note that Bob can do zero changes if the word $s$ is already $k$-complete.
You are required to answer $t$ test cases independently.
-----Input-----
The first line contains a single integer $t$ ($1 \le t\le 10^5$) — the number of test cases.
The first line of each test case contains two integers $n$ and $k$ ($1 \le k < n \le 2 \cdot 10^5$, $n$ is divisible by $k$).
The second line of each test case contains a word $s$ of length $n$.
It is guaranteed that word $s$ only contains lowercase Latin letters. And it is guaranteed that the sum of $n$ over all test cases will not exceed $2 \cdot 10^5$.
-----Output-----
For each test case, output one integer, representing the minimum number of characters he has to replace to convert $s$ to any $k$-complete word.
-----Example-----
Input
4
6 2
abaaba
6 3
abaaba
36 9
hippopotomonstrosesquippedaliophobia
21 7
wudixiaoxingxingheclp
Output
2
0
23
16
-----Note-----
In the first test case, one optimal solution is aaaaaa.
In the second test case, the given word itself is $k$-complete.
|
t = int(input())
cases = []
for _ in range(t):
a = [int(i) for i in input().split()]
n = a[0]
k = a[1]
string = input()
cases.append((k, string))
for k, string in cases:
l = len(string)
n = l // k
ans = 0
for i in range(k // 2):
d = {}
for j in range(i, l, k):
s = string[j]
d[s] = d.get(s, 0) + 1
for j in range(k - 1 - i, l, k):
s = string[j]
d[s] = d.get(s, 0) + 1
res = max(d.values())
diff = n * 2 - res
ans += diff
if k % 2 == 1:
d = {}
for j in range(k // 2, l, k):
s = string[j]
d[s] = d.get(s, 0) + 1
res = max(d.values())
diff = n - res
ans += diff
print(ans)
|
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR VAR FOR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR DICT FOR VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR BIN_OP BIN_OP VAR NUMBER VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR BIN_OP BIN_OP VAR NUMBER VAR VAR VAR IF BIN_OP VAR NUMBER NUMBER ASSIGN VAR DICT FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR BIN_OP VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR
|
Word $s$ of length $n$ is called $k$-complete if $s$ is a palindrome, i.e. $s_i=s_{n+1-i}$ for all $1 \le i \le n$; $s$ has a period of $k$, i.e. $s_i=s_{k+i}$ for all $1 \le i \le n-k$.
For example, "abaaba" is a $3$-complete word, while "abccba" is not.
Bob is given a word $s$ of length $n$ consisting of only lowercase Latin letters and an integer $k$, such that $n$ is divisible by $k$. He wants to convert $s$ to any $k$-complete word.
To do this Bob can choose some $i$ ($1 \le i \le n$) and replace the letter at position $i$ with some other lowercase Latin letter.
So now Bob wants to know the minimum number of letters he has to replace to convert $s$ to any $k$-complete word.
Note that Bob can do zero changes if the word $s$ is already $k$-complete.
You are required to answer $t$ test cases independently.
-----Input-----
The first line contains a single integer $t$ ($1 \le t\le 10^5$) — the number of test cases.
The first line of each test case contains two integers $n$ and $k$ ($1 \le k < n \le 2 \cdot 10^5$, $n$ is divisible by $k$).
The second line of each test case contains a word $s$ of length $n$.
It is guaranteed that word $s$ only contains lowercase Latin letters. And it is guaranteed that the sum of $n$ over all test cases will not exceed $2 \cdot 10^5$.
-----Output-----
For each test case, output one integer, representing the minimum number of characters he has to replace to convert $s$ to any $k$-complete word.
-----Example-----
Input
4
6 2
abaaba
6 3
abaaba
36 9
hippopotomonstrosesquippedaliophobia
21 7
wudixiaoxingxingheclp
Output
2
0
23
16
-----Note-----
In the first test case, one optimal solution is aaaaaa.
In the second test case, the given word itself is $k$-complete.
|
T = int(input())
for _ in range(T):
n, k = map(int, input().split())
s = input()
i = 0
tc = 0
k1 = k
while i < k:
A = [
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
]
m = 0
if (n - 2 * i - 1) % k1 == 0:
for j in range(i, n, k1):
A[ord(s[j]) - 97] = A[ord(s[j]) - 97] + 1
m = max(m, A[ord(s[j]) - 97])
tc = tc + n // k1 - m
else:
for j in range(i, n, k1):
A[ord(s[n - j - 1]) - 97] = A[ord(s[n - j - 1]) - 97] + 1
A[ord(s[j]) - 97] = A[ord(s[j]) - 97] + 1
m = max(m, A[ord(s[j]) - 97], A[ord(s[n - j - 1]) - 97])
k = k - 1
tc = tc + n // k1 * 2 - m
i = i + 1
print(tc)
|
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR WHILE VAR VAR ASSIGN VAR LIST NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER ASSIGN VAR NUMBER IF BIN_OP BIN_OP BIN_OP VAR BIN_OP NUMBER VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR VAR NUMBER BIN_OP VAR BIN_OP FUNC_CALL VAR VAR VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR BIN_OP FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR BIN_OP VAR VAR VAR FOR VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR NUMBER NUMBER BIN_OP VAR BIN_OP FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR NUMBER NUMBER NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR VAR NUMBER BIN_OP VAR BIN_OP FUNC_CALL VAR VAR VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR BIN_OP FUNC_CALL VAR VAR VAR NUMBER VAR BIN_OP FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR BIN_OP BIN_OP VAR VAR NUMBER VAR ASSIGN VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR
|
Word $s$ of length $n$ is called $k$-complete if $s$ is a palindrome, i.e. $s_i=s_{n+1-i}$ for all $1 \le i \le n$; $s$ has a period of $k$, i.e. $s_i=s_{k+i}$ for all $1 \le i \le n-k$.
For example, "abaaba" is a $3$-complete word, while "abccba" is not.
Bob is given a word $s$ of length $n$ consisting of only lowercase Latin letters and an integer $k$, such that $n$ is divisible by $k$. He wants to convert $s$ to any $k$-complete word.
To do this Bob can choose some $i$ ($1 \le i \le n$) and replace the letter at position $i$ with some other lowercase Latin letter.
So now Bob wants to know the minimum number of letters he has to replace to convert $s$ to any $k$-complete word.
Note that Bob can do zero changes if the word $s$ is already $k$-complete.
You are required to answer $t$ test cases independently.
-----Input-----
The first line contains a single integer $t$ ($1 \le t\le 10^5$) — the number of test cases.
The first line of each test case contains two integers $n$ and $k$ ($1 \le k < n \le 2 \cdot 10^5$, $n$ is divisible by $k$).
The second line of each test case contains a word $s$ of length $n$.
It is guaranteed that word $s$ only contains lowercase Latin letters. And it is guaranteed that the sum of $n$ over all test cases will not exceed $2 \cdot 10^5$.
-----Output-----
For each test case, output one integer, representing the minimum number of characters he has to replace to convert $s$ to any $k$-complete word.
-----Example-----
Input
4
6 2
abaaba
6 3
abaaba
36 9
hippopotomonstrosesquippedaliophobia
21 7
wudixiaoxingxingheclp
Output
2
0
23
16
-----Note-----
In the first test case, one optimal solution is aaaaaa.
In the second test case, the given word itself is $k$-complete.
|
def Bfs(id, n, k, visited: list):
queue1, queue2 = [], []
visited[id] = True
queue1.append(id)
while queue1:
i = queue1.pop()
queue2.append(i)
if i + k < n and not visited[i + k]:
visited[i + k] = True
queue1.append(i + k)
if i - k >= 0 and not visited[i - k]:
visited[i - k] = True
queue1.append(i - k)
if not visited[n - 1 - i]:
visited[n - 1 - i] = True
queue1.append(n - 1 - i)
return queue2
def countReplacement(string: str, queue: list):
total, count, chrcount = len(queue), 0, {}
while queue:
i = queue.pop()
chrcount[string[i]] = chrcount.get(string[i], 0) + 1
count = max(count, chrcount[string[i]])
return total - count
t = int(input())
while t > 0:
n, k = [int(x) for x in input().split()]
string, count = input(), 0
visited = [False] * len(string)
for id in range(len(string)):
if not visited[id]:
queue = Bfs(id, n, k, visited)
count += countReplacement(string, queue)
print(count)
t -= 1
|
FUNC_DEF VAR ASSIGN VAR VAR LIST LIST ASSIGN VAR VAR NUMBER EXPR FUNC_CALL VAR VAR WHILE VAR ASSIGN VAR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR IF BIN_OP VAR VAR VAR VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR VAR IF BIN_OP VAR VAR NUMBER VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR VAR IF VAR BIN_OP BIN_OP VAR NUMBER VAR ASSIGN VAR BIN_OP BIN_OP VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR BIN_OP BIN_OP VAR NUMBER VAR RETURN VAR FUNC_DEF VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR NUMBER DICT WHILE VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR VAR VAR BIN_OP FUNC_CALL VAR VAR VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR RETURN BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR WHILE VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR NUMBER
|
Word $s$ of length $n$ is called $k$-complete if $s$ is a palindrome, i.e. $s_i=s_{n+1-i}$ for all $1 \le i \le n$; $s$ has a period of $k$, i.e. $s_i=s_{k+i}$ for all $1 \le i \le n-k$.
For example, "abaaba" is a $3$-complete word, while "abccba" is not.
Bob is given a word $s$ of length $n$ consisting of only lowercase Latin letters and an integer $k$, such that $n$ is divisible by $k$. He wants to convert $s$ to any $k$-complete word.
To do this Bob can choose some $i$ ($1 \le i \le n$) and replace the letter at position $i$ with some other lowercase Latin letter.
So now Bob wants to know the minimum number of letters he has to replace to convert $s$ to any $k$-complete word.
Note that Bob can do zero changes if the word $s$ is already $k$-complete.
You are required to answer $t$ test cases independently.
-----Input-----
The first line contains a single integer $t$ ($1 \le t\le 10^5$) — the number of test cases.
The first line of each test case contains two integers $n$ and $k$ ($1 \le k < n \le 2 \cdot 10^5$, $n$ is divisible by $k$).
The second line of each test case contains a word $s$ of length $n$.
It is guaranteed that word $s$ only contains lowercase Latin letters. And it is guaranteed that the sum of $n$ over all test cases will not exceed $2 \cdot 10^5$.
-----Output-----
For each test case, output one integer, representing the minimum number of characters he has to replace to convert $s$ to any $k$-complete word.
-----Example-----
Input
4
6 2
abaaba
6 3
abaaba
36 9
hippopotomonstrosesquippedaliophobia
21 7
wudixiaoxingxingheclp
Output
2
0
23
16
-----Note-----
In the first test case, one optimal solution is aaaaaa.
In the second test case, the given word itself is $k$-complete.
|
from sys import stderr
def count_min_to_equalize(cs):
c_counts = [(0) for _ in range(ord("z") - ord("a") + 1)]
for c in cs:
c_counts[ord(c) - ord("a")] += 1
return len(cs) - max(c_counts)
for _ in range(int(input())):
n, k = map(int, input().split(" "))
s = input()
n_min = 0
s_new = [None for c in s]
for i in range(k // 2):
cs = [s[i + j * k] for j in range(0, n // k)]
cs += [s[(j + 1) * k - (i + 1)] for j in range(0, n // k)]
n_min += count_min_to_equalize(cs)
if k % 2 == 1:
n_min += count_min_to_equalize([s[k // 2 + i * k] for i in range(n // k)])
print(n_min)
|
FUNC_DEF ASSIGN VAR NUMBER VAR FUNC_CALL VAR BIN_OP BIN_OP FUNC_CALL VAR STRING FUNC_CALL VAR STRING NUMBER FOR VAR VAR VAR BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR STRING NUMBER RETURN BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NONE VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR VAR BIN_OP VAR BIN_OP VAR VAR VAR FUNC_CALL VAR NUMBER BIN_OP VAR VAR VAR VAR BIN_OP BIN_OP BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR NUMBER BIN_OP VAR VAR VAR FUNC_CALL VAR VAR IF BIN_OP VAR NUMBER NUMBER VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR NUMBER BIN_OP VAR VAR VAR FUNC_CALL VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR
|
Word $s$ of length $n$ is called $k$-complete if $s$ is a palindrome, i.e. $s_i=s_{n+1-i}$ for all $1 \le i \le n$; $s$ has a period of $k$, i.e. $s_i=s_{k+i}$ for all $1 \le i \le n-k$.
For example, "abaaba" is a $3$-complete word, while "abccba" is not.
Bob is given a word $s$ of length $n$ consisting of only lowercase Latin letters and an integer $k$, such that $n$ is divisible by $k$. He wants to convert $s$ to any $k$-complete word.
To do this Bob can choose some $i$ ($1 \le i \le n$) and replace the letter at position $i$ with some other lowercase Latin letter.
So now Bob wants to know the minimum number of letters he has to replace to convert $s$ to any $k$-complete word.
Note that Bob can do zero changes if the word $s$ is already $k$-complete.
You are required to answer $t$ test cases independently.
-----Input-----
The first line contains a single integer $t$ ($1 \le t\le 10^5$) — the number of test cases.
The first line of each test case contains two integers $n$ and $k$ ($1 \le k < n \le 2 \cdot 10^5$, $n$ is divisible by $k$).
The second line of each test case contains a word $s$ of length $n$.
It is guaranteed that word $s$ only contains lowercase Latin letters. And it is guaranteed that the sum of $n$ over all test cases will not exceed $2 \cdot 10^5$.
-----Output-----
For each test case, output one integer, representing the minimum number of characters he has to replace to convert $s$ to any $k$-complete word.
-----Example-----
Input
4
6 2
abaaba
6 3
abaaba
36 9
hippopotomonstrosesquippedaliophobia
21 7
wudixiaoxingxingheclp
Output
2
0
23
16
-----Note-----
In the first test case, one optimal solution is aaaaaa.
In the second test case, the given word itself is $k$-complete.
|
t = int(input())
for _ in range(t):
n, k = map(int, input().split())
s = input()
c = 0
for i in range(0, k // 2):
d = {}
g = 0
for j in range(i, n, k):
g += 1
if s[j] not in d:
d[s[j]] = 0
d[s[j]] += 1
for j in range(k - i - 1, n, k):
g += 1
if s[j] not in d:
d[s[j]] = 0
d[s[j]] += 1
m = max(d.values())
m = g - m
c += m
if k % 2 != 0:
re = k // 2
g = 0
d = {}
for i in range(re, n, k):
g += 1
if s[i] not in d:
d[s[i]] = 0
d[s[i]] += 1
m = max(d.values())
m = g - m
c += m
print(c)
|
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR DICT ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR VAR VAR NUMBER IF VAR VAR VAR ASSIGN VAR VAR VAR NUMBER VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR NUMBER VAR VAR VAR NUMBER IF VAR VAR VAR ASSIGN VAR VAR VAR NUMBER VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR BIN_OP VAR VAR VAR VAR IF BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR DICT FOR VAR FUNC_CALL VAR VAR VAR VAR VAR NUMBER IF VAR VAR VAR ASSIGN VAR VAR VAR NUMBER VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR BIN_OP VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR
|
Word $s$ of length $n$ is called $k$-complete if $s$ is a palindrome, i.e. $s_i=s_{n+1-i}$ for all $1 \le i \le n$; $s$ has a period of $k$, i.e. $s_i=s_{k+i}$ for all $1 \le i \le n-k$.
For example, "abaaba" is a $3$-complete word, while "abccba" is not.
Bob is given a word $s$ of length $n$ consisting of only lowercase Latin letters and an integer $k$, such that $n$ is divisible by $k$. He wants to convert $s$ to any $k$-complete word.
To do this Bob can choose some $i$ ($1 \le i \le n$) and replace the letter at position $i$ with some other lowercase Latin letter.
So now Bob wants to know the minimum number of letters he has to replace to convert $s$ to any $k$-complete word.
Note that Bob can do zero changes if the word $s$ is already $k$-complete.
You are required to answer $t$ test cases independently.
-----Input-----
The first line contains a single integer $t$ ($1 \le t\le 10^5$) — the number of test cases.
The first line of each test case contains two integers $n$ and $k$ ($1 \le k < n \le 2 \cdot 10^5$, $n$ is divisible by $k$).
The second line of each test case contains a word $s$ of length $n$.
It is guaranteed that word $s$ only contains lowercase Latin letters. And it is guaranteed that the sum of $n$ over all test cases will not exceed $2 \cdot 10^5$.
-----Output-----
For each test case, output one integer, representing the minimum number of characters he has to replace to convert $s$ to any $k$-complete word.
-----Example-----
Input
4
6 2
abaaba
6 3
abaaba
36 9
hippopotomonstrosesquippedaliophobia
21 7
wudixiaoxingxingheclp
Output
2
0
23
16
-----Note-----
In the first test case, one optimal solution is aaaaaa.
In the second test case, the given word itself is $k$-complete.
|
t = int(input())
res = []
for i in range(t):
summ_all = 0
n, k = map(int, input().split())
s = input()
ans = [([0] * 26) for j in range((k + 1) // 2)]
for j in range(n):
ans[min(j % k, k - j % k - 1)][ord(s[j]) - 97] += 1
for j in ans:
summ = 0
maximum = 0
for k in j:
summ += k
maximum = max(maximum, k)
summ_all += summ - maximum
res.append(summ_all)
for i in res:
print(i)
|
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER NUMBER VAR FUNC_CALL VAR BIN_OP BIN_OP VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR BIN_OP VAR VAR BIN_OP BIN_OP VAR BIN_OP VAR VAR NUMBER BIN_OP FUNC_CALL VAR VAR VAR NUMBER NUMBER FOR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR FOR VAR VAR EXPR FUNC_CALL VAR VAR
|
Word $s$ of length $n$ is called $k$-complete if $s$ is a palindrome, i.e. $s_i=s_{n+1-i}$ for all $1 \le i \le n$; $s$ has a period of $k$, i.e. $s_i=s_{k+i}$ for all $1 \le i \le n-k$.
For example, "abaaba" is a $3$-complete word, while "abccba" is not.
Bob is given a word $s$ of length $n$ consisting of only lowercase Latin letters and an integer $k$, such that $n$ is divisible by $k$. He wants to convert $s$ to any $k$-complete word.
To do this Bob can choose some $i$ ($1 \le i \le n$) and replace the letter at position $i$ with some other lowercase Latin letter.
So now Bob wants to know the minimum number of letters he has to replace to convert $s$ to any $k$-complete word.
Note that Bob can do zero changes if the word $s$ is already $k$-complete.
You are required to answer $t$ test cases independently.
-----Input-----
The first line contains a single integer $t$ ($1 \le t\le 10^5$) — the number of test cases.
The first line of each test case contains two integers $n$ and $k$ ($1 \le k < n \le 2 \cdot 10^5$, $n$ is divisible by $k$).
The second line of each test case contains a word $s$ of length $n$.
It is guaranteed that word $s$ only contains lowercase Latin letters. And it is guaranteed that the sum of $n$ over all test cases will not exceed $2 \cdot 10^5$.
-----Output-----
For each test case, output one integer, representing the minimum number of characters he has to replace to convert $s$ to any $k$-complete word.
-----Example-----
Input
4
6 2
abaaba
6 3
abaaba
36 9
hippopotomonstrosesquippedaliophobia
21 7
wudixiaoxingxingheclp
Output
2
0
23
16
-----Note-----
In the first test case, one optimal solution is aaaaaa.
In the second test case, the given word itself is $k$-complete.
|
def main():
n, k = map(int, input().split())
s = input()
dct = dict()
b = 0
if k % 2 == 1:
b = 1
for i in range(k // 2 + b):
dct[i] = dict()
for i in range(0, k // 2 + b):
for r in range(0, n // k):
if not s[k * r + i] in dct[i]:
dct[i][s[k * r + i]] = 1
else:
dct[i][s[k * r + i]] += 1
m = 0
for i in range(k - 1, k // 2 + b - 1, -1):
for r in range(0, n // k):
if not s[k * r + i] in dct[m]:
dct[m][s[k * r + i]] = 1
else:
dct[m][s[k * r + i]] += 1
m += 1
sm = 0
lst = [(0) for _ in range(k)]
for i in dct:
sm += dct[i][max(dct[i], key=lambda x: dct[i][x])]
print(n - sm)
t = int(input())
for i in range(t):
main()
|
FUNC_DEF ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER IF BIN_OP VAR NUMBER NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP BIN_OP VAR NUMBER VAR ASSIGN VAR VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP BIN_OP VAR NUMBER VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR VAR IF VAR BIN_OP BIN_OP VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR BIN_OP BIN_OP VAR VAR VAR NUMBER VAR VAR VAR BIN_OP BIN_OP VAR VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP BIN_OP BIN_OP VAR NUMBER VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR VAR IF VAR BIN_OP BIN_OP VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR BIN_OP BIN_OP VAR VAR VAR NUMBER VAR VAR VAR BIN_OP BIN_OP VAR VAR VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR FOR VAR VAR VAR VAR VAR FUNC_CALL VAR VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR
|
Word $s$ of length $n$ is called $k$-complete if $s$ is a palindrome, i.e. $s_i=s_{n+1-i}$ for all $1 \le i \le n$; $s$ has a period of $k$, i.e. $s_i=s_{k+i}$ for all $1 \le i \le n-k$.
For example, "abaaba" is a $3$-complete word, while "abccba" is not.
Bob is given a word $s$ of length $n$ consisting of only lowercase Latin letters and an integer $k$, such that $n$ is divisible by $k$. He wants to convert $s$ to any $k$-complete word.
To do this Bob can choose some $i$ ($1 \le i \le n$) and replace the letter at position $i$ with some other lowercase Latin letter.
So now Bob wants to know the minimum number of letters he has to replace to convert $s$ to any $k$-complete word.
Note that Bob can do zero changes if the word $s$ is already $k$-complete.
You are required to answer $t$ test cases independently.
-----Input-----
The first line contains a single integer $t$ ($1 \le t\le 10^5$) — the number of test cases.
The first line of each test case contains two integers $n$ and $k$ ($1 \le k < n \le 2 \cdot 10^5$, $n$ is divisible by $k$).
The second line of each test case contains a word $s$ of length $n$.
It is guaranteed that word $s$ only contains lowercase Latin letters. And it is guaranteed that the sum of $n$ over all test cases will not exceed $2 \cdot 10^5$.
-----Output-----
For each test case, output one integer, representing the minimum number of characters he has to replace to convert $s$ to any $k$-complete word.
-----Example-----
Input
4
6 2
abaaba
6 3
abaaba
36 9
hippopotomonstrosesquippedaliophobia
21 7
wudixiaoxingxingheclp
Output
2
0
23
16
-----Note-----
In the first test case, one optimal solution is aaaaaa.
In the second test case, the given word itself is $k$-complete.
|
t = int(input())
for _ in range(t):
n, k = [int(x) for x in input().split()]
s = input()
l = []
for i in range(k):
l.append([])
for i in range(n):
l[i % k].append(s[i])
ans = 0
for i in range(k // 2):
x = l[i] + l[k - 1 - i]
dict = {}
for j in range(len(x)):
if dict.get(x[j]) == None:
dict[x[j]] = 0
dict[x[j]] += 1
ans += 2 * (n // k) - max(list(dict.values()))
if k % 2 != 0:
temp = k
k = k // 2
x = l[k]
dict = {}
for j in range(len(x)):
if dict.get(x[j]) is None:
dict[x[j]] = 0
dict[x[j]] += 1
ans += n // temp - max(list(dict.values()))
print(ans)
|
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR LIST FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR VAR VAR BIN_OP BIN_OP VAR NUMBER VAR ASSIGN VAR DICT FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR VAR NONE ASSIGN VAR VAR VAR NUMBER VAR VAR VAR NUMBER VAR BIN_OP BIN_OP NUMBER BIN_OP VAR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR IF BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR DICT FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR VAR NONE ASSIGN VAR VAR VAR NUMBER VAR VAR VAR NUMBER VAR BIN_OP BIN_OP VAR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR
|
Word $s$ of length $n$ is called $k$-complete if $s$ is a palindrome, i.e. $s_i=s_{n+1-i}$ for all $1 \le i \le n$; $s$ has a period of $k$, i.e. $s_i=s_{k+i}$ for all $1 \le i \le n-k$.
For example, "abaaba" is a $3$-complete word, while "abccba" is not.
Bob is given a word $s$ of length $n$ consisting of only lowercase Latin letters and an integer $k$, such that $n$ is divisible by $k$. He wants to convert $s$ to any $k$-complete word.
To do this Bob can choose some $i$ ($1 \le i \le n$) and replace the letter at position $i$ with some other lowercase Latin letter.
So now Bob wants to know the minimum number of letters he has to replace to convert $s$ to any $k$-complete word.
Note that Bob can do zero changes if the word $s$ is already $k$-complete.
You are required to answer $t$ test cases independently.
-----Input-----
The first line contains a single integer $t$ ($1 \le t\le 10^5$) — the number of test cases.
The first line of each test case contains two integers $n$ and $k$ ($1 \le k < n \le 2 \cdot 10^5$, $n$ is divisible by $k$).
The second line of each test case contains a word $s$ of length $n$.
It is guaranteed that word $s$ only contains lowercase Latin letters. And it is guaranteed that the sum of $n$ over all test cases will not exceed $2 \cdot 10^5$.
-----Output-----
For each test case, output one integer, representing the minimum number of characters he has to replace to convert $s$ to any $k$-complete word.
-----Example-----
Input
4
6 2
abaaba
6 3
abaaba
36 9
hippopotomonstrosesquippedaliophobia
21 7
wudixiaoxingxingheclp
Output
2
0
23
16
-----Note-----
In the first test case, one optimal solution is aaaaaa.
In the second test case, the given word itself is $k$-complete.
|
for t in range(int(input())):
n, k = map(int, input().split())
s = input()
div = n // k
ans = 0
if k % 2 == 0:
a = k // 2
else:
a = k // 2 + 1
for i in range(a):
arr = [0] * 26
for j in range(div):
if i != k - 1 - i:
arr[ord(s[j * k + i]) - 97] += 1
arr[ord(s[j * k + k - i - 1]) - 97] += 1
else:
arr[ord(s[j * k + i]) - 97] += 1
ans += sum(arr) - max(arr)
print(ans)
|
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR NUMBER IF BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR BIN_OP BIN_OP VAR NUMBER VAR VAR BIN_OP FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR VAR NUMBER NUMBER VAR BIN_OP FUNC_CALL VAR VAR BIN_OP BIN_OP BIN_OP BIN_OP VAR VAR VAR VAR NUMBER NUMBER NUMBER VAR BIN_OP FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR VAR NUMBER NUMBER VAR BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR
|
Word $s$ of length $n$ is called $k$-complete if $s$ is a palindrome, i.e. $s_i=s_{n+1-i}$ for all $1 \le i \le n$; $s$ has a period of $k$, i.e. $s_i=s_{k+i}$ for all $1 \le i \le n-k$.
For example, "abaaba" is a $3$-complete word, while "abccba" is not.
Bob is given a word $s$ of length $n$ consisting of only lowercase Latin letters and an integer $k$, such that $n$ is divisible by $k$. He wants to convert $s$ to any $k$-complete word.
To do this Bob can choose some $i$ ($1 \le i \le n$) and replace the letter at position $i$ with some other lowercase Latin letter.
So now Bob wants to know the minimum number of letters he has to replace to convert $s$ to any $k$-complete word.
Note that Bob can do zero changes if the word $s$ is already $k$-complete.
You are required to answer $t$ test cases independently.
-----Input-----
The first line contains a single integer $t$ ($1 \le t\le 10^5$) — the number of test cases.
The first line of each test case contains two integers $n$ and $k$ ($1 \le k < n \le 2 \cdot 10^5$, $n$ is divisible by $k$).
The second line of each test case contains a word $s$ of length $n$.
It is guaranteed that word $s$ only contains lowercase Latin letters. And it is guaranteed that the sum of $n$ over all test cases will not exceed $2 \cdot 10^5$.
-----Output-----
For each test case, output one integer, representing the minimum number of characters he has to replace to convert $s$ to any $k$-complete word.
-----Example-----
Input
4
6 2
abaaba
6 3
abaaba
36 9
hippopotomonstrosesquippedaliophobia
21 7
wudixiaoxingxingheclp
Output
2
0
23
16
-----Note-----
In the first test case, one optimal solution is aaaaaa.
In the second test case, the given word itself is $k$-complete.
|
from sys import stdin
input = stdin.readline
def A():
t = int(input())
for _ in range(t):
a, b, c, d = map(int, input().split())
x, y, x1, y1, x2, y2 = map(int, input().split())
netr = b - a
netu = d - c
if x1 == x2 and (a > 0 or b > 0):
print("No")
continue
if y1 == y2 and (c > 0 or d > 0):
print("No")
continue
if netr + x < x1 or netr + x > x2:
print("No")
continue
if netu + y < y1 or netu + y > y2:
print("No")
continue
print("Yes")
def B():
t = int(input())
for _ in range(t):
m = int(input())
a = list(map(int, input().split()))
c = [0] * m
p = [2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31]
used = [False] * 11
for i in range(m):
j = 0
while a[i] % p[j] != 0:
j += 1
used[j] = True
c[i] = j
used[0] = int(used[0])
for i in range(1, 11):
used[i] = int(used[i]) + used[i - 1]
for i in range(m):
c[i] = used[c[i]]
print(max(used))
print(*c)
def C():
t = int(input())
for _ in range(t):
n, k = map(int, input().split())
s = [(ord(x) - 97) for x in input().rstrip()]
l = [[(0) for i in range(26)] for j in range(k)]
for i in range(n):
l[i % k][s[i]] += 1
for i in range(k // 2):
for j in range(26):
l[i][j] += l[k - i - 1][j]
for i in range((k + 1) // 2):
n -= max(l[i])
print(n)
C()
|
ASSIGN VAR VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR VAR VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR VAR IF VAR VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR STRING IF VAR VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR STRING IF BIN_OP VAR VAR VAR BIN_OP VAR VAR VAR EXPR FUNC_CALL VAR STRING IF BIN_OP VAR VAR VAR BIN_OP VAR VAR VAR EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR LIST NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER ASSIGN VAR BIN_OP LIST NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER WHILE BIN_OP VAR VAR VAR VAR NUMBER VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR NUMBER FUNC_CALL VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER NUMBER ASSIGN VAR VAR BIN_OP FUNC_CALL VAR VAR VAR VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR NUMBER VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR VAR BIN_OP VAR VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR VAR VAR VAR BIN_OP BIN_OP VAR VAR NUMBER VAR FOR VAR FUNC_CALL VAR BIN_OP BIN_OP VAR NUMBER NUMBER VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR
|
Word $s$ of length $n$ is called $k$-complete if $s$ is a palindrome, i.e. $s_i=s_{n+1-i}$ for all $1 \le i \le n$; $s$ has a period of $k$, i.e. $s_i=s_{k+i}$ for all $1 \le i \le n-k$.
For example, "abaaba" is a $3$-complete word, while "abccba" is not.
Bob is given a word $s$ of length $n$ consisting of only lowercase Latin letters and an integer $k$, such that $n$ is divisible by $k$. He wants to convert $s$ to any $k$-complete word.
To do this Bob can choose some $i$ ($1 \le i \le n$) and replace the letter at position $i$ with some other lowercase Latin letter.
So now Bob wants to know the minimum number of letters he has to replace to convert $s$ to any $k$-complete word.
Note that Bob can do zero changes if the word $s$ is already $k$-complete.
You are required to answer $t$ test cases independently.
-----Input-----
The first line contains a single integer $t$ ($1 \le t\le 10^5$) — the number of test cases.
The first line of each test case contains two integers $n$ and $k$ ($1 \le k < n \le 2 \cdot 10^5$, $n$ is divisible by $k$).
The second line of each test case contains a word $s$ of length $n$.
It is guaranteed that word $s$ only contains lowercase Latin letters. And it is guaranteed that the sum of $n$ over all test cases will not exceed $2 \cdot 10^5$.
-----Output-----
For each test case, output one integer, representing the minimum number of characters he has to replace to convert $s$ to any $k$-complete word.
-----Example-----
Input
4
6 2
abaaba
6 3
abaaba
36 9
hippopotomonstrosesquippedaliophobia
21 7
wudixiaoxingxingheclp
Output
2
0
23
16
-----Note-----
In the first test case, one optimal solution is aaaaaa.
In the second test case, the given word itself is $k$-complete.
|
t = int(input())
while t:
n, k = [int(i) for i in input().split()]
s = input()
ans = 0
reps = n // k
for i in range((k - 1) // 2 + 1):
mem = {}
for j in range(i, n, k):
if s[j] in mem:
mem[s[j]] += 1
else:
mem[s[j]] = 1
if s[k - 1 - i + k * (j // k)] in mem:
mem[s[k - 1 - i + k * (j // k)]] += 1
else:
mem[s[k - 1 - i + k * (j // k)]] = 1
change = 2 * reps - max(mem.values())
if k % 2 == 1 and i == k // 2:
change = reps - max(mem.values()) // 2
ans += change
print(ans)
t -= 1
|
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR WHILE VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR VAR FOR VAR FUNC_CALL VAR BIN_OP BIN_OP BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR DICT FOR VAR FUNC_CALL VAR VAR VAR VAR IF VAR VAR VAR VAR VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER IF VAR BIN_OP BIN_OP BIN_OP VAR NUMBER VAR BIN_OP VAR BIN_OP VAR VAR VAR VAR VAR BIN_OP BIN_OP BIN_OP VAR NUMBER VAR BIN_OP VAR BIN_OP VAR VAR NUMBER ASSIGN VAR VAR BIN_OP BIN_OP BIN_OP VAR NUMBER VAR BIN_OP VAR BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP NUMBER VAR FUNC_CALL VAR FUNC_CALL VAR IF BIN_OP VAR NUMBER NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR BIN_OP FUNC_CALL VAR FUNC_CALL VAR NUMBER VAR VAR EXPR FUNC_CALL VAR VAR VAR NUMBER
|
Word $s$ of length $n$ is called $k$-complete if $s$ is a palindrome, i.e. $s_i=s_{n+1-i}$ for all $1 \le i \le n$; $s$ has a period of $k$, i.e. $s_i=s_{k+i}$ for all $1 \le i \le n-k$.
For example, "abaaba" is a $3$-complete word, while "abccba" is not.
Bob is given a word $s$ of length $n$ consisting of only lowercase Latin letters and an integer $k$, such that $n$ is divisible by $k$. He wants to convert $s$ to any $k$-complete word.
To do this Bob can choose some $i$ ($1 \le i \le n$) and replace the letter at position $i$ with some other lowercase Latin letter.
So now Bob wants to know the minimum number of letters he has to replace to convert $s$ to any $k$-complete word.
Note that Bob can do zero changes if the word $s$ is already $k$-complete.
You are required to answer $t$ test cases independently.
-----Input-----
The first line contains a single integer $t$ ($1 \le t\le 10^5$) — the number of test cases.
The first line of each test case contains two integers $n$ and $k$ ($1 \le k < n \le 2 \cdot 10^5$, $n$ is divisible by $k$).
The second line of each test case contains a word $s$ of length $n$.
It is guaranteed that word $s$ only contains lowercase Latin letters. And it is guaranteed that the sum of $n$ over all test cases will not exceed $2 \cdot 10^5$.
-----Output-----
For each test case, output one integer, representing the minimum number of characters he has to replace to convert $s$ to any $k$-complete word.
-----Example-----
Input
4
6 2
abaaba
6 3
abaaba
36 9
hippopotomonstrosesquippedaliophobia
21 7
wudixiaoxingxingheclp
Output
2
0
23
16
-----Note-----
In the first test case, one optimal solution is aaaaaa.
In the second test case, the given word itself is $k$-complete.
|
def solve(n, k, s):
i = 0
j = k - 1
ans = 0
while i <= j:
fre = [(0) for i in range(0, 26)]
for ind in range(i, len(s), k):
fre[ord(s[ind]) - 97] += 1
if i != j:
for ind in range(j, len(s), k):
fre[ord(s[ind]) - 97] += 1
ans += sum(fre) - max(fre)
i += 1
j -= 1
print(ans)
t = int(input())
for i in range(0, t):
inp = list(map(lambda x: int(x), input().split()))
s = input()
solve(inp[0], inp[1], s)
|
FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR VAR BIN_OP FUNC_CALL VAR VAR VAR NUMBER NUMBER IF VAR VAR FOR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR VAR BIN_OP FUNC_CALL VAR VAR VAR NUMBER NUMBER VAR BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR NUMBER VAR NUMBER VAR
|
Word $s$ of length $n$ is called $k$-complete if $s$ is a palindrome, i.e. $s_i=s_{n+1-i}$ for all $1 \le i \le n$; $s$ has a period of $k$, i.e. $s_i=s_{k+i}$ for all $1 \le i \le n-k$.
For example, "abaaba" is a $3$-complete word, while "abccba" is not.
Bob is given a word $s$ of length $n$ consisting of only lowercase Latin letters and an integer $k$, such that $n$ is divisible by $k$. He wants to convert $s$ to any $k$-complete word.
To do this Bob can choose some $i$ ($1 \le i \le n$) and replace the letter at position $i$ with some other lowercase Latin letter.
So now Bob wants to know the minimum number of letters he has to replace to convert $s$ to any $k$-complete word.
Note that Bob can do zero changes if the word $s$ is already $k$-complete.
You are required to answer $t$ test cases independently.
-----Input-----
The first line contains a single integer $t$ ($1 \le t\le 10^5$) — the number of test cases.
The first line of each test case contains two integers $n$ and $k$ ($1 \le k < n \le 2 \cdot 10^5$, $n$ is divisible by $k$).
The second line of each test case contains a word $s$ of length $n$.
It is guaranteed that word $s$ only contains lowercase Latin letters. And it is guaranteed that the sum of $n$ over all test cases will not exceed $2 \cdot 10^5$.
-----Output-----
For each test case, output one integer, representing the minimum number of characters he has to replace to convert $s$ to any $k$-complete word.
-----Example-----
Input
4
6 2
abaaba
6 3
abaaba
36 9
hippopotomonstrosesquippedaliophobia
21 7
wudixiaoxingxingheclp
Output
2
0
23
16
-----Note-----
In the first test case, one optimal solution is aaaaaa.
In the second test case, the given word itself is $k$-complete.
|
t = int(input())
for _ in range(t):
n, k = list(map(int, input().split()))
s = str(input())
lis = [([0] * 26) for i in range((k + 1) // 2)]
for i in range(n):
lis[min(i % k, k - i % k - 1)][ord(s[i]) - 97] += 1
ans = 0
for i in range(k // 2):
a = max(lis[i])
b = sum(lis[i])
ans += b - a
if k % 2 == 1:
lis = [0] * 26
for i in range(n):
if i % k == k // 2:
lis[ord(s[i]) - 97] += 1
a = max(lis)
b = sum(lis)
ans += b - a
print(ans)
|
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER NUMBER VAR FUNC_CALL VAR BIN_OP BIN_OP VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR BIN_OP VAR VAR BIN_OP BIN_OP VAR BIN_OP VAR VAR NUMBER BIN_OP FUNC_CALL VAR VAR VAR NUMBER NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR BIN_OP VAR VAR IF BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP LIST NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR IF BIN_OP VAR VAR BIN_OP VAR NUMBER VAR BIN_OP FUNC_CALL VAR VAR VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR
|
Word $s$ of length $n$ is called $k$-complete if $s$ is a palindrome, i.e. $s_i=s_{n+1-i}$ for all $1 \le i \le n$; $s$ has a period of $k$, i.e. $s_i=s_{k+i}$ for all $1 \le i \le n-k$.
For example, "abaaba" is a $3$-complete word, while "abccba" is not.
Bob is given a word $s$ of length $n$ consisting of only lowercase Latin letters and an integer $k$, such that $n$ is divisible by $k$. He wants to convert $s$ to any $k$-complete word.
To do this Bob can choose some $i$ ($1 \le i \le n$) and replace the letter at position $i$ with some other lowercase Latin letter.
So now Bob wants to know the minimum number of letters he has to replace to convert $s$ to any $k$-complete word.
Note that Bob can do zero changes if the word $s$ is already $k$-complete.
You are required to answer $t$ test cases independently.
-----Input-----
The first line contains a single integer $t$ ($1 \le t\le 10^5$) — the number of test cases.
The first line of each test case contains two integers $n$ and $k$ ($1 \le k < n \le 2 \cdot 10^5$, $n$ is divisible by $k$).
The second line of each test case contains a word $s$ of length $n$.
It is guaranteed that word $s$ only contains lowercase Latin letters. And it is guaranteed that the sum of $n$ over all test cases will not exceed $2 \cdot 10^5$.
-----Output-----
For each test case, output one integer, representing the minimum number of characters he has to replace to convert $s$ to any $k$-complete word.
-----Example-----
Input
4
6 2
abaaba
6 3
abaaba
36 9
hippopotomonstrosesquippedaliophobia
21 7
wudixiaoxingxingheclp
Output
2
0
23
16
-----Note-----
In the first test case, one optimal solution is aaaaaa.
In the second test case, the given word itself is $k$-complete.
|
from sys import stdin
t = int(stdin.readline())
for _ in range(t):
n, k = map(int, stdin.readline().split())
s = stdin.readline()
su = 0
for x in range(k // 2):
d = {}
for i in range(n // k):
if s[x + k * i] in d:
d[s[x + k * i]] += 1
else:
d[s[x + k * i]] = 1
if s[k - x - 1 + k * i] in d:
d[s[k - x - 1 + k * i]] += 1
else:
d[s[k - x - 1 + k * i]] = 1
u = max(d, key=d.get)
su += int(d.get(u))
if k % 2 != 0:
d = {}
for i in range(n // k):
if s[k // 2 + k * i] in d:
d[s[k // 2 + k * i]] += 1
else:
d[s[k // 2 + k * i]] = 1
u = max(d, key=d.get)
su += int(d.get(u))
print(n - su)
|
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR DICT FOR VAR FUNC_CALL VAR BIN_OP VAR VAR IF VAR BIN_OP VAR BIN_OP VAR VAR VAR VAR VAR BIN_OP VAR BIN_OP VAR VAR NUMBER ASSIGN VAR VAR BIN_OP VAR BIN_OP VAR VAR NUMBER IF VAR BIN_OP BIN_OP BIN_OP VAR VAR NUMBER BIN_OP VAR VAR VAR VAR VAR BIN_OP BIN_OP BIN_OP VAR VAR NUMBER BIN_OP VAR VAR NUMBER ASSIGN VAR VAR BIN_OP BIN_OP BIN_OP VAR VAR NUMBER BIN_OP VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF BIN_OP VAR NUMBER NUMBER ASSIGN VAR DICT FOR VAR FUNC_CALL VAR BIN_OP VAR VAR IF VAR BIN_OP BIN_OP VAR NUMBER BIN_OP VAR VAR VAR VAR VAR BIN_OP BIN_OP VAR NUMBER BIN_OP VAR VAR NUMBER ASSIGN VAR VAR BIN_OP BIN_OP VAR NUMBER BIN_OP VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR
|
Word $s$ of length $n$ is called $k$-complete if $s$ is a palindrome, i.e. $s_i=s_{n+1-i}$ for all $1 \le i \le n$; $s$ has a period of $k$, i.e. $s_i=s_{k+i}$ for all $1 \le i \le n-k$.
For example, "abaaba" is a $3$-complete word, while "abccba" is not.
Bob is given a word $s$ of length $n$ consisting of only lowercase Latin letters and an integer $k$, such that $n$ is divisible by $k$. He wants to convert $s$ to any $k$-complete word.
To do this Bob can choose some $i$ ($1 \le i \le n$) and replace the letter at position $i$ with some other lowercase Latin letter.
So now Bob wants to know the minimum number of letters he has to replace to convert $s$ to any $k$-complete word.
Note that Bob can do zero changes if the word $s$ is already $k$-complete.
You are required to answer $t$ test cases independently.
-----Input-----
The first line contains a single integer $t$ ($1 \le t\le 10^5$) — the number of test cases.
The first line of each test case contains two integers $n$ and $k$ ($1 \le k < n \le 2 \cdot 10^5$, $n$ is divisible by $k$).
The second line of each test case contains a word $s$ of length $n$.
It is guaranteed that word $s$ only contains lowercase Latin letters. And it is guaranteed that the sum of $n$ over all test cases will not exceed $2 \cdot 10^5$.
-----Output-----
For each test case, output one integer, representing the minimum number of characters he has to replace to convert $s$ to any $k$-complete word.
-----Example-----
Input
4
6 2
abaaba
6 3
abaaba
36 9
hippopotomonstrosesquippedaliophobia
21 7
wudixiaoxingxingheclp
Output
2
0
23
16
-----Note-----
In the first test case, one optimal solution is aaaaaa.
In the second test case, the given word itself is $k$-complete.
|
for _ in range(int(input())):
n, k = tuple(map(int, input().split()))
s = input()
dicts = [{} for i in range((k - 1) // 2 + 1)]
for num, mydict in enumerate(dicts):
for i in range(num, len(s), k):
if s[i] not in mydict:
mydict[s[i]] = 0
mydict[s[i]] += 1
if num == len(dicts) - 1 and k % 2 == 1:
break
for i in range(k - num - 1, len(s), k):
if s[i] not in mydict:
mydict[s[i]] = 0
mydict[s[i]] += 1
ans = 0
for mydict in dicts:
values = list(mydict.values())
ans += sum(values) - max(values)
print(ans)
|
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR DICT VAR FUNC_CALL VAR BIN_OP BIN_OP BIN_OP VAR NUMBER NUMBER NUMBER FOR VAR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR IF VAR VAR VAR ASSIGN VAR VAR VAR NUMBER VAR VAR VAR NUMBER IF VAR BIN_OP FUNC_CALL VAR VAR NUMBER BIN_OP VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR NUMBER FUNC_CALL VAR VAR VAR IF VAR VAR VAR ASSIGN VAR VAR VAR NUMBER VAR VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR
|
Word $s$ of length $n$ is called $k$-complete if $s$ is a palindrome, i.e. $s_i=s_{n+1-i}$ for all $1 \le i \le n$; $s$ has a period of $k$, i.e. $s_i=s_{k+i}$ for all $1 \le i \le n-k$.
For example, "abaaba" is a $3$-complete word, while "abccba" is not.
Bob is given a word $s$ of length $n$ consisting of only lowercase Latin letters and an integer $k$, such that $n$ is divisible by $k$. He wants to convert $s$ to any $k$-complete word.
To do this Bob can choose some $i$ ($1 \le i \le n$) and replace the letter at position $i$ with some other lowercase Latin letter.
So now Bob wants to know the minimum number of letters he has to replace to convert $s$ to any $k$-complete word.
Note that Bob can do zero changes if the word $s$ is already $k$-complete.
You are required to answer $t$ test cases independently.
-----Input-----
The first line contains a single integer $t$ ($1 \le t\le 10^5$) — the number of test cases.
The first line of each test case contains two integers $n$ and $k$ ($1 \le k < n \le 2 \cdot 10^5$, $n$ is divisible by $k$).
The second line of each test case contains a word $s$ of length $n$.
It is guaranteed that word $s$ only contains lowercase Latin letters. And it is guaranteed that the sum of $n$ over all test cases will not exceed $2 \cdot 10^5$.
-----Output-----
For each test case, output one integer, representing the minimum number of characters he has to replace to convert $s$ to any $k$-complete word.
-----Example-----
Input
4
6 2
abaaba
6 3
abaaba
36 9
hippopotomonstrosesquippedaliophobia
21 7
wudixiaoxingxingheclp
Output
2
0
23
16
-----Note-----
In the first test case, one optimal solution is aaaaaa.
In the second test case, the given word itself is $k$-complete.
|
for krya in range(int(input())):
n, k = map(int, input().split())
st = input()
ans = 0
stn = []
for i in range(k):
stn.append(st[i::k])
newlst = []
for i in range(k // 2):
newlst.append(stn[i] + stn[-1 - i])
if k % 2 == 1:
newlst.append(stn[k // 2])
for i in newlst:
bukvi = [0] * 26
num = 0
for j in list(i):
num += 1
bukvi[ord(str(j)) - ord("a")] += 1
ans += num - max(bukvi)
print(ans)
|
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR VAR VAR BIN_OP NUMBER VAR IF BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER FOR VAR VAR ASSIGN VAR BIN_OP LIST NUMBER NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR NUMBER VAR BIN_OP FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL VAR STRING NUMBER VAR BIN_OP VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR
|
Word $s$ of length $n$ is called $k$-complete if $s$ is a palindrome, i.e. $s_i=s_{n+1-i}$ for all $1 \le i \le n$; $s$ has a period of $k$, i.e. $s_i=s_{k+i}$ for all $1 \le i \le n-k$.
For example, "abaaba" is a $3$-complete word, while "abccba" is not.
Bob is given a word $s$ of length $n$ consisting of only lowercase Latin letters and an integer $k$, such that $n$ is divisible by $k$. He wants to convert $s$ to any $k$-complete word.
To do this Bob can choose some $i$ ($1 \le i \le n$) and replace the letter at position $i$ with some other lowercase Latin letter.
So now Bob wants to know the minimum number of letters he has to replace to convert $s$ to any $k$-complete word.
Note that Bob can do zero changes if the word $s$ is already $k$-complete.
You are required to answer $t$ test cases independently.
-----Input-----
The first line contains a single integer $t$ ($1 \le t\le 10^5$) — the number of test cases.
The first line of each test case contains two integers $n$ and $k$ ($1 \le k < n \le 2 \cdot 10^5$, $n$ is divisible by $k$).
The second line of each test case contains a word $s$ of length $n$.
It is guaranteed that word $s$ only contains lowercase Latin letters. And it is guaranteed that the sum of $n$ over all test cases will not exceed $2 \cdot 10^5$.
-----Output-----
For each test case, output one integer, representing the minimum number of characters he has to replace to convert $s$ to any $k$-complete word.
-----Example-----
Input
4
6 2
abaaba
6 3
abaaba
36 9
hippopotomonstrosesquippedaliophobia
21 7
wudixiaoxingxingheclp
Output
2
0
23
16
-----Note-----
In the first test case, one optimal solution is aaaaaa.
In the second test case, the given word itself is $k$-complete.
|
tt = int(input())
while tt != 0:
n, k = map(int, input().split())
s = list(input())
ans = 0
p = n // k
t = -1
if k % 2 == 0:
t = k // 2
else:
t = k // 2 + 1
for i in range(t):
temp = [0] * 26
for j in range(n // k):
if s[j * k + i] == s[j * k + k - i - 1]:
if j * k + i == j * k + k - i - 1:
temp[ord(s[j * k + i]) - ord("a")] += 1
else:
temp[ord(s[j * k + i]) - ord("a")] += 2
else:
temp[ord(s[j * k + i]) - ord("a")] += 1
temp[ord(s[j * k + k - i - 1]) - ord("a")] += 1
ans += sum(temp) - max(temp)
print(ans)
tt -= 1
|
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR WHILE VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR NUMBER IF BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR VAR IF VAR BIN_OP BIN_OP VAR VAR VAR VAR BIN_OP BIN_OP BIN_OP BIN_OP VAR VAR VAR VAR NUMBER IF BIN_OP BIN_OP VAR VAR VAR BIN_OP BIN_OP BIN_OP BIN_OP VAR VAR VAR VAR NUMBER VAR BIN_OP FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR VAR FUNC_CALL VAR STRING NUMBER VAR BIN_OP FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR VAR FUNC_CALL VAR STRING NUMBER VAR BIN_OP FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR VAR FUNC_CALL VAR STRING NUMBER VAR BIN_OP FUNC_CALL VAR VAR BIN_OP BIN_OP BIN_OP BIN_OP VAR VAR VAR VAR NUMBER FUNC_CALL VAR STRING NUMBER VAR BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR NUMBER
|
Word $s$ of length $n$ is called $k$-complete if $s$ is a palindrome, i.e. $s_i=s_{n+1-i}$ for all $1 \le i \le n$; $s$ has a period of $k$, i.e. $s_i=s_{k+i}$ for all $1 \le i \le n-k$.
For example, "abaaba" is a $3$-complete word, while "abccba" is not.
Bob is given a word $s$ of length $n$ consisting of only lowercase Latin letters and an integer $k$, such that $n$ is divisible by $k$. He wants to convert $s$ to any $k$-complete word.
To do this Bob can choose some $i$ ($1 \le i \le n$) and replace the letter at position $i$ with some other lowercase Latin letter.
So now Bob wants to know the minimum number of letters he has to replace to convert $s$ to any $k$-complete word.
Note that Bob can do zero changes if the word $s$ is already $k$-complete.
You are required to answer $t$ test cases independently.
-----Input-----
The first line contains a single integer $t$ ($1 \le t\le 10^5$) — the number of test cases.
The first line of each test case contains two integers $n$ and $k$ ($1 \le k < n \le 2 \cdot 10^5$, $n$ is divisible by $k$).
The second line of each test case contains a word $s$ of length $n$.
It is guaranteed that word $s$ only contains lowercase Latin letters. And it is guaranteed that the sum of $n$ over all test cases will not exceed $2 \cdot 10^5$.
-----Output-----
For each test case, output one integer, representing the minimum number of characters he has to replace to convert $s$ to any $k$-complete word.
-----Example-----
Input
4
6 2
abaaba
6 3
abaaba
36 9
hippopotomonstrosesquippedaliophobia
21 7
wudixiaoxingxingheclp
Output
2
0
23
16
-----Note-----
In the first test case, one optimal solution is aaaaaa.
In the second test case, the given word itself is $k$-complete.
|
for _ in range(int(input())):
n, k = map(int, input().split())
s = input()
a = 0
for i in range(0, k):
b = [0] * 26
for j in range(i, n, k):
b[ord(s[j]) - 97] += 1
b[ord(s[k - 1 - j]) - 97] += 1
a += sum(b) - max(b)
print(a // 2)
|
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR BIN_OP LIST NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR VAR VAR VAR BIN_OP FUNC_CALL VAR VAR VAR NUMBER NUMBER VAR BIN_OP FUNC_CALL VAR VAR BIN_OP BIN_OP VAR NUMBER VAR NUMBER NUMBER VAR BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER
|
Word $s$ of length $n$ is called $k$-complete if $s$ is a palindrome, i.e. $s_i=s_{n+1-i}$ for all $1 \le i \le n$; $s$ has a period of $k$, i.e. $s_i=s_{k+i}$ for all $1 \le i \le n-k$.
For example, "abaaba" is a $3$-complete word, while "abccba" is not.
Bob is given a word $s$ of length $n$ consisting of only lowercase Latin letters and an integer $k$, such that $n$ is divisible by $k$. He wants to convert $s$ to any $k$-complete word.
To do this Bob can choose some $i$ ($1 \le i \le n$) and replace the letter at position $i$ with some other lowercase Latin letter.
So now Bob wants to know the minimum number of letters he has to replace to convert $s$ to any $k$-complete word.
Note that Bob can do zero changes if the word $s$ is already $k$-complete.
You are required to answer $t$ test cases independently.
-----Input-----
The first line contains a single integer $t$ ($1 \le t\le 10^5$) — the number of test cases.
The first line of each test case contains two integers $n$ and $k$ ($1 \le k < n \le 2 \cdot 10^5$, $n$ is divisible by $k$).
The second line of each test case contains a word $s$ of length $n$.
It is guaranteed that word $s$ only contains lowercase Latin letters. And it is guaranteed that the sum of $n$ over all test cases will not exceed $2 \cdot 10^5$.
-----Output-----
For each test case, output one integer, representing the minimum number of characters he has to replace to convert $s$ to any $k$-complete word.
-----Example-----
Input
4
6 2
abaaba
6 3
abaaba
36 9
hippopotomonstrosesquippedaliophobia
21 7
wudixiaoxingxingheclp
Output
2
0
23
16
-----Note-----
In the first test case, one optimal solution is aaaaaa.
In the second test case, the given word itself is $k$-complete.
|
from itertools import groupby
for _ in range(int(input())):
n, k = map(int, input().split())
s = input()
x = n // k
c = 0
if k % 2 != 0:
k2 = k // 2 + 1
else:
k2 = k // 2
for i in range(k2):
l = []
l1 = []
for j in range(0, x):
if i != k // 2:
y = j * k + i
y1 = j * k + k - i - 1
l.append(s[y])
l.append(s[y1])
l1.append(y)
l1.append(y1)
else:
y = j * k + i
l.append(s[y])
l1.append(y)
l.sort()
m = 0
for key, group in groupby(l):
k1 = len(list(group))
if k1 > m:
m = k1
c = c + len(l) - m
print(c)
|
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR NUMBER IF BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR LIST ASSIGN VAR LIST FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP BIN_OP VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR
|
Word $s$ of length $n$ is called $k$-complete if $s$ is a palindrome, i.e. $s_i=s_{n+1-i}$ for all $1 \le i \le n$; $s$ has a period of $k$, i.e. $s_i=s_{k+i}$ for all $1 \le i \le n-k$.
For example, "abaaba" is a $3$-complete word, while "abccba" is not.
Bob is given a word $s$ of length $n$ consisting of only lowercase Latin letters and an integer $k$, such that $n$ is divisible by $k$. He wants to convert $s$ to any $k$-complete word.
To do this Bob can choose some $i$ ($1 \le i \le n$) and replace the letter at position $i$ with some other lowercase Latin letter.
So now Bob wants to know the minimum number of letters he has to replace to convert $s$ to any $k$-complete word.
Note that Bob can do zero changes if the word $s$ is already $k$-complete.
You are required to answer $t$ test cases independently.
-----Input-----
The first line contains a single integer $t$ ($1 \le t\le 10^5$) — the number of test cases.
The first line of each test case contains two integers $n$ and $k$ ($1 \le k < n \le 2 \cdot 10^5$, $n$ is divisible by $k$).
The second line of each test case contains a word $s$ of length $n$.
It is guaranteed that word $s$ only contains lowercase Latin letters. And it is guaranteed that the sum of $n$ over all test cases will not exceed $2 \cdot 10^5$.
-----Output-----
For each test case, output one integer, representing the minimum number of characters he has to replace to convert $s$ to any $k$-complete word.
-----Example-----
Input
4
6 2
abaaba
6 3
abaaba
36 9
hippopotomonstrosesquippedaliophobia
21 7
wudixiaoxingxingheclp
Output
2
0
23
16
-----Note-----
In the first test case, one optimal solution is aaaaaa.
In the second test case, the given word itself is $k$-complete.
|
import sys
def answer(n, k, s):
odd = False
if k % 2 == 1:
l = k // 2 + 1
odd = True
else:
l = k // 2
base = ord("a")
cnt = [[(0) for j in range(26)] for i in range(l)]
for i in range(n):
c_num = i % k
if c_num >= l:
c_num = k - (c_num + 1)
offset = ord(s[i]) - base
cnt[c_num][offset] += 1
score = 0
num_times = n // k
for i in range(l):
if odd and i == l - 1:
score += num_times - max(cnt[i])
else:
score += 2 * num_times - max(cnt[i])
return score
def main():
t = int(sys.stdin.readline())
while t:
n, k = map(int, sys.stdin.readline().split())
s = sys.stdin.readline().rstrip()
print(answer(n, k, s))
t -= 1
return
main()
|
IMPORT FUNC_DEF ASSIGN VAR NUMBER IF BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP BIN_OP VAR NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR STRING ASSIGN VAR NUMBER VAR FUNC_CALL VAR NUMBER VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR VAR IF VAR VAR ASSIGN VAR BIN_OP VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR VAR VAR VAR VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR FUNC_CALL VAR VAR VAR VAR BIN_OP BIN_OP NUMBER VAR FUNC_CALL VAR VAR VAR RETURN VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR WHILE VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR VAR NUMBER RETURN EXPR FUNC_CALL VAR
|
Word $s$ of length $n$ is called $k$-complete if $s$ is a palindrome, i.e. $s_i=s_{n+1-i}$ for all $1 \le i \le n$; $s$ has a period of $k$, i.e. $s_i=s_{k+i}$ for all $1 \le i \le n-k$.
For example, "abaaba" is a $3$-complete word, while "abccba" is not.
Bob is given a word $s$ of length $n$ consisting of only lowercase Latin letters and an integer $k$, such that $n$ is divisible by $k$. He wants to convert $s$ to any $k$-complete word.
To do this Bob can choose some $i$ ($1 \le i \le n$) and replace the letter at position $i$ with some other lowercase Latin letter.
So now Bob wants to know the minimum number of letters he has to replace to convert $s$ to any $k$-complete word.
Note that Bob can do zero changes if the word $s$ is already $k$-complete.
You are required to answer $t$ test cases independently.
-----Input-----
The first line contains a single integer $t$ ($1 \le t\le 10^5$) — the number of test cases.
The first line of each test case contains two integers $n$ and $k$ ($1 \le k < n \le 2 \cdot 10^5$, $n$ is divisible by $k$).
The second line of each test case contains a word $s$ of length $n$.
It is guaranteed that word $s$ only contains lowercase Latin letters. And it is guaranteed that the sum of $n$ over all test cases will not exceed $2 \cdot 10^5$.
-----Output-----
For each test case, output one integer, representing the minimum number of characters he has to replace to convert $s$ to any $k$-complete word.
-----Example-----
Input
4
6 2
abaaba
6 3
abaaba
36 9
hippopotomonstrosesquippedaliophobia
21 7
wudixiaoxingxingheclp
Output
2
0
23
16
-----Note-----
In the first test case, one optimal solution is aaaaaa.
In the second test case, the given word itself is $k$-complete.
|
a = int(input())
for _ in range(a):
b, c = map(int, input().split())
d = input()
ans = 0
for i in range(c // 2):
now = i
data = {}
while now < b:
if d[now] not in data:
data[d[now]] = 0
data[d[now]] += 1
now += c
now = c - i - 1
while now < b:
if d[now] not in data:
data[d[now]] = 0
data[d[now]] += 1
now += c
ans += max(data.values())
if c % 2 == 1:
now = c // 2
data = {}
while now < b:
if d[now] not in data:
data[d[now]] = 0
data[d[now]] += 1
now += c
ans += max(data.values())
print(b - ans)
|
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR VAR ASSIGN VAR DICT WHILE VAR VAR IF VAR VAR VAR ASSIGN VAR VAR VAR NUMBER VAR VAR VAR NUMBER VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER WHILE VAR VAR IF VAR VAR VAR ASSIGN VAR VAR VAR NUMBER VAR VAR VAR NUMBER VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR IF BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR DICT WHILE VAR VAR IF VAR VAR VAR ASSIGN VAR VAR VAR NUMBER VAR VAR VAR NUMBER VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR
|
Word $s$ of length $n$ is called $k$-complete if $s$ is a palindrome, i.e. $s_i=s_{n+1-i}$ for all $1 \le i \le n$; $s$ has a period of $k$, i.e. $s_i=s_{k+i}$ for all $1 \le i \le n-k$.
For example, "abaaba" is a $3$-complete word, while "abccba" is not.
Bob is given a word $s$ of length $n$ consisting of only lowercase Latin letters and an integer $k$, such that $n$ is divisible by $k$. He wants to convert $s$ to any $k$-complete word.
To do this Bob can choose some $i$ ($1 \le i \le n$) and replace the letter at position $i$ with some other lowercase Latin letter.
So now Bob wants to know the minimum number of letters he has to replace to convert $s$ to any $k$-complete word.
Note that Bob can do zero changes if the word $s$ is already $k$-complete.
You are required to answer $t$ test cases independently.
-----Input-----
The first line contains a single integer $t$ ($1 \le t\le 10^5$) — the number of test cases.
The first line of each test case contains two integers $n$ and $k$ ($1 \le k < n \le 2 \cdot 10^5$, $n$ is divisible by $k$).
The second line of each test case contains a word $s$ of length $n$.
It is guaranteed that word $s$ only contains lowercase Latin letters. And it is guaranteed that the sum of $n$ over all test cases will not exceed $2 \cdot 10^5$.
-----Output-----
For each test case, output one integer, representing the minimum number of characters he has to replace to convert $s$ to any $k$-complete word.
-----Example-----
Input
4
6 2
abaaba
6 3
abaaba
36 9
hippopotomonstrosesquippedaliophobia
21 7
wudixiaoxingxingheclp
Output
2
0
23
16
-----Note-----
In the first test case, one optimal solution is aaaaaa.
In the second test case, the given word itself is $k$-complete.
|
import sys
ne = [[]]
word = []
def dfs(v, fd):
dic = [0] * 26
st = [v]
fd[v] = 1
dic[word[v]] = 1
while st:
h = st.pop()
for nei in ne[h]:
if not fd[nei]:
fd[nei] = 1
dic[word[nei]] += 1
st.append(nei)
return sum(dic) - max(dic)
for q in range(int(sys.stdin.readline())):
n, k = [int(i) for i in sys.stdin.readline().split()]
oa = ord("a")
word = [(ord(o) - oa) for o in sys.stdin.readline().strip()]
ne = [[] for i in range(n)]
for i in range(n):
if i >= k:
ne[i].append(i - k)
if i + k < n:
ne[i].append(i + k)
if 2 * i + 1 != n:
ne[i].append(n - 1 - i)
fnd = [0] * n
ans = 0
for i in range(n):
if not fnd[i]:
ans += dfs(i, fnd)
sys.stdout.write(str(ans) + "\n")
|
IMPORT ASSIGN VAR LIST LIST ASSIGN VAR LIST FUNC_DEF ASSIGN VAR BIN_OP LIST NUMBER NUMBER ASSIGN VAR LIST VAR ASSIGN VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER WHILE VAR ASSIGN VAR FUNC_CALL VAR FOR VAR VAR VAR IF VAR VAR ASSIGN VAR VAR NUMBER VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR RETURN BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR STRING ASSIGN VAR BIN_OP FUNC_CALL VAR VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR EXPR FUNC_CALL VAR VAR BIN_OP VAR VAR IF BIN_OP VAR VAR VAR EXPR FUNC_CALL VAR VAR BIN_OP VAR VAR IF BIN_OP BIN_OP NUMBER VAR NUMBER VAR EXPR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR NUMBER VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR STRING
|
Word $s$ of length $n$ is called $k$-complete if $s$ is a palindrome, i.e. $s_i=s_{n+1-i}$ for all $1 \le i \le n$; $s$ has a period of $k$, i.e. $s_i=s_{k+i}$ for all $1 \le i \le n-k$.
For example, "abaaba" is a $3$-complete word, while "abccba" is not.
Bob is given a word $s$ of length $n$ consisting of only lowercase Latin letters and an integer $k$, such that $n$ is divisible by $k$. He wants to convert $s$ to any $k$-complete word.
To do this Bob can choose some $i$ ($1 \le i \le n$) and replace the letter at position $i$ with some other lowercase Latin letter.
So now Bob wants to know the minimum number of letters he has to replace to convert $s$ to any $k$-complete word.
Note that Bob can do zero changes if the word $s$ is already $k$-complete.
You are required to answer $t$ test cases independently.
-----Input-----
The first line contains a single integer $t$ ($1 \le t\le 10^5$) — the number of test cases.
The first line of each test case contains two integers $n$ and $k$ ($1 \le k < n \le 2 \cdot 10^5$, $n$ is divisible by $k$).
The second line of each test case contains a word $s$ of length $n$.
It is guaranteed that word $s$ only contains lowercase Latin letters. And it is guaranteed that the sum of $n$ over all test cases will not exceed $2 \cdot 10^5$.
-----Output-----
For each test case, output one integer, representing the minimum number of characters he has to replace to convert $s$ to any $k$-complete word.
-----Example-----
Input
4
6 2
abaaba
6 3
abaaba
36 9
hippopotomonstrosesquippedaliophobia
21 7
wudixiaoxingxingheclp
Output
2
0
23
16
-----Note-----
In the first test case, one optimal solution is aaaaaa.
In the second test case, the given word itself is $k$-complete.
|
import sys
input = lambda: sys.stdin.readline().rstrip()
def calc(n, k, A):
X = [([0] * 26) for _ in range((k + 1) // 2)]
for i, a in enumerate(A):
j = i % k
j = min(j, k - 1 - j)
X[j][a] += 1
return sum([(sum(x) - max(x)) for x in X])
T = int(input())
for _ in range(T):
N, K = list(map(int, input().split()))
S = [(ord(a) - 97) for a in input()]
print(calc(N, K, S))
|
IMPORT ASSIGN VAR FUNC_CALL FUNC_CALL VAR FUNC_DEF ASSIGN VAR BIN_OP LIST NUMBER NUMBER VAR FUNC_CALL VAR BIN_OP BIN_OP VAR NUMBER NUMBER FOR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR NUMBER VAR VAR VAR VAR NUMBER RETURN FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER VAR FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR
|
Word $s$ of length $n$ is called $k$-complete if $s$ is a palindrome, i.e. $s_i=s_{n+1-i}$ for all $1 \le i \le n$; $s$ has a period of $k$, i.e. $s_i=s_{k+i}$ for all $1 \le i \le n-k$.
For example, "abaaba" is a $3$-complete word, while "abccba" is not.
Bob is given a word $s$ of length $n$ consisting of only lowercase Latin letters and an integer $k$, such that $n$ is divisible by $k$. He wants to convert $s$ to any $k$-complete word.
To do this Bob can choose some $i$ ($1 \le i \le n$) and replace the letter at position $i$ with some other lowercase Latin letter.
So now Bob wants to know the minimum number of letters he has to replace to convert $s$ to any $k$-complete word.
Note that Bob can do zero changes if the word $s$ is already $k$-complete.
You are required to answer $t$ test cases independently.
-----Input-----
The first line contains a single integer $t$ ($1 \le t\le 10^5$) — the number of test cases.
The first line of each test case contains two integers $n$ and $k$ ($1 \le k < n \le 2 \cdot 10^5$, $n$ is divisible by $k$).
The second line of each test case contains a word $s$ of length $n$.
It is guaranteed that word $s$ only contains lowercase Latin letters. And it is guaranteed that the sum of $n$ over all test cases will not exceed $2 \cdot 10^5$.
-----Output-----
For each test case, output one integer, representing the minimum number of characters he has to replace to convert $s$ to any $k$-complete word.
-----Example-----
Input
4
6 2
abaaba
6 3
abaaba
36 9
hippopotomonstrosesquippedaliophobia
21 7
wudixiaoxingxingheclp
Output
2
0
23
16
-----Note-----
In the first test case, one optimal solution is aaaaaa.
In the second test case, the given word itself is $k$-complete.
|
for test in range(int(input())):
n, k = list(map(int, input().split()))
s = input()
ans = 0
for i in range(k // 2):
cnt = 0
d = dict()
maxn = 0
for j in range(i, n, k):
cnt += 2
if s[j] not in d:
d[s[j]] = 1
else:
d[s[j]] += 1
if d[s[j]] > maxn:
maxn = d[s[j]]
j2 = (j // k + 1) * k - 1 - i
if s[j2] not in d:
d[s[j2]] = 1
else:
d[s[j2]] += 1
if d[s[j2]] > maxn:
maxn = d[s[j2]]
ans += cnt - maxn
if k % 2 == 1:
cnt = 0
d = dict()
maxn = 0
for j in range(k // 2, n, k):
cnt += 1
if s[j] not in d:
d[s[j]] = 1
else:
d[s[j]] += 1
if d[s[j]] > maxn:
maxn = d[s[j]]
ans += cnt - maxn
print(ans)
|
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR VAR VAR NUMBER IF VAR VAR VAR ASSIGN VAR VAR VAR NUMBER VAR VAR VAR NUMBER IF VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP BIN_OP BIN_OP VAR VAR NUMBER VAR NUMBER VAR IF VAR VAR VAR ASSIGN VAR VAR VAR NUMBER VAR VAR VAR NUMBER IF VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR BIN_OP VAR VAR IF BIN_OP VAR NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR VAR NUMBER IF VAR VAR VAR ASSIGN VAR VAR VAR NUMBER VAR VAR VAR NUMBER IF VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR
|
Word $s$ of length $n$ is called $k$-complete if $s$ is a palindrome, i.e. $s_i=s_{n+1-i}$ for all $1 \le i \le n$; $s$ has a period of $k$, i.e. $s_i=s_{k+i}$ for all $1 \le i \le n-k$.
For example, "abaaba" is a $3$-complete word, while "abccba" is not.
Bob is given a word $s$ of length $n$ consisting of only lowercase Latin letters and an integer $k$, such that $n$ is divisible by $k$. He wants to convert $s$ to any $k$-complete word.
To do this Bob can choose some $i$ ($1 \le i \le n$) and replace the letter at position $i$ with some other lowercase Latin letter.
So now Bob wants to know the minimum number of letters he has to replace to convert $s$ to any $k$-complete word.
Note that Bob can do zero changes if the word $s$ is already $k$-complete.
You are required to answer $t$ test cases independently.
-----Input-----
The first line contains a single integer $t$ ($1 \le t\le 10^5$) — the number of test cases.
The first line of each test case contains two integers $n$ and $k$ ($1 \le k < n \le 2 \cdot 10^5$, $n$ is divisible by $k$).
The second line of each test case contains a word $s$ of length $n$.
It is guaranteed that word $s$ only contains lowercase Latin letters. And it is guaranteed that the sum of $n$ over all test cases will not exceed $2 \cdot 10^5$.
-----Output-----
For each test case, output one integer, representing the minimum number of characters he has to replace to convert $s$ to any $k$-complete word.
-----Example-----
Input
4
6 2
abaaba
6 3
abaaba
36 9
hippopotomonstrosesquippedaliophobia
21 7
wudixiaoxingxingheclp
Output
2
0
23
16
-----Note-----
In the first test case, one optimal solution is aaaaaa.
In the second test case, the given word itself is $k$-complete.
|
t = int(input())
while t > 0:
n, k = map(int, input().split())
s = input()
count = 0
no = n // k
for i in range(0, k // 2):
l = []
for x in range(26):
l.append(0)
for j in range(no):
l[ord(s[j * k + i]) - 97] += 1
l[ord(s[k * (j + 1) - 1 - i]) - 97] += 1
ns = max(l)
count = count + 2 * no - ns
if k % 2 == 1:
i = k // 2
l = []
for x in range(26):
l.append(0)
for j in range(no):
l[ord(s[j * k + i]) - 97] += 1
ns = max(l)
count = count + no - ns
print(count)
t -= 1
|
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR WHILE VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR BIN_OP FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR VAR NUMBER NUMBER VAR BIN_OP FUNC_CALL VAR VAR BIN_OP BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR BIN_OP NUMBER VAR VAR IF BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR BIN_OP FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR NUMBER
|
Word $s$ of length $n$ is called $k$-complete if $s$ is a palindrome, i.e. $s_i=s_{n+1-i}$ for all $1 \le i \le n$; $s$ has a period of $k$, i.e. $s_i=s_{k+i}$ for all $1 \le i \le n-k$.
For example, "abaaba" is a $3$-complete word, while "abccba" is not.
Bob is given a word $s$ of length $n$ consisting of only lowercase Latin letters and an integer $k$, such that $n$ is divisible by $k$. He wants to convert $s$ to any $k$-complete word.
To do this Bob can choose some $i$ ($1 \le i \le n$) and replace the letter at position $i$ with some other lowercase Latin letter.
So now Bob wants to know the minimum number of letters he has to replace to convert $s$ to any $k$-complete word.
Note that Bob can do zero changes if the word $s$ is already $k$-complete.
You are required to answer $t$ test cases independently.
-----Input-----
The first line contains a single integer $t$ ($1 \le t\le 10^5$) — the number of test cases.
The first line of each test case contains two integers $n$ and $k$ ($1 \le k < n \le 2 \cdot 10^5$, $n$ is divisible by $k$).
The second line of each test case contains a word $s$ of length $n$.
It is guaranteed that word $s$ only contains lowercase Latin letters. And it is guaranteed that the sum of $n$ over all test cases will not exceed $2 \cdot 10^5$.
-----Output-----
For each test case, output one integer, representing the minimum number of characters he has to replace to convert $s$ to any $k$-complete word.
-----Example-----
Input
4
6 2
abaaba
6 3
abaaba
36 9
hippopotomonstrosesquippedaliophobia
21 7
wudixiaoxingxingheclp
Output
2
0
23
16
-----Note-----
In the first test case, one optimal solution is aaaaaa.
In the second test case, the given word itself is $k$-complete.
|
for i in range(int(input())):
n, k = map(int, input().split())
t = input()
ans = 0
for i in range(k // 2):
b = {}
h = i
while h < n:
if t[h] not in b:
b[t[h]] = 1
else:
b[t[h]] += 1
if t[k - h - 1] not in b:
b[t[k - h - 1]] = 1
else:
b[t[k - h - 1]] += 1
h += k
b = list(b.values())
ans += sum(b) - max(b)
if k % 2:
u = {}
i = k // 2
while i < n:
if t[i] not in u:
u[t[i]] = 1
else:
u[t[i]] += 1
i += k
h = list(u.values())
ans += sum(h) - max(h)
print(ans)
|
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR DICT ASSIGN VAR VAR WHILE VAR VAR IF VAR VAR VAR ASSIGN VAR VAR VAR NUMBER VAR VAR VAR NUMBER IF VAR BIN_OP BIN_OP VAR VAR NUMBER VAR ASSIGN VAR VAR BIN_OP BIN_OP VAR VAR NUMBER NUMBER VAR VAR BIN_OP BIN_OP VAR VAR NUMBER NUMBER VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR VAR IF BIN_OP VAR NUMBER ASSIGN VAR DICT ASSIGN VAR BIN_OP VAR NUMBER WHILE VAR VAR IF VAR VAR VAR ASSIGN VAR VAR VAR NUMBER VAR VAR VAR NUMBER VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR
|
Word $s$ of length $n$ is called $k$-complete if $s$ is a palindrome, i.e. $s_i=s_{n+1-i}$ for all $1 \le i \le n$; $s$ has a period of $k$, i.e. $s_i=s_{k+i}$ for all $1 \le i \le n-k$.
For example, "abaaba" is a $3$-complete word, while "abccba" is not.
Bob is given a word $s$ of length $n$ consisting of only lowercase Latin letters and an integer $k$, such that $n$ is divisible by $k$. He wants to convert $s$ to any $k$-complete word.
To do this Bob can choose some $i$ ($1 \le i \le n$) and replace the letter at position $i$ with some other lowercase Latin letter.
So now Bob wants to know the minimum number of letters he has to replace to convert $s$ to any $k$-complete word.
Note that Bob can do zero changes if the word $s$ is already $k$-complete.
You are required to answer $t$ test cases independently.
-----Input-----
The first line contains a single integer $t$ ($1 \le t\le 10^5$) — the number of test cases.
The first line of each test case contains two integers $n$ and $k$ ($1 \le k < n \le 2 \cdot 10^5$, $n$ is divisible by $k$).
The second line of each test case contains a word $s$ of length $n$.
It is guaranteed that word $s$ only contains lowercase Latin letters. And it is guaranteed that the sum of $n$ over all test cases will not exceed $2 \cdot 10^5$.
-----Output-----
For each test case, output one integer, representing the minimum number of characters he has to replace to convert $s$ to any $k$-complete word.
-----Example-----
Input
4
6 2
abaaba
6 3
abaaba
36 9
hippopotomonstrosesquippedaliophobia
21 7
wudixiaoxingxingheclp
Output
2
0
23
16
-----Note-----
In the first test case, one optimal solution is aaaaaa.
In the second test case, the given word itself is $k$-complete.
|
import sys
input = sys.stdin.readline
t = int(input())
for _ in range(t):
n, k = map(int, input().split())
s = input().rstrip()
ans = 0
for i in range(k // 2):
alph = [0] * 26
for j in range(n // k):
alph[ord(s[j * k + i]) - ord("a")] += 1
alph[ord(s[j * k + k - 1 - i]) - ord("a")] += 1
ans += sum(alph) - max(alph)
if k % 2:
alph = [0] * 26
for j in range(n // k):
alph[ord(s[j * k + k // 2]) - ord("a")] += 1
ans += sum(alph) - max(alph)
print(ans)
|
IMPORT ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR VAR VAR BIN_OP FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR VAR FUNC_CALL VAR STRING NUMBER VAR BIN_OP FUNC_CALL VAR VAR BIN_OP BIN_OP BIN_OP BIN_OP VAR VAR VAR NUMBER VAR FUNC_CALL VAR STRING NUMBER VAR BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR VAR IF BIN_OP VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR VAR VAR BIN_OP FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR BIN_OP VAR NUMBER FUNC_CALL VAR STRING NUMBER VAR BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR
|
Word $s$ of length $n$ is called $k$-complete if $s$ is a palindrome, i.e. $s_i=s_{n+1-i}$ for all $1 \le i \le n$; $s$ has a period of $k$, i.e. $s_i=s_{k+i}$ for all $1 \le i \le n-k$.
For example, "abaaba" is a $3$-complete word, while "abccba" is not.
Bob is given a word $s$ of length $n$ consisting of only lowercase Latin letters and an integer $k$, such that $n$ is divisible by $k$. He wants to convert $s$ to any $k$-complete word.
To do this Bob can choose some $i$ ($1 \le i \le n$) and replace the letter at position $i$ with some other lowercase Latin letter.
So now Bob wants to know the minimum number of letters he has to replace to convert $s$ to any $k$-complete word.
Note that Bob can do zero changes if the word $s$ is already $k$-complete.
You are required to answer $t$ test cases independently.
-----Input-----
The first line contains a single integer $t$ ($1 \le t\le 10^5$) — the number of test cases.
The first line of each test case contains two integers $n$ and $k$ ($1 \le k < n \le 2 \cdot 10^5$, $n$ is divisible by $k$).
The second line of each test case contains a word $s$ of length $n$.
It is guaranteed that word $s$ only contains lowercase Latin letters. And it is guaranteed that the sum of $n$ over all test cases will not exceed $2 \cdot 10^5$.
-----Output-----
For each test case, output one integer, representing the minimum number of characters he has to replace to convert $s$ to any $k$-complete word.
-----Example-----
Input
4
6 2
abaaba
6 3
abaaba
36 9
hippopotomonstrosesquippedaliophobia
21 7
wudixiaoxingxingheclp
Output
2
0
23
16
-----Note-----
In the first test case, one optimal solution is aaaaaa.
In the second test case, the given word itself is $k$-complete.
|
from sys import stdin
t = int(stdin.readline())
output = []
for tc in range(t):
n, k = map(int, stdin.readline().split())
w = stdin.readline()
e = [[] for i in range(n)]
for i in range(n // 2):
e[i].append(n - i - 1)
e[n - i - 1].append(i)
for i in range(n - k):
e[i].append(i + k)
e[i + k].append(i)
sol = 0
used = [(False) for i in range(n)]
for i in range(n):
if used[i]:
continue
sz = 0
fr = {}
q = [i]
while len(q):
u = q.pop()
if used[u]:
continue
used[u] = True
sz += 1
fr[w[u]] = fr.get(w[u], 0) + 1
for v in e[u]:
if not used[v]:
q.append(v)
sol += sz - max(fr.values())
output.append(str(sol))
print("\n".join(output))
|
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR LIST VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR NUMBER VAR FOR VAR FUNC_CALL VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR ASSIGN VAR NUMBER ASSIGN VAR DICT ASSIGN VAR LIST VAR WHILE FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR IF VAR VAR ASSIGN VAR VAR NUMBER VAR NUMBER ASSIGN VAR VAR VAR BIN_OP FUNC_CALL VAR VAR VAR NUMBER NUMBER FOR VAR VAR VAR IF VAR VAR EXPR FUNC_CALL VAR VAR VAR BIN_OP VAR FUNC_CALL VAR FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL STRING VAR
|
Word $s$ of length $n$ is called $k$-complete if $s$ is a palindrome, i.e. $s_i=s_{n+1-i}$ for all $1 \le i \le n$; $s$ has a period of $k$, i.e. $s_i=s_{k+i}$ for all $1 \le i \le n-k$.
For example, "abaaba" is a $3$-complete word, while "abccba" is not.
Bob is given a word $s$ of length $n$ consisting of only lowercase Latin letters and an integer $k$, such that $n$ is divisible by $k$. He wants to convert $s$ to any $k$-complete word.
To do this Bob can choose some $i$ ($1 \le i \le n$) and replace the letter at position $i$ with some other lowercase Latin letter.
So now Bob wants to know the minimum number of letters he has to replace to convert $s$ to any $k$-complete word.
Note that Bob can do zero changes if the word $s$ is already $k$-complete.
You are required to answer $t$ test cases independently.
-----Input-----
The first line contains a single integer $t$ ($1 \le t\le 10^5$) — the number of test cases.
The first line of each test case contains two integers $n$ and $k$ ($1 \le k < n \le 2 \cdot 10^5$, $n$ is divisible by $k$).
The second line of each test case contains a word $s$ of length $n$.
It is guaranteed that word $s$ only contains lowercase Latin letters. And it is guaranteed that the sum of $n$ over all test cases will not exceed $2 \cdot 10^5$.
-----Output-----
For each test case, output one integer, representing the minimum number of characters he has to replace to convert $s$ to any $k$-complete word.
-----Example-----
Input
4
6 2
abaaba
6 3
abaaba
36 9
hippopotomonstrosesquippedaliophobia
21 7
wudixiaoxingxingheclp
Output
2
0
23
16
-----Note-----
In the first test case, one optimal solution is aaaaaa.
In the second test case, the given word itself is $k$-complete.
|
testcases = int(input())
for testcase in range(testcases):
temp = input()
temp = temp.split()
n = int(temp[0])
nlen = int(temp[1])
x = input()
group = []
chunks, chunk_size = n, nlen
group = [x[i : i + chunk_size] for i in range(0, chunks, chunk_size)]
dicts = {}
for i in group:
index = 0
for j in i:
if index not in dicts:
dicts[index] = []
dicts[index].append(j)
index += 1
left = 0
right = len(dicts) - 1
while left < right:
tempindex = dicts[right]
for i in tempindex:
dicts[left].append(i)
del dicts[right]
left += 1
right -= 1
total = 0
for key, value in dicts.items():
needsame = len(value)
needadd = len(value)
tdicts = {}
for i in value:
if i not in tdicts:
tdicts[i] = 1
else:
tdicts[i] += 1
for key, value in tdicts.items():
diffval = needsame - value
needadd = min(needadd, diffval)
total += needadd
print(total)
|
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR VAR VAR VAR ASSIGN VAR VAR VAR BIN_OP VAR VAR VAR FUNC_CALL VAR NUMBER VAR VAR ASSIGN VAR DICT FOR VAR VAR ASSIGN VAR NUMBER FOR VAR VAR IF VAR VAR ASSIGN VAR VAR LIST EXPR FUNC_CALL VAR VAR VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER WHILE VAR VAR ASSIGN VAR VAR VAR FOR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR VAR VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR DICT FOR VAR VAR IF VAR VAR ASSIGN VAR VAR NUMBER VAR VAR NUMBER FOR VAR VAR FUNC_CALL VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR
|
Word $s$ of length $n$ is called $k$-complete if $s$ is a palindrome, i.e. $s_i=s_{n+1-i}$ for all $1 \le i \le n$; $s$ has a period of $k$, i.e. $s_i=s_{k+i}$ for all $1 \le i \le n-k$.
For example, "abaaba" is a $3$-complete word, while "abccba" is not.
Bob is given a word $s$ of length $n$ consisting of only lowercase Latin letters and an integer $k$, such that $n$ is divisible by $k$. He wants to convert $s$ to any $k$-complete word.
To do this Bob can choose some $i$ ($1 \le i \le n$) and replace the letter at position $i$ with some other lowercase Latin letter.
So now Bob wants to know the minimum number of letters he has to replace to convert $s$ to any $k$-complete word.
Note that Bob can do zero changes if the word $s$ is already $k$-complete.
You are required to answer $t$ test cases independently.
-----Input-----
The first line contains a single integer $t$ ($1 \le t\le 10^5$) — the number of test cases.
The first line of each test case contains two integers $n$ and $k$ ($1 \le k < n \le 2 \cdot 10^5$, $n$ is divisible by $k$).
The second line of each test case contains a word $s$ of length $n$.
It is guaranteed that word $s$ only contains lowercase Latin letters. And it is guaranteed that the sum of $n$ over all test cases will not exceed $2 \cdot 10^5$.
-----Output-----
For each test case, output one integer, representing the minimum number of characters he has to replace to convert $s$ to any $k$-complete word.
-----Example-----
Input
4
6 2
abaaba
6 3
abaaba
36 9
hippopotomonstrosesquippedaliophobia
21 7
wudixiaoxingxingheclp
Output
2
0
23
16
-----Note-----
In the first test case, one optimal solution is aaaaaa.
In the second test case, the given word itself is $k$-complete.
|
for t in range(int(input())):
n, k = [int(i) for i in input().split()]
p = n // k
s = input()
a = [([0] * 26) for i in range(k)]
for i in range(n):
a[i % k][ord(s[i]) - ord("a")] += 1
a[i % k][ord(s[n - 1 - i]) - ord("a")] += 1
total = 0
for i in a:
total += 2 * p - max(i)
print(total // 2)
|
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER NUMBER VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR VAR BIN_OP VAR VAR BIN_OP FUNC_CALL VAR VAR VAR FUNC_CALL VAR STRING NUMBER VAR BIN_OP VAR VAR BIN_OP FUNC_CALL VAR VAR BIN_OP BIN_OP VAR NUMBER VAR FUNC_CALL VAR STRING NUMBER ASSIGN VAR NUMBER FOR VAR VAR VAR BIN_OP BIN_OP NUMBER VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER
|
Word $s$ of length $n$ is called $k$-complete if $s$ is a palindrome, i.e. $s_i=s_{n+1-i}$ for all $1 \le i \le n$; $s$ has a period of $k$, i.e. $s_i=s_{k+i}$ for all $1 \le i \le n-k$.
For example, "abaaba" is a $3$-complete word, while "abccba" is not.
Bob is given a word $s$ of length $n$ consisting of only lowercase Latin letters and an integer $k$, such that $n$ is divisible by $k$. He wants to convert $s$ to any $k$-complete word.
To do this Bob can choose some $i$ ($1 \le i \le n$) and replace the letter at position $i$ with some other lowercase Latin letter.
So now Bob wants to know the minimum number of letters he has to replace to convert $s$ to any $k$-complete word.
Note that Bob can do zero changes if the word $s$ is already $k$-complete.
You are required to answer $t$ test cases independently.
-----Input-----
The first line contains a single integer $t$ ($1 \le t\le 10^5$) — the number of test cases.
The first line of each test case contains two integers $n$ and $k$ ($1 \le k < n \le 2 \cdot 10^5$, $n$ is divisible by $k$).
The second line of each test case contains a word $s$ of length $n$.
It is guaranteed that word $s$ only contains lowercase Latin letters. And it is guaranteed that the sum of $n$ over all test cases will not exceed $2 \cdot 10^5$.
-----Output-----
For each test case, output one integer, representing the minimum number of characters he has to replace to convert $s$ to any $k$-complete word.
-----Example-----
Input
4
6 2
abaaba
6 3
abaaba
36 9
hippopotomonstrosesquippedaliophobia
21 7
wudixiaoxingxingheclp
Output
2
0
23
16
-----Note-----
In the first test case, one optimal solution is aaaaaa.
In the second test case, the given word itself is $k$-complete.
|
from sys import stdin, stdout
T = int(stdin.readline())
def func(s, k, i):
a = [0] * 29
n = len(s)
for u in i:
j = u
while j < n:
a[ord(s[j]) - ord("a")] += 1
j = j + k
sm = 0
mx = -1
for j in a:
if j > mx:
mx = j
sm += j
return sm - mx
for _ in range(T):
n, k = map(int, stdin.readline().split())
s = input()
ans = 0
done = [0] * k
for i in range(k):
fr = i
bk = (n - 1 - i) % k
dd = []
if done[i] == 0:
dd.append(fr)
done[i] = 1
if fr != bk:
done[bk] = 1
dd.append(bk)
ans += func(s, k, dd)
stdout.write(str(ans))
stdout.write("\n")
|
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_DEF ASSIGN VAR BIN_OP LIST NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR FOR VAR VAR ASSIGN VAR VAR WHILE VAR VAR VAR BIN_OP FUNC_CALL VAR VAR VAR FUNC_CALL VAR STRING NUMBER ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR IF VAR VAR ASSIGN VAR VAR VAR VAR RETURN BIN_OP VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR NUMBER VAR VAR ASSIGN VAR LIST IF VAR VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR NUMBER IF VAR VAR ASSIGN VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR STRING
|
Word $s$ of length $n$ is called $k$-complete if $s$ is a palindrome, i.e. $s_i=s_{n+1-i}$ for all $1 \le i \le n$; $s$ has a period of $k$, i.e. $s_i=s_{k+i}$ for all $1 \le i \le n-k$.
For example, "abaaba" is a $3$-complete word, while "abccba" is not.
Bob is given a word $s$ of length $n$ consisting of only lowercase Latin letters and an integer $k$, such that $n$ is divisible by $k$. He wants to convert $s$ to any $k$-complete word.
To do this Bob can choose some $i$ ($1 \le i \le n$) and replace the letter at position $i$ with some other lowercase Latin letter.
So now Bob wants to know the minimum number of letters he has to replace to convert $s$ to any $k$-complete word.
Note that Bob can do zero changes if the word $s$ is already $k$-complete.
You are required to answer $t$ test cases independently.
-----Input-----
The first line contains a single integer $t$ ($1 \le t\le 10^5$) — the number of test cases.
The first line of each test case contains two integers $n$ and $k$ ($1 \le k < n \le 2 \cdot 10^5$, $n$ is divisible by $k$).
The second line of each test case contains a word $s$ of length $n$.
It is guaranteed that word $s$ only contains lowercase Latin letters. And it is guaranteed that the sum of $n$ over all test cases will not exceed $2 \cdot 10^5$.
-----Output-----
For each test case, output one integer, representing the minimum number of characters he has to replace to convert $s$ to any $k$-complete word.
-----Example-----
Input
4
6 2
abaaba
6 3
abaaba
36 9
hippopotomonstrosesquippedaliophobia
21 7
wudixiaoxingxingheclp
Output
2
0
23
16
-----Note-----
In the first test case, one optimal solution is aaaaaa.
In the second test case, the given word itself is $k$-complete.
|
import sys
LI = lambda: list(map(int, sys.stdin.readline().strip("\n").split()))
MI = lambda: map(int, sys.stdin.readline().strip("\n").split())
SI = lambda: sys.stdin.readline().strip("\n")
II = lambda: int(sys.stdin.readline().strip("\n"))
for _ in range(II()):
n, k = MI()
s = SI()
ans = 0
for i in range(k):
cnt = {}
for j in range(i, n, k):
cnt[s[j]] = cnt.get(s[j], 0) + 1
for j in range(1, n // k + 1):
cnt[s[j * k - i - 1]] = cnt.get(s[j * k - i - 1], 0) + 1
ans += 2 * (n // k) - max(cnt.values())
print(ans // 2)
|
IMPORT ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR FUNC_CALL FUNC_CALL VAR STRING FOR VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR DICT FOR VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR VAR VAR BIN_OP FUNC_CALL VAR VAR VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR VAR BIN_OP BIN_OP BIN_OP VAR VAR VAR NUMBER BIN_OP FUNC_CALL VAR VAR BIN_OP BIN_OP BIN_OP VAR VAR VAR NUMBER NUMBER NUMBER VAR BIN_OP BIN_OP NUMBER BIN_OP VAR VAR FUNC_CALL VAR FUNC_CALL VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER
|
Word $s$ of length $n$ is called $k$-complete if $s$ is a palindrome, i.e. $s_i=s_{n+1-i}$ for all $1 \le i \le n$; $s$ has a period of $k$, i.e. $s_i=s_{k+i}$ for all $1 \le i \le n-k$.
For example, "abaaba" is a $3$-complete word, while "abccba" is not.
Bob is given a word $s$ of length $n$ consisting of only lowercase Latin letters and an integer $k$, such that $n$ is divisible by $k$. He wants to convert $s$ to any $k$-complete word.
To do this Bob can choose some $i$ ($1 \le i \le n$) and replace the letter at position $i$ with some other lowercase Latin letter.
So now Bob wants to know the minimum number of letters he has to replace to convert $s$ to any $k$-complete word.
Note that Bob can do zero changes if the word $s$ is already $k$-complete.
You are required to answer $t$ test cases independently.
-----Input-----
The first line contains a single integer $t$ ($1 \le t\le 10^5$) — the number of test cases.
The first line of each test case contains two integers $n$ and $k$ ($1 \le k < n \le 2 \cdot 10^5$, $n$ is divisible by $k$).
The second line of each test case contains a word $s$ of length $n$.
It is guaranteed that word $s$ only contains lowercase Latin letters. And it is guaranteed that the sum of $n$ over all test cases will not exceed $2 \cdot 10^5$.
-----Output-----
For each test case, output one integer, representing the minimum number of characters he has to replace to convert $s$ to any $k$-complete word.
-----Example-----
Input
4
6 2
abaaba
6 3
abaaba
36 9
hippopotomonstrosesquippedaliophobia
21 7
wudixiaoxingxingheclp
Output
2
0
23
16
-----Note-----
In the first test case, one optimal solution is aaaaaa.
In the second test case, the given word itself is $k$-complete.
|
t = int(input())
run = 0
while run < t:
n, k = map(int, input().split())
s = input()
rep = int(n / k)
units = [s[j * k : (j + 1) * k] for j in range(rep)]
changes = 0
if k % 2 == 0:
for i in range(k // 2):
arr1 = [units[j][i] for j in range(rep)]
arr2 = [units[j][k - i - 1] for j in range(rep)]
arr = arr1 + arr2
letters = []
counts = []
for letter in arr:
if letter not in letters:
letters.append(letter)
counts.append(arr.count(letter))
changes = changes + len(arr) - max(counts)
print(changes)
else:
for i in range(k // 2 + 1):
arr1 = [units[j][i] for j in range(rep)]
arr2 = [units[j][k - i - 1] for j in range(rep)]
arr = arr1 + arr2
letters = []
counts = []
for letter in arr:
if letter not in letters:
letters.append(letter)
counts.append(arr.count(letter))
if i == k // 2:
changes = changes + (len(arr) - max(counts)) / 2
else:
changes = changes + len(arr) - max(counts)
print(int(changes))
run += 1
|
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR NUMBER WHILE VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR ASSIGN VAR VAR BIN_OP VAR VAR BIN_OP BIN_OP VAR NUMBER VAR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER IF BIN_OP VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR BIN_OP BIN_OP VAR VAR NUMBER VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR LIST ASSIGN VAR LIST FOR VAR VAR IF VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR VAR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR BIN_OP BIN_OP VAR VAR NUMBER VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR LIST ASSIGN VAR LIST FOR VAR VAR IF VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR BIN_OP BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR NUMBER
|
Word $s$ of length $n$ is called $k$-complete if $s$ is a palindrome, i.e. $s_i=s_{n+1-i}$ for all $1 \le i \le n$; $s$ has a period of $k$, i.e. $s_i=s_{k+i}$ for all $1 \le i \le n-k$.
For example, "abaaba" is a $3$-complete word, while "abccba" is not.
Bob is given a word $s$ of length $n$ consisting of only lowercase Latin letters and an integer $k$, such that $n$ is divisible by $k$. He wants to convert $s$ to any $k$-complete word.
To do this Bob can choose some $i$ ($1 \le i \le n$) and replace the letter at position $i$ with some other lowercase Latin letter.
So now Bob wants to know the minimum number of letters he has to replace to convert $s$ to any $k$-complete word.
Note that Bob can do zero changes if the word $s$ is already $k$-complete.
You are required to answer $t$ test cases independently.
-----Input-----
The first line contains a single integer $t$ ($1 \le t\le 10^5$) — the number of test cases.
The first line of each test case contains two integers $n$ and $k$ ($1 \le k < n \le 2 \cdot 10^5$, $n$ is divisible by $k$).
The second line of each test case contains a word $s$ of length $n$.
It is guaranteed that word $s$ only contains lowercase Latin letters. And it is guaranteed that the sum of $n$ over all test cases will not exceed $2 \cdot 10^5$.
-----Output-----
For each test case, output one integer, representing the minimum number of characters he has to replace to convert $s$ to any $k$-complete word.
-----Example-----
Input
4
6 2
abaaba
6 3
abaaba
36 9
hippopotomonstrosesquippedaliophobia
21 7
wudixiaoxingxingheclp
Output
2
0
23
16
-----Note-----
In the first test case, one optimal solution is aaaaaa.
In the second test case, the given word itself is $k$-complete.
|
import sys
input = sys.stdin.readline
t = int(input())
for _ in range(t):
n, k = map(int, input().split())
s = str(input())
c = [([0] * 26) for _ in range((k + 1) // 2)]
for i in range(n):
c[min(i % k, k - i % k - 1)][ord(s[i]) - ord("a")] += 1
ans = 0
if k % 2 == 0:
for i in range(len(c)):
ans += n // k * 2 - max(c[i])
else:
for i in range(len(c)):
if i != len(c) - 1:
ans += n // k * 2 - max(c[i])
else:
ans += n // k - max(c[i])
print(ans)
|
IMPORT ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER NUMBER VAR FUNC_CALL VAR BIN_OP BIN_OP VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR BIN_OP VAR VAR BIN_OP BIN_OP VAR BIN_OP VAR VAR NUMBER BIN_OP FUNC_CALL VAR VAR VAR FUNC_CALL VAR STRING NUMBER ASSIGN VAR NUMBER IF BIN_OP VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR BIN_OP BIN_OP BIN_OP VAR VAR NUMBER FUNC_CALL VAR VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR BIN_OP FUNC_CALL VAR VAR NUMBER VAR BIN_OP BIN_OP BIN_OP VAR VAR NUMBER FUNC_CALL VAR VAR VAR VAR BIN_OP BIN_OP VAR VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR
|
Word $s$ of length $n$ is called $k$-complete if $s$ is a palindrome, i.e. $s_i=s_{n+1-i}$ for all $1 \le i \le n$; $s$ has a period of $k$, i.e. $s_i=s_{k+i}$ for all $1 \le i \le n-k$.
For example, "abaaba" is a $3$-complete word, while "abccba" is not.
Bob is given a word $s$ of length $n$ consisting of only lowercase Latin letters and an integer $k$, such that $n$ is divisible by $k$. He wants to convert $s$ to any $k$-complete word.
To do this Bob can choose some $i$ ($1 \le i \le n$) and replace the letter at position $i$ with some other lowercase Latin letter.
So now Bob wants to know the minimum number of letters he has to replace to convert $s$ to any $k$-complete word.
Note that Bob can do zero changes if the word $s$ is already $k$-complete.
You are required to answer $t$ test cases independently.
-----Input-----
The first line contains a single integer $t$ ($1 \le t\le 10^5$) — the number of test cases.
The first line of each test case contains two integers $n$ and $k$ ($1 \le k < n \le 2 \cdot 10^5$, $n$ is divisible by $k$).
The second line of each test case contains a word $s$ of length $n$.
It is guaranteed that word $s$ only contains lowercase Latin letters. And it is guaranteed that the sum of $n$ over all test cases will not exceed $2 \cdot 10^5$.
-----Output-----
For each test case, output one integer, representing the minimum number of characters he has to replace to convert $s$ to any $k$-complete word.
-----Example-----
Input
4
6 2
abaaba
6 3
abaaba
36 9
hippopotomonstrosesquippedaliophobia
21 7
wudixiaoxingxingheclp
Output
2
0
23
16
-----Note-----
In the first test case, one optimal solution is aaaaaa.
In the second test case, the given word itself is $k$-complete.
|
import sys
def f(l):
d = {}
for i in l:
if i in d:
d[i] += 1
else:
d[i] = 1
return len(l) - max(d.values())
for i in range(int(sys.stdin.readline())):
n, k = map(int, sys.stdin.readline().split())
s = input()
print(
sum(f(s[j::k] + s[k - 1 - j :: k]) for j in range(k // 2))
+ sum(f(s[k // 2 :: k]) for j in range(k % 2))
)
|
IMPORT FUNC_DEF ASSIGN VAR DICT FOR VAR VAR IF VAR VAR VAR VAR NUMBER ASSIGN VAR VAR NUMBER RETURN BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR EXPR FUNC_CALL VAR BIN_OP FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR BIN_OP BIN_OP VAR NUMBER VAR VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER FUNC_CALL VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER
|
Word $s$ of length $n$ is called $k$-complete if $s$ is a palindrome, i.e. $s_i=s_{n+1-i}$ for all $1 \le i \le n$; $s$ has a period of $k$, i.e. $s_i=s_{k+i}$ for all $1 \le i \le n-k$.
For example, "abaaba" is a $3$-complete word, while "abccba" is not.
Bob is given a word $s$ of length $n$ consisting of only lowercase Latin letters and an integer $k$, such that $n$ is divisible by $k$. He wants to convert $s$ to any $k$-complete word.
To do this Bob can choose some $i$ ($1 \le i \le n$) and replace the letter at position $i$ with some other lowercase Latin letter.
So now Bob wants to know the minimum number of letters he has to replace to convert $s$ to any $k$-complete word.
Note that Bob can do zero changes if the word $s$ is already $k$-complete.
You are required to answer $t$ test cases independently.
-----Input-----
The first line contains a single integer $t$ ($1 \le t\le 10^5$) — the number of test cases.
The first line of each test case contains two integers $n$ and $k$ ($1 \le k < n \le 2 \cdot 10^5$, $n$ is divisible by $k$).
The second line of each test case contains a word $s$ of length $n$.
It is guaranteed that word $s$ only contains lowercase Latin letters. And it is guaranteed that the sum of $n$ over all test cases will not exceed $2 \cdot 10^5$.
-----Output-----
For each test case, output one integer, representing the minimum number of characters he has to replace to convert $s$ to any $k$-complete word.
-----Example-----
Input
4
6 2
abaaba
6 3
abaaba
36 9
hippopotomonstrosesquippedaliophobia
21 7
wudixiaoxingxingheclp
Output
2
0
23
16
-----Note-----
In the first test case, one optimal solution is aaaaaa.
In the second test case, the given word itself is $k$-complete.
|
from sys import stdin
t = int(stdin.readline())
for _ in range(t):
n, k = map(int, stdin.readline().split())
s = stdin.readline()
cnt = [[(0) for i in range(26)] for j in range((k + 1) // 2)]
for i in range(n):
cnt[min(i % k, k - i % k - 1)][ord(s[i]) - ord("a")] += 1
ans = 0
for i in range(k // 2):
ans += 2 * n // k - max(cnt[i])
if k % 2 == 1:
ans += n // k - max(cnt[k // 2])
print(ans)
|
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR NUMBER VAR FUNC_CALL VAR BIN_OP BIN_OP VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR BIN_OP VAR VAR BIN_OP BIN_OP VAR BIN_OP VAR VAR NUMBER BIN_OP FUNC_CALL VAR VAR VAR FUNC_CALL VAR STRING NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR BIN_OP BIN_OP BIN_OP NUMBER VAR VAR FUNC_CALL VAR VAR VAR IF BIN_OP VAR NUMBER NUMBER VAR BIN_OP BIN_OP VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR
|
Word $s$ of length $n$ is called $k$-complete if $s$ is a palindrome, i.e. $s_i=s_{n+1-i}$ for all $1 \le i \le n$; $s$ has a period of $k$, i.e. $s_i=s_{k+i}$ for all $1 \le i \le n-k$.
For example, "abaaba" is a $3$-complete word, while "abccba" is not.
Bob is given a word $s$ of length $n$ consisting of only lowercase Latin letters and an integer $k$, such that $n$ is divisible by $k$. He wants to convert $s$ to any $k$-complete word.
To do this Bob can choose some $i$ ($1 \le i \le n$) and replace the letter at position $i$ with some other lowercase Latin letter.
So now Bob wants to know the minimum number of letters he has to replace to convert $s$ to any $k$-complete word.
Note that Bob can do zero changes if the word $s$ is already $k$-complete.
You are required to answer $t$ test cases independently.
-----Input-----
The first line contains a single integer $t$ ($1 \le t\le 10^5$) — the number of test cases.
The first line of each test case contains two integers $n$ and $k$ ($1 \le k < n \le 2 \cdot 10^5$, $n$ is divisible by $k$).
The second line of each test case contains a word $s$ of length $n$.
It is guaranteed that word $s$ only contains lowercase Latin letters. And it is guaranteed that the sum of $n$ over all test cases will not exceed $2 \cdot 10^5$.
-----Output-----
For each test case, output one integer, representing the minimum number of characters he has to replace to convert $s$ to any $k$-complete word.
-----Example-----
Input
4
6 2
abaaba
6 3
abaaba
36 9
hippopotomonstrosesquippedaliophobia
21 7
wudixiaoxingxingheclp
Output
2
0
23
16
-----Note-----
In the first test case, one optimal solution is aaaaaa.
In the second test case, the given word itself is $k$-complete.
|
def count(s):
a = [0] * 26
for x in s:
a[ord(x) - 97] += 1
return len(s) - max(a)
for _ in range(int(input())):
n, k = map(int, input().split())
s = input()
i = 0
p = []
for _ in range((k + 1) // 2):
p.append([])
while i <= n - k:
for j in range(0, (k + 1) // 2):
if i + j != i + k - 1 - j:
p[j].append(s[i + k - 1 - j])
p[j].append(s[i + j])
i += k
print(sum(list(map(count, p))))
|
FUNC_DEF ASSIGN VAR BIN_OP LIST NUMBER NUMBER FOR VAR VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER RETURN BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR BIN_OP BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR LIST WHILE VAR BIN_OP VAR VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP BIN_OP VAR NUMBER NUMBER IF BIN_OP VAR VAR BIN_OP BIN_OP BIN_OP VAR VAR NUMBER VAR EXPR FUNC_CALL VAR VAR VAR BIN_OP BIN_OP BIN_OP VAR VAR NUMBER VAR EXPR FUNC_CALL VAR VAR VAR BIN_OP VAR VAR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR
|
Word $s$ of length $n$ is called $k$-complete if $s$ is a palindrome, i.e. $s_i=s_{n+1-i}$ for all $1 \le i \le n$; $s$ has a period of $k$, i.e. $s_i=s_{k+i}$ for all $1 \le i \le n-k$.
For example, "abaaba" is a $3$-complete word, while "abccba" is not.
Bob is given a word $s$ of length $n$ consisting of only lowercase Latin letters and an integer $k$, such that $n$ is divisible by $k$. He wants to convert $s$ to any $k$-complete word.
To do this Bob can choose some $i$ ($1 \le i \le n$) and replace the letter at position $i$ with some other lowercase Latin letter.
So now Bob wants to know the minimum number of letters he has to replace to convert $s$ to any $k$-complete word.
Note that Bob can do zero changes if the word $s$ is already $k$-complete.
You are required to answer $t$ test cases independently.
-----Input-----
The first line contains a single integer $t$ ($1 \le t\le 10^5$) — the number of test cases.
The first line of each test case contains two integers $n$ and $k$ ($1 \le k < n \le 2 \cdot 10^5$, $n$ is divisible by $k$).
The second line of each test case contains a word $s$ of length $n$.
It is guaranteed that word $s$ only contains lowercase Latin letters. And it is guaranteed that the sum of $n$ over all test cases will not exceed $2 \cdot 10^5$.
-----Output-----
For each test case, output one integer, representing the minimum number of characters he has to replace to convert $s$ to any $k$-complete word.
-----Example-----
Input
4
6 2
abaaba
6 3
abaaba
36 9
hippopotomonstrosesquippedaliophobia
21 7
wudixiaoxingxingheclp
Output
2
0
23
16
-----Note-----
In the first test case, one optimal solution is aaaaaa.
In the second test case, the given word itself is $k$-complete.
|
def hamming_dist(a, b):
n = len(a)
d = [(1 if a[i] != b[i] else 0) for i in range(n)]
return sum(d)
def letter_frequencies(word, k):
dict_list = []
for i in range(k // 2):
sub_wrd1 = word[i::k]
sub_wrd2 = word[k - i - 1 :: k]
sub_wrd = sub_wrd1 + sub_wrd2
my_dict = {}
for c in sub_wrd:
if c not in my_dict:
my_dict[c] = 0
my_dict[c] += 1
dict_list.append(my_dict)
if k % 2:
my_dict = {}
sub_wrd = word[k // 2 :: k]
for c in sub_wrd:
if c not in my_dict:
my_dict[c] = 0
my_dict[c] += 1
dict_list.append(my_dict)
return dict_list
def dict_max(dic):
best_key = None
for k in dic:
if not best_key:
best_key = k
if dic[k] > dic[best_key]:
best_key = k
return best_key, dic[best_key]
t = int(input())
for _ in range(t):
n, k = list(map(int, input().split()))
word = list(input())
dict_list = letter_frequencies(word, k)
letters = [dict_max(dic) for dic in dict_list]
unit = list(range(k))
for i in range(k // 2):
unit[i] = letters[i][0]
unit[-i - 1] = letters[i][0]
if k % 2:
unit[k // 2] = letters[k // 2][0]
best_word = "".join(unit * (n // k))
print(hamming_dist(best_word, word))
|
FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR VAR NUMBER NUMBER VAR FUNC_CALL VAR VAR RETURN FUNC_CALL VAR VAR FUNC_DEF ASSIGN VAR LIST FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR VAR ASSIGN VAR VAR BIN_OP BIN_OP VAR VAR NUMBER VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR DICT FOR VAR VAR IF VAR VAR ASSIGN VAR VAR NUMBER VAR VAR NUMBER EXPR FUNC_CALL VAR VAR IF BIN_OP VAR NUMBER ASSIGN VAR DICT ASSIGN VAR VAR BIN_OP VAR NUMBER VAR FOR VAR VAR IF VAR VAR ASSIGN VAR VAR NUMBER VAR VAR NUMBER EXPR FUNC_CALL VAR VAR RETURN VAR FUNC_DEF ASSIGN VAR NONE FOR VAR VAR IF VAR ASSIGN VAR VAR IF VAR VAR VAR VAR ASSIGN VAR VAR RETURN VAR VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER VAR VAR NUMBER IF BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL STRING BIN_OP VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR
|
Word $s$ of length $n$ is called $k$-complete if $s$ is a palindrome, i.e. $s_i=s_{n+1-i}$ for all $1 \le i \le n$; $s$ has a period of $k$, i.e. $s_i=s_{k+i}$ for all $1 \le i \le n-k$.
For example, "abaaba" is a $3$-complete word, while "abccba" is not.
Bob is given a word $s$ of length $n$ consisting of only lowercase Latin letters and an integer $k$, such that $n$ is divisible by $k$. He wants to convert $s$ to any $k$-complete word.
To do this Bob can choose some $i$ ($1 \le i \le n$) and replace the letter at position $i$ with some other lowercase Latin letter.
So now Bob wants to know the minimum number of letters he has to replace to convert $s$ to any $k$-complete word.
Note that Bob can do zero changes if the word $s$ is already $k$-complete.
You are required to answer $t$ test cases independently.
-----Input-----
The first line contains a single integer $t$ ($1 \le t\le 10^5$) — the number of test cases.
The first line of each test case contains two integers $n$ and $k$ ($1 \le k < n \le 2 \cdot 10^5$, $n$ is divisible by $k$).
The second line of each test case contains a word $s$ of length $n$.
It is guaranteed that word $s$ only contains lowercase Latin letters. And it is guaranteed that the sum of $n$ over all test cases will not exceed $2 \cdot 10^5$.
-----Output-----
For each test case, output one integer, representing the minimum number of characters he has to replace to convert $s$ to any $k$-complete word.
-----Example-----
Input
4
6 2
abaaba
6 3
abaaba
36 9
hippopotomonstrosesquippedaliophobia
21 7
wudixiaoxingxingheclp
Output
2
0
23
16
-----Note-----
In the first test case, one optimal solution is aaaaaa.
In the second test case, the given word itself is $k$-complete.
|
t = int(input())
for _ in range(t):
n, k = map(int, input().split())
l = list(input())
ls = []
for i in range(26):
ls.append(0)
count = 0
if k % 2 == 1:
i = k // 2
while i < n:
ls[ord(l[i]) - 97] += 1
i += k
maximum = max(ls)
count = count + n // k - maximum
z = 2
else:
z = 1
i = k // 2 - 1
while i >= 0:
j = i
for temp in range(26):
ls[temp] = 0
while j < n:
ls[ord(l[j]) - 97] += 1
j += k
j = i + z
while j < n:
ls[ord(l[j]) - 97] += 1
j += k
maximum = max(ls)
count = count + 2 * n // k - maximum
i = i - 1
z = z + 2
print(count)
|
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER IF BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR NUMBER WHILE VAR VAR VAR BIN_OP FUNC_CALL VAR VAR VAR NUMBER NUMBER VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR BIN_OP VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR NUMBER NUMBER WHILE VAR NUMBER ASSIGN VAR VAR FOR VAR FUNC_CALL VAR NUMBER ASSIGN VAR VAR NUMBER WHILE VAR VAR VAR BIN_OP FUNC_CALL VAR VAR VAR NUMBER NUMBER VAR VAR ASSIGN VAR BIN_OP VAR VAR WHILE VAR VAR VAR BIN_OP FUNC_CALL VAR VAR VAR NUMBER NUMBER VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR BIN_OP BIN_OP NUMBER VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR
|
Word $s$ of length $n$ is called $k$-complete if $s$ is a palindrome, i.e. $s_i=s_{n+1-i}$ for all $1 \le i \le n$; $s$ has a period of $k$, i.e. $s_i=s_{k+i}$ for all $1 \le i \le n-k$.
For example, "abaaba" is a $3$-complete word, while "abccba" is not.
Bob is given a word $s$ of length $n$ consisting of only lowercase Latin letters and an integer $k$, such that $n$ is divisible by $k$. He wants to convert $s$ to any $k$-complete word.
To do this Bob can choose some $i$ ($1 \le i \le n$) and replace the letter at position $i$ with some other lowercase Latin letter.
So now Bob wants to know the minimum number of letters he has to replace to convert $s$ to any $k$-complete word.
Note that Bob can do zero changes if the word $s$ is already $k$-complete.
You are required to answer $t$ test cases independently.
-----Input-----
The first line contains a single integer $t$ ($1 \le t\le 10^5$) — the number of test cases.
The first line of each test case contains two integers $n$ and $k$ ($1 \le k < n \le 2 \cdot 10^5$, $n$ is divisible by $k$).
The second line of each test case contains a word $s$ of length $n$.
It is guaranteed that word $s$ only contains lowercase Latin letters. And it is guaranteed that the sum of $n$ over all test cases will not exceed $2 \cdot 10^5$.
-----Output-----
For each test case, output one integer, representing the minimum number of characters he has to replace to convert $s$ to any $k$-complete word.
-----Example-----
Input
4
6 2
abaaba
6 3
abaaba
36 9
hippopotomonstrosesquippedaliophobia
21 7
wudixiaoxingxingheclp
Output
2
0
23
16
-----Note-----
In the first test case, one optimal solution is aaaaaa.
In the second test case, the given word itself is $k$-complete.
|
for _ in range(int(input())):
n, k = map(int, input().split())
s = [i for i in input()]
mas = [s[i * k : i * k + k] for i in range(n // k)]
num = [0] * 27
ans = 0
for i in range(k):
for j in range(n // k):
num[ord(mas[j][i]) - 97] += 1
num[ord(mas[j][k - 1 - i]) - 97] += 1
ans += 2 * n // k - max(num)
num = [0] * 27
print(ans // 2)
|
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR VAR FUNC_CALL VAR ASSIGN VAR VAR BIN_OP VAR VAR BIN_OP BIN_OP VAR VAR VAR VAR FUNC_CALL VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP LIST NUMBER NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR VAR VAR BIN_OP FUNC_CALL VAR VAR VAR VAR NUMBER NUMBER VAR BIN_OP FUNC_CALL VAR VAR VAR BIN_OP BIN_OP VAR NUMBER VAR NUMBER NUMBER VAR BIN_OP BIN_OP BIN_OP NUMBER VAR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER
|
Word $s$ of length $n$ is called $k$-complete if $s$ is a palindrome, i.e. $s_i=s_{n+1-i}$ for all $1 \le i \le n$; $s$ has a period of $k$, i.e. $s_i=s_{k+i}$ for all $1 \le i \le n-k$.
For example, "abaaba" is a $3$-complete word, while "abccba" is not.
Bob is given a word $s$ of length $n$ consisting of only lowercase Latin letters and an integer $k$, such that $n$ is divisible by $k$. He wants to convert $s$ to any $k$-complete word.
To do this Bob can choose some $i$ ($1 \le i \le n$) and replace the letter at position $i$ with some other lowercase Latin letter.
So now Bob wants to know the minimum number of letters he has to replace to convert $s$ to any $k$-complete word.
Note that Bob can do zero changes if the word $s$ is already $k$-complete.
You are required to answer $t$ test cases independently.
-----Input-----
The first line contains a single integer $t$ ($1 \le t\le 10^5$) — the number of test cases.
The first line of each test case contains two integers $n$ and $k$ ($1 \le k < n \le 2 \cdot 10^5$, $n$ is divisible by $k$).
The second line of each test case contains a word $s$ of length $n$.
It is guaranteed that word $s$ only contains lowercase Latin letters. And it is guaranteed that the sum of $n$ over all test cases will not exceed $2 \cdot 10^5$.
-----Output-----
For each test case, output one integer, representing the minimum number of characters he has to replace to convert $s$ to any $k$-complete word.
-----Example-----
Input
4
6 2
abaaba
6 3
abaaba
36 9
hippopotomonstrosesquippedaliophobia
21 7
wudixiaoxingxingheclp
Output
2
0
23
16
-----Note-----
In the first test case, one optimal solution is aaaaaa.
In the second test case, the given word itself is $k$-complete.
|
for h in range(int(input())):
n, k = map(int, input().split())
s = input()
matrix = [s[i : i + k] for i in range(0, n, k)]
ans = 0
if k % 2 == 1:
cnt = [0] * 26
for i in range(n // k):
cnt[ord(matrix[i][k // 2]) - 97] += 1
ans += n // k - max(cnt)
else:
for i in range(n // k):
matrix[i] = matrix[i][: k // 2] + "#" + matrix[i][k // 2 :]
k += 1
for j in range(k // 2):
cnt = [0] * 26
for i in range(len(matrix)):
cnt[ord(matrix[i][j]) - 97] += 1
cnt[ord(matrix[i][k - j - 1]) - 97] += 1
ans += 2 * len(matrix) - max(cnt)
print(ans)
|
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR VAR VAR BIN_OP VAR VAR VAR FUNC_CALL VAR NUMBER VAR VAR ASSIGN VAR NUMBER IF BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP LIST NUMBER NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR VAR VAR BIN_OP FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER NUMBER NUMBER VAR BIN_OP BIN_OP VAR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR VAR ASSIGN VAR VAR BIN_OP BIN_OP VAR VAR BIN_OP VAR NUMBER STRING VAR VAR BIN_OP VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR BIN_OP FUNC_CALL VAR VAR VAR VAR NUMBER NUMBER VAR BIN_OP FUNC_CALL VAR VAR VAR BIN_OP BIN_OP VAR VAR NUMBER NUMBER NUMBER VAR BIN_OP BIN_OP NUMBER FUNC_CALL VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR
|
Word $s$ of length $n$ is called $k$-complete if $s$ is a palindrome, i.e. $s_i=s_{n+1-i}$ for all $1 \le i \le n$; $s$ has a period of $k$, i.e. $s_i=s_{k+i}$ for all $1 \le i \le n-k$.
For example, "abaaba" is a $3$-complete word, while "abccba" is not.
Bob is given a word $s$ of length $n$ consisting of only lowercase Latin letters and an integer $k$, such that $n$ is divisible by $k$. He wants to convert $s$ to any $k$-complete word.
To do this Bob can choose some $i$ ($1 \le i \le n$) and replace the letter at position $i$ with some other lowercase Latin letter.
So now Bob wants to know the minimum number of letters he has to replace to convert $s$ to any $k$-complete word.
Note that Bob can do zero changes if the word $s$ is already $k$-complete.
You are required to answer $t$ test cases independently.
-----Input-----
The first line contains a single integer $t$ ($1 \le t\le 10^5$) — the number of test cases.
The first line of each test case contains two integers $n$ and $k$ ($1 \le k < n \le 2 \cdot 10^5$, $n$ is divisible by $k$).
The second line of each test case contains a word $s$ of length $n$.
It is guaranteed that word $s$ only contains lowercase Latin letters. And it is guaranteed that the sum of $n$ over all test cases will not exceed $2 \cdot 10^5$.
-----Output-----
For each test case, output one integer, representing the minimum number of characters he has to replace to convert $s$ to any $k$-complete word.
-----Example-----
Input
4
6 2
abaaba
6 3
abaaba
36 9
hippopotomonstrosesquippedaliophobia
21 7
wudixiaoxingxingheclp
Output
2
0
23
16
-----Note-----
In the first test case, one optimal solution is aaaaaa.
In the second test case, the given word itself is $k$-complete.
|
t = int(input())
for i in range(t):
n, k = map(int, input().split())
s = input()
d = {}
i = 0
while i < n:
q = s[i : i + k]
for j in range(k // 2):
if j not in d:
d[j] = {}
if q[j] not in d[j]:
d[j][q[j]] = 0
if q[-j - 1] not in d[j]:
d[j][q[-j - 1]] = 0
d[j][q[-j - 1]] = d[j][q[-j - 1]] + 1
d[j][q[j]] = d[j][q[j]] + 1
if k % 2 == 1:
if k // 2 not in d:
d[k // 2] = {}
if q[k // 2] not in d[k // 2]:
d[k // 2][q[k // 2]] = 0
d[k // 2][q[k // 2]] = d[k // 2][q[k // 2]] + 1
i = i + k
ans = 0
for i in d:
m = 0
for s in d[i]:
if d[i][s] > m:
m = d[i][s]
ans = ans + d[i][s]
ans = ans - m
print(ans)
|
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR DICT ASSIGN VAR NUMBER WHILE VAR VAR ASSIGN VAR VAR VAR BIN_OP VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER IF VAR VAR ASSIGN VAR VAR DICT IF VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR NUMBER IF VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR VAR VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR VAR BIN_OP VAR NUMBER BIN_OP VAR VAR VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR VAR VAR BIN_OP VAR VAR VAR VAR NUMBER IF BIN_OP VAR NUMBER NUMBER IF BIN_OP VAR NUMBER VAR ASSIGN VAR BIN_OP VAR NUMBER DICT IF VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER BIN_OP VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR NUMBER FOR VAR VAR ASSIGN VAR NUMBER FOR VAR VAR VAR IF VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR
|
Word $s$ of length $n$ is called $k$-complete if $s$ is a palindrome, i.e. $s_i=s_{n+1-i}$ for all $1 \le i \le n$; $s$ has a period of $k$, i.e. $s_i=s_{k+i}$ for all $1 \le i \le n-k$.
For example, "abaaba" is a $3$-complete word, while "abccba" is not.
Bob is given a word $s$ of length $n$ consisting of only lowercase Latin letters and an integer $k$, such that $n$ is divisible by $k$. He wants to convert $s$ to any $k$-complete word.
To do this Bob can choose some $i$ ($1 \le i \le n$) and replace the letter at position $i$ with some other lowercase Latin letter.
So now Bob wants to know the minimum number of letters he has to replace to convert $s$ to any $k$-complete word.
Note that Bob can do zero changes if the word $s$ is already $k$-complete.
You are required to answer $t$ test cases independently.
-----Input-----
The first line contains a single integer $t$ ($1 \le t\le 10^5$) — the number of test cases.
The first line of each test case contains two integers $n$ and $k$ ($1 \le k < n \le 2 \cdot 10^5$, $n$ is divisible by $k$).
The second line of each test case contains a word $s$ of length $n$.
It is guaranteed that word $s$ only contains lowercase Latin letters. And it is guaranteed that the sum of $n$ over all test cases will not exceed $2 \cdot 10^5$.
-----Output-----
For each test case, output one integer, representing the minimum number of characters he has to replace to convert $s$ to any $k$-complete word.
-----Example-----
Input
4
6 2
abaaba
6 3
abaaba
36 9
hippopotomonstrosesquippedaliophobia
21 7
wudixiaoxingxingheclp
Output
2
0
23
16
-----Note-----
In the first test case, one optimal solution is aaaaaa.
In the second test case, the given word itself is $k$-complete.
|
q = int(input())
alph = "abcdefghijklmnopqrstuvwxyz"
def to_arr(s):
a = [0] * len(s)
for i in range(len(s)):
a[i] = alph.index(s[i])
return a
for _ in range(q):
n, k = map(int, input().split())
s = to_arr(input())
ans = 0
for i in range(k // 2):
r = [0] * 26
ml = 1
r[s[i]] += 1
r[s[k - i - 1]] += 1
if r[s[k - i - 1]] > ml:
ml = r[s[k - i - 1]]
for m in range(1, n // k):
r[s[i + m * k]] += 1
if r[s[i + m * k]] > ml:
ml = r[s[i + m * k]]
r[s[k - i - 1 + m * k]] += 1
if r[s[k - i - 1 + m * k]] > ml:
ml = r[s[k - i - 1 + m * k]]
ans += n // k * 2 - ml
if k % 2:
r = [0] * 26
ml = 1
r[s[k // 2]] += 1
for m in range(1, n // k):
r[s[k // 2 + m * k]] += 1
if r[s[k // 2 + m * k]] > ml:
ml = r[s[k // 2 + m * k]]
ans += n // k - ml
print(ans)
|
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR STRING FUNC_DEF ASSIGN VAR BIN_OP LIST NUMBER FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR RETURN VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER NUMBER ASSIGN VAR NUMBER VAR VAR VAR NUMBER VAR VAR BIN_OP BIN_OP VAR VAR NUMBER NUMBER IF VAR VAR BIN_OP BIN_OP VAR VAR NUMBER VAR ASSIGN VAR VAR VAR BIN_OP BIN_OP VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR VAR VAR VAR BIN_OP VAR BIN_OP VAR VAR NUMBER IF VAR VAR BIN_OP VAR BIN_OP VAR VAR VAR ASSIGN VAR VAR VAR BIN_OP VAR BIN_OP VAR VAR VAR VAR BIN_OP BIN_OP BIN_OP VAR VAR NUMBER BIN_OP VAR VAR NUMBER IF VAR VAR BIN_OP BIN_OP BIN_OP VAR VAR NUMBER BIN_OP VAR VAR VAR ASSIGN VAR VAR VAR BIN_OP BIN_OP BIN_OP VAR VAR NUMBER BIN_OP VAR VAR VAR BIN_OP BIN_OP BIN_OP VAR VAR NUMBER VAR IF BIN_OP VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER NUMBER ASSIGN VAR NUMBER VAR VAR BIN_OP VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR VAR VAR VAR BIN_OP BIN_OP VAR NUMBER BIN_OP VAR VAR NUMBER IF VAR VAR BIN_OP BIN_OP VAR NUMBER BIN_OP VAR VAR VAR ASSIGN VAR VAR VAR BIN_OP BIN_OP VAR NUMBER BIN_OP VAR VAR VAR BIN_OP BIN_OP VAR VAR VAR EXPR FUNC_CALL VAR VAR
|
Word $s$ of length $n$ is called $k$-complete if $s$ is a palindrome, i.e. $s_i=s_{n+1-i}$ for all $1 \le i \le n$; $s$ has a period of $k$, i.e. $s_i=s_{k+i}$ for all $1 \le i \le n-k$.
For example, "abaaba" is a $3$-complete word, while "abccba" is not.
Bob is given a word $s$ of length $n$ consisting of only lowercase Latin letters and an integer $k$, such that $n$ is divisible by $k$. He wants to convert $s$ to any $k$-complete word.
To do this Bob can choose some $i$ ($1 \le i \le n$) and replace the letter at position $i$ with some other lowercase Latin letter.
So now Bob wants to know the minimum number of letters he has to replace to convert $s$ to any $k$-complete word.
Note that Bob can do zero changes if the word $s$ is already $k$-complete.
You are required to answer $t$ test cases independently.
-----Input-----
The first line contains a single integer $t$ ($1 \le t\le 10^5$) — the number of test cases.
The first line of each test case contains two integers $n$ and $k$ ($1 \le k < n \le 2 \cdot 10^5$, $n$ is divisible by $k$).
The second line of each test case contains a word $s$ of length $n$.
It is guaranteed that word $s$ only contains lowercase Latin letters. And it is guaranteed that the sum of $n$ over all test cases will not exceed $2 \cdot 10^5$.
-----Output-----
For each test case, output one integer, representing the minimum number of characters he has to replace to convert $s$ to any $k$-complete word.
-----Example-----
Input
4
6 2
abaaba
6 3
abaaba
36 9
hippopotomonstrosesquippedaliophobia
21 7
wudixiaoxingxingheclp
Output
2
0
23
16
-----Note-----
In the first test case, one optimal solution is aaaaaa.
In the second test case, the given word itself is $k$-complete.
|
testcases = int(input())
for j in range(testcases):
values = list(map(int, input().split()))
n = values[0]
k = values[1]
words = [(ord(k) - 96) for k in input()]
moves = 0
if k % 2 == 0:
for s in range(k // 2):
store1 = [(0) for i in range(26)]
start = s
inc = k
cv = 0
counter = 0
while start + cv * inc <= n - 1:
store1[words[start + cv * inc] - 1] += 1
store1[words[k - 1 - start + cv * inc] - 1] += 1
cv += 1
a1 = 2 * n // k - max(store1)
moves += a1
elif k % 2 == 1:
for s in range(k // 2 + 1):
store1 = [(0) for i in range(26)]
start = s
inc = k
cv = 0
counter = 0
if start != k // 2:
while start + cv * inc <= n - 1:
store1[words[start + cv * inc] - 1] += 1
store1[words[k - 1 - start + cv * inc] - 1] += 1
cv += 1
a1 = 2 * n // k - max(store1)
moves += a1
elif start == k // 2:
while start + cv * inc <= n - 1:
store1[words[start + cv * inc] - 1] += 1
cv += 1
a1 = n // k - max(store1)
moves += a1
print(moves)
|
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER VAR FUNC_CALL VAR ASSIGN VAR NUMBER IF BIN_OP VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR NUMBER ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE BIN_OP VAR BIN_OP VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR BIN_OP VAR BIN_OP VAR VAR NUMBER NUMBER VAR BIN_OP VAR BIN_OP BIN_OP BIN_OP VAR NUMBER VAR BIN_OP VAR VAR NUMBER NUMBER VAR NUMBER ASSIGN VAR BIN_OP BIN_OP BIN_OP NUMBER VAR VAR FUNC_CALL VAR VAR VAR VAR IF BIN_OP VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR BIN_OP BIN_OP VAR NUMBER NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR NUMBER ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER IF VAR BIN_OP VAR NUMBER WHILE BIN_OP VAR BIN_OP VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR BIN_OP VAR BIN_OP VAR VAR NUMBER NUMBER VAR BIN_OP VAR BIN_OP BIN_OP BIN_OP VAR NUMBER VAR BIN_OP VAR VAR NUMBER NUMBER VAR NUMBER ASSIGN VAR BIN_OP BIN_OP BIN_OP NUMBER VAR VAR FUNC_CALL VAR VAR VAR VAR IF VAR BIN_OP VAR NUMBER WHILE BIN_OP VAR BIN_OP VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR BIN_OP VAR BIN_OP VAR VAR NUMBER NUMBER VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR
|
Word $s$ of length $n$ is called $k$-complete if $s$ is a palindrome, i.e. $s_i=s_{n+1-i}$ for all $1 \le i \le n$; $s$ has a period of $k$, i.e. $s_i=s_{k+i}$ for all $1 \le i \le n-k$.
For example, "abaaba" is a $3$-complete word, while "abccba" is not.
Bob is given a word $s$ of length $n$ consisting of only lowercase Latin letters and an integer $k$, such that $n$ is divisible by $k$. He wants to convert $s$ to any $k$-complete word.
To do this Bob can choose some $i$ ($1 \le i \le n$) and replace the letter at position $i$ with some other lowercase Latin letter.
So now Bob wants to know the minimum number of letters he has to replace to convert $s$ to any $k$-complete word.
Note that Bob can do zero changes if the word $s$ is already $k$-complete.
You are required to answer $t$ test cases independently.
-----Input-----
The first line contains a single integer $t$ ($1 \le t\le 10^5$) — the number of test cases.
The first line of each test case contains two integers $n$ and $k$ ($1 \le k < n \le 2 \cdot 10^5$, $n$ is divisible by $k$).
The second line of each test case contains a word $s$ of length $n$.
It is guaranteed that word $s$ only contains lowercase Latin letters. And it is guaranteed that the sum of $n$ over all test cases will not exceed $2 \cdot 10^5$.
-----Output-----
For each test case, output one integer, representing the minimum number of characters he has to replace to convert $s$ to any $k$-complete word.
-----Example-----
Input
4
6 2
abaaba
6 3
abaaba
36 9
hippopotomonstrosesquippedaliophobia
21 7
wudixiaoxingxingheclp
Output
2
0
23
16
-----Note-----
In the first test case, one optimal solution is aaaaaa.
In the second test case, the given word itself is $k$-complete.
|
t = int(input())
for _ in range(t):
count = 0
n, k = map(int, input().split())
l = input()
s = list(l)
for i in range(k):
d = {}
j = 0
while i + j * k < n:
if s[i + j * k] in d:
d[s[i + j * k]] += 1
else:
d[s[i + j * k]] = 1
z = k - i - 1
if s[z + j * k] in d:
d[s[z + j * k]] += 1
else:
d[s[z + j * k]] = 1
j += 1
p = max(d, key=d.get)
j = 0
while i + j * k < n:
if s[i + j * k] != p:
s[i + j * k] = p
count += 1
z = k - i - 1
if s[z + j * k] != p:
s[z + j * k] = p
count += 1
j += 1
m = "".join(s)
print(count)
|
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR DICT ASSIGN VAR NUMBER WHILE BIN_OP VAR BIN_OP VAR VAR VAR IF VAR BIN_OP VAR BIN_OP VAR VAR VAR VAR VAR BIN_OP VAR BIN_OP VAR VAR NUMBER ASSIGN VAR VAR BIN_OP VAR BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF VAR BIN_OP VAR BIN_OP VAR VAR VAR VAR VAR BIN_OP VAR BIN_OP VAR VAR NUMBER ASSIGN VAR VAR BIN_OP VAR BIN_OP VAR VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR NUMBER WHILE BIN_OP VAR BIN_OP VAR VAR VAR IF VAR BIN_OP VAR BIN_OP VAR VAR VAR ASSIGN VAR BIN_OP VAR BIN_OP VAR VAR VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF VAR BIN_OP VAR BIN_OP VAR VAR VAR ASSIGN VAR BIN_OP VAR BIN_OP VAR VAR VAR VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL STRING VAR EXPR FUNC_CALL VAR VAR
|
Word $s$ of length $n$ is called $k$-complete if $s$ is a palindrome, i.e. $s_i=s_{n+1-i}$ for all $1 \le i \le n$; $s$ has a period of $k$, i.e. $s_i=s_{k+i}$ for all $1 \le i \le n-k$.
For example, "abaaba" is a $3$-complete word, while "abccba" is not.
Bob is given a word $s$ of length $n$ consisting of only lowercase Latin letters and an integer $k$, such that $n$ is divisible by $k$. He wants to convert $s$ to any $k$-complete word.
To do this Bob can choose some $i$ ($1 \le i \le n$) and replace the letter at position $i$ with some other lowercase Latin letter.
So now Bob wants to know the minimum number of letters he has to replace to convert $s$ to any $k$-complete word.
Note that Bob can do zero changes if the word $s$ is already $k$-complete.
You are required to answer $t$ test cases independently.
-----Input-----
The first line contains a single integer $t$ ($1 \le t\le 10^5$) — the number of test cases.
The first line of each test case contains two integers $n$ and $k$ ($1 \le k < n \le 2 \cdot 10^5$, $n$ is divisible by $k$).
The second line of each test case contains a word $s$ of length $n$.
It is guaranteed that word $s$ only contains lowercase Latin letters. And it is guaranteed that the sum of $n$ over all test cases will not exceed $2 \cdot 10^5$.
-----Output-----
For each test case, output one integer, representing the minimum number of characters he has to replace to convert $s$ to any $k$-complete word.
-----Example-----
Input
4
6 2
abaaba
6 3
abaaba
36 9
hippopotomonstrosesquippedaliophobia
21 7
wudixiaoxingxingheclp
Output
2
0
23
16
-----Note-----
In the first test case, one optimal solution is aaaaaa.
In the second test case, the given word itself is $k$-complete.
|
for t in range(int(input())):
n, k = [int(i) for i in input().split()]
s = input()
ss = 0
mm = []
for i in range(0, k):
kk = [0] * 26
for j in range(i, n, k):
kk[ord(s[j]) - ord("a")] += 1
mm.append(kk)
cc = []
for i in range(0, k // 2):
tt = [(mm[i][j] + mm[k - i - 1][j]) for j in range(0, 26)]
ss += sum(tt) - max(tt)
if k % 2:
tt = [mm[k // 2][j] for j in range(0, 26)]
ss += sum(tt) - max(tt)
print(ss)
|
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR BIN_OP LIST NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR VAR VAR VAR BIN_OP FUNC_CALL VAR VAR VAR FUNC_CALL VAR STRING NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR VAR VAR VAR BIN_OP BIN_OP VAR VAR NUMBER VAR VAR FUNC_CALL VAR NUMBER NUMBER VAR BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR VAR IF BIN_OP VAR NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER VAR VAR FUNC_CALL VAR NUMBER NUMBER VAR BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR
|
Word $s$ of length $n$ is called $k$-complete if $s$ is a palindrome, i.e. $s_i=s_{n+1-i}$ for all $1 \le i \le n$; $s$ has a period of $k$, i.e. $s_i=s_{k+i}$ for all $1 \le i \le n-k$.
For example, "abaaba" is a $3$-complete word, while "abccba" is not.
Bob is given a word $s$ of length $n$ consisting of only lowercase Latin letters and an integer $k$, such that $n$ is divisible by $k$. He wants to convert $s$ to any $k$-complete word.
To do this Bob can choose some $i$ ($1 \le i \le n$) and replace the letter at position $i$ with some other lowercase Latin letter.
So now Bob wants to know the minimum number of letters he has to replace to convert $s$ to any $k$-complete word.
Note that Bob can do zero changes if the word $s$ is already $k$-complete.
You are required to answer $t$ test cases independently.
-----Input-----
The first line contains a single integer $t$ ($1 \le t\le 10^5$) — the number of test cases.
The first line of each test case contains two integers $n$ and $k$ ($1 \le k < n \le 2 \cdot 10^5$, $n$ is divisible by $k$).
The second line of each test case contains a word $s$ of length $n$.
It is guaranteed that word $s$ only contains lowercase Latin letters. And it is guaranteed that the sum of $n$ over all test cases will not exceed $2 \cdot 10^5$.
-----Output-----
For each test case, output one integer, representing the minimum number of characters he has to replace to convert $s$ to any $k$-complete word.
-----Example-----
Input
4
6 2
abaaba
6 3
abaaba
36 9
hippopotomonstrosesquippedaliophobia
21 7
wudixiaoxingxingheclp
Output
2
0
23
16
-----Note-----
In the first test case, one optimal solution is aaaaaa.
In the second test case, the given word itself is $k$-complete.
|
t = int(input())
for z in range(0, t):
n, k = map(int, input().split())
word = []
ip = list(input())
temp = []
num = int(n / k)
count = 0
for i in range(0, n):
temp.append(ip[i])
count = count + 1
if count % k == 0:
word.append(temp)
temp = []
count = 0
ppp = k
if k % 2 != 0:
ppp = k + 1
ppp = int(ppp / 2)
for i in range(0, ppp):
dict = {}
indexfirst = i
indexlast = k - 1 - i
if indexfirst != indexlast:
for j in range(0, num):
aa = word[j][indexfirst]
bb = word[j][indexlast]
if aa not in dict:
dict[aa] = 1
else:
dict[aa] = dict[aa] + 1
if bb not in dict:
dict[bb] = 1
else:
dict[bb] = dict[bb] + 1
max_key = max(dict, key=dict.get)
val = dict[max_key]
num_replace = 2 * num - val
count = count + num_replace
else:
for j in range(0, num):
aa = word[j][indexfirst]
if aa not in dict:
dict[aa] = 1
else:
dict[aa] = dict[aa] + 1
max_key = max(dict, key=dict.get)
val = dict[max_key]
num_replace = num - val
count = count + num_replace
print(count)
|
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER IF BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR LIST ASSIGN VAR NUMBER ASSIGN VAR VAR IF BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR DICT ASSIGN VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR NUMBER VAR IF VAR VAR FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR IF VAR VAR ASSIGN VAR VAR NUMBER ASSIGN VAR VAR BIN_OP VAR VAR NUMBER IF VAR VAR ASSIGN VAR VAR NUMBER ASSIGN VAR VAR BIN_OP VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP NUMBER VAR VAR ASSIGN VAR BIN_OP VAR VAR FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR VAR VAR IF VAR VAR ASSIGN VAR VAR NUMBER ASSIGN VAR VAR BIN_OP VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR
|
Word $s$ of length $n$ is called $k$-complete if $s$ is a palindrome, i.e. $s_i=s_{n+1-i}$ for all $1 \le i \le n$; $s$ has a period of $k$, i.e. $s_i=s_{k+i}$ for all $1 \le i \le n-k$.
For example, "abaaba" is a $3$-complete word, while "abccba" is not.
Bob is given a word $s$ of length $n$ consisting of only lowercase Latin letters and an integer $k$, such that $n$ is divisible by $k$. He wants to convert $s$ to any $k$-complete word.
To do this Bob can choose some $i$ ($1 \le i \le n$) and replace the letter at position $i$ with some other lowercase Latin letter.
So now Bob wants to know the minimum number of letters he has to replace to convert $s$ to any $k$-complete word.
Note that Bob can do zero changes if the word $s$ is already $k$-complete.
You are required to answer $t$ test cases independently.
-----Input-----
The first line contains a single integer $t$ ($1 \le t\le 10^5$) — the number of test cases.
The first line of each test case contains two integers $n$ and $k$ ($1 \le k < n \le 2 \cdot 10^5$, $n$ is divisible by $k$).
The second line of each test case contains a word $s$ of length $n$.
It is guaranteed that word $s$ only contains lowercase Latin letters. And it is guaranteed that the sum of $n$ over all test cases will not exceed $2 \cdot 10^5$.
-----Output-----
For each test case, output one integer, representing the minimum number of characters he has to replace to convert $s$ to any $k$-complete word.
-----Example-----
Input
4
6 2
abaaba
6 3
abaaba
36 9
hippopotomonstrosesquippedaliophobia
21 7
wudixiaoxingxingheclp
Output
2
0
23
16
-----Note-----
In the first test case, one optimal solution is aaaaaa.
In the second test case, the given word itself is $k$-complete.
|
t = int(input())
for i in range(t):
inp1 = list(map(int, input().split()))
n = inp1[0]
k = inp1[1]
s = input()
anss = 0
for p in range(int(k / 2)):
aplha = ""
for j in range(int(n / k)):
aplha = aplha + s[j * k + p] + s[(j + 1) * k - 1 - p]
dicti = [0] * 28
for j in aplha:
dicti[ord(j) - 96] += 1
maxfre = max(dicti)
anss += 2 * int(n / k) - maxfre
if k & 1:
p = int(k / 2 + 1)
aplha = ""
for j in range(int(n / k)):
aplha = aplha + s[j * k + p - 1]
dicti = [0] * 28
for j in aplha:
dicti[ord(j) - 96] += 1
maxfre = max(dicti)
anss += int(n / k) - maxfre
print(int(anss))
|
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR STRING FOR VAR FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR BIN_OP BIN_OP VAR VAR VAR VAR BIN_OP BIN_OP BIN_OP BIN_OP VAR NUMBER VAR NUMBER VAR ASSIGN VAR BIN_OP LIST NUMBER NUMBER FOR VAR VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR BIN_OP BIN_OP NUMBER FUNC_CALL VAR BIN_OP VAR VAR VAR IF BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP BIN_OP VAR NUMBER NUMBER ASSIGN VAR STRING FOR VAR FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR VAR BIN_OP BIN_OP BIN_OP VAR VAR VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER NUMBER FOR VAR VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR BIN_OP FUNC_CALL VAR BIN_OP VAR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR
|
Word $s$ of length $n$ is called $k$-complete if $s$ is a palindrome, i.e. $s_i=s_{n+1-i}$ for all $1 \le i \le n$; $s$ has a period of $k$, i.e. $s_i=s_{k+i}$ for all $1 \le i \le n-k$.
For example, "abaaba" is a $3$-complete word, while "abccba" is not.
Bob is given a word $s$ of length $n$ consisting of only lowercase Latin letters and an integer $k$, such that $n$ is divisible by $k$. He wants to convert $s$ to any $k$-complete word.
To do this Bob can choose some $i$ ($1 \le i \le n$) and replace the letter at position $i$ with some other lowercase Latin letter.
So now Bob wants to know the minimum number of letters he has to replace to convert $s$ to any $k$-complete word.
Note that Bob can do zero changes if the word $s$ is already $k$-complete.
You are required to answer $t$ test cases independently.
-----Input-----
The first line contains a single integer $t$ ($1 \le t\le 10^5$) — the number of test cases.
The first line of each test case contains two integers $n$ and $k$ ($1 \le k < n \le 2 \cdot 10^5$, $n$ is divisible by $k$).
The second line of each test case contains a word $s$ of length $n$.
It is guaranteed that word $s$ only contains lowercase Latin letters. And it is guaranteed that the sum of $n$ over all test cases will not exceed $2 \cdot 10^5$.
-----Output-----
For each test case, output one integer, representing the minimum number of characters he has to replace to convert $s$ to any $k$-complete word.
-----Example-----
Input
4
6 2
abaaba
6 3
abaaba
36 9
hippopotomonstrosesquippedaliophobia
21 7
wudixiaoxingxingheclp
Output
2
0
23
16
-----Note-----
In the first test case, one optimal solution is aaaaaa.
In the second test case, the given word itself is $k$-complete.
|
for _ in range(int(input())):
n, k = map(int, input().split())
p = n // k
s = "0" + input()
count = 0
for i in range(1, k // 2 + 1):
dict = {}
maxx = 0
for j in range(p):
if s[j * k + i] in dict:
dict[s[j * k + i]] += 1
else:
dict[s[j * k + i]] = 1
maxx = max(maxx, dict[s[j * k + i]])
for j in range(1, p + 1):
if s[j * k - i + 1] in dict:
dict[s[j * k - i + 1]] += 1
else:
dict[s[j * k - i + 1]] = 1
maxx = max(maxx, dict[s[j * k - i + 1]])
count += 2 * p - maxx
if k % 2 != 0:
maxx = 0
dict = {}
i = k // 2 + 1
for j in range(p):
if s[j * k + i] in dict:
dict[s[j * k + i]] += 1
else:
dict[s[j * k + i]] = 1
maxx = max(maxx, dict[s[j * k + i]])
count += p - maxx
print(count)
|
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP STRING FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP BIN_OP VAR NUMBER NUMBER ASSIGN VAR DICT ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR BIN_OP BIN_OP VAR VAR VAR VAR VAR VAR BIN_OP BIN_OP VAR VAR VAR NUMBER ASSIGN VAR VAR BIN_OP BIN_OP VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR BIN_OP BIN_OP VAR VAR VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER IF VAR BIN_OP BIN_OP BIN_OP VAR VAR VAR NUMBER VAR VAR VAR BIN_OP BIN_OP BIN_OP VAR VAR VAR NUMBER NUMBER ASSIGN VAR VAR BIN_OP BIN_OP BIN_OP VAR VAR VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR BIN_OP BIN_OP BIN_OP VAR VAR VAR NUMBER VAR BIN_OP BIN_OP NUMBER VAR VAR IF BIN_OP VAR NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR DICT ASSIGN VAR BIN_OP BIN_OP VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR BIN_OP BIN_OP VAR VAR VAR VAR VAR VAR BIN_OP BIN_OP VAR VAR VAR NUMBER ASSIGN VAR VAR BIN_OP BIN_OP VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR BIN_OP BIN_OP VAR VAR VAR VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR
|
Word $s$ of length $n$ is called $k$-complete if $s$ is a palindrome, i.e. $s_i=s_{n+1-i}$ for all $1 \le i \le n$; $s$ has a period of $k$, i.e. $s_i=s_{k+i}$ for all $1 \le i \le n-k$.
For example, "abaaba" is a $3$-complete word, while "abccba" is not.
Bob is given a word $s$ of length $n$ consisting of only lowercase Latin letters and an integer $k$, such that $n$ is divisible by $k$. He wants to convert $s$ to any $k$-complete word.
To do this Bob can choose some $i$ ($1 \le i \le n$) and replace the letter at position $i$ with some other lowercase Latin letter.
So now Bob wants to know the minimum number of letters he has to replace to convert $s$ to any $k$-complete word.
Note that Bob can do zero changes if the word $s$ is already $k$-complete.
You are required to answer $t$ test cases independently.
-----Input-----
The first line contains a single integer $t$ ($1 \le t\le 10^5$) — the number of test cases.
The first line of each test case contains two integers $n$ and $k$ ($1 \le k < n \le 2 \cdot 10^5$, $n$ is divisible by $k$).
The second line of each test case contains a word $s$ of length $n$.
It is guaranteed that word $s$ only contains lowercase Latin letters. And it is guaranteed that the sum of $n$ over all test cases will not exceed $2 \cdot 10^5$.
-----Output-----
For each test case, output one integer, representing the minimum number of characters he has to replace to convert $s$ to any $k$-complete word.
-----Example-----
Input
4
6 2
abaaba
6 3
abaaba
36 9
hippopotomonstrosesquippedaliophobia
21 7
wudixiaoxingxingheclp
Output
2
0
23
16
-----Note-----
In the first test case, one optimal solution is aaaaaa.
In the second test case, the given word itself is $k$-complete.
|
t = int(input())
for _ in range(t):
n, k = map(int, input().split())
a = input()
c = 0
for i in range(k // 2):
d = dict()
for j in range(i, n, k):
if a[j] in d:
d[a[j]] += 1
else:
d[a[j]] = 1
for j in range(k - i - 1, n, k):
if a[j] in d:
d[a[j]] += 1
else:
d[a[j]] = 1
maxi = 0
for i in d:
if d[i] > maxi:
maxi = d[i]
p = i
c += maxi
if k % 2 == 1:
d = dict()
for j in range(k // 2, n, k):
if a[j] in d:
d[a[j]] += 1
else:
d[a[j]] = 1
maxi = 0
for i in d:
if d[i] > maxi:
maxi = d[i]
p = i
c += maxi
print(n - c)
|
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR VAR VAR IF VAR VAR VAR VAR VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR NUMBER VAR VAR IF VAR VAR VAR VAR VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR IF VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR VAR IF BIN_OP VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR IF VAR VAR VAR VAR VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR IF VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR
|
Word $s$ of length $n$ is called $k$-complete if $s$ is a palindrome, i.e. $s_i=s_{n+1-i}$ for all $1 \le i \le n$; $s$ has a period of $k$, i.e. $s_i=s_{k+i}$ for all $1 \le i \le n-k$.
For example, "abaaba" is a $3$-complete word, while "abccba" is not.
Bob is given a word $s$ of length $n$ consisting of only lowercase Latin letters and an integer $k$, such that $n$ is divisible by $k$. He wants to convert $s$ to any $k$-complete word.
To do this Bob can choose some $i$ ($1 \le i \le n$) and replace the letter at position $i$ with some other lowercase Latin letter.
So now Bob wants to know the minimum number of letters he has to replace to convert $s$ to any $k$-complete word.
Note that Bob can do zero changes if the word $s$ is already $k$-complete.
You are required to answer $t$ test cases independently.
-----Input-----
The first line contains a single integer $t$ ($1 \le t\le 10^5$) — the number of test cases.
The first line of each test case contains two integers $n$ and $k$ ($1 \le k < n \le 2 \cdot 10^5$, $n$ is divisible by $k$).
The second line of each test case contains a word $s$ of length $n$.
It is guaranteed that word $s$ only contains lowercase Latin letters. And it is guaranteed that the sum of $n$ over all test cases will not exceed $2 \cdot 10^5$.
-----Output-----
For each test case, output one integer, representing the minimum number of characters he has to replace to convert $s$ to any $k$-complete word.
-----Example-----
Input
4
6 2
abaaba
6 3
abaaba
36 9
hippopotomonstrosesquippedaliophobia
21 7
wudixiaoxingxingheclp
Output
2
0
23
16
-----Note-----
In the first test case, one optimal solution is aaaaaa.
In the second test case, the given word itself is $k$-complete.
|
t = int(input())
for i in range(t):
n, k = map(int, input().split())
s = list(input())
ans = 0
for i in range(k // 2):
cnt = [(0) for i in range(26)]
st = 0
while st + k - 1 < n:
i1 = st + i
i2 = st + (k - 1 - i)
cnt[ord(s[i1]) - ord("a")] += 1
cnt[ord(s[i2]) - ord("a")] += 1
st += k
req = 2 * (n // k)
mx = max(cnt)
ans += req - mx
if k % 2 == 1:
cnt2 = [(0) for i in range(26)]
for i in range(k // 2, n, k):
cnt2[ord(s[i]) - ord("a")] += 1
req = n // k
mx = max(cnt2)
ans += req - mx
print(ans)
|
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER WHILE BIN_OP BIN_OP VAR VAR NUMBER VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR BIN_OP BIN_OP VAR NUMBER VAR VAR BIN_OP FUNC_CALL VAR VAR VAR FUNC_CALL VAR STRING NUMBER VAR BIN_OP FUNC_CALL VAR VAR VAR FUNC_CALL VAR STRING NUMBER VAR VAR ASSIGN VAR BIN_OP NUMBER BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR BIN_OP VAR VAR IF BIN_OP VAR NUMBER NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR VAR BIN_OP FUNC_CALL VAR VAR VAR FUNC_CALL VAR STRING NUMBER ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR
|
Word $s$ of length $n$ is called $k$-complete if $s$ is a palindrome, i.e. $s_i=s_{n+1-i}$ for all $1 \le i \le n$; $s$ has a period of $k$, i.e. $s_i=s_{k+i}$ for all $1 \le i \le n-k$.
For example, "abaaba" is a $3$-complete word, while "abccba" is not.
Bob is given a word $s$ of length $n$ consisting of only lowercase Latin letters and an integer $k$, such that $n$ is divisible by $k$. He wants to convert $s$ to any $k$-complete word.
To do this Bob can choose some $i$ ($1 \le i \le n$) and replace the letter at position $i$ with some other lowercase Latin letter.
So now Bob wants to know the minimum number of letters he has to replace to convert $s$ to any $k$-complete word.
Note that Bob can do zero changes if the word $s$ is already $k$-complete.
You are required to answer $t$ test cases independently.
-----Input-----
The first line contains a single integer $t$ ($1 \le t\le 10^5$) — the number of test cases.
The first line of each test case contains two integers $n$ and $k$ ($1 \le k < n \le 2 \cdot 10^5$, $n$ is divisible by $k$).
The second line of each test case contains a word $s$ of length $n$.
It is guaranteed that word $s$ only contains lowercase Latin letters. And it is guaranteed that the sum of $n$ over all test cases will not exceed $2 \cdot 10^5$.
-----Output-----
For each test case, output one integer, representing the minimum number of characters he has to replace to convert $s$ to any $k$-complete word.
-----Example-----
Input
4
6 2
abaaba
6 3
abaaba
36 9
hippopotomonstrosesquippedaliophobia
21 7
wudixiaoxingxingheclp
Output
2
0
23
16
-----Note-----
In the first test case, one optimal solution is aaaaaa.
In the second test case, the given word itself is $k$-complete.
|
DEBUG = 0
def least_cost(alpha_cnt, k_clusters, i, mid):
for k_wrd in k_clusters:
ind_s = ord(k_wrd[i]) - 97
alpha_cnt[ind_s] += 1
if DEBUG:
print(k_wrd, k_wrd[i], k_wrd[-i - 1])
print(ind_s)
if not mid:
ind_e = ord(k_wrd[-i - 1]) - 97
alpha_cnt[ind_e] += 1
if DEBUG:
print(ind_e)
if DEBUG:
print("ind: ", i, alpha_cnt)
max_ind = alpha_cnt.index(max(alpha_cnt))
cost = 0
for i in range(26):
if i == max_ind:
continue
cost += alpha_cnt[i]
return cost
def main():
t = int(input())
test_no = 0
alpha_cnt = [(0) for x in range(26)]
while test_no < t:
n, k = map(int, input().split())
s = input()
k_clusters = []
for i in range(n // k):
k_clusters.append(s[i * k : (i + 1) * k])
if DEBUG:
print(k_clusters)
cost = 0
for i in range(k // 2):
lc = least_cost(alpha_cnt.copy(), k_clusters, i, 0)
cost += lc
if k % 2 == 1:
cost += least_cost(alpha_cnt.copy(), k_clusters, k // 2, 1)
print(cost)
test_no += 1
main()
|
ASSIGN VAR NUMBER FUNC_DEF FOR VAR VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR VAR NUMBER VAR VAR NUMBER IF VAR EXPR FUNC_CALL VAR VAR VAR VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR IF VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR BIN_OP VAR NUMBER NUMBER VAR VAR NUMBER IF VAR EXPR FUNC_CALL VAR VAR IF VAR EXPR FUNC_CALL VAR STRING VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER IF VAR VAR VAR VAR VAR RETURN VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR NUMBER WHILE VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR BIN_OP VAR VAR BIN_OP BIN_OP VAR NUMBER VAR IF VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR NUMBER VAR VAR IF BIN_OP VAR NUMBER NUMBER VAR FUNC_CALL VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR VAR NUMBER EXPR FUNC_CALL VAR
|
Word $s$ of length $n$ is called $k$-complete if $s$ is a palindrome, i.e. $s_i=s_{n+1-i}$ for all $1 \le i \le n$; $s$ has a period of $k$, i.e. $s_i=s_{k+i}$ for all $1 \le i \le n-k$.
For example, "abaaba" is a $3$-complete word, while "abccba" is not.
Bob is given a word $s$ of length $n$ consisting of only lowercase Latin letters and an integer $k$, such that $n$ is divisible by $k$. He wants to convert $s$ to any $k$-complete word.
To do this Bob can choose some $i$ ($1 \le i \le n$) and replace the letter at position $i$ with some other lowercase Latin letter.
So now Bob wants to know the minimum number of letters he has to replace to convert $s$ to any $k$-complete word.
Note that Bob can do zero changes if the word $s$ is already $k$-complete.
You are required to answer $t$ test cases independently.
-----Input-----
The first line contains a single integer $t$ ($1 \le t\le 10^5$) — the number of test cases.
The first line of each test case contains two integers $n$ and $k$ ($1 \le k < n \le 2 \cdot 10^5$, $n$ is divisible by $k$).
The second line of each test case contains a word $s$ of length $n$.
It is guaranteed that word $s$ only contains lowercase Latin letters. And it is guaranteed that the sum of $n$ over all test cases will not exceed $2 \cdot 10^5$.
-----Output-----
For each test case, output one integer, representing the minimum number of characters he has to replace to convert $s$ to any $k$-complete word.
-----Example-----
Input
4
6 2
abaaba
6 3
abaaba
36 9
hippopotomonstrosesquippedaliophobia
21 7
wudixiaoxingxingheclp
Output
2
0
23
16
-----Note-----
In the first test case, one optimal solution is aaaaaa.
In the second test case, the given word itself is $k$-complete.
|
for _ in range(int(input())):
n, k = map(int, input().split())
s = input()
ans = 0
for i in range(k // 2):
l = [0] * 26
c = 0
for j in range(i, n, k):
c += 1
val = ord(s[j]) - ord("a")
l[val] += 1
for j in range(k - i - 1, n, k):
c += 1
val = ord(s[j]) - ord("a")
l[val] += 1
ans += c - max(l)
if k % 2 != 0:
c = 0
l = [0] * 26
for j in range(k // 2, n, k):
c += 1
val = ord(s[j]) - ord("a")
l[val] += 1
ans += c - max(l)
print(ans)
|
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR VAR VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR VAR FUNC_CALL VAR STRING VAR VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR NUMBER VAR VAR VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR VAR FUNC_CALL VAR STRING VAR VAR NUMBER VAR BIN_OP VAR FUNC_CALL VAR VAR IF BIN_OP VAR NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR VAR FUNC_CALL VAR STRING VAR VAR NUMBER VAR BIN_OP VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR
|
Word $s$ of length $n$ is called $k$-complete if $s$ is a palindrome, i.e. $s_i=s_{n+1-i}$ for all $1 \le i \le n$; $s$ has a period of $k$, i.e. $s_i=s_{k+i}$ for all $1 \le i \le n-k$.
For example, "abaaba" is a $3$-complete word, while "abccba" is not.
Bob is given a word $s$ of length $n$ consisting of only lowercase Latin letters and an integer $k$, such that $n$ is divisible by $k$. He wants to convert $s$ to any $k$-complete word.
To do this Bob can choose some $i$ ($1 \le i \le n$) and replace the letter at position $i$ with some other lowercase Latin letter.
So now Bob wants to know the minimum number of letters he has to replace to convert $s$ to any $k$-complete word.
Note that Bob can do zero changes if the word $s$ is already $k$-complete.
You are required to answer $t$ test cases independently.
-----Input-----
The first line contains a single integer $t$ ($1 \le t\le 10^5$) — the number of test cases.
The first line of each test case contains two integers $n$ and $k$ ($1 \le k < n \le 2 \cdot 10^5$, $n$ is divisible by $k$).
The second line of each test case contains a word $s$ of length $n$.
It is guaranteed that word $s$ only contains lowercase Latin letters. And it is guaranteed that the sum of $n$ over all test cases will not exceed $2 \cdot 10^5$.
-----Output-----
For each test case, output one integer, representing the minimum number of characters he has to replace to convert $s$ to any $k$-complete word.
-----Example-----
Input
4
6 2
abaaba
6 3
abaaba
36 9
hippopotomonstrosesquippedaliophobia
21 7
wudixiaoxingxingheclp
Output
2
0
23
16
-----Note-----
In the first test case, one optimal solution is aaaaaa.
In the second test case, the given word itself is $k$-complete.
|
from sys import stdin
t = int(input())
for _ in range(t):
n, k = map(int, stdin.readline().split())
s = stdin.readline().strip()
ans = 0
vis = [(False) for i in range(n)]
for i in range(n):
if vis[i]:
continue
check = set()
for j in range(i, n, k):
check.add(j)
new = set()
for j in check:
new.add(n - 1 - j)
for j in new:
check.add(j)
cnt = [(0) for i in range(26)]
for j in check:
cnt[ord(s[j]) - 97] += 1
vis[j] = True
ans += len(check) - max(cnt)
print(ans)
|
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR ASSIGN VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FOR VAR VAR EXPR FUNC_CALL VAR BIN_OP BIN_OP VAR NUMBER VAR FOR VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR NUMBER FOR VAR VAR VAR BIN_OP FUNC_CALL VAR VAR VAR NUMBER NUMBER ASSIGN VAR VAR NUMBER VAR BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR
|
Word $s$ of length $n$ is called $k$-complete if $s$ is a palindrome, i.e. $s_i=s_{n+1-i}$ for all $1 \le i \le n$; $s$ has a period of $k$, i.e. $s_i=s_{k+i}$ for all $1 \le i \le n-k$.
For example, "abaaba" is a $3$-complete word, while "abccba" is not.
Bob is given a word $s$ of length $n$ consisting of only lowercase Latin letters and an integer $k$, such that $n$ is divisible by $k$. He wants to convert $s$ to any $k$-complete word.
To do this Bob can choose some $i$ ($1 \le i \le n$) and replace the letter at position $i$ with some other lowercase Latin letter.
So now Bob wants to know the minimum number of letters he has to replace to convert $s$ to any $k$-complete word.
Note that Bob can do zero changes if the word $s$ is already $k$-complete.
You are required to answer $t$ test cases independently.
-----Input-----
The first line contains a single integer $t$ ($1 \le t\le 10^5$) — the number of test cases.
The first line of each test case contains two integers $n$ and $k$ ($1 \le k < n \le 2 \cdot 10^5$, $n$ is divisible by $k$).
The second line of each test case contains a word $s$ of length $n$.
It is guaranteed that word $s$ only contains lowercase Latin letters. And it is guaranteed that the sum of $n$ over all test cases will not exceed $2 \cdot 10^5$.
-----Output-----
For each test case, output one integer, representing the minimum number of characters he has to replace to convert $s$ to any $k$-complete word.
-----Example-----
Input
4
6 2
abaaba
6 3
abaaba
36 9
hippopotomonstrosesquippedaliophobia
21 7
wudixiaoxingxingheclp
Output
2
0
23
16
-----Note-----
In the first test case, one optimal solution is aaaaaa.
In the second test case, the given word itself is $k$-complete.
|
import sys
input = sys.stdin.readline
Q = int(input())
Query = []
for _ in range(Q):
N, K = map(int, input().split())
S = list(input().rstrip())
Query.append((N, K, S))
for N, K, S in Query:
ans = 0
L = N // K
for r in range(K):
if K % 2 == 0 and r >= K // 2:
break
if K % 2 == 1 and r == K // 2:
dic = {}
for i in range(L):
s = S[i * K + r]
if s in dic:
dic[s] += 1
else:
dic[s] = 1
ans += L - max(list(dic.values()))
break
else:
dic = {}
for i in range(L):
Ss = [S[i * K + r], S[i * K + (K - r - 1)]]
for s in Ss:
if s in dic:
dic[s] += 1
else:
dic[s] = 1
ans += 2 * L - max(list(dic.values()))
print(ans)
|
IMPORT ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR VAR VAR VAR FOR VAR VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR VAR FOR VAR FUNC_CALL VAR VAR IF BIN_OP VAR NUMBER NUMBER VAR BIN_OP VAR NUMBER IF BIN_OP VAR NUMBER NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR DICT FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR BIN_OP BIN_OP VAR VAR VAR IF VAR VAR VAR VAR NUMBER ASSIGN VAR VAR NUMBER VAR BIN_OP VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR DICT FOR VAR FUNC_CALL VAR VAR ASSIGN VAR LIST VAR BIN_OP BIN_OP VAR VAR VAR VAR BIN_OP BIN_OP VAR VAR BIN_OP BIN_OP VAR VAR NUMBER FOR VAR VAR IF VAR VAR VAR VAR NUMBER ASSIGN VAR VAR NUMBER VAR BIN_OP BIN_OP NUMBER VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR
|
Word $s$ of length $n$ is called $k$-complete if $s$ is a palindrome, i.e. $s_i=s_{n+1-i}$ for all $1 \le i \le n$; $s$ has a period of $k$, i.e. $s_i=s_{k+i}$ for all $1 \le i \le n-k$.
For example, "abaaba" is a $3$-complete word, while "abccba" is not.
Bob is given a word $s$ of length $n$ consisting of only lowercase Latin letters and an integer $k$, such that $n$ is divisible by $k$. He wants to convert $s$ to any $k$-complete word.
To do this Bob can choose some $i$ ($1 \le i \le n$) and replace the letter at position $i$ with some other lowercase Latin letter.
So now Bob wants to know the minimum number of letters he has to replace to convert $s$ to any $k$-complete word.
Note that Bob can do zero changes if the word $s$ is already $k$-complete.
You are required to answer $t$ test cases independently.
-----Input-----
The first line contains a single integer $t$ ($1 \le t\le 10^5$) — the number of test cases.
The first line of each test case contains two integers $n$ and $k$ ($1 \le k < n \le 2 \cdot 10^5$, $n$ is divisible by $k$).
The second line of each test case contains a word $s$ of length $n$.
It is guaranteed that word $s$ only contains lowercase Latin letters. And it is guaranteed that the sum of $n$ over all test cases will not exceed $2 \cdot 10^5$.
-----Output-----
For each test case, output one integer, representing the minimum number of characters he has to replace to convert $s$ to any $k$-complete word.
-----Example-----
Input
4
6 2
abaaba
6 3
abaaba
36 9
hippopotomonstrosesquippedaliophobia
21 7
wudixiaoxingxingheclp
Output
2
0
23
16
-----Note-----
In the first test case, one optimal solution is aaaaaa.
In the second test case, the given word itself is $k$-complete.
|
import sys
input = lambda: sys.stdin.readline().strip()
ipnut = input
for i in range(int(input())):
n, k = map(int, ipnut().split())
s = list(ipnut())
l = n // k
lol = [[] for _ in range(k)]
lo = [[] for _ in range((k + 1) // 2)]
for i in range(n):
lol[i % k].append(s[i])
for i in range(k // 2):
lo[i] = lol[i] + lol[-i - 1]
if k % 2:
lo[k // 2] = lol[k // 2]
ans = 0
for i in lo:
s = {}
for j in i:
s[j] = s.get(j, 0) + 1
ans += len(i) - max(s.values())
print(ans)
|
IMPORT ASSIGN VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR LIST VAR FUNC_CALL VAR VAR ASSIGN VAR LIST VAR FUNC_CALL VAR BIN_OP BIN_OP VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER IF BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR ASSIGN VAR DICT FOR VAR VAR ASSIGN VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER VAR BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR
|
Word $s$ of length $n$ is called $k$-complete if $s$ is a palindrome, i.e. $s_i=s_{n+1-i}$ for all $1 \le i \le n$; $s$ has a period of $k$, i.e. $s_i=s_{k+i}$ for all $1 \le i \le n-k$.
For example, "abaaba" is a $3$-complete word, while "abccba" is not.
Bob is given a word $s$ of length $n$ consisting of only lowercase Latin letters and an integer $k$, such that $n$ is divisible by $k$. He wants to convert $s$ to any $k$-complete word.
To do this Bob can choose some $i$ ($1 \le i \le n$) and replace the letter at position $i$ with some other lowercase Latin letter.
So now Bob wants to know the minimum number of letters he has to replace to convert $s$ to any $k$-complete word.
Note that Bob can do zero changes if the word $s$ is already $k$-complete.
You are required to answer $t$ test cases independently.
-----Input-----
The first line contains a single integer $t$ ($1 \le t\le 10^5$) — the number of test cases.
The first line of each test case contains two integers $n$ and $k$ ($1 \le k < n \le 2 \cdot 10^5$, $n$ is divisible by $k$).
The second line of each test case contains a word $s$ of length $n$.
It is guaranteed that word $s$ only contains lowercase Latin letters. And it is guaranteed that the sum of $n$ over all test cases will not exceed $2 \cdot 10^5$.
-----Output-----
For each test case, output one integer, representing the minimum number of characters he has to replace to convert $s$ to any $k$-complete word.
-----Example-----
Input
4
6 2
abaaba
6 3
abaaba
36 9
hippopotomonstrosesquippedaliophobia
21 7
wudixiaoxingxingheclp
Output
2
0
23
16
-----Note-----
In the first test case, one optimal solution is aaaaaa.
In the second test case, the given word itself is $k$-complete.
|
import sys
def solve():
split = str.split
t = int(sys.stdin.readline())
while t > 0:
n, k = map(int, split(sys.stdin.readline()))
s = sys.stdin.readline()
ans = 0
index = 0
max_chars = []
mappend = max_chars.append
while index <= (k - 1) // 2:
mx = 0
mx_char = "a"
chars = dict()
pindex = k - index - 1
cur_index = index
while cur_index < n:
count = chars.get(s[cur_index], 0) + 1
chars[s[cur_index]] = count
if count > mx:
mx_char = s[cur_index]
mx = count
cur_index += k
while pindex < n:
count = chars.get(s[pindex], 0) + 1
chars[s[pindex]] = count
if count > mx:
mx_char = s[pindex]
mx = count
pindex += k
mappend(mx_char)
index += 1
index = 0
while index <= (k - 1) // 2:
max_char = max_chars[index]
pindex = k - index - 1
cur_index = index
if pindex != cur_index:
while pindex < n:
if s[pindex] != max_char:
ans += 1
pindex += k
while cur_index < n:
if s[cur_index] != max_char:
ans += 1
cur_index += k
index += 1
print(ans)
t -= 1
solve()
|
IMPORT FUNC_DEF ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR WHILE VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR LIST ASSIGN VAR VAR WHILE VAR BIN_OP BIN_OP VAR NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR STRING ASSIGN VAR FUNC_CALL VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR VAR WHILE VAR VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR VAR NUMBER NUMBER ASSIGN VAR VAR VAR VAR IF VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR VAR WHILE VAR VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR VAR NUMBER NUMBER ASSIGN VAR VAR VAR VAR IF VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR NUMBER WHILE VAR BIN_OP BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR VAR IF VAR VAR WHILE VAR VAR IF VAR VAR VAR VAR NUMBER VAR VAR WHILE VAR VAR IF VAR VAR VAR VAR NUMBER VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR NUMBER EXPR FUNC_CALL VAR
|
Word $s$ of length $n$ is called $k$-complete if $s$ is a palindrome, i.e. $s_i=s_{n+1-i}$ for all $1 \le i \le n$; $s$ has a period of $k$, i.e. $s_i=s_{k+i}$ for all $1 \le i \le n-k$.
For example, "abaaba" is a $3$-complete word, while "abccba" is not.
Bob is given a word $s$ of length $n$ consisting of only lowercase Latin letters and an integer $k$, such that $n$ is divisible by $k$. He wants to convert $s$ to any $k$-complete word.
To do this Bob can choose some $i$ ($1 \le i \le n$) and replace the letter at position $i$ with some other lowercase Latin letter.
So now Bob wants to know the minimum number of letters he has to replace to convert $s$ to any $k$-complete word.
Note that Bob can do zero changes if the word $s$ is already $k$-complete.
You are required to answer $t$ test cases independently.
-----Input-----
The first line contains a single integer $t$ ($1 \le t\le 10^5$) — the number of test cases.
The first line of each test case contains two integers $n$ and $k$ ($1 \le k < n \le 2 \cdot 10^5$, $n$ is divisible by $k$).
The second line of each test case contains a word $s$ of length $n$.
It is guaranteed that word $s$ only contains lowercase Latin letters. And it is guaranteed that the sum of $n$ over all test cases will not exceed $2 \cdot 10^5$.
-----Output-----
For each test case, output one integer, representing the minimum number of characters he has to replace to convert $s$ to any $k$-complete word.
-----Example-----
Input
4
6 2
abaaba
6 3
abaaba
36 9
hippopotomonstrosesquippedaliophobia
21 7
wudixiaoxingxingheclp
Output
2
0
23
16
-----Note-----
In the first test case, one optimal solution is aaaaaa.
In the second test case, the given word itself is $k$-complete.
|
for t in range(int(input())):
n, l = map(int, input().split())
s = input()
d = n // l
ans = 0
for k in range(0, l // 2):
a = [(0) for o in range(26)]
b = [(0) for o in range(26)]
for p in range(k, n, l):
a[ord(s[p]) - 97] += 1
b[ord(s[l - p - 1]) - 97] += 1
minn = 10**9
for p in range(26):
minn = min(minn, 2 * d - a[p] - b[p])
ans += minn
if l % 2 == 1:
k = l // 2
minn = 10**9
a = [(0) for o in range(26)]
for p in range(k, n, l):
a[ord(s[p]) - 97] += 1
for p in range(26):
minn = min(minn, d - a[p])
ans += minn
print(ans)
|
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR VAR VAR BIN_OP FUNC_CALL VAR VAR VAR NUMBER NUMBER VAR BIN_OP FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR NUMBER NUMBER NUMBER ASSIGN VAR BIN_OP NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP BIN_OP NUMBER VAR VAR VAR VAR VAR VAR VAR IF BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP NUMBER NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR VAR VAR BIN_OP FUNC_CALL VAR VAR VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR
|
Word $s$ of length $n$ is called $k$-complete if $s$ is a palindrome, i.e. $s_i=s_{n+1-i}$ for all $1 \le i \le n$; $s$ has a period of $k$, i.e. $s_i=s_{k+i}$ for all $1 \le i \le n-k$.
For example, "abaaba" is a $3$-complete word, while "abccba" is not.
Bob is given a word $s$ of length $n$ consisting of only lowercase Latin letters and an integer $k$, such that $n$ is divisible by $k$. He wants to convert $s$ to any $k$-complete word.
To do this Bob can choose some $i$ ($1 \le i \le n$) and replace the letter at position $i$ with some other lowercase Latin letter.
So now Bob wants to know the minimum number of letters he has to replace to convert $s$ to any $k$-complete word.
Note that Bob can do zero changes if the word $s$ is already $k$-complete.
You are required to answer $t$ test cases independently.
-----Input-----
The first line contains a single integer $t$ ($1 \le t\le 10^5$) — the number of test cases.
The first line of each test case contains two integers $n$ and $k$ ($1 \le k < n \le 2 \cdot 10^5$, $n$ is divisible by $k$).
The second line of each test case contains a word $s$ of length $n$.
It is guaranteed that word $s$ only contains lowercase Latin letters. And it is guaranteed that the sum of $n$ over all test cases will not exceed $2 \cdot 10^5$.
-----Output-----
For each test case, output one integer, representing the minimum number of characters he has to replace to convert $s$ to any $k$-complete word.
-----Example-----
Input
4
6 2
abaaba
6 3
abaaba
36 9
hippopotomonstrosesquippedaliophobia
21 7
wudixiaoxingxingheclp
Output
2
0
23
16
-----Note-----
In the first test case, one optimal solution is aaaaaa.
In the second test case, the given word itself is $k$-complete.
|
t = int(input())
for i in range(t):
n, k = map(int, input().split())
s = input()
answer = 0
for j in range((k + 1) // 2):
d = dict()
o = j
d[s[o]] = 1
o += k - 1 - j * 2
count = 1
if k == j * 2 + 1:
if k == 1:
o += 1
else:
o += j * 2 + 1
while o < n:
if s[o] not in d:
d[s[o]] = 0
d[s[o]] += 1
count += 1
if o + j * 2 + 1 < n:
o += j * 2 + 1
if s[o] not in d:
d[s[o]] = 0
d[s[o]] += 1
count += 1
else:
break
if k == j * 2 + 1:
if k == 1:
o += 1
else:
o += j * 2 + 1
else:
o += k - 1 - j * 2
answer += count - max(d.values())
print(answer)
|
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP BIN_OP VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR ASSIGN VAR VAR ASSIGN VAR VAR VAR NUMBER VAR BIN_OP BIN_OP VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR NUMBER IF VAR BIN_OP BIN_OP VAR NUMBER NUMBER IF VAR NUMBER VAR NUMBER VAR BIN_OP BIN_OP VAR NUMBER NUMBER WHILE VAR VAR IF VAR VAR VAR ASSIGN VAR VAR VAR NUMBER VAR VAR VAR NUMBER VAR NUMBER IF BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER VAR VAR BIN_OP BIN_OP VAR NUMBER NUMBER IF VAR VAR VAR ASSIGN VAR VAR VAR NUMBER VAR VAR VAR NUMBER VAR NUMBER IF VAR BIN_OP BIN_OP VAR NUMBER NUMBER IF VAR NUMBER VAR NUMBER VAR BIN_OP BIN_OP VAR NUMBER NUMBER VAR BIN_OP BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR BIN_OP VAR FUNC_CALL VAR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR
|
Word $s$ of length $n$ is called $k$-complete if $s$ is a palindrome, i.e. $s_i=s_{n+1-i}$ for all $1 \le i \le n$; $s$ has a period of $k$, i.e. $s_i=s_{k+i}$ for all $1 \le i \le n-k$.
For example, "abaaba" is a $3$-complete word, while "abccba" is not.
Bob is given a word $s$ of length $n$ consisting of only lowercase Latin letters and an integer $k$, such that $n$ is divisible by $k$. He wants to convert $s$ to any $k$-complete word.
To do this Bob can choose some $i$ ($1 \le i \le n$) and replace the letter at position $i$ with some other lowercase Latin letter.
So now Bob wants to know the minimum number of letters he has to replace to convert $s$ to any $k$-complete word.
Note that Bob can do zero changes if the word $s$ is already $k$-complete.
You are required to answer $t$ test cases independently.
-----Input-----
The first line contains a single integer $t$ ($1 \le t\le 10^5$) — the number of test cases.
The first line of each test case contains two integers $n$ and $k$ ($1 \le k < n \le 2 \cdot 10^5$, $n$ is divisible by $k$).
The second line of each test case contains a word $s$ of length $n$.
It is guaranteed that word $s$ only contains lowercase Latin letters. And it is guaranteed that the sum of $n$ over all test cases will not exceed $2 \cdot 10^5$.
-----Output-----
For each test case, output one integer, representing the minimum number of characters he has to replace to convert $s$ to any $k$-complete word.
-----Example-----
Input
4
6 2
abaaba
6 3
abaaba
36 9
hippopotomonstrosesquippedaliophobia
21 7
wudixiaoxingxingheclp
Output
2
0
23
16
-----Note-----
In the first test case, one optimal solution is aaaaaa.
In the second test case, the given word itself is $k$-complete.
|
def solve(ans):
n, k = [int(x) for x in input().split()]
s = input()
occur = [([0] * 26) for i in range((k + 1) // 2)]
for i in range(0, n, k):
for j in range((k + 1) // 2):
occur[j][ord(s[i + j]) - ord("a")] += 1
for j in range(k // 2):
occur[j][ord(s[i + k - j - 1]) - ord("a")] += 1
change = 0
for j in range(k // 2):
max_at_pos = max(occur[j])
change += 2 * (n // k) - max_at_pos
if k % 2 != 0:
change += n // k - max(occur[-1])
ans.append(change)
return
n = int(input())
ans = []
for i in range(n):
solve(ans)
for ele in ans:
print(ele)
|
FUNC_DEF ASSIGN VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER NUMBER VAR FUNC_CALL VAR BIN_OP BIN_OP VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR VAR FOR VAR FUNC_CALL VAR BIN_OP BIN_OP VAR NUMBER NUMBER VAR VAR BIN_OP FUNC_CALL VAR VAR BIN_OP VAR VAR FUNC_CALL VAR STRING NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR BIN_OP FUNC_CALL VAR VAR BIN_OP BIN_OP BIN_OP VAR VAR VAR NUMBER FUNC_CALL VAR STRING NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR BIN_OP BIN_OP NUMBER BIN_OP VAR VAR VAR IF BIN_OP VAR NUMBER NUMBER VAR BIN_OP BIN_OP VAR VAR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR VAR RETURN ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR FOR VAR VAR EXPR FUNC_CALL VAR VAR
|
Word $s$ of length $n$ is called $k$-complete if $s$ is a palindrome, i.e. $s_i=s_{n+1-i}$ for all $1 \le i \le n$; $s$ has a period of $k$, i.e. $s_i=s_{k+i}$ for all $1 \le i \le n-k$.
For example, "abaaba" is a $3$-complete word, while "abccba" is not.
Bob is given a word $s$ of length $n$ consisting of only lowercase Latin letters and an integer $k$, such that $n$ is divisible by $k$. He wants to convert $s$ to any $k$-complete word.
To do this Bob can choose some $i$ ($1 \le i \le n$) and replace the letter at position $i$ with some other lowercase Latin letter.
So now Bob wants to know the minimum number of letters he has to replace to convert $s$ to any $k$-complete word.
Note that Bob can do zero changes if the word $s$ is already $k$-complete.
You are required to answer $t$ test cases independently.
-----Input-----
The first line contains a single integer $t$ ($1 \le t\le 10^5$) — the number of test cases.
The first line of each test case contains two integers $n$ and $k$ ($1 \le k < n \le 2 \cdot 10^5$, $n$ is divisible by $k$).
The second line of each test case contains a word $s$ of length $n$.
It is guaranteed that word $s$ only contains lowercase Latin letters. And it is guaranteed that the sum of $n$ over all test cases will not exceed $2 \cdot 10^5$.
-----Output-----
For each test case, output one integer, representing the minimum number of characters he has to replace to convert $s$ to any $k$-complete word.
-----Example-----
Input
4
6 2
abaaba
6 3
abaaba
36 9
hippopotomonstrosesquippedaliophobia
21 7
wudixiaoxingxingheclp
Output
2
0
23
16
-----Note-----
In the first test case, one optimal solution is aaaaaa.
In the second test case, the given word itself is $k$-complete.
|
t = int(input())
for _ in range(t):
n, k = map(int, input().split())
s = list(input())
data = []
for j in range(k):
temp = []
for l in range(n // k):
temp.append(s[l * k + j])
data.append(temp)
ans = 0
for i in range(k // 2):
l1 = data[i]
l1.extend(data[k - i - 1])
l1 = sorted(l1)
c = 1
m = 1
for j in range(1, len(l1)):
if l1[j] == l1[j - 1]:
c += 1
else:
if c > m:
m = c
c = 1
if c > m:
m = c
ans += 2 * (n // k) - m
if k % 2 != 0:
l1 = data[k // 2]
l1 = sorted(l1)
c = 1
m = 1
for j in range(1, len(l1)):
if l1[j] == l1[j - 1]:
c += 1
else:
if c > m:
m = c
c = 1
if c > m:
m = c
ans += n // k - m
print(ans)
|
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR EXPR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR IF VAR VAR VAR BIN_OP VAR NUMBER VAR NUMBER IF VAR VAR ASSIGN VAR VAR ASSIGN VAR NUMBER IF VAR VAR ASSIGN VAR VAR VAR BIN_OP BIN_OP NUMBER BIN_OP VAR VAR VAR IF BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR IF VAR VAR VAR BIN_OP VAR NUMBER VAR NUMBER IF VAR VAR ASSIGN VAR VAR ASSIGN VAR NUMBER IF VAR VAR ASSIGN VAR VAR VAR BIN_OP BIN_OP VAR VAR VAR EXPR FUNC_CALL VAR VAR
|
Word $s$ of length $n$ is called $k$-complete if $s$ is a palindrome, i.e. $s_i=s_{n+1-i}$ for all $1 \le i \le n$; $s$ has a period of $k$, i.e. $s_i=s_{k+i}$ for all $1 \le i \le n-k$.
For example, "abaaba" is a $3$-complete word, while "abccba" is not.
Bob is given a word $s$ of length $n$ consisting of only lowercase Latin letters and an integer $k$, such that $n$ is divisible by $k$. He wants to convert $s$ to any $k$-complete word.
To do this Bob can choose some $i$ ($1 \le i \le n$) and replace the letter at position $i$ with some other lowercase Latin letter.
So now Bob wants to know the minimum number of letters he has to replace to convert $s$ to any $k$-complete word.
Note that Bob can do zero changes if the word $s$ is already $k$-complete.
You are required to answer $t$ test cases independently.
-----Input-----
The first line contains a single integer $t$ ($1 \le t\le 10^5$) — the number of test cases.
The first line of each test case contains two integers $n$ and $k$ ($1 \le k < n \le 2 \cdot 10^5$, $n$ is divisible by $k$).
The second line of each test case contains a word $s$ of length $n$.
It is guaranteed that word $s$ only contains lowercase Latin letters. And it is guaranteed that the sum of $n$ over all test cases will not exceed $2 \cdot 10^5$.
-----Output-----
For each test case, output one integer, representing the minimum number of characters he has to replace to convert $s$ to any $k$-complete word.
-----Example-----
Input
4
6 2
abaaba
6 3
abaaba
36 9
hippopotomonstrosesquippedaliophobia
21 7
wudixiaoxingxingheclp
Output
2
0
23
16
-----Note-----
In the first test case, one optimal solution is aaaaaa.
In the second test case, the given word itself is $k$-complete.
|
import sys
def input():
return sys.stdin.readline().strip()
def iinput():
return int(input())
def rinput():
return map(int, input().split())
def rlinput():
return list(map(int, input().split()))
def main():
def pro(a, b, x, x1, x2):
if (a == -2 * b or x != x1 or x != x2 or b == 0) and x1 <= x - a + b <= x2:
return True
return False
n, k = rinput()
s = list(input())
h = n // k
res = 0
f = (k + 1) // 2
fl = 0
if k % 2:
fl = 1
f = k // 2 + fl
for i in range(f):
q = dict()
w = 0
for o in range(h):
if s[k * (o + 1) - i - 1] in q:
q[s[k * (o + 1) - i - 1]] += 1
else:
q[s[k * (o + 1) - i - 1]] = 1
if s[i + k * o] in q:
q[s[i + k * o]] += 1
else:
q[s[i + k * o]] = 1
for o in q:
w = max(w, q[o])
if fl and i + 1 == f:
res += h - w // 2
else:
res += 2 * h - w
print(res)
for i in range(iinput()):
main()
|
IMPORT FUNC_DEF RETURN FUNC_CALL FUNC_CALL VAR FUNC_DEF RETURN FUNC_CALL VAR FUNC_CALL VAR FUNC_DEF RETURN FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FUNC_DEF RETURN FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FUNC_DEF FUNC_DEF IF VAR BIN_OP NUMBER VAR VAR VAR VAR VAR VAR NUMBER VAR BIN_OP BIN_OP VAR VAR VAR VAR RETURN NUMBER RETURN NUMBER ASSIGN VAR VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR NUMBER NUMBER ASSIGN VAR NUMBER IF BIN_OP VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR NUMBER VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR BIN_OP BIN_OP BIN_OP VAR BIN_OP VAR NUMBER VAR NUMBER VAR VAR VAR BIN_OP BIN_OP BIN_OP VAR BIN_OP VAR NUMBER VAR NUMBER NUMBER ASSIGN VAR VAR BIN_OP BIN_OP BIN_OP VAR BIN_OP VAR NUMBER VAR NUMBER NUMBER IF VAR BIN_OP VAR BIN_OP VAR VAR VAR VAR VAR BIN_OP VAR BIN_OP VAR VAR NUMBER ASSIGN VAR VAR BIN_OP VAR BIN_OP VAR VAR NUMBER FOR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR IF VAR BIN_OP VAR NUMBER VAR VAR BIN_OP VAR BIN_OP VAR NUMBER VAR BIN_OP BIN_OP NUMBER VAR VAR EXPR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR EXPR FUNC_CALL VAR
|
Word $s$ of length $n$ is called $k$-complete if $s$ is a palindrome, i.e. $s_i=s_{n+1-i}$ for all $1 \le i \le n$; $s$ has a period of $k$, i.e. $s_i=s_{k+i}$ for all $1 \le i \le n-k$.
For example, "abaaba" is a $3$-complete word, while "abccba" is not.
Bob is given a word $s$ of length $n$ consisting of only lowercase Latin letters and an integer $k$, such that $n$ is divisible by $k$. He wants to convert $s$ to any $k$-complete word.
To do this Bob can choose some $i$ ($1 \le i \le n$) and replace the letter at position $i$ with some other lowercase Latin letter.
So now Bob wants to know the minimum number of letters he has to replace to convert $s$ to any $k$-complete word.
Note that Bob can do zero changes if the word $s$ is already $k$-complete.
You are required to answer $t$ test cases independently.
-----Input-----
The first line contains a single integer $t$ ($1 \le t\le 10^5$) — the number of test cases.
The first line of each test case contains two integers $n$ and $k$ ($1 \le k < n \le 2 \cdot 10^5$, $n$ is divisible by $k$).
The second line of each test case contains a word $s$ of length $n$.
It is guaranteed that word $s$ only contains lowercase Latin letters. And it is guaranteed that the sum of $n$ over all test cases will not exceed $2 \cdot 10^5$.
-----Output-----
For each test case, output one integer, representing the minimum number of characters he has to replace to convert $s$ to any $k$-complete word.
-----Example-----
Input
4
6 2
abaaba
6 3
abaaba
36 9
hippopotomonstrosesquippedaliophobia
21 7
wudixiaoxingxingheclp
Output
2
0
23
16
-----Note-----
In the first test case, one optimal solution is aaaaaa.
In the second test case, the given word itself is $k$-complete.
|
import sys
input = sys.stdin.readline
t = int(input())
for _ in range(t):
N, K = map(int, input().split())
S = list(input())[:-1]
table = [([0] * 26) for _ in range(K)]
a = ord("a")
for i in range(N):
table[i % K][ord(S[i]) - a] += 1
smtable = [0] * K
for k in range(26):
for i in range(K):
smtable[i] += table[i][k]
res = 0
for i in range(K // 2):
j = K - i - 1
tt = 10**6
for k in range(26):
x = smtable[i] - table[i][k]
y = smtable[j] - table[j][k]
tt = min(tt, x + y)
res += tt
if K % 2:
tt = 10**6
for k in range(26):
x = smtable[K // 2] - table[K // 2][k]
tt = min(tt, x)
res += tt
print(res)
|
IMPORT ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER NUMBER VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR STRING FOR VAR FUNC_CALL VAR VAR VAR BIN_OP VAR VAR BIN_OP FUNC_CALL VAR VAR VAR VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR VAR VAR VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER ASSIGN VAR BIN_OP VAR VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR VAR VAR IF BIN_OP VAR NUMBER ASSIGN VAR BIN_OP NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER ASSIGN VAR BIN_OP VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR
|
Word $s$ of length $n$ is called $k$-complete if $s$ is a palindrome, i.e. $s_i=s_{n+1-i}$ for all $1 \le i \le n$; $s$ has a period of $k$, i.e. $s_i=s_{k+i}$ for all $1 \le i \le n-k$.
For example, "abaaba" is a $3$-complete word, while "abccba" is not.
Bob is given a word $s$ of length $n$ consisting of only lowercase Latin letters and an integer $k$, such that $n$ is divisible by $k$. He wants to convert $s$ to any $k$-complete word.
To do this Bob can choose some $i$ ($1 \le i \le n$) and replace the letter at position $i$ with some other lowercase Latin letter.
So now Bob wants to know the minimum number of letters he has to replace to convert $s$ to any $k$-complete word.
Note that Bob can do zero changes if the word $s$ is already $k$-complete.
You are required to answer $t$ test cases independently.
-----Input-----
The first line contains a single integer $t$ ($1 \le t\le 10^5$) — the number of test cases.
The first line of each test case contains two integers $n$ and $k$ ($1 \le k < n \le 2 \cdot 10^5$, $n$ is divisible by $k$).
The second line of each test case contains a word $s$ of length $n$.
It is guaranteed that word $s$ only contains lowercase Latin letters. And it is guaranteed that the sum of $n$ over all test cases will not exceed $2 \cdot 10^5$.
-----Output-----
For each test case, output one integer, representing the minimum number of characters he has to replace to convert $s$ to any $k$-complete word.
-----Example-----
Input
4
6 2
abaaba
6 3
abaaba
36 9
hippopotomonstrosesquippedaliophobia
21 7
wudixiaoxingxingheclp
Output
2
0
23
16
-----Note-----
In the first test case, one optimal solution is aaaaaa.
In the second test case, the given word itself is $k$-complete.
|
T = int(input())
for test in range(T):
N, K = list(map(int, input().split()))
word = input()
if K == 1:
bins = [(0) for i in range(128)]
for c in word:
bins[ord(c)] += 1
print(N - max(bins[97:]))
continue
MDcount = N // K
changes = 0
for pos in range(K // 2):
bins = [(0) for i in range(128)]
indices = [(i * K + pos) for i in range(MDcount)] + [
((i + 1) * K - 1 - pos) for i in range(MDcount)
]
for index in indices:
bins[ord(word[index])] += 1
changes += 2 * MDcount - max(bins[97:])
if K % 2 == 1:
bins = [(0) for i in range(128)]
indices = [((K - 1) // 2 + i * K) for i in range(MDcount)]
for index in indices:
bins[ord(word[index])] += 1
changes += MDcount - max(bins[97:])
print(changes)
|
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR IF VAR NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR NUMBER FOR VAR VAR VAR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR NUMBER ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR VAR VAR VAR FUNC_CALL VAR VAR BIN_OP BIN_OP BIN_OP BIN_OP VAR NUMBER VAR NUMBER VAR VAR FUNC_CALL VAR VAR FOR VAR VAR VAR FUNC_CALL VAR VAR VAR NUMBER VAR BIN_OP BIN_OP NUMBER VAR FUNC_CALL VAR VAR NUMBER IF BIN_OP VAR NUMBER NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR NUMBER ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR NUMBER NUMBER BIN_OP VAR VAR VAR FUNC_CALL VAR VAR FOR VAR VAR VAR FUNC_CALL VAR VAR VAR NUMBER VAR BIN_OP VAR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR VAR
|
Word $s$ of length $n$ is called $k$-complete if $s$ is a palindrome, i.e. $s_i=s_{n+1-i}$ for all $1 \le i \le n$; $s$ has a period of $k$, i.e. $s_i=s_{k+i}$ for all $1 \le i \le n-k$.
For example, "abaaba" is a $3$-complete word, while "abccba" is not.
Bob is given a word $s$ of length $n$ consisting of only lowercase Latin letters and an integer $k$, such that $n$ is divisible by $k$. He wants to convert $s$ to any $k$-complete word.
To do this Bob can choose some $i$ ($1 \le i \le n$) and replace the letter at position $i$ with some other lowercase Latin letter.
So now Bob wants to know the minimum number of letters he has to replace to convert $s$ to any $k$-complete word.
Note that Bob can do zero changes if the word $s$ is already $k$-complete.
You are required to answer $t$ test cases independently.
-----Input-----
The first line contains a single integer $t$ ($1 \le t\le 10^5$) — the number of test cases.
The first line of each test case contains two integers $n$ and $k$ ($1 \le k < n \le 2 \cdot 10^5$, $n$ is divisible by $k$).
The second line of each test case contains a word $s$ of length $n$.
It is guaranteed that word $s$ only contains lowercase Latin letters. And it is guaranteed that the sum of $n$ over all test cases will not exceed $2 \cdot 10^5$.
-----Output-----
For each test case, output one integer, representing the minimum number of characters he has to replace to convert $s$ to any $k$-complete word.
-----Example-----
Input
4
6 2
abaaba
6 3
abaaba
36 9
hippopotomonstrosesquippedaliophobia
21 7
wudixiaoxingxingheclp
Output
2
0
23
16
-----Note-----
In the first test case, one optimal solution is aaaaaa.
In the second test case, the given word itself is $k$-complete.
|
import sys
reader = (s.rstrip() for s in sys.stdin)
input = reader.__next__
def gift():
for _ in range(t):
n, k = list(map(int, input().split()))
string = input()
res = []
for i in range(k // 2 + k % 2):
res.append({})
for i in range(n // k):
for j in range(k // 2 + k % 2):
freq = res[j].get(string[i * k + j], 0)
res[j][string[i * k + j]] = freq + 1
if k % 2 != 0 and j == k // 2 + k % 2 - 1:
continue
else:
freq = res[j].get(string[(i + 1) * k - j - 1], 0)
res[j][string[(i + 1) * k - j - 1]] = freq + 1
ans = 0
for i in range(k // 2 + k % 2):
each = res[i]
maxval = each[max(each, key=each.get)]
if k % 2 != 0 and i == k // 2 + k % 2 - 1:
ans += n // k - maxval
else:
ans += n // k * 2 - maxval
yield ans
t = int(input())
ans = gift()
print(*ans, sep="\n")
|
IMPORT ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR FUNC_DEF FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR BIN_OP BIN_OP VAR NUMBER BIN_OP VAR NUMBER EXPR FUNC_CALL VAR DICT FOR VAR FUNC_CALL VAR BIN_OP VAR VAR FOR VAR FUNC_CALL VAR BIN_OP BIN_OP VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR BIN_OP BIN_OP VAR VAR VAR NUMBER ASSIGN VAR VAR VAR BIN_OP BIN_OP VAR VAR VAR BIN_OP VAR NUMBER IF BIN_OP VAR NUMBER NUMBER VAR BIN_OP BIN_OP BIN_OP VAR NUMBER BIN_OP VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR BIN_OP BIN_OP BIN_OP BIN_OP VAR NUMBER VAR VAR NUMBER NUMBER ASSIGN VAR VAR VAR BIN_OP BIN_OP BIN_OP BIN_OP VAR NUMBER VAR VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP BIN_OP VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR IF BIN_OP VAR NUMBER NUMBER VAR BIN_OP BIN_OP BIN_OP VAR NUMBER BIN_OP VAR NUMBER NUMBER VAR BIN_OP BIN_OP VAR VAR VAR VAR BIN_OP BIN_OP BIN_OP VAR VAR NUMBER VAR EXPR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR STRING
|
Word $s$ of length $n$ is called $k$-complete if $s$ is a palindrome, i.e. $s_i=s_{n+1-i}$ for all $1 \le i \le n$; $s$ has a period of $k$, i.e. $s_i=s_{k+i}$ for all $1 \le i \le n-k$.
For example, "abaaba" is a $3$-complete word, while "abccba" is not.
Bob is given a word $s$ of length $n$ consisting of only lowercase Latin letters and an integer $k$, such that $n$ is divisible by $k$. He wants to convert $s$ to any $k$-complete word.
To do this Bob can choose some $i$ ($1 \le i \le n$) and replace the letter at position $i$ with some other lowercase Latin letter.
So now Bob wants to know the minimum number of letters he has to replace to convert $s$ to any $k$-complete word.
Note that Bob can do zero changes if the word $s$ is already $k$-complete.
You are required to answer $t$ test cases independently.
-----Input-----
The first line contains a single integer $t$ ($1 \le t\le 10^5$) — the number of test cases.
The first line of each test case contains two integers $n$ and $k$ ($1 \le k < n \le 2 \cdot 10^5$, $n$ is divisible by $k$).
The second line of each test case contains a word $s$ of length $n$.
It is guaranteed that word $s$ only contains lowercase Latin letters. And it is guaranteed that the sum of $n$ over all test cases will not exceed $2 \cdot 10^5$.
-----Output-----
For each test case, output one integer, representing the minimum number of characters he has to replace to convert $s$ to any $k$-complete word.
-----Example-----
Input
4
6 2
abaaba
6 3
abaaba
36 9
hippopotomonstrosesquippedaliophobia
21 7
wudixiaoxingxingheclp
Output
2
0
23
16
-----Note-----
In the first test case, one optimal solution is aaaaaa.
In the second test case, the given word itself is $k$-complete.
|
t = int(input())
for _ in range(t):
n, k = [int(p) for p in input().split()]
s = input()
subStr = []
for i in range(0, n, k):
subStr.append(s[i : i + k])
cnt = 0
p = k
if k % 2 != 0:
p += 1
for i in range(p // 2):
letters = [0] * 26
for sub in subStr:
letters[ord(sub[i]) - 97] += 1
letters[ord(sub[k - i - 1]) - 97] += 1
final = max(letters)
if k - i - 1 != i:
cnt += 2 * (n // k) - final
else:
cnt += n // k - final // 2
print(cnt)
|
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR NUMBER VAR VAR EXPR FUNC_CALL VAR VAR VAR BIN_OP VAR VAR ASSIGN VAR NUMBER ASSIGN VAR VAR IF BIN_OP VAR NUMBER NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER NUMBER FOR VAR VAR VAR BIN_OP FUNC_CALL VAR VAR VAR NUMBER NUMBER VAR BIN_OP FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR NUMBER NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR IF BIN_OP BIN_OP VAR VAR NUMBER VAR VAR BIN_OP BIN_OP NUMBER BIN_OP VAR VAR VAR VAR BIN_OP BIN_OP VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR
|
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.