description
stringlengths
171
4k
code
stringlengths
94
3.98k
normalized_code
stringlengths
57
4.99k
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 c = [{} for _ in range(k)] for i in range(n): x = i % k y = k - 1 - x x = min(x, y) c[x][s[i]] = c[x].get(s[i], 0) + 1 for i in range(k): if c[i]: x = sum(c[i].values()) y = max(c[i].values()) ans += x - y 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 DICT VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR VAR VAR BIN_OP FUNC_CALL VAR VAR VAR VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL 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.
def check(s, k): string = s[0:k] if s == s[::-1]: for i in range(len(s) // k): if s[i * k : (i + 1) * k] != string: return False return True else: return False def calc(s): options = {} i, j, count = 0, len(s) - 1, 0 while i <= j: if s[i] != s[j]: count += 1 if i != j: options[i] = [s[i], s[j]] else: options[i] = [s[i]] i += 1 j -= 1 return options, count for nt in range(int(input())): n, k = map(int, input().split()) s = input() if check(s, k): print(0) else: freq = {} ans = 0 for i in range(n // k): d, ans = calc(s[i * k : k * (i + 1)]) for j in d: for m in d[j]: if j in freq: if m in freq[j]: freq[j][m] += 1 else: freq[j][m] = 1 else: freq[j] = {m: 1} ans = 0 for i in freq: s = 0 m = 0 for j in freq[i]: s += freq[i][j] if freq[i][j] > m: m = freq[i][j] ans += s - m print(ans)
FUNC_DEF ASSIGN VAR VAR NUMBER VAR IF VAR VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR VAR IF VAR BIN_OP VAR VAR BIN_OP BIN_OP VAR NUMBER VAR VAR RETURN NUMBER RETURN NUMBER RETURN NUMBER FUNC_DEF ASSIGN VAR DICT ASSIGN VAR VAR VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER WHILE VAR VAR IF VAR VAR VAR VAR VAR NUMBER IF VAR VAR ASSIGN VAR VAR LIST VAR VAR VAR VAR ASSIGN VAR VAR LIST VAR VAR VAR NUMBER VAR NUMBER RETURN 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 IF FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR NUMBER ASSIGN VAR DICT ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR BIN_OP VAR VAR BIN_OP VAR BIN_OP VAR NUMBER FOR VAR VAR FOR VAR VAR VAR IF VAR VAR IF VAR VAR VAR VAR VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER ASSIGN VAR VAR DICT VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR VAR VAR VAR VAR VAR 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.
for _ in range(int(input())): [n, k] = list(map(int, input().split(" "))) s = input() ans = 0 for i in range((k + 1) // 2): d = [(0) for i in range(26)] for j in range(n // k): if i != k - 1 - i: d[ord(s[i + k * j]) - ord("a")] += 1 d[ord(s[k - 1 - i + k * j]) - ord("a")] += 1 else: d[ord(s[i + k * j]) - ord("a")] += 1 if i == k - 1 - i: ans += n // k - max(d) else: ans += 2 * n // k - max(d) print(ans)
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN LIST VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP BIN_OP VAR NUMBER NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR VAR IF VAR BIN_OP BIN_OP VAR NUMBER VAR VAR BIN_OP FUNC_CALL VAR VAR BIN_OP VAR BIN_OP VAR VAR FUNC_CALL VAR STRING NUMBER VAR BIN_OP FUNC_CALL VAR VAR BIN_OP BIN_OP BIN_OP VAR NUMBER VAR BIN_OP VAR VAR FUNC_CALL VAR STRING NUMBER VAR BIN_OP FUNC_CALL VAR VAR BIN_OP VAR BIN_OP VAR VAR FUNC_CALL VAR STRING NUMBER IF VAR BIN_OP BIN_OP VAR NUMBER VAR VAR BIN_OP BIN_OP VAR VAR FUNC_CALL VAR VAR VAR BIN_OP BIN_OP BIN_OP NUMBER 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 q = int(input()) for rwerwe in range(q): n, k = map(int, input().split()) s = input() if k == n: print(0) else: wyn = 0 for poz in range((k + 1) // 2): zaj = [0] * 126 i = poz while i < n: zaj[ord(s[i])] += 1 i += k i = k - 1 - poz while i < n: zaj[ord(s[i])] += 1 i += k cyk = max(zaj) if poz * 2 + 1 != k: wyn += 2 * n // k - cyk else: wyn += n // k - cyk // 2 print(wyn)
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 IF VAR VAR EXPR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP LIST NUMBER NUMBER ASSIGN VAR VAR WHILE VAR VAR VAR FUNC_CALL VAR VAR VAR NUMBER VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR NUMBER VAR WHILE VAR VAR VAR FUNC_CALL VAR VAR VAR NUMBER VAR VAR ASSIGN VAR FUNC_CALL VAR VAR IF BIN_OP BIN_OP VAR NUMBER NUMBER VAR VAR BIN_OP BIN_OP BIN_OP NUMBER VAR VAR VAR VAR BIN_OP BIN_OP 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 slv(s, n, k): ans = 0 for i in range((k + 1) // 2): c = {} sm = 0 for j in range(n // k): a = s[j * k + i] if a not in c: c[a] = 0 c[a] += 1 sm += 1 if i != k - i - 1: a = s[j * k + k - i - 1] if a not in c: c[a] = 0 c[a] += 1 sm += 1 m = max([c[i] for i in c]) ans += sm - m return ans t = int(input()) for _ in range(t): n, k = map(int, input().split()) s = input() print(slv(s, n, k))
FUNC_DEF ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP BIN_OP VAR NUMBER NUMBER ASSIGN VAR DICT ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR VAR ASSIGN VAR VAR BIN_OP BIN_OP VAR VAR VAR IF VAR VAR ASSIGN VAR VAR NUMBER VAR VAR NUMBER VAR NUMBER IF VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR VAR BIN_OP BIN_OP BIN_OP BIN_OP VAR VAR VAR VAR NUMBER IF VAR VAR ASSIGN VAR VAR NUMBER VAR VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR VAR BIN_OP VAR VAR RETURN 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 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())): n, k = [int(c) for c in input().split()] s = [i for i in input()] ans = 0 dic = [(0) for i in range(30)] h = k // 2 if k % 2 == 0 else k // 2 + 1 for i in range(h): mx = 0 m = n // k dic = [(0) for o in range(30)] for j in range(i, n, k): dic[ord(s[j]) - 97] += 1 if k - 1 - i != i: l = k - 1 - i m = 2 * (n // k) for j in range(l, n, k): dic[ord(s[j]) - 97] += 1 mx = max(dic) ans += m - mx print(ans)
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 VAR VAR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL 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 NUMBER ASSIGN VAR BIN_OP VAR VAR 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 IF BIN_OP BIN_OP VAR NUMBER VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR NUMBER VAR ASSIGN VAR BIN_OP NUMBER BIN_OP VAR VAR FOR VAR FUNC_CALL VAR VAR VAR VAR VAR BIN_OP FUNC_CALL VAR 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.
t = int(input()) for _ in range(t): n, k = map(int, input().split()) arr = input() s = 0 for i in range(k): d = {} count = 0 for j in range(n // k): x = i + j * k if arr[x] in d: d[arr[x]] += 1 else: d[arr[x]] = 1 if arr[n - x - 1] in d: d[arr[n - x - 1]] += 1 else: d[arr[n - x - 1]] = 1 count += 2 max1 = max(d.values()) s += count - max1 print(s // 2)
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 VAR ASSIGN VAR DICT ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR BIN_OP VAR VAR IF VAR VAR VAR VAR VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER IF VAR BIN_OP BIN_OP VAR VAR NUMBER VAR VAR VAR BIN_OP BIN_OP VAR VAR NUMBER NUMBER ASSIGN VAR VAR BIN_OP BIN_OP VAR VAR NUMBER NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR BIN_OP 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 = [int(s) for s in input().split()] s = input() arr = [[(0) for i in range(k)] for j in range(n // k)] for i in range(n // k): for j in range(k): arr[i][j] = s[i * k + j] sum = 0 for i in range((k + 1) // 2): freq = [0] * 26 maxi = 0 for j in range(n // k): x = ord(arr[j][i]) - ord("a") freq[x] += 1 if i != k - 1 - i: y = ord(arr[j][k - i - 1]) - ord("a") freq[y] += 1 maxi = max(maxi, freq[y]) maxi = max(maxi, freq[x]) if i == k - i - 1: sum += n // k - maxi else: sum = sum + (2 * (n // k) - maxi) print(sum)
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 VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR BIN_OP VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR BIN_OP BIN_OP VAR VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP LIST NUMBER NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR VAR VAR FUNC_CALL VAR STRING VAR VAR NUMBER IF VAR BIN_OP BIN_OP VAR NUMBER VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR VAR BIN_OP BIN_OP VAR VAR NUMBER FUNC_CALL VAR STRING VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR IF VAR BIN_OP BIN_OP VAR VAR NUMBER VAR BIN_OP BIN_OP VAR VAR VAR ASSIGN VAR BIN_OP VAR BIN_OP BIN_OP NUMBER 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.
for t in range(int(input())): n, k = map(int, input().split()) s = list(input()) alp = "abcdefghijklmnopqrstuvwxyz" arr = [[(0) for i in range(26)] for j in range(k)] for i in range(n): arr[i % k][alp.index(s[i])] += 1 ans = 0 k1 = k if k % 2 == 1: k1 += 1 for i1 in range(1, k1 // 2 + 1): i = i1 - 1 j = k - i - 1 if i != j: mx, total = 0, 0 for kk in range(26): total += arr[i][kk] + arr[j][kk] mx = max(mx, arr[i][kk] + arr[j][kk]) ans += total - mx else: mx, total = 0, 0 for kk in range(26): total += arr[i][kk] mx = max(mx, arr[i][kk]) ans += total - mx 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 FUNC_CALL VAR ASSIGN VAR STRING ASSIGN VAR NUMBER VAR FUNC_CALL VAR NUMBER VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR VAR BIN_OP VAR VAR FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR IF BIN_OP VAR NUMBER NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF VAR VAR ASSIGN VAR VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR BIN_OP VAR VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR VAR VAR VAR VAR VAR BIN_OP VAR VAR ASSIGN VAR VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR 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.
import sys def main(): import sys input = sys.stdin.readline for _ in range(int(input())): N, K = map(int, input().split()) S = input().rstrip("\n") cnt = [([0] * K) for _ in range(26)] for i, s in enumerate(S): j = ord(s) - 97 cnt[j][i % K] += 1 ans = 0 L = N // K * 2 for i in range(K // 2): tmp = N + 1 for j in range(26): tmp = min(tmp, L - (cnt[j][i] + cnt[j][K - i - 1])) ans += tmp if K & 1: tmp = N + 1 for j in range(26): tmp = min(tmp, N // K - cnt[j][K // 2]) ans += tmp print(ans) main()
IMPORT FUNC_DEF IMPORT 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 FUNC_CALL VAR STRING ASSIGN VAR BIN_OP LIST NUMBER VAR VAR FUNC_CALL VAR NUMBER FOR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER VAR VAR BIN_OP VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR BIN_OP VAR VAR VAR VAR VAR BIN_OP BIN_OP VAR VAR NUMBER VAR VAR IF BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR VAR VAR BIN_OP VAR NUMBER 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.
from sys import stdin, stdout input = stdin.readline print = stdout.write for t in range(int(input())): n, k = map(int, input().split()) a = input().strip() b = [[(0) for i in range(26)] for j in range(k // 2 + k % 2)] for i in range(k // 2 + k % 2): for j in range(i, n, k): if k % 2 == 1 and i == k // 2 + k % 2 - 1: b[i][ord(a[j]) - 97] += 1 else: b[i][ord(a[j]) - 97] += 1 b[i][ord(a[j + k - 2 * i - 1]) - 97] += 1 c = 0 for i in range(k // 2 + k % 2): if k % 2 == 1 and i == k // 2 + k % 2 - 1: c += n // k - max(b[i]) else: c += 2 * (n // k) - max(b[i]) print(str(c) + "\n")
ASSIGN VAR 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 FUNC_CALL VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR NUMBER VAR FUNC_CALL VAR BIN_OP BIN_OP VAR NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP BIN_OP VAR NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR VAR IF BIN_OP VAR NUMBER NUMBER VAR BIN_OP BIN_OP BIN_OP VAR NUMBER BIN_OP VAR NUMBER NUMBER VAR VAR BIN_OP FUNC_CALL VAR VAR VAR NUMBER NUMBER VAR VAR BIN_OP FUNC_CALL VAR VAR VAR NUMBER NUMBER VAR VAR BIN_OP FUNC_CALL VAR VAR BIN_OP BIN_OP BIN_OP VAR VAR BIN_OP NUMBER VAR NUMBER NUMBER NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP BIN_OP VAR NUMBER BIN_OP VAR NUMBER 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 FUNC_CALL VAR VAR VAR VAR BIN_OP BIN_OP NUMBER BIN_OP 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.
def solve(s, k): L = [] for j in range(k // 2): M = [] for i in range(len(s) // k): M.append(s[i * k + j]) M.append(s[i * k + k - j - 1]) L.append(M) if k % 2 == 1: M = [] for i in range(len(s) // k): M.append(s[i * k + k // 2]) L.append(M) c = 0 for i in L: cc = max(count(i)) c += len(i) - cc return c def count(s): L = [0] * 26 for i in s: L[ord(i) - 97] += 1 return L for i in " " * int(input()): n, k = map(int, input().split()) print(solve(input(), k))
FUNC_DEF ASSIGN VAR LIST FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR VAR EXPR FUNC_CALL VAR VAR BIN_OP BIN_OP BIN_OP BIN_OP VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR IF BIN_OP VAR NUMBER NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR BIN_OP FUNC_CALL VAR VAR VAR RETURN VAR FUNC_DEF ASSIGN VAR BIN_OP LIST NUMBER NUMBER FOR VAR VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER RETURN VAR FOR VAR BIN_OP STRING FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR 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.
t = int(input()) while t: n, k = map(int, input().split()) s = input() s = [s[i] for i in range(n)] ans = 0 if k % 2 == 0: for i in range(k // 2): freq = [(0) for j in range(26)] for j in range(i, n, k): freq[ord(s[j]) - 97] += 1 for j in range(k - 1 - i, n, k): freq[ord(s[j]) - 97] += 1 total = 0 maxi = 0 for j in range(26): total += freq[j] maxi = max(maxi, freq[j]) ans += total - maxi else: for i in range(k // 2): freq = [(0) for j in range(26)] for j in range(i, n, k): freq[ord(s[j]) - 97] += 1 for j in range(k - 1 - i, n, k): freq[ord(s[j]) - 97] += 1 total = 0 maxi = 0 for j in range(26): total += freq[j] maxi = max(maxi, freq[j]) ans += total - maxi freq = [(0) for j in range(26)] for j in range(k // 2, n, k): freq[ord(s[j]) - 97] += 1 total = 0 maxi = 0 for j in range(26): total += freq[j] maxi = max(maxi, freq[j]) ans += total - maxi print(ans) t -= 1
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 VAR ASSIGN VAR VAR 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 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 BIN_OP BIN_OP VAR NUMBER VAR VAR VAR VAR BIN_OP FUNC_CALL VAR VAR VAR NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR BIN_OP VAR VAR FOR VAR FUNC_CALL VAR BIN_OP 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 FOR VAR FUNC_CALL VAR BIN_OP BIN_OP VAR NUMBER VAR VAR VAR VAR BIN_OP FUNC_CALL VAR VAR VAR NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR BIN_OP VAR VAR 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 NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR BIN_OP 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 stdin, stdout an = [] for h in range(int(stdin.readline())): n, k = map(int, stdin.readline().strip().split()) s = stdin.readline() dp = [] c = 1 if k == 1: dp = [(1) for i in range(n)] else: dp = [(0) for i in range(n)] for i in range(k - 1): if dp[i] == 0: for j in range(i, n, k): dp[j] = dp[n - 1 - j] = c c += 1 dicti = [[0, [(0) for i in range(26)]] for i in range(c)] for i in range(n): dicti[dp[i] - 1][0] += 1 dicti[dp[i] - 1][1][ord(s[i]) - ord("a")] += 1 ans = 0 for i in range(c): ans += dicti[i][0] - max(dicti[i][1]) an.append(str(ans)) stdout.write("\n".join(an))
ASSIGN VAR LIST FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR NUMBER IF VAR NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER IF VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR VAR VAR BIN_OP BIN_OP VAR NUMBER VAR VAR VAR NUMBER ASSIGN VAR LIST NUMBER NUMBER VAR FUNC_CALL VAR NUMBER VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR VAR BIN_OP VAR VAR NUMBER NUMBER NUMBER VAR BIN_OP VAR VAR NUMBER NUMBER BIN_OP FUNC_CALL VAR VAR VAR FUNC_CALL VAR STRING NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR BIN_OP VAR VAR NUMBER FUNC_CALL VAR VAR VAR NUMBER 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.
import sys input = sys.stdin.readline (t,) = map(int, input().split()) for _ in range(t): a, b = map(int, input().split()) s = input().strip() r = 0 if 1: for i in range(b // 2): d = dict() for j in range(a // b): c = s[i + j * b] if c not in d: d[c] = 0 d[c] += 1 c = s[b - i - 1 + j * b] if c not in d: d[c] = 0 d[c] += 1 mx = 0 for c in "abcdefghijklmnopqrstuvwxyz": if c in d: mx = max(mx, d[c]) r += a // b * 2 - mx if b % 2: d = dict() for j in range(a // b): c = s[b // 2 + j * b] if c not in d: d[c] = 0 d[c] += 1 mx = 0 for c in "abcdefghijklmnopqrstuvwxyz": if c in d: mx = max(mx, d[c]) r += a // b - mx print(r)
IMPORT ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL 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 IF NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR BIN_OP VAR VAR ASSIGN VAR VAR BIN_OP VAR BIN_OP VAR VAR IF VAR VAR ASSIGN VAR VAR NUMBER VAR VAR NUMBER ASSIGN VAR VAR BIN_OP BIN_OP BIN_OP VAR VAR NUMBER BIN_OP VAR VAR IF VAR VAR ASSIGN VAR VAR NUMBER VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR STRING IF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR BIN_OP BIN_OP BIN_OP VAR VAR NUMBER VAR IF BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR BIN_OP VAR VAR ASSIGN VAR VAR BIN_OP BIN_OP VAR NUMBER BIN_OP VAR VAR IF VAR VAR ASSIGN VAR VAR NUMBER VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR STRING IF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR 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.
t = int(input()) delay = [] for i in range(t): n, k = map(int, input().split()) word = input() chunk = [] for i in range(k // 2): a = list(word[i:n:k] + word[k - i - 1 : n : k]) chunk.append(a) if k % 2 == 1: a = list(word[k // 2 : n : k]) chunk.append(a) inv = 0 num_chunks = len(chunk) - 1 for ii, c in enumerate(chunk): num = 0 let = "!" d = {} for i in c: if i not in d: d[i] = 1 else: d[i] += 1 for key in d: if d[key] > num: num = d[key] let = key total = len(c) inv += total - num delay.append(inv) for d in delay: print(d)
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 FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR VAR BIN_OP BIN_OP VAR VAR NUMBER VAR VAR EXPR FUNC_CALL VAR VAR IF BIN_OP VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER FOR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR STRING ASSIGN VAR DICT FOR VAR VAR IF VAR VAR ASSIGN VAR VAR NUMBER VAR VAR NUMBER FOR VAR VAR IF VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR FUNC_CALL 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 solve(s, k): m = [([0] * 26) for _ in range((k + 1) // 2)] for i, e in enumerate(s): r = i % k if r >= (k + 1) // 2: r = k - 1 - r m[r][ord(e) - ord("a")] += 1 res = 0 for row in m: res += sum(row) - max(row) return res t = int(input()) for _ in range(t): n, k = map(int, input().split()) s = input() print(solve(s, k))
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 IF VAR BIN_OP BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP BIN_OP VAR NUMBER VAR VAR VAR BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR STRING NUMBER ASSIGN VAR NUMBER FOR VAR VAR VAR BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR VAR RETURN 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 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.
from sys import stdin, stdout def read(): x0 = stdin.readline().rstrip() return x0 def __main__(): t = int(read()) for t0 in range(t): n, k = map(int, read().split()) s = read() res = 0 ind1 = 0 ind2 = k s_list = [] for j in range(n // k): s_list.append(s[ind1:ind2]) ind1 += k ind2 += k it = k // 2 left_i = 0 right_i = -1 for i in range(it): d1 = {} d2 = {} for w in s_list: left = w[left_i] right = w[right_i] if left not in d1: d1.update({left: 1}) else: d1.update({left: d1.get(left) + 1}) if right not in d2: d2.update({right: 1}) else: d2.update({right: d2.get(right) + 1}) dr = dict(d1) for g in d2: if g in dr: dr.update({g: dr.get(g) + d2.get(g)}) else: dr.update({g: d2.get(g)}) max0 = 0 for r in dr: m = dr.get(r) if m > max0: max0 = m summ = n // k * 2 res += summ - max0 left_i += 1 right_i -= 1 if k % 2 == 1: dk = {} ind = k // 2 for w in s_list: w0 = w[ind] if w0 not in dk: dk.update({w0: 1}) else: dk.update({w0: dk.get(w0) + 1}) m0 = 0 for h in dk: h0 = dk.get(h) if h0 > m0: m0 = h0 res += n // k - m0 nx = str(res) + "\n" stdout.write(nx) __main__()
FUNC_DEF ASSIGN VAR FUNC_CALL FUNC_CALL VAR RETURN 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 ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR DICT ASSIGN VAR DICT FOR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR IF VAR VAR EXPR FUNC_CALL VAR DICT VAR NUMBER EXPR FUNC_CALL VAR DICT VAR BIN_OP FUNC_CALL VAR VAR NUMBER IF VAR VAR EXPR FUNC_CALL VAR DICT VAR NUMBER EXPR FUNC_CALL VAR DICT VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR FOR VAR VAR IF VAR VAR EXPR FUNC_CALL VAR DICT VAR BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR DICT VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR IF VAR VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER VAR BIN_OP VAR VAR VAR NUMBER VAR NUMBER IF BIN_OP VAR NUMBER NUMBER ASSIGN VAR DICT ASSIGN VAR BIN_OP VAR NUMBER FOR VAR VAR ASSIGN VAR VAR VAR IF VAR VAR EXPR FUNC_CALL VAR DICT VAR NUMBER EXPR FUNC_CALL VAR DICT VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR IF VAR VAR ASSIGN VAR VAR VAR BIN_OP BIN_OP VAR VAR VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR STRING 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.
ans_ = [] for _ in range(int(input())): n, k = map(int, input().split()) s = list(input()) for i in range(n): s[i] = ord(s[i]) - ord("a") ans = 0 for p in range(k // 2): abc = [0] * 26 for i in range(p, n, k): abc[s[i]] += 1 abc[s[i - 1 - p * 2]] += 1 s[i] = "+" s[i - 1 - p * 2] = "+" ans += sum(abc) - max(abc) if k % 2 == 1: abc = [0] * 26 for i in range(k // 2, n, k): abc[s[i]] += 1 s[i] = "+" ans += sum(abc) - max(abc) ans_.append(ans) print("\n".join(map(str, ans_)))
ASSIGN VAR LIST 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 FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR BIN_OP FUNC_CALL VAR VAR VAR FUNC_CALL VAR STRING 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 VAR VAR NUMBER VAR VAR BIN_OP BIN_OP VAR NUMBER BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR STRING ASSIGN VAR BIN_OP BIN_OP VAR NUMBER BIN_OP VAR NUMBER STRING VAR BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR VAR IF BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP LIST NUMBER NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR VAR VAR VAR NUMBER ASSIGN VAR VAR STRING VAR BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL STRING 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 solve(s, k): n = len(s) r = n // k res = 0 for j in range(k // 2): d = [0] * 26 for i in range(r): d[ord(s[i * k + j]) - 97] += 1 d[ord(s[i * k + k - 1 - j]) - 97] += 1 res += 2 * r - max(d) if k % 2 == 1: d = [0] * 26 for i in range(r): d[ord(s[i * k + k // 2]) - 97] += 1 res += r - max(d) return res t = int(input()) for _ in range(t): n, k = map(int, input().split()) s = input() print(solve(s, k))
FUNC_DEF 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 BIN_OP LIST NUMBER 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 BIN_OP VAR VAR VAR NUMBER VAR NUMBER NUMBER VAR BIN_OP BIN_OP NUMBER VAR FUNC_CALL VAR VAR IF BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP LIST NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR VAR BIN_OP FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR BIN_OP VAR NUMBER NUMBER NUMBER VAR BIN_OP VAR FUNC_CALL VAR VAR RETURN 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 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.
def CountFrequency(my_list): freq = {} for item in my_list: if item in freq: freq[item] += 1 else: freq[item] = 1 return freq t = int(input()) for test in range(t): n, k = [int(i) for i in input().split()] s = input() moves = 0 for index in range(k // 2): ilst = [(i * k + index) for i in range(n // k)] + [ ((i + 1) * k - 1 - index) for i in range(n // k) ] letters = [s[i] for i in ilst] ilfreq = CountFrequency(letters) moves += len(letters) - ilfreq[max(ilfreq, key=ilfreq.get)] if k % 2 == 1: ilst = [(i * k + k // 2) for i in range(n // k)] letters = [s[i] for i in ilst] ilfreq = CountFrequency(letters) moves += len(letters) - ilfreq[max(ilfreq, key=ilfreq.get)] print(moves)
FUNC_DEF ASSIGN VAR DICT FOR VAR VAR IF VAR VAR VAR VAR NUMBER ASSIGN VAR VAR NUMBER RETURN VAR 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 NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR VAR VAR VAR FUNC_CALL VAR BIN_OP VAR VAR BIN_OP BIN_OP BIN_OP BIN_OP VAR NUMBER VAR NUMBER VAR VAR FUNC_CALL VAR BIN_OP VAR VAR ASSIGN VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR BIN_OP FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR IF BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR VAR ASSIGN VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR BIN_OP FUNC_CALL VAR 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.
from sys import stdin for _ in range(int(stdin.readline())): n, k = list(map(int, stdin.readline().split())) s = stdin.readline().strip() group = n // k res = 0 for i in range(k // 2): max_appear = 0 max_appear_char = "" d = {} for j in range(group): idxes = [j * k + i, j * k + (k - i - 1)] for idx in idxes: if s[idx] not in d: d[s[idx]] = 1 else: d[s[idx]] += 1 if d[s[idx]] > max_appear: max_appear = d[s[idx]] max_appear_char = s[idx] for char, val in d.items(): if char != max_appear_char: res += val if k % 2 == 1: t = k // 2 d = {} max_appear = 0 max_appear_char = "" for i in range(t, n, k): c = s[i] if c not in d: d[c] = 1 else: d[c] += 1 if d[c] > max_appear: max_appear = d[c] max_appear_char = c for char, val in d.items(): if char != max_appear_char: res += val print(res)
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 FUNC_CALL VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR STRING ASSIGN VAR DICT FOR VAR FUNC_CALL VAR VAR ASSIGN VAR LIST BIN_OP BIN_OP VAR VAR VAR BIN_OP BIN_OP VAR VAR BIN_OP BIN_OP VAR VAR NUMBER FOR VAR VAR 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 VAR VAR FOR VAR VAR FUNC_CALL VAR IF VAR VAR VAR VAR IF BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR DICT ASSIGN VAR NUMBER ASSIGN VAR STRING FOR VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR VAR VAR IF VAR VAR ASSIGN VAR VAR NUMBER VAR VAR NUMBER IF VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR FOR VAR VAR FUNC_CALL VAR IF 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 _ in range(int(input())): n, k = map(int, input().split()) s = list(input()) aa = [([0] * 26) for i in range(k)] ans = 0 for i in range(k): for j in range(i, n, k): a = 1 aa[i][ord(s[j]) - ord("a")] += 1 grp = [0] * k for i in range(n // 2): if grp[i % k] == 1: continue if i % k == (n - 1 - i) % k: ans += sum(aa[i % k]) - max(aa[i % k]) grp[i % k] = 1 else: ab = sum(aa[i % k]) ac = sum(aa[(n - i - 1) % k]) m = 0 for j in range(26): m = max(m, aa[i % k][j] + aa[(n - i - 1) % k][j]) grp[i % k] = 1 grp[(n - i - 1) % k] = 1 ans += ab + ac - m 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 FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER NUMBER VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR NUMBER VAR VAR BIN_OP FUNC_CALL VAR VAR VAR FUNC_CALL VAR STRING NUMBER ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER IF VAR BIN_OP VAR VAR NUMBER IF BIN_OP VAR VAR BIN_OP BIN_OP BIN_OP VAR NUMBER VAR VAR VAR BIN_OP FUNC_CALL VAR VAR BIN_OP VAR VAR FUNC_CALL VAR VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP BIN_OP VAR VAR NUMBER VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR BIN_OP VAR VAR VAR VAR BIN_OP BIN_OP BIN_OP VAR VAR NUMBER VAR VAR ASSIGN VAR BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR VAR NUMBER VAR NUMBER 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.
t = int(input()) cases = [] for i in range(t): length, k = map(int, input().strip().split()) word = input() cases.append([word, k]) def solve(case): word = case[0] k = case[1] reps = len(word) // k ans = 0 for i in range(k // 2 if k % 2 == 0 else k // 2 + 1): print() count = {} for j in range(reps + 1): left, right = (j + 1) * k - 1 - i, j * k + i if 0 <= left < len(word): if word[left] in count: count[word[left]] += 1 else: count[word[left]] = 1 if 0 <= right < len(word) and left != right: if word[right] in count: count[word[right]] += 1 else: count[word[right]] = 1 replace = sum(count.values()) - max(count.values()) ans += replace print(ans) for case in cases: solve(case)
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 FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR EXPR FUNC_CALL VAR LIST VAR VAR FUNC_DEF ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER BIN_OP VAR NUMBER BIN_OP BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR ASSIGN VAR DICT FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR VAR BIN_OP BIN_OP BIN_OP BIN_OP VAR NUMBER VAR NUMBER VAR BIN_OP BIN_OP VAR VAR VAR IF NUMBER VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER IF NUMBER VAR FUNC_CALL VAR VAR VAR VAR IF VAR VAR VAR VAR VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR 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.
from sys import stdin t = int(input()) for _ in range(t): n, k = map(int, stdin.readline().split()) s = str(stdin.readline()) check = [0] * (n + 1) ans = 0 for i in range(k): ma = 0 v, count = [], [0] * 26 for i2 in range(i, n, k): if check[i2] == 0: check[i2] = 1 v.append(s[i2]) for i2 in range(n - 1 - i, -1, -k): if check[i2] == 0: check[i2] = 1 v.append(s[i2]) for i2 in v: count[ord(i2) - 97] += 1 ma = max(ma, count[ord(i2) - 97]) ans += len(v) - ma 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 BIN_OP LIST NUMBER BIN_OP VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR VAR LIST BIN_OP LIST NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR VAR VAR IF VAR VAR NUMBER ASSIGN VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR FOR VAR FUNC_CALL VAR BIN_OP BIN_OP VAR NUMBER VAR NUMBER VAR IF VAR VAR NUMBER ASSIGN VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR FOR VAR VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER VAR BIN_OP 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.
for _ in range(int(input())): n, k = map(int, input().split()) s = [el for el in input()] ans = 0 for i in range((k + 1) // 2): start = i end = k - i - 1 o = 0 seen = {} ind = set() while start < n: if s[start] not in seen: seen[s[start]] = 0 if start not in ind: seen[s[start]] += 1 ind.add(start) start += k while end < n: if s[end] not in seen: seen[s[end]] = 0 if end not in ind: seen[s[end]] += 1 ind.add(end) end += k m = max(seen.values()) ans += len(ind) - m 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 VAR VAR FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR DICT ASSIGN VAR FUNC_CALL VAR WHILE VAR VAR IF VAR VAR VAR ASSIGN VAR VAR VAR NUMBER IF VAR VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR WHILE VAR VAR IF VAR VAR VAR ASSIGN VAR VAR VAR NUMBER IF VAR VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR BIN_OP 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.
t = int(input()) for i in range(t): n, k = map(int, input().split()) s = input() ans = 0 for j in range(k // 2): d = {} ob = 0 el = j while el < n: ob += 1 d[s[el]] = d.get(s[el], 0) + 1 if ob % 2 == 1: el = el + k - 1 - 2 * j else: el += 1 + 2 * j ma = 0 for key, val in d.items(): ma = max(ma, val) ans += ob - ma if k % 2 == 1: el = k // 2 d = {} ob = 0 while el < n: ob += 1 d[s[el]] = d.get(s[el], 0) + 1 el += k ma = 0 for key, val in d.items(): ma = max(ma, val) ans += ob - ma 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 FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR DICT ASSIGN VAR NUMBER ASSIGN VAR VAR WHILE VAR VAR VAR NUMBER ASSIGN VAR VAR VAR BIN_OP FUNC_CALL VAR VAR VAR NUMBER NUMBER IF BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR VAR NUMBER BIN_OP NUMBER VAR VAR BIN_OP NUMBER BIN_OP NUMBER VAR ASSIGN VAR NUMBER FOR VAR VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR BIN_OP VAR VAR IF BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR DICT ASSIGN VAR NUMBER WHILE VAR VAR VAR NUMBER ASSIGN VAR VAR VAR BIN_OP FUNC_CALL VAR VAR VAR NUMBER NUMBER VAR VAR ASSIGN VAR NUMBER FOR VAR VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL 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.
def solve(string, n, k): lenper = n // k iterind = k // 2 ans = 0 for ind in range(iterind): voc = {} for i in range(ind, n, k): if string[i] not in voc: voc[string[i]] = 1 else: voc[string[i]] += 1 for i in range(k - ind - 1, n, k): if string[i] not in voc: voc[string[i]] = 1 else: voc[string[i]] += 1 if len(voc) != 1: pop_sym = max(voc.values()) ans += 2 * lenper - pop_sym if k % 2 == 1: voc = {} for i in range(iterind, n, k): if string[i] not in voc: voc[string[i]] = 1 else: voc[string[i]] += 1 if len(voc) != 1: pop_sym = max(voc.values()) ans += lenper - pop_sym print(ans) for _ in range(int(input())): n, k = [int(x) for x in input().split()] s = input() solve(s, n, k)
FUNC_DEF ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR DICT FOR VAR FUNC_CALL VAR VAR VAR VAR 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 IF VAR VAR VAR ASSIGN VAR VAR VAR NUMBER VAR VAR VAR NUMBER IF FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR BIN_OP BIN_OP NUMBER VAR VAR IF BIN_OP VAR NUMBER NUMBER ASSIGN VAR DICT FOR VAR FUNC_CALL VAR VAR VAR VAR IF VAR VAR VAR ASSIGN VAR VAR VAR NUMBER VAR VAR VAR NUMBER IF FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR BIN_OP VAR VAR EXPR 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 EXPR 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.
def find_root(g, x): while x != g[x]: x = g[x] return x def find_condition(n, k): start = {i: i for i in range(k)} for j in range(n): i1 = j % k i2 = (n - 1 - j) % k I1 = find_root(start, i1) I2 = find_root(start, i2) start[I1] = min(I1, I2) start[I2] = min(I1, I2) answer = {} for x in start: answer[x] = find_root(start, x) return answer def process(S, n, k): a1 = find_condition(n, k) counting = {} for i in range(n): I = a1[i % k] if I not in counting: counting[I] = [0, {}, 0] counting[I][0] += 1 c = S[i] if c not in counting[I][1]: counting[I][1][c] = 0 counting[I][1][c] += 1 counting[I][2] = max(counting[I][2], counting[I][1][c]) answer = 0 for x in counting: answer += counting[x][0] - counting[x][2] return answer t = int(input()) for i in range(t): n, k = [int(x) for x in input().split()] S = input() print(process(S, n, k))
FUNC_DEF WHILE VAR VAR VAR ASSIGN VAR VAR VAR RETURN VAR FUNC_DEF ASSIGN VAR VAR VAR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR NUMBER VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR DICT FOR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR RETURN VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR DICT FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR BIN_OP VAR VAR IF VAR VAR ASSIGN VAR VAR LIST NUMBER DICT NUMBER VAR VAR NUMBER NUMBER ASSIGN VAR VAR VAR IF VAR VAR VAR NUMBER ASSIGN VAR VAR NUMBER VAR NUMBER VAR VAR NUMBER VAR NUMBER ASSIGN VAR VAR NUMBER FUNC_CALL VAR VAR VAR NUMBER VAR VAR NUMBER VAR ASSIGN VAR NUMBER FOR VAR VAR VAR BIN_OP VAR VAR NUMBER VAR VAR NUMBER RETURN VAR 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 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.
def solve(): n, k = map(int, input().split()) s = list(input()) c = 0 for i, s_i in enumerate(s[: k // 2 + 1]): idxs = [*range(i, n, k), *range(k - i - 1, n, k)] cd = {} max_count = 0 max_char = None for j in idxs: s_j = s[j] count = cd.get(s_j, 0) + 1 if count > max_count: max_count = count max_char = s_j cd[s_j] = count for j in idxs: if s[j] != max_char: s[j] = max_char c += 1 print(c) def main(): t = int(input()) for _ in range(t): solve() main()
FUNC_DEF 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 VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR NUMBER NUMBER ASSIGN VAR LIST FUNC_CALL VAR VAR VAR VAR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR NUMBER VAR VAR ASSIGN VAR DICT ASSIGN VAR NUMBER ASSIGN VAR NONE FOR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER IF VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR VAR FOR VAR VAR IF VAR VAR VAR ASSIGN VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR EXPR 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.
def NO(): return print("NO") def YES(): return print("YES") def INT(): return int(input()) def LIST(): return list(map(int, input().split())) def STR(): return input() def MAP(): return map(int, input().split()) for _ in range(INT()): n, k = MAP() s = STR() a = [] for j in range(k): b = [] for _ in range(26): b.append([0]) a.append(b) if k % 2 == 0: for j in range(n): t = j % k if t < k // 2: a[t][ord(s[j]) - 97][0] += 1 else: a[k - t - 1][ord(s[j]) - 97][0] += 1 else: for j in range(n): t = j % k if t <= k // 2: a[t][ord(s[j]) - 97][0] += 1 else: a[k - t - 1][ord(s[j]) - 97][0] += 1 ans = 0 for j in range(k): p = -1 f = 0 for i in range(26): f += a[j][i][0] p = max(p, a[j][i][0]) ans += f - p print(ans)
FUNC_DEF RETURN FUNC_CALL VAR STRING FUNC_DEF RETURN FUNC_CALL VAR STRING FUNC_DEF RETURN FUNC_CALL VAR FUNC_CALL VAR FUNC_DEF RETURN FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FUNC_DEF RETURN FUNC_CALL VAR FUNC_DEF RETURN FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR LIST NUMBER EXPR FUNC_CALL VAR VAR IF BIN_OP VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR VAR IF VAR BIN_OP VAR NUMBER VAR VAR BIN_OP FUNC_CALL VAR VAR VAR NUMBER NUMBER NUMBER VAR BIN_OP BIN_OP VAR VAR NUMBER BIN_OP FUNC_CALL VAR VAR VAR NUMBER NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR VAR IF VAR BIN_OP VAR NUMBER VAR VAR BIN_OP FUNC_CALL VAR VAR VAR NUMBER NUMBER NUMBER VAR BIN_OP BIN_OP VAR VAR NUMBER BIN_OP FUNC_CALL VAR VAR VAR NUMBER NUMBER NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR NUMBER 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 inp = stdin.readline t = int(inp().strip()) for c in range(t): n, k = [int(x) for x in inp().strip().split()] array = list(inp().strip()) d1 = {} for i in range(n): if d1.get(min(i % k, k - i % k - 1), 0) == 0: d1[min(i % k, k - i % k - 1)] = {array[i]: 1} elif d1[min(i % k, k - i % k - 1)].get(array[i], 0) == 0: d1[min(i % k, k - i % k - 1)][array[i]] = 1 else: d1[min(i % k, k - i % k - 1)][array[i]] += 1 number = n // k numberOfMoves = 0 for i in d1.keys(): maximum = 0 for j in d1[i].keys(): if d1[i][j] > maximum: maximum = d1[i][j] if k % 2 == 1 and i == k // 2: numberOfMoves += number - maximum else: numberOfMoves += 2 * number - maximum print(numberOfMoves)
ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR DICT FOR VAR FUNC_CALL VAR VAR IF FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR VAR BIN_OP BIN_OP VAR BIN_OP VAR VAR NUMBER NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR BIN_OP BIN_OP VAR BIN_OP VAR VAR NUMBER DICT VAR VAR NUMBER IF FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR VAR BIN_OP BIN_OP VAR BIN_OP VAR VAR NUMBER VAR VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR BIN_OP BIN_OP VAR BIN_OP VAR VAR NUMBER VAR VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR VAR BIN_OP BIN_OP VAR BIN_OP VAR VAR NUMBER VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR IF BIN_OP VAR NUMBER NUMBER VAR BIN_OP VAR NUMBER VAR BIN_OP VAR VAR VAR BIN_OP BIN_OP NUMBER 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(): return "\n".join(nCharactersToReplaceMin() for _ in range(int(input()))) def nCharactersToReplaceMin(): def nDesiredCharacters(limit1, limit2): def countOfMostRepeatedCharacter(i1, i2): lettersCount = {} for j1, j2 in zip( range(i1, len(word), period), range(i2, len(word), period) ): if word[j1] not in lettersCount: lettersCount[word[j1]] = 1 else: lettersCount[word[j1]] += 1 if word[j2] not in lettersCount: lettersCount[word[j2]] = 1 else: lettersCount[word[j2]] += 1 return max(lettersCount.values()) return sum( countOfMostRepeatedCharacter(i1, i2) for i1, i2 in zip(range(limit1), range(period - 1, limit2, -1)) ) word_sLength, period = map(int, input().split()) word = input() limit = period // 2 if period % 2: middleLettersCount = {} for i in range(limit, word_sLength, period): if word[i] not in middleLettersCount: middleLettersCount[word[i]] = 1 else: middleLettersCount[word[i]] += 1 return str( word_sLength - max(middleLettersCount.values()) - nDesiredCharacters(limit, limit) ) else: return str(word_sLength - nDesiredCharacters(limit, limit - 1)) print(main())
FUNC_DEF RETURN FUNC_CALL STRING FUNC_CALL VAR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_DEF FUNC_DEF FUNC_DEF ASSIGN VAR DICT FOR VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR 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 VAR VAR ASSIGN VAR VAR VAR NUMBER VAR VAR VAR NUMBER RETURN FUNC_CALL VAR FUNC_CALL VAR RETURN FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL VAR BIN_OP 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 NUMBER IF BIN_OP VAR NUMBER ASSIGN VAR DICT FOR VAR FUNC_CALL VAR VAR VAR VAR IF VAR VAR VAR ASSIGN VAR VAR VAR NUMBER VAR VAR VAR NUMBER RETURN FUNC_CALL VAR BIN_OP BIN_OP VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR RETURN FUNC_CALL VAR BIN_OP VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR 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 i in range(t): n, k = map(int, input().split()) st = input() v = k // 2 + k % 2 fin = [[(0) for l in range(26)] for j in range(v)] j = 0 while j < n: ind = 0 for l in range(k // 2): fin[l][ord(st[j + ind]) - 97] += 1 ind += 1 if k % 2 != 0: fin[k // 2][ord(st[j + ind]) - 97] += 1 ind += 1 for l in range(k // 2): fin[k // 2 - l - 1][ord(st[j + ind]) - 97] += 1 ind += 1 j += ind tot = 0 for j in range(v): tot += max(fin[j]) print(n - tot)
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 BIN_OP BIN_OP VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR NUMBER VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER WHILE VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR BIN_OP FUNC_CALL VAR VAR BIN_OP VAR VAR NUMBER NUMBER VAR NUMBER IF BIN_OP VAR NUMBER NUMBER VAR BIN_OP VAR NUMBER BIN_OP FUNC_CALL VAR VAR BIN_OP VAR VAR NUMBER NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR BIN_OP BIN_OP BIN_OP VAR NUMBER VAR NUMBER BIN_OP FUNC_CALL VAR VAR BIN_OP VAR VAR NUMBER NUMBER VAR NUMBER VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR FUNC_CALL 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.
for t in range(int(input())): n, k = map(int, input().split()) s = input() strings = [s[i : i + k] for i in range(0, n, k)] ans = 0 for i in range(k // 2 + k % 2): arr = [] freq = {} for j in range(n // k): arr.append(strings[j][i]) freq[strings[j][i]] = freq.get(strings[j][i], 0) + 1 if i != k // 2: arr.append(strings[j][k - 1 - i]) freq[strings[j][k - 1 - i]] = freq.get(strings[j][k - 1 - i], 0) + 1 freq = freq.values() freq = (1 + (i != k // 2)) * (n // k) - max(freq) ans += freq 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 FOR VAR FUNC_CALL VAR BIN_OP BIN_OP VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR LIST ASSIGN VAR DICT FOR VAR FUNC_CALL VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR BIN_OP FUNC_CALL VAR VAR VAR VAR NUMBER NUMBER IF VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR VAR BIN_OP BIN_OP VAR NUMBER VAR ASSIGN VAR VAR VAR BIN_OP BIN_OP VAR NUMBER VAR BIN_OP FUNC_CALL VAR VAR VAR BIN_OP BIN_OP VAR NUMBER VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP NUMBER VAR BIN_OP VAR NUMBER 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.
import sys input = lambda: sys.stdin.readline().strip() t = int(input()) while t: t -= 1 n, k = map(int, input().split()) s = list(input()) d = [([0] * 26) for _ in range(k)] for i in range(n): d[i % k][ord(s[i]) - ord("a")] += 1 ans = 0 i = 0 j = k - 1 while i <= j: if i == j: ans += n // k - max(d[i]) i += 1 j -= 1 continue for l in range(26): d[i][l] += d[j][l] ans += 2 * (n // k) - max(d[i]) i += 1 j -= 1 print(ans)
IMPORT ASSIGN VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR WHILE VAR VAR NUMBER 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 VAR FOR VAR FUNC_CALL VAR VAR VAR BIN_OP VAR VAR BIN_OP FUNC_CALL VAR VAR VAR FUNC_CALL VAR STRING NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER WHILE VAR VAR IF VAR VAR VAR BIN_OP BIN_OP VAR VAR FUNC_CALL VAR VAR VAR VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR VAR VAR VAR VAR VAR VAR BIN_OP BIN_OP NUMBER BIN_OP VAR VAR FUNC_CALL VAR VAR VAR VAR NUMBER 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 i in range(int(input())): n, k = map(int, input().split()) s = input() ans = 0 for j in range(k): d = {} for l in range(j, n, k): if s[l] in d: d[s[l]] += 1 else: d[s[l]] = 1 for l in range(k - j - 1, n, k): if s[l] in d: d[s[l]] += 1 else: d[s[l]] = 1 ans += n // k * 2 - max(d.values()) 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 FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR DICT 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 VAR BIN_OP BIN_OP BIN_OP VAR VAR NUMBER 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.
for _ in range(int(input())): n, k = map(int, input().split()) s = input() a, p, m, o = [[]], 0, n // k, ord("a") for i in range(n): a[p].append(s[i]) if (i + 1) % k == 0: a.append([]) p += 1 l, r, res = 0, k - 1, 0 while l <= r: cnt = [0] * 36 for i in range(m): cnt[ord(a[i][l]) - o] += 1 if l != r: cnt[ord(a[i][r]) - o] += 1 t = max(cnt) res += 2 * m - t if l != r else m - t l += 1 r -= 1 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 VAR VAR VAR LIST LIST NUMBER BIN_OP VAR VAR FUNC_CALL VAR STRING FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR IF BIN_OP BIN_OP VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR LIST VAR NUMBER ASSIGN VAR VAR VAR NUMBER BIN_OP VAR NUMBER NUMBER WHILE VAR VAR ASSIGN VAR BIN_OP LIST NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR VAR BIN_OP FUNC_CALL VAR VAR VAR VAR VAR NUMBER IF VAR VAR VAR BIN_OP FUNC_CALL VAR VAR VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR BIN_OP BIN_OP NUMBER VAR VAR BIN_OP VAR VAR VAR NUMBER 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.
x = int(input()) abc = "abcdefghijklmnopqrstuvwxyz" dic = {} for i in abc: dic[i] = 0 for i in range(x): temp1, temp2 = map(int, input().split()) temp3 = int(temp1 / temp2) st = input() ans = 0 for k in range(temp2 // 2): y = dic.copy() for j in range(temp3): y[st[j * temp2 + k]] += 1 y[st[(j + 1) * temp2 - k - 1]] += 1 ans += 2 * temp3 - max(y.values()) if temp2 % 2 != 0: y = dic.copy() for j in range(temp3): y[st[j * temp2 + temp2 // 2]] += 1 ans += temp3 - max(y.values()) print(ans)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR STRING ASSIGN VAR DICT FOR VAR VAR ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR 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 BIN_OP BIN_OP VAR VAR VAR NUMBER VAR VAR BIN_OP BIN_OP BIN_OP BIN_OP VAR NUMBER VAR VAR NUMBER NUMBER VAR BIN_OP BIN_OP NUMBER VAR FUNC_CALL VAR FUNC_CALL VAR IF BIN_OP VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR VAR VAR BIN_OP BIN_OP VAR VAR BIN_OP VAR NUMBER 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.
from sys import stdin input = stdin.readline for _ in range(int(input())): n, k = map(int, input().split()) s = input().rstrip() chk = [0] * n q = [[0] * 26] now = 1 cnt = 0 for x in range(n // 2): if chk[x] != 0: continue chk[x] = now q[now - 1][ord(s[x]) - ord("a")] += 1 for y in range(x, n, k): if chk[y] == 0: chk[y] = now q[now - 1][ord(s[y]) - ord("a")] += 1 if n - y - 1 >= 0 and chk[n - y - 1] == 0: chk[n - y - 1] = now q[now - 1][ord(s[n - y - 1]) - ord("a")] += 1 now += 1 q.append([0] * 26) del q[-1] for x in q: cnt += sum(x) - max(x) print(cnt)
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 FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR LIST BIN_OP LIST NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER IF VAR VAR NUMBER ASSIGN VAR VAR VAR VAR BIN_OP VAR NUMBER BIN_OP FUNC_CALL VAR VAR VAR FUNC_CALL VAR STRING NUMBER FOR VAR FUNC_CALL VAR VAR VAR VAR IF VAR VAR NUMBER ASSIGN VAR VAR VAR VAR BIN_OP VAR NUMBER BIN_OP FUNC_CALL VAR VAR VAR FUNC_CALL VAR STRING NUMBER IF BIN_OP BIN_OP VAR VAR NUMBER NUMBER VAR BIN_OP BIN_OP VAR VAR NUMBER NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER VAR VAR BIN_OP VAR NUMBER BIN_OP FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR NUMBER FUNC_CALL VAR STRING NUMBER VAR NUMBER EXPR FUNC_CALL VAR BIN_OP LIST NUMBER NUMBER 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.
ttt = int(input()) for tttt in range(ttt): nk = input().split(" ") n = int(nk[0]) k = int(nk[1]) s = input() m = round(n / k) a = [] for i in range(m): row = [] for j in range(k): row.append(s[i * k + j]) a.append(row) q = 0 if k % 2 == 0: for i in range(round(k / 2)): listt = [] for j in range(m): listt.append(a[j][i]) for j in range(m): listt.append(a[j][k - i - 1]) sett = set(listt) count = [] for x in sett: count.append(listt.count(x)) q += len(listt) - max(count) print(q) else: for i in range(round((k - 1) / 2)): listt = [] for j in range(m): listt.append(a[j][i]) for j in range(m): listt.append(a[j][k - i - 1]) sett = set(listt) count = [] for x in sett: count.append(listt.count(x)) q += len(listt) - max(count) listt = [] for j in range(m): listt.append(a[j][int(k / 2)]) sett = set(listt) count = [] for x in sett: count.append(listt.count(x)) q += len(listt) - max(count) print(q)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER IF BIN_OP VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR LIST 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 VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR BIN_OP BIN_OP VAR NUMBER NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR LIST FOR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR LIST 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 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().rstrip() t = int(input()) for _ in range(t): n, k = map(int, input().split()) S = input() ans = 0 for i in range((k + 1) // 2): A = [0] * 26 for j in range(n // k): A[ord(S[j * k + i]) - 97] += 1 if 2 * i + 1 != k: A[ord(S[(j + 1) * k - i - 1]) - 97] += 1 ans += sum(A) - max(A) print(ans)
IMPORT ASSIGN VAR FUNC_CALL FUNC_CALL 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 ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP 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 BIN_OP BIN_OP VAR VAR VAR NUMBER NUMBER IF BIN_OP BIN_OP NUMBER VAR NUMBER VAR VAR BIN_OP FUNC_CALL VAR VAR BIN_OP BIN_OP BIN_OP BIN_OP VAR NUMBER VAR VAR NUMBER 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.
import sys input = sys.stdin.readline t = int(input()) for _ in range(t): n, k = map(int, input().split()) s = input() temp = [] b = [] j = 0 while j < k: for i in range(j, n, k): temp += [s[i]] j += 1 b += [temp] temp = [] te = [] for i in range(k // 2): te += [b[i] + b[k - i - 1]] if k % 2 == 1: te += [b[k // 2]] l2 = [] lol = 0 for i in range(len(te)): d = dict() for j in range(len(te[i])): if te[i][j] in d: d[te[i][j]] += 1 else: d[te[i][j]] = 1 lol += 1 l1 = list(d.values()) l2 += [len(te[i]) - max(l1)] print(sum(l2))
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 ASSIGN VAR LIST ASSIGN VAR LIST ASSIGN VAR NUMBER WHILE VAR VAR FOR VAR FUNC_CALL VAR VAR VAR VAR VAR LIST VAR VAR VAR NUMBER VAR LIST VAR ASSIGN VAR LIST ASSIGN VAR LIST FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR LIST BIN_OP VAR VAR VAR BIN_OP BIN_OP VAR VAR NUMBER IF BIN_OP VAR NUMBER NUMBER VAR LIST VAR BIN_OP VAR NUMBER ASSIGN VAR LIST ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR IF VAR VAR VAR VAR VAR VAR VAR VAR NUMBER ASSIGN VAR VAR VAR VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR LIST BIN_OP FUNC_CALL VAR VAR VAR FUNC_CALL 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.
def main(): n, k = map(int, input().split()) string = str(input()) a = list(string) ans = 0 for i in range(k): dp = [(0) for _ in range(26)] for j in range(i, n, k): dp[ord(a[j]) - ord("a")] += 1 dp[ord(a[n - j - 1]) - ord("a")] += 1 mx = -1 index = -1 for j in range(26): if mx < dp[j]: mx = dp[j] index = j ch = chr(index + ord("a")) for j in range(i, n, k): if a[j] != ch: a[j] = ch ans += 1 if a[n - j - 1] != ch: a[n - j - 1] = ch ans += 1 print(ans) return def test(): t = int(input()) while t: main() t -= 1 test()
FUNC_DEF ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR VAR VAR BIN_OP FUNC_CALL VAR VAR VAR FUNC_CALL VAR STRING NUMBER VAR BIN_OP FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR NUMBER FUNC_CALL VAR STRING NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER IF VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR FUNC_CALL VAR STRING FOR VAR FUNC_CALL VAR VAR VAR VAR IF VAR VAR VAR ASSIGN VAR VAR VAR VAR NUMBER IF VAR BIN_OP BIN_OP VAR VAR NUMBER VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER VAR VAR NUMBER EXPR FUNC_CALL VAR VAR RETURN FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR WHILE VAR EXPR FUNC_CALL 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.
def to_list(s): return list(map(lambda x: int(x), s.split(" "))) def get_replace_count(item): moda_count = get_moda_count(item) return len(item) - moda_count def get_moda_count(item): sorted_item = sorted(item) len_ = 1 max_len = 1 curr_letter = sorted_item[0] prev_letter = sorted_item[0] for i in range(1, len(item)): curr_letter = sorted_item[i] if curr_letter != prev_letter: if len_ > max_len: max_len = len_ len_ = 1 else: len_ += 1 prev_letter = curr_letter if len_ > max_len: return len_ return max_len def solve(s, k): if k == 1: return str(len(s) - get_moda_count(s)) arr = [] for i in range(0, len(s), k): arr.append(s[i : i + k]) dict_ = {} for j in range(int(k / 2)): dict_[j] = "" if k % 2 != 0: dict_[int(k / 2)] = "" for item in arr: for i in range(int(k / 2)): dict_[i] += item[i] dict_[i] += item[-i - 1] if k % 2 != 0: dict_[int(k / 2)] += item[int(k / 2)] replace_count = 0 for item in list(dict_.values()): replace_count += get_replace_count(item) return str(replace_count) answers = [] t = int(input()) for i in range(t): n, k = to_list(input()) s = input() answers.append(solve(s, k)) print("\n".join(answers))
FUNC_DEF RETURN FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL VAR STRING FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR RETURN BIN_OP FUNC_CALL VAR VAR VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR ASSIGN VAR VAR VAR IF VAR VAR IF VAR VAR ASSIGN VAR VAR ASSIGN VAR NUMBER VAR NUMBER ASSIGN VAR VAR IF VAR VAR RETURN VAR RETURN VAR FUNC_DEF IF VAR NUMBER RETURN FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR BIN_OP VAR VAR ASSIGN VAR DICT FOR VAR FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR VAR STRING IF BIN_OP VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER STRING FOR VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR VAR VAR VAR VAR VAR BIN_OP VAR NUMBER IF BIN_OP VAR NUMBER NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR RETURN FUNC_CALL VAR VAR ASSIGN VAR LIST ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR 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.
t = int(input()) for i in range(t): n, k = [int(i) for i in input().split()] s = input() ans = 0 for i in range(k // 2): cnt = [(0) for i in range(26)] l = k - i - 1 for j in range(i, len(s), k): cnt[ord(s[j]) - ord("a")] += 1 for j in range(l, len(s), k): cnt[ord(s[j]) - ord("a")] += 1 ans += sum(cnt) - max(cnt) if k % 2 == 1: i = k // 2 cnt = [(0) for i in range(26)] for j in range(i, len(s), k): cnt[ord(s[j]) - ord("a")] += 1 ans += sum(cnt) - 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 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 VAR FUNC_CALL VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR VAR BIN_OP FUNC_CALL VAR VAR VAR FUNC_CALL VAR STRING NUMBER FOR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR VAR BIN_OP FUNC_CALL VAR VAR VAR FUNC_CALL VAR STRING NUMBER VAR BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR VAR IF BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR VAR BIN_OP FUNC_CALL VAR VAR VAR 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.
import sys DEBUG = False if DEBUG: import sys sys.stdin = open("input.txt", "r") sys.stdout = open("output.txt", "w") def solve(): n, k = map(int, input().split()) num = n // k s = input() ans = 0 for i in range(k // 2): d = [0] * 26 for j in range(i, n, k): d[ord(s[j]) - ord("a")] += 1 for j in range(k - i - 1, n, k): d[ord(s[j]) - ord("a")] += 1 ans += 2 * num - max(d) if k % 2 == 1: d = [0] * 26 for j in range(k // 2, n, k): d[ord(s[j]) - ord("a")] += 1 ans += num - max(d) return ans t = int(input()) for _ in range(t): print(solve())
IMPORT ASSIGN VAR NUMBER IF VAR IMPORT ASSIGN VAR FUNC_CALL VAR STRING STRING ASSIGN VAR FUNC_CALL VAR STRING STRING FUNC_DEF ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP VAR 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 FUNC_CALL VAR STRING NUMBER FOR VAR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR NUMBER VAR VAR VAR BIN_OP FUNC_CALL VAR VAR VAR FUNC_CALL VAR STRING NUMBER VAR BIN_OP BIN_OP NUMBER VAR FUNC_CALL VAR VAR IF BIN_OP VAR NUMBER 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 FUNC_CALL VAR STRING NUMBER VAR BIN_OP VAR FUNC_CALL VAR VAR RETURN VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR 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.
def most_frequent(List): dict = {} count, itm = 0, "" for item in reversed(List): dict[item] = dict.get(item, 0) + 1 if dict[item] >= count: count, itm = dict[item], item return count for _ in range(int(input())): n, k = map(int, input().split()) s = str(input()) nos = n // k if k % 2 != 0: nolist = k // 2 + 1 else: nolist = k // 2 ans = 0 for i in range(nolist): a = [] for j in range(nos): if k % 2 == 0: a.append(s[j * k + i]) a.append(s[j * k + k - i - 1]) elif i == nolist - 1: a.append(s[j * k + i]) else: a.append(s[j * k + i]) a.append(s[j * k - i + k - 1]) sub = int(most_frequent(a)) ans += len(a) - sub print(ans)
FUNC_DEF ASSIGN VAR DICT ASSIGN VAR VAR NUMBER STRING FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER IF VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR RETURN 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 IF BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR IF BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR VAR EXPR FUNC_CALL VAR VAR BIN_OP BIN_OP BIN_OP BIN_OP VAR VAR VAR VAR NUMBER IF VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR VAR EXPR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR VAR EXPR FUNC_CALL VAR VAR BIN_OP BIN_OP BIN_OP BIN_OP VAR VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR BIN_OP 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.
def mostFrequent(arr, n): arr.sort() max_count = 1 res = arr[0] curr_count = 1 for i in range(1, n): if arr[i] == arr[i - 1]: curr_count += 1 else: if curr_count > max_count: max_count = curr_count res = arr[i - 1] curr_count = 1 if curr_count > max_count: max_count = curr_count return max_count testCases = int(input()) for t in range(testCases): parameters = list(map(int, input().split())) word = input() wordSplit = [] for i1 in range(parameters[1] // 2): temp = [] for i2 in range(parameters[0] // parameters[1]): temp.append(word[i2 * parameters[1] + i1]) temp.append(word[i2 * parameters[1] + (parameters[1] - 1 - i1)]) wordSplit.append(temp) if parameters[1] % 2 == 1: middleLetters = [] for i1 in range(parameters[0] // parameters[1]): middleLetters.append(word[i1 * parameters[1] + parameters[1] // 2]) wordSplit.append(middleLetters) sum_unchanged = 0 for i1 in wordSplit: sum_unchanged += mostFrequent(i1, len(i1)) print(len(word) - sum_unchanged)
FUNC_DEF EXPR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR VAR VAR BIN_OP VAR NUMBER VAR NUMBER IF VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER IF VAR VAR ASSIGN VAR VAR RETURN VAR 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 FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR NUMBER VAR EXPR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR NUMBER BIN_OP BIN_OP VAR NUMBER NUMBER VAR EXPR FUNC_CALL VAR VAR IF BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR NUMBER BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP 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()) s = input() bags = [[(0) for i in range(26)] for i in range(k // 2 + 1)] maxims = [(0) for i in range(k // 2 + 1)] for i in range(n): bag_number = i % k if bag_number >= k // 2: bag_number = k - 1 - bag_number bags[bag_number][ord(s[i]) - ord("a")] += 1 if maxims[bag_number] < bags[bag_number][ord(s[i]) - ord("a")]: maxims[bag_number] = bags[bag_number][ord(s[i]) - ord("a")] print(n - sum(maxims))
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 ASSIGN VAR NUMBER VAR FUNC_CALL VAR BIN_OP BIN_OP VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR VAR IF VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR NUMBER VAR VAR VAR BIN_OP FUNC_CALL VAR VAR VAR FUNC_CALL VAR STRING NUMBER IF VAR VAR VAR VAR BIN_OP FUNC_CALL VAR VAR VAR FUNC_CALL VAR STRING ASSIGN VAR VAR VAR VAR BIN_OP FUNC_CALL VAR VAR VAR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR BIN_OP 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 i in range(int(input())): n, k = map(int, input().split()) s = input() ans = 0 for j in range(k // 2): it = j cnt = [0] * 26 while it < n: cnt[ord(s[it]) - ord("a")] += 1 it += k it = k - j - 1 while it < n: cnt[ord(s[it]) - ord("a")] += 1 it += k ans += sum(cnt) - max(cnt) if k % 2 != 0: it = k // 2 cnt = [0] * 26 while it < n: cnt[ord(s[it]) - ord("a")] += 1 it += k ans += sum(cnt) - 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 NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR VAR ASSIGN VAR BIN_OP LIST NUMBER NUMBER WHILE VAR VAR VAR BIN_OP FUNC_CALL VAR VAR VAR FUNC_CALL VAR STRING NUMBER VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER WHILE VAR VAR VAR BIN_OP FUNC_CALL VAR VAR VAR FUNC_CALL VAR STRING NUMBER VAR VAR VAR BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR VAR IF BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER NUMBER WHILE VAR VAR VAR BIN_OP FUNC_CALL VAR VAR VAR FUNC_CALL VAR STRING NUMBER 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.
from sys import stdin, stdout def main(): t = int(stdin.readline()) for _ in range(t): n, k = list(map(int, stdin.readline().split())) s = stdin.readline().rstrip() cnt = 0 lp = int(k / 2) if k % 2 == 1: lp += 1 for i in range(lp): st = set() tmp = i while tmp < n: st.add(tmp) st.add(n - tmp - 1) tmp += k a = [0] * 30 mx = -1 for pos in st: alpha = s[pos] a[ord(alpha) - 97] += 1 if a[ord(alpha) - 97] > mx: mx = a[ord(alpha) - 97] cnt += len(st) - mx stdout.write(str(cnt) + "\n") main()
FUNC_DEF 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 FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER IF BIN_OP VAR NUMBER NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR VAR WHILE VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR NUMBER VAR VAR ASSIGN VAR BIN_OP LIST NUMBER NUMBER ASSIGN VAR NUMBER FOR VAR VAR ASSIGN VAR VAR VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER IF VAR BIN_OP FUNC_CALL VAR VAR NUMBER VAR ASSIGN VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER VAR BIN_OP FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR STRING 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 = list(map(int, input().split())) s = input() z = n // k ans = 0 for i in range(k // 2): d, c = {}, 0 for j in range(0, n, k): x, y = i + j, k - 1 + j - i if s[x] in d: d[s[x]] += 1 else: d[s[x]] = 1 if s[y] in d: d[s[y]] += 1 else: d[s[y]] = 1 if d[s[x]] > c: c = d[s[x]] if d[s[y]] > c: c = d[s[y]] ans += 2 * z - c if k % 2 != 0: i = k // 2 d, c = {}, 0 for j in range(0, n, k): x = i + j if s[x] in d: d[s[x]] += 1 else: d[s[x]] = 1 if d[s[x]] > c: c = d[s[x]] ans += z - c 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 BIN_OP VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR VAR DICT NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR VAR ASSIGN VAR VAR BIN_OP VAR VAR BIN_OP BIN_OP BIN_OP VAR NUMBER VAR VAR IF VAR VAR VAR VAR VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER IF VAR VAR VAR VAR VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER IF VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR IF VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR BIN_OP BIN_OP NUMBER VAR VAR IF BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR VAR DICT NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR VAR ASSIGN VAR BIN_OP VAR VAR IF VAR VAR VAR VAR VAR VAR NUMBER ASSIGN 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.
oa = ord("a") st = lambda x: ord(x) - oa t = int(input()) for _ in range(t): n, k = map(int, input().split()) s = list(map(st, list(input()))) l = [s[i : i + k] for i in range(0, n, k)] c = n // k ans = n for i in range(k // 2): d = [0] * 30 for x in l: d[x[i]] += 1 d[x[k - i - 1]] += 1 ans -= max(d) if k % 2: d = [0] * 30 for x in l: d[x[k // 2]] += 1 ans -= max(d) print(ans)
ASSIGN VAR FUNC_CALL VAR STRING ASSIGN VAR BIN_OP FUNC_CALL VAR 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 VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR VAR BIN_OP VAR VAR VAR FUNC_CALL VAR NUMBER VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER NUMBER FOR VAR VAR VAR VAR VAR NUMBER VAR VAR BIN_OP BIN_OP VAR VAR NUMBER NUMBER VAR FUNC_CALL VAR VAR IF BIN_OP VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER NUMBER FOR VAR VAR VAR VAR BIN_OP VAR NUMBER NUMBER 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 _ in range(int(input())): n, k = map(int, input().split()) s = list(str(input())) l1 = [0] * n i = 0 c = 0 if n == 2: if s[0] == s[1]: print(0) else: print(1) continue while i <= n // 2: l2 = [] for j in range(i, n, k): if l1[j] == 0: l2.append(s[j]) l1[j] = 1 if l1[n - j - 1] == 0: l2.append(s[n - j - 1]) l1[n - j - 1] = 1 dict = {} count, itm = 0, "" for item in reversed(l2): dict[item] = dict.get(item, 0) + 1 if dict[item] >= count: count, itm = dict[item], item aa = count c += len(l2) - count for j in range(i, n): if l1[j] == 1: i += 1 if i > n // 2: break else: break 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 FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER IF VAR NUMBER IF VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR NUMBER WHILE VAR BIN_OP VAR NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR VAR VAR IF VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR NUMBER IF VAR BIN_OP BIN_OP VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER NUMBER ASSIGN VAR DICT ASSIGN VAR VAR NUMBER STRING FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER IF VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR BIN_OP FUNC_CALL VAR VAR VAR FOR VAR FUNC_CALL VAR VAR VAR IF VAR VAR NUMBER VAR NUMBER IF 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(s): visited[s] = 1 ch = [0] * 26 c = 1 ch[ord(a[s]) - 97] += 1 frontier = [s] while frontier: next = [] for u in frontier: for v in adj[u]: if v not in visited: visited[v] = 1 ch[ord(a[v]) - 97] += 1 c += 1 next.append(v) frontier = next return c - max(ch) t = int(input()) for w in range(t): n, k = map(int, input().split()) a = list(input()) adj = {} for i in range(n): if i + k < n: if i not in adj: adj[i] = [i + k] else: adj[i].append(i + k) if i not in adj: adj[i] = [n - 1 - i] else: adj[i].append(n - 1 - i) visited = {} res = 0 for i in range(n): if i not in visited: res += bfs(i) print(res)
FUNC_DEF ASSIGN VAR VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER NUMBER ASSIGN VAR NUMBER VAR BIN_OP FUNC_CALL VAR VAR VAR NUMBER NUMBER ASSIGN VAR LIST VAR WHILE VAR ASSIGN VAR LIST FOR VAR VAR FOR VAR VAR VAR IF VAR VAR ASSIGN VAR VAR NUMBER VAR BIN_OP FUNC_CALL VAR VAR VAR NUMBER NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR RETURN BIN_OP VAR FUNC_CALL 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 DICT FOR VAR FUNC_CALL VAR VAR IF BIN_OP VAR VAR VAR IF VAR VAR ASSIGN VAR VAR LIST BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR BIN_OP VAR VAR IF VAR VAR ASSIGN VAR VAR LIST BIN_OP BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR NUMBER VAR ASSIGN VAR DICT ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR 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() count = 0 for i in range(k): d = dict() summ = 0 for j in range((n - i - 1) // k + 1): t = s[i + k * j] if t in d: d[t] += 1 else: d[t] = 1 summ += 1 for j in range((n - i - 1) // k + 1): t = s[n - 1 - i - k * j] if t in d: d[t] += 1 else: d[t] = 1 summ += 1 count += summ - max(d.values()) print(count // 2)
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 VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP BIN_OP BIN_OP BIN_OP VAR VAR NUMBER VAR NUMBER ASSIGN VAR VAR BIN_OP VAR BIN_OP VAR VAR IF VAR VAR VAR VAR NUMBER ASSIGN VAR VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP BIN_OP BIN_OP BIN_OP VAR VAR NUMBER VAR NUMBER ASSIGN VAR VAR BIN_OP BIN_OP BIN_OP VAR NUMBER VAR BIN_OP VAR VAR IF VAR VAR VAR VAR NUMBER ASSIGN VAR VAR NUMBER VAR NUMBER VAR BIN_OP 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.
import sys def set_debug(debug_mode=False): if debug_mode: fin = open("input.txt", "r") sys.stdin = fin set_debug(False) for _ in range(int(input())): n, k = map(int, input().split()) word = input() res = 0 for i in range(k): count = [0] * 26 for j in range(i, n, k): count[ord(word[j]) - ord("a")] += 1 count[ord(word[k - 1 - j]) - ord("a")] += 1 res += sum(count) - max(count) print(res // 2)
IMPORT FUNC_DEF NUMBER IF VAR ASSIGN VAR FUNC_CALL VAR STRING STRING ASSIGN VAR VAR EXPR FUNC_CALL VAR NUMBER 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 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 VAR BIN_OP FUNC_CALL VAR VAR BIN_OP BIN_OP VAR NUMBER VAR FUNC_CALL VAR STRING 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 sys import stdin L = int(stdin.readline()) X = [0] * L for _ in range(L): N, K = map(int, stdin.readline().split()) S = stdin.readline()[:N] T = list(S) ANS = 0 for I in range(K // 2 + 1): A = { "a": 0, "b": 0, "c": 0, "d": 0, "e": 0, "f": 0, "g": 0, "h": 0, "i": 0, "j": 0, "k": 0, "l": 0, "m": 0, "n": 0, "o": 0, "p": 0, "q": 0, "r": 0, "s": 0, "t": 0, "u": 0, "v": 0, "w": 0, "x": 0, "y": 0, "z": 0, } for J in range(I, N, K): A[S[J]] += 1 A[S[N - J - 1]] += 1 Z = max(A, key=A.get) for J in range(I, N, K): if T[J] != Z: T[J] = Z ANS += 1 if T[N - J - 1] != Z: T[N - J - 1] = Z ANS += 1 X[_] = ANS print(*X, sep="\n")
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP BIN_OP VAR NUMBER NUMBER ASSIGN VAR DICT STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING 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 FOR VAR FUNC_CALL VAR VAR VAR VAR VAR VAR VAR NUMBER VAR VAR BIN_OP BIN_OP VAR VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR FOR VAR FUNC_CALL VAR VAR VAR VAR IF VAR VAR VAR ASSIGN VAR VAR VAR VAR NUMBER IF VAR BIN_OP BIN_OP VAR VAR NUMBER VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER VAR VAR NUMBER ASSIGN VAR VAR 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.
for _ in range(int(input())): n, k = list(map(int, input().split())) a = list(input()) ans = 0 if k % 2 == 0: for i in range(k // 2): ar = dict() for r in range(i, n, k): if a[r] not in ar: ar[a[r]] = 1 else: ar[a[r]] += 1 for r in range(n - 1 - i, -1, -k): if a[r] not in ar: ar[a[r]] = 1 else: ar[a[r]] += 1 m = 0 for elem in ar: if ar[elem] > m: m = ar[elem] ans += 2 * (n // k) - m print(ans) else: for i in range(k // 2): ar = dict() for r in range(i, n, k): if a[r] not in ar: ar[a[r]] = 1 else: ar[a[r]] += 1 for r in range(n - 1 - i, -1, -k): if a[r] not in ar: ar[a[r]] = 1 else: ar[a[r]] += 1 m = 0 for elem in ar: if ar[elem] > m: m = ar[elem] ans += 2 * (n // k) - m ar = dict() for i in range(k // 2, n, k): if a[i] not in ar: ar[a[i]] = 1 else: ar[a[i]] += 1 m = 0 for elem in ar: if ar[elem] > m: m = ar[elem] ans += n // k - m 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 FUNC_CALL VAR ASSIGN VAR NUMBER IF BIN_OP VAR NUMBER 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 ASSIGN VAR VAR VAR NUMBER VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP BIN_OP VAR NUMBER VAR NUMBER VAR IF VAR VAR VAR ASSIGN VAR VAR VAR NUMBER VAR VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR IF VAR VAR VAR ASSIGN VAR VAR VAR VAR BIN_OP BIN_OP NUMBER BIN_OP VAR VAR VAR EXPR FUNC_CALL VAR VAR 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 ASSIGN VAR VAR VAR NUMBER VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP BIN_OP VAR NUMBER VAR NUMBER VAR IF VAR VAR VAR ASSIGN VAR VAR VAR NUMBER VAR VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR IF VAR VAR VAR ASSIGN VAR VAR VAR VAR BIN_OP BIN_OP NUMBER BIN_OP VAR VAR VAR ASSIGN VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR IF VAR VAR VAR ASSIGN VAR VAR VAR NUMBER VAR VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR IF VAR VAR VAR ASSIGN VAR 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 input = sys.stdin.readline t = int(input()) def Find(x, par): if par[x] < 0: return x else: par[x] = Find(par[x], par) return par[x] def Unite(x, y, par, rank): x = Find(x, par) y = Find(y, par) if x != y: if rank[x] < rank[y]: par[y] += par[x] par[x] = y else: par[x] += par[y] par[y] = x if rank[x] == rank[y]: rank[x] += 1 def Same(x, y, par): return Find(x, par) == Find(y, par) def Size(x, par): return -par[Find(x, par)] for _ in range(t): n, k = map(int, input().split()) s = str(input()) par = [-1] * n rank = [0] * n for i in range(n): Unite(i, i % k, par, rank) Unite(i, n - 1 - i, par, rank) d = {} j = 0 for i in range(n): if par[i] < 0: d[i] = j j += 1 g = len(d.keys()) L = [{} for _ in range(g)] for i in range(n): p = Find(i, par) id = d[p] if s[i] not in L[id]: L[id][s[i]] = 1 else: L[id][s[i]] += 1 ans = 0 for i in range(g): d_ = L[i] vs = list(d_.values()) ans += sum(vs) - max(vs) print(ans)
IMPORT ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_DEF IF VAR VAR NUMBER RETURN VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR VAR RETURN VAR VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR IF VAR VAR IF VAR VAR VAR VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR IF VAR VAR VAR VAR VAR VAR NUMBER FUNC_DEF RETURN FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR FUNC_DEF RETURN VAR FUNC_CALL VAR 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 FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR BIN_OP VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR NUMBER VAR VAR VAR ASSIGN VAR DICT ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER ASSIGN VAR VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR DICT VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR VAR IF VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR NUMBER VAR VAR VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR 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 readline = sys.stdin.readline T = int(readline()) Ans = [None] * T als = 26 for qu in range(T): N, K = map(int, readline().split()) S = list(map(lambda x: ord(x) - 97, readline().strip())) R = N // K cost = [([2 * R] * als) for _ in range(K // 2)] if K & 1: cost.append([R] * als) for i in range(N): pidx = i % K pidx = min(pidx, K - 1 - pidx) cost[pidx][S[i]] -= 1 Ans[qu] = sum(map(min, cost)) print("\n".join(map(str, Ans)))
IMPORT ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NONE VAR ASSIGN VAR NUMBER 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 BIN_OP FUNC_CALL VAR VAR NUMBER FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP LIST BIN_OP NUMBER VAR VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER IF BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP LIST VAR VAR FOR 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 VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL STRING 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 solve(n, k, word): counters = [{} for _ in range((k + 1) // 2)] for p in range(n): i = p % k if i > k - i - 1: i = k - i - 1 counters[i][word[p]] = counters[i].get(word[p], 0) + 1 ans = n - sum(max(c.values()) for c in counters) return ans def test(): assert solve(6, 2, "abaaba") == 2 assert solve(6, 3, "abaaba") == 0 assert solve(36, 9, "hippopotomonstrosesquippedaliophobia") == 23 assert solve(21, 7, "wudixiaoxingxingheclp") == 16 test() t = int(input()) for i in range(t): n, k = [int(x) for x in input().split()] s = input() print(solve(n, k, s))
FUNC_DEF ASSIGN VAR DICT VAR FUNC_CALL VAR BIN_OP BIN_OP VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR VAR IF VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR VAR VAR VAR BIN_OP FUNC_CALL VAR VAR VAR VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR RETURN VAR FUNC_DEF FUNC_CALL VAR NUMBER NUMBER STRING NUMBER FUNC_CALL VAR NUMBER NUMBER STRING NUMBER FUNC_CALL VAR NUMBER NUMBER STRING NUMBER FUNC_CALL VAR NUMBER NUMBER STRING NUMBER EXPR FUNC_CALL VAR 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 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.
import sys def run_case(): n, k = map(int, input().split()) s = input() cnt = [([0] * 26) for j in range((k + 1) // 2)] ch = list(map(lambda x: ord(x) - ord("a"), s)) for i in range(n): cnt[min(i % k, k - i % k - 1)][ch[i]] += 1 res = 0 for i in range(k // 2): res += 2 * n // k - max(cnt[i]) if k % 2 == 1: res += n // k - max(cnt[k // 2]) print(res) t = int(input()) for _ in range(t): run_case()
IMPORT FUNC_DEF 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 ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR STRING VAR FOR 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 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 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 i in range(t): n, k = map(int, input().split()) s, p, k = input(), n, n // k for i in range(n // k // 2): d, m = dict(), 0 for j in range(k): d[s[j * (n // k) + i]] = d.get(s[j * (n // k) + i], 0) + 1 d[s[(j + 1) * (n // k) - i - 1]] = ( d.get(s[(j + 1) * (n // k) - i - 1], 0) + 1 ) for i in d: if m < d[i]: m = d[i] p = p - m if n // k % 2 == 1: d, m = dict(), 0 for j in range(k): d[s[j * (n // k) + n // k // 2]] = ( d.get(s[j * (n // k) + n // k // 2], 0) + 1 ) for i in d: if m < d[i]: m = d[i] p = p - m print(p)
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 VAR VAR FUNC_CALL VAR VAR BIN_OP VAR VAR FOR VAR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR BIN_OP BIN_OP VAR BIN_OP VAR VAR VAR BIN_OP FUNC_CALL VAR VAR BIN_OP BIN_OP VAR BIN_OP VAR VAR VAR NUMBER NUMBER ASSIGN VAR VAR BIN_OP BIN_OP BIN_OP BIN_OP VAR NUMBER BIN_OP VAR VAR VAR NUMBER BIN_OP FUNC_CALL VAR VAR BIN_OP BIN_OP BIN_OP BIN_OP VAR NUMBER BIN_OP VAR VAR VAR NUMBER NUMBER NUMBER FOR VAR VAR IF VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR IF BIN_OP BIN_OP VAR VAR NUMBER NUMBER ASSIGN VAR VAR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR BIN_OP BIN_OP VAR BIN_OP VAR VAR BIN_OP BIN_OP VAR VAR NUMBER BIN_OP FUNC_CALL VAR VAR BIN_OP BIN_OP VAR BIN_OP VAR VAR BIN_OP BIN_OP VAR VAR NUMBER NUMBER NUMBER FOR VAR VAR IF VAR VAR VAR ASSIGN 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.
import sys input = sys.stdin.readline t = int(input()) for _ in range(t): n, k = map(int, input().split()) s = input()[:-1] cnt = [([0] * 26) for _ in range((k + 1) // 2)] for i in range(n): cnt[min(i % k, k - 1 - i % k)][ord(s[i]) - ord("a")] += 1 ans = 0 for i in range((k + 1) // 2): ans += sum(cnt[i]) - max(cnt[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 NUMBER 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 NUMBER BIN_OP 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 BIN_OP FUNC_CALL VAR 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.
t = int(input()) for _ in range(t): n, k = map(int, input().split()) s = input() count = 0 ls = [] i = 0 for j in range(n // k): ls.append(s[i : i + k]) i += k for i in range((k + 1) // 2): dct = {} mx = 1 res = ls[0][i] for st in ls: if st[i] not in dct: dct[st[i]] = 1 else: dct[st[i]] += 1 if dct[st[i]] > mx: mx = dct[st[i]] res = st[i] if i != k - i - 1: if st[k - i - 1] not in dct: dct[st[k - i - 1]] = 1 else: dct[st[k - i - 1]] += 1 if dct[st[k - i - 1]] > mx: mx = dct[st[k - i - 1]] res = st[k - i - 1] for key in dct.keys(): if key != res: count += dct[key] 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 ASSIGN VAR NUMBER ASSIGN VAR LIST ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR VAR BIN_OP VAR VAR VAR VAR FOR VAR FUNC_CALL VAR BIN_OP BIN_OP VAR NUMBER NUMBER ASSIGN VAR DICT ASSIGN VAR NUMBER ASSIGN VAR VAR NUMBER VAR FOR VAR VAR 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 VAR VAR IF VAR BIN_OP BIN_OP 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 IF VAR VAR BIN_OP BIN_OP VAR VAR NUMBER VAR ASSIGN VAR VAR VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR VAR BIN_OP BIN_OP VAR VAR NUMBER FOR VAR FUNC_CALL VAR IF 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.
input = __import__("sys").stdin.readline print = __import__("sys").stdout.write for _ in range(int(input())): n, k = map(int, input().split()) word = input() r = n // k ans = 0 tmp = [[(0) for _ in range(26)] for i in range(k)] for i in range(k): j = i while j < n: tmp[i][ord(word[j]) - 97] += 1 j += k until = k // 2 for i in range(until): for j in range(26): tmp[i][j] += tmp[k - i - 1][j] ans += r * 2 - max(tmp[i]) if k % 2 == 1: ans += r - max(tmp[until]) print(f"{ans}\n")
ASSIGN VAR FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR STRING 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 ASSIGN VAR NUMBER VAR FUNC_CALL VAR NUMBER VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR WHILE VAR VAR VAR VAR BIN_OP FUNC_CALL VAR VAR VAR NUMBER NUMBER VAR VAR ASSIGN VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR NUMBER VAR VAR VAR VAR BIN_OP BIN_OP VAR VAR NUMBER VAR VAR BIN_OP BIN_OP VAR NUMBER FUNC_CALL VAR VAR VAR IF BIN_OP VAR NUMBER NUMBER VAR BIN_OP VAR FUNC_CALL VAR VAR 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.
from sys import stdin def solve(n, k, s): ans = 0 for i in range(k // 2): d = {} for j in range(n // k): if s[i + k * j] in d: d[s[i + k * j]] += 1 else: d[s[i + k * j]] = 1 if s[k * j - 1 - i] in d: d[s[k * j - 1 - i]] += 1 else: d[s[k * j - 1 - i]] = 1 m = 0 for u in d.keys(): m = max(m, d[u]) ans += n // k * 2 - m if k % 2 == 1: d = {} p = k // 2 for j in range(n // k): if s[p + k * j] in d: d[s[p + k * j]] += 1 else: d[s[p + k * j]] = 1 m = 0 for u in d.keys(): m = max(m, d[u]) ans += n // k - m print(ans) def main(): input = stdin.readline for _ in range(int(input())): n, k = map(int, input().split()) s = input().strip() solve(n, k, s) main()
FUNC_DEF 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 VAR VAR VAR VAR BIN_OP BIN_OP BIN_OP VAR VAR NUMBER VAR NUMBER ASSIGN VAR VAR BIN_OP BIN_OP BIN_OP VAR VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR BIN_OP BIN_OP BIN_OP VAR VAR NUMBER VAR IF BIN_OP VAR NUMBER NUMBER ASSIGN VAR DICT ASSIGN VAR BIN_OP VAR NUMBER 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 ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR BIN_OP BIN_OP VAR VAR VAR EXPR FUNC_CALL VAR VAR FUNC_DEF 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 FUNC_CALL VAR EXPR FUNC_CALL VAR VAR 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.
for _ in range(int(input())): n, k = map(int, input().split()) s = input() l = [] for i in s: l.append(i) a = 0 for i in range(k // 2): c = [] for j in range(26): c.append(0) for j in range(i, n, k): c[ord(s[j]) - 97] += 1 for j in range(k - i - 1, n, k): c[ord(s[j]) - 97] += 1 m = max(c) m = c.index(m) z = chr(m + 97) for j in range(i, n, k): if l[j] != z: a += 1 l[j] = z for j in range(k - i - 1, n, k): if l[j] != z: a += 1 l[j] = z if k % 2 == 1: c = [] for j in range(26): c.append(0) for j in range(k // 2, n, k): c[ord(s[j]) - 97] += 1 m = max(c) m = c.index(m) z = chr(m + 97) for j in range(k // 2, n, k): if l[j] != z: a += 1 l[j] = z print(a)
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 FOR VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL 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 VAR VAR BIN_OP FUNC_CALL VAR VAR VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR NUMBER VAR VAR VAR BIN_OP FUNC_CALL VAR VAR VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR VAR IF VAR VAR VAR VAR NUMBER ASSIGN VAR VAR VAR FOR VAR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR NUMBER VAR VAR IF VAR VAR VAR VAR NUMBER ASSIGN VAR VAR VAR IF BIN_OP VAR NUMBER NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR VAR BIN_OP FUNC_CALL VAR VAR VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR IF VAR VAR VAR VAR NUMBER ASSIGN 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.
x = [ "a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z", ] t = int(input()) for j in range(t): n, k = map(int, input().split()) ans = n s = str(input()) if k % 2 == 0: for i in range(k // 2): b = [0] * 26 for w in range(n // k): b[x.index(s[i + w * k])] += 1 b[x.index(s[k - i - 1 + w * k])] += 1 ans -= max(b) print(ans) else: for i in range(k // 2): b = [0] * 26 for w in range(n // k): b[x.index(s[i + w * k])] += 1 b[x.index(s[k - i - 1 + w * k])] += 1 ans -= max(b) b1 = [0] * 26 for w1 in range(n // k): b1[x.index(s[k // 2 + w1 * k])] += 1 ans -= max(b1) print(ans)
ASSIGN VAR LIST STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING 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 VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR IF BIN_OP VAR NUMBER 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 FUNC_CALL VAR VAR BIN_OP VAR BIN_OP VAR VAR NUMBER VAR FUNC_CALL VAR VAR BIN_OP BIN_OP BIN_OP VAR VAR NUMBER BIN_OP VAR VAR NUMBER VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR 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 FUNC_CALL VAR VAR BIN_OP VAR BIN_OP VAR VAR NUMBER VAR FUNC_CALL VAR VAR BIN_OP BIN_OP BIN_OP VAR VAR NUMBER BIN_OP VAR VAR NUMBER VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR VAR VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR NUMBER BIN_OP VAR VAR NUMBER 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 _ in range(t): n, k = [int(x) for x in input().split()] s = input() changes = 0 counts = dict() for i in range((k + 1) // 2): counts.clear() for j in range(i, n, k): counts[s[j]] = counts.get(s[j], 0) + 1 if i != k // 2: for j in range(k - i - 1, n, k): counts[s[j]] = counts.get(s[j], 0) + 1 changes += sum(counts.values()) - max(counts.values()) print(changes)
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 NUMBER ASSIGN VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR BIN_OP BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR VAR VAR BIN_OP FUNC_CALL VAR VAR VAR NUMBER NUMBER IF VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR NUMBER VAR VAR ASSIGN VAR VAR VAR BIN_OP FUNC_CALL VAR VAR VAR NUMBER NUMBER VAR BIN_OP FUNC_CALL 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.
t = int(input()) for _ in range(t): n, k = (int(i) for i in input().split()) s = input() ans = "" for i in range((k + 1) // 2): ch = [0] * 26 for j in range(i, n, k): ch[ord(s[j]) - 97] += 1 for j in range(k - i - 1, n, k): ch[ord(s[j]) - 97] += 1 ind = max(ch) for temp in range(26): if ch[temp] == ind: ind = temp break ans = ans + chr(97 + ind) count = 0 for ii in range(k // 2 - 1, -1, -1): ans = ans + ans[ii] j = 0 for i in range(n): if s[i] != ans[j]: count += 1 j += 1 if j == k: j = 0 print(count)
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 STRING FOR VAR FUNC_CALL VAR BIN_OP BIN_OP VAR NUMBER 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 FOR VAR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR NUMBER VAR VAR VAR BIN_OP FUNC_CALL VAR VAR VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR NUMBER IF VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR FUNC_CALL VAR BIN_OP NUMBER VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP BIN_OP VAR NUMBER NUMBER NUMBER NUMBER ASSIGN VAR BIN_OP VAR VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR VAR NUMBER VAR NUMBER IF VAR VAR ASSIGN 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.
from sys import stdin inp = stdin.readline t = int(inp().strip()) for c in range(t): n, k = [int(x) for x in inp().strip().split()] array = list(inp().strip()) d1 = {} for i in range(n): if i % k < k / 2: dIndex = k - 1 - i % k else: dIndex = i % k if not d1.get(dIndex, 0): d1[dIndex] = {array[i]: 1} elif not d1[dIndex].get(array[i], 0): d1[dIndex][array[i]] = 1 else: d1[dIndex][array[i]] += 1 number = n // k numberOfMoves = 0 for i in d1.values(): maximum = 0 for j in i.values(): if j > maximum: maximum = j numberOfMoves += 2 * number - maximum if k % 2: numberOfMoves -= number print(numberOfMoves)
ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR DICT FOR VAR FUNC_CALL VAR VAR IF BIN_OP VAR VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR NUMBER BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR VAR IF FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR DICT VAR VAR NUMBER IF FUNC_CALL VAR VAR VAR VAR NUMBER ASSIGN VAR VAR VAR VAR NUMBER VAR VAR VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR IF VAR VAR ASSIGN VAR VAR VAR BIN_OP BIN_OP NUMBER VAR VAR IF BIN_OP VAR NUMBER 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 x in range(int(input())): n, k = map(int, input().split()) s = input() ans = 0 if k % 2 == 0: for i in range(k // 2): li = [0] * 27 for j in range(n // k): li[ord(s[k * j + i]) - 96] += 1 li[ord(s[k * (j + 1) - (i + 1)]) - 96] += 1 ans += 2 * (n // k) - max(li) print(ans) else: for i in range(k // 2): li = [0] * 27 for j in range(n // k): li[ord(s[k * j + i]) - 96] += 1 li[ord(s[k * (j + 1) - (i + 1)]) - 96] += 1 ans += 2 * (n // k) - max(li) li = [0] * 27 for j in range(n // k): li[ord(s[k * j + k // 2]) - 96] += 1 ans += n // k - max(li) 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 IF BIN_OP VAR NUMBER 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 NUMBER NUMBER VAR BIN_OP FUNC_CALL VAR VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER NUMBER NUMBER VAR BIN_OP BIN_OP NUMBER BIN_OP VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR 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 NUMBER NUMBER VAR BIN_OP FUNC_CALL VAR VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER NUMBER NUMBER VAR BIN_OP BIN_OP NUMBER BIN_OP VAR VAR FUNC_CALL VAR VAR 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 NUMBER NUMBER VAR BIN_OP BIN_OP 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()) while t: t -= 1 n, k = map(int, input().split()) a = input() r1 = [[] for i in range(k)] for i in range(n): r1[i % k].append(a[i]) s = 0 l = len(r1) for i in range(l // 2): f = [(0) for k in range(26)] for j in r1[i]: f[ord(j) - 97] += 1 for j in r1[-1 - i]: f[ord(j) - 97] += 1 z = max(f) s += sum(f) - z if l % 2: f = [(0) for k in range(26)] for j in r1[l // 2]: f[ord(j) - 97] += 1 z = max(f) s += sum(f) - z print(s)
IMPORT ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR WHILE VAR VAR NUMBER 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 VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR NUMBER FOR VAR VAR VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER FOR VAR VAR BIN_OP NUMBER VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR BIN_OP FUNC_CALL VAR VAR VAR IF BIN_OP VAR NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR NUMBER FOR VAR VAR BIN_OP VAR NUMBER VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR BIN_OP 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.
def dfs(node, edges, s, visited): letters = [0] * 26 stack = [node] while stack: curr = stack.pop() if curr not in visited: visited.add(curr) letters[ord(s[curr - 1]) - ord("a")] += 1 for kid in edges[curr]: stack.append(kid) letters.sort() letters.pop() return sum(letters) def solve(s, n, k, ans): edges = {} for i in range(1, n + 1): edges[i] = set() for i in range(1, n + 1): if i + k <= n: edges[i].add(i + k) edges[i + k].add(i) if i - k >= 1: edges[i].add(i - k) edges[i - k].add(i) edges[i].add(n + 1 - i) edges[n + 1 - i].add(i) visited = set() count = 0 for i in range(1, n + 1): if i not in visited: count += dfs(i, edges, s, visited) ans.append(count) def main(): t = int(input()) ans = [] for i in range(t): n, k = map(int, input().split()) s = input() solve(s, n, k, ans) for i in ans: print(i) main()
FUNC_DEF ASSIGN VAR BIN_OP LIST NUMBER NUMBER ASSIGN VAR LIST VAR WHILE VAR ASSIGN VAR FUNC_CALL VAR IF VAR VAR EXPR FUNC_CALL VAR VAR VAR BIN_OP FUNC_CALL VAR VAR BIN_OP VAR NUMBER FUNC_CALL VAR STRING NUMBER FOR VAR VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR RETURN FUNC_CALL VAR VAR FUNC_DEF ASSIGN VAR DICT FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER IF BIN_OP VAR VAR VAR EXPR FUNC_CALL VAR VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR VAR IF BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR VAR EXPR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR BIN_OP BIN_OP VAR NUMBER VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER IF VAR VAR VAR FUNC_CALL VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR FUNC_DEF 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 EXPR FUNC_CALL VAR VAR VAR VAR VAR FOR 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()) for _ in range(t): n, k = [int(i) for i in input().split()] s = input() out = 0 for i in range(k // 2): d = dict() for j in range(n // k): c1 = s[i + j * k] c2 = s[k - i - 1 + j * k] d[c1] = d[c1] + 1 if c1 in d else 1 d[c2] = d[c2] + 1 if c2 in d else 1 out += 2 * n // k - max(d.values()) if k % 2 == 1: d = dict() for j in range(n // k): c1 = s[j * k + k // 2] d[c1] = d[c1] + 1 if c1 in d else 1 out += n // k - max(d.values()) print(out)
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 NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR BIN_OP VAR VAR ASSIGN VAR VAR BIN_OP VAR BIN_OP VAR VAR ASSIGN VAR VAR BIN_OP BIN_OP BIN_OP VAR VAR NUMBER BIN_OP VAR VAR ASSIGN VAR VAR VAR VAR BIN_OP VAR VAR NUMBER NUMBER ASSIGN VAR VAR VAR VAR BIN_OP VAR VAR NUMBER NUMBER VAR BIN_OP BIN_OP BIN_OP NUMBER VAR VAR FUNC_CALL VAR FUNC_CALL VAR IF BIN_OP VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR BIN_OP VAR VAR ASSIGN VAR VAR BIN_OP BIN_OP VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR VAR BIN_OP VAR VAR NUMBER NUMBER VAR BIN_OP BIN_OP 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.
t = int(input()) while t != 0: n, k = map(int, input().split()) list1 = list(input()) ind = 0 ans = 0 for i in range(k // 2): count = 0 arr = [0] * 26 for j in range(n // k): p = list1[count + ind] count += k arr[ord(p) - ord("a")] += 1 count = 0 for j in range(n // k): p = list1[count + k - ind - 1] count += k arr[ord(p) - ord("a")] += 1 ind += 1 ans += n // k + n // k - max(arr) if k % 2 == 1: count = 0 ind = k // 2 arr = [0] * 26 for i in range(n // k): p = list1[count + ind] count += k arr[ord(p) - ord("a")] += 1 ans += n // k - max(arr) print(ans) 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 FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR VAR ASSIGN VAR VAR BIN_OP VAR VAR VAR VAR VAR BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR STRING NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR VAR ASSIGN VAR VAR BIN_OP BIN_OP BIN_OP VAR VAR VAR NUMBER VAR VAR VAR BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR STRING NUMBER VAR NUMBER VAR BIN_OP BIN_OP BIN_OP VAR VAR BIN_OP VAR VAR FUNC_CALL VAR VAR IF BIN_OP VAR NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR VAR ASSIGN VAR VAR BIN_OP VAR VAR VAR VAR VAR BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR STRING NUMBER VAR BIN_OP BIN_OP 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.
def primeFacorize(x): p = [7, 11, 13, 17, 19, 23, 29, 31, 37] ans = [] t = 0 for i in p: if x % i == 0: t = i break return [t, x // t] for _ in range(int(input())): n, k = map(int, input().split()) s = input() h = 0 if k % 2 == 0: h = k // 2 - 1 else: h = k // 2 ans = 0 for i in range(h + 1): D = {} maxV = 0 for j in range(0, n, k): if s[j + i] not in D: D[s[j + i]] = 1 else: D[s[j + i]] += 1 if D[s[j + i]] > maxV: maxV = D[s[j + i]] if j + i != j + k - 1 - i: if s[j + k - 1 - i] not in D: D[s[j + k - 1 - i]] = 1 else: D[s[j + k - 1 - i]] += 1 if D[s[j + k - 1 - i]] > maxV: maxV = D[s[j + k - 1 - i]] if i == h and k % 2 != 0: ans += n // k - maxV else: ans += n // k * 2 - maxV print(ans)
FUNC_DEF ASSIGN VAR LIST NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER ASSIGN VAR LIST ASSIGN VAR NUMBER FOR VAR VAR IF BIN_OP VAR VAR NUMBER ASSIGN VAR VAR RETURN LIST VAR BIN_OP 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 IF BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR DICT ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR VAR IF VAR BIN_OP VAR VAR VAR ASSIGN VAR VAR BIN_OP VAR VAR NUMBER VAR VAR BIN_OP VAR VAR NUMBER IF VAR VAR BIN_OP VAR VAR VAR ASSIGN VAR VAR VAR BIN_OP VAR VAR IF BIN_OP VAR VAR BIN_OP BIN_OP BIN_OP VAR VAR NUMBER VAR IF VAR BIN_OP BIN_OP BIN_OP VAR VAR NUMBER VAR VAR ASSIGN VAR VAR BIN_OP BIN_OP BIN_OP VAR VAR NUMBER VAR NUMBER VAR VAR BIN_OP BIN_OP BIN_OP VAR VAR NUMBER VAR NUMBER IF VAR VAR BIN_OP BIN_OP BIN_OP VAR VAR NUMBER VAR VAR ASSIGN VAR VAR VAR BIN_OP BIN_OP BIN_OP VAR VAR NUMBER VAR IF VAR VAR 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 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 = [int(x) for x in input().strip().split()] s = input() mat = [[(0) for i in range(26)] for j in range(k)] i = 0 while i < n: j = 0 while j < k: char = ord(s[i + j]) - 97 mat[j][char] += 1 j += 1 i += k def fmax(mat, l, h): res = 10**20 for i in range(26): temp = n // k - mat[l][i] + n // k - mat[h][i] res = min(res, temp) if l == h: res = res // 2 return res res = 0 l = 0 h = k - 1 while l <= h: if l == h: res += fmax(mat, l, l) break res += fmax(mat, l, h) l += 1 h -= 1 print(res)
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 FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR NUMBER VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER WHILE VAR VAR ASSIGN VAR NUMBER WHILE VAR VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR BIN_OP VAR VAR NUMBER VAR VAR VAR NUMBER VAR NUMBER VAR VAR FUNC_DEF ASSIGN VAR BIN_OP NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER ASSIGN VAR BIN_OP BIN_OP BIN_OP BIN_OP VAR VAR VAR VAR VAR BIN_OP VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR IF VAR VAR ASSIGN VAR BIN_OP VAR NUMBER RETURN VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER WHILE VAR VAR IF VAR VAR VAR FUNC_CALL VAR VAR VAR VAR VAR FUNC_CALL VAR VAR VAR VAR VAR NUMBER 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 w in range(int(input())): n, k = tuple(map(int, input().split())) s = str(input()) vis = [0] * n a = [] ans = 0 for i in range(n): if vis[i] == 0: if i != n - 1 - i: l = [i, n - 1 - i] vis[i] = 1 vis[n - 1 - i] = 1 else: l = [i] vis[i] = 1 for j in range(i + k, n, k): if vis[j] == 0: if j != n - 1 - j: l.append(j) l.append(n - 1 - j) vis[j] = 1 vis[n - 1 - j] = 1 else: l.append(j) vis[j] = 1 a.append(l) for i in range(len(a)): d = {} x = len(a[i]) for j in range(x): if s[a[i][j]] not in d: d[s[a[i][j]]] = 1 else: d[s[a[i][j]]] += 1 m = 0 for j in d: m = max(m, d[j]) ans += x - m 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 FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR LIST ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER IF VAR BIN_OP BIN_OP VAR NUMBER VAR ASSIGN VAR LIST VAR BIN_OP BIN_OP VAR NUMBER VAR ASSIGN VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR LIST VAR ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR IF VAR VAR NUMBER IF VAR BIN_OP BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP BIN_OP VAR NUMBER VAR ASSIGN VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR NUMBER EXPR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR DICT ASSIGN VAR FUNC_CALL VAR VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR NUMBER VAR VAR VAR VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR ASSIGN VAR FUNC_CALL 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.
def freq(word, K, idx): is_middle = K % 2 != 0 and K // 2 == idx letters = [0] * 26 off = K - idx * 2 - 1 while idx < len(word): letters[ord(word[idx]) - ord("a")] += 1 if not is_middle: letters[ord(word[idx + off]) - ord("a")] += 1 idx += K if is_middle: return len(word) // K - max(letters) return len(word) // K * 2 - max(letters) T = int(input()) for _ in range(T): N, K = map(int, input().split()) word = list(input()) sum = 0 for idx in range((K + 1) // 2): sum += freq(word, K, idx) print(sum)
FUNC_DEF ASSIGN VAR BIN_OP VAR NUMBER NUMBER BIN_OP VAR NUMBER VAR ASSIGN VAR BIN_OP LIST NUMBER NUMBER ASSIGN VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER WHILE VAR FUNC_CALL VAR VAR VAR BIN_OP FUNC_CALL VAR VAR VAR FUNC_CALL VAR STRING NUMBER IF VAR VAR BIN_OP FUNC_CALL VAR VAR BIN_OP VAR VAR FUNC_CALL VAR STRING NUMBER VAR VAR IF VAR RETURN BIN_OP BIN_OP FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR RETURN BIN_OP BIN_OP BIN_OP FUNC_CALL VAR VAR VAR NUMBER FUNC_CALL 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 NUMBER FOR VAR FUNC_CALL VAR BIN_OP BIN_OP VAR NUMBER NUMBER 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): n, k = map(int, input().split()) s = input() a = [] for i in range(n // k): a.append(s[k * i : k * (i + 1)]) ans = 0 for j in range(k // 2): c = [0] * 26 for i in range(len(a)): c[ord(a[i][j]) - ord("a")] += 1 c[ord(a[i][-j - 1]) - ord("a")] += 1 ans += len(a) * 2 - max(c) if k % 2 == 1: c = [0] * 26 for i in range(len(a)): c[ord(a[i][k // 2]) - ord("a")] += 1 ans += len(a) - max(c) 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 LIST FOR VAR FUNC_CALL VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR BIN_OP VAR VAR BIN_OP VAR BIN_OP VAR NUMBER ASSIGN 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 FUNC_CALL VAR STRING NUMBER VAR BIN_OP FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER FUNC_CALL VAR STRING NUMBER VAR BIN_OP BIN_OP FUNC_CALL VAR VAR NUMBER FUNC_CALL VAR VAR IF BIN_OP VAR NUMBER 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 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.
o = int(input()) for _ in range(o): n, k = list(map(int, input().split(" "))) s = input() ans = 0 count = 2 * n // k for i in range(k): j = k - 1 - i if j <= i: break else: dic = {} for t in range(i, n, k): x = s[t] if x in dic: dic[x] += 1 else: dic[x] = 1 for t in range(j, n, k): x = s[t] if x in dic: dic[x] += 1 else: dic[x] = 1 MAX = 0 temp = list(dic.keys()) for i in temp: MAX = max(MAX, dic[i]) ans += count - MAX if k % 2 == 1: i = (k - 1) // 2 dic = {} for t in range(i, n, k): x = s[t] if x in dic: dic[x] += 1 else: dic[x] = 1 MAX = 0 temp = list(dic.keys()) for i in temp: MAX = max(MAX, dic[i]) count = n // k ans += count - MAX 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 STRING ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP BIN_OP NUMBER VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR NUMBER VAR IF VAR VAR ASSIGN VAR DICT FOR VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR VAR VAR IF VAR VAR VAR VAR NUMBER ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR VAR VAR IF VAR VAR VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR BIN_OP VAR VAR IF BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP BIN_OP VAR NUMBER NUMBER ASSIGN VAR DICT FOR VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR VAR VAR IF VAR VAR VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR BIN_OP 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 i in range(0, t, 1): n, k = [int(x) for x in input().split(" ")] s = input() total = 0 for i in range(0, k // 2, 1): count = [0] * 123 maxi = 0 for j in range(0, n, k): s1 = j + i s2 = j + (k - 1 - i) count[ord(s[s1])] += 1 count[ord(s[s2])] += 1 maxi = max(count) req = 2 * (n // k) total = total + (req - maxi) if k % 2 != 0: count = [0] * 123 for i in range(k // 2, n, k): count[ord(s[i])] += 1 maxi = max(count) total = total + (n // k - maxi) print(total)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR NUMBER VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP LIST NUMBER NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR BIN_OP BIN_OP VAR NUMBER VAR VAR FUNC_CALL VAR VAR VAR NUMBER VAR FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP NUMBER BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR BIN_OP VAR VAR IF BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP LIST NUMBER NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR VAR FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP 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 input = sys.stdin.readline t = int(input()) def find(no): global par if par[no] == no: return no par[no] = find(par[no]) return par[no] for _ in range(t): n, k = map(int, input().split()) s = [(ord(i) - 97) for i in input()] par = [i for i in range(n)] vis = [(0) for i in range(n)] for i in range(k): for j in range(i, n, k): vis[j] = 1 x = n - j - 1 aa = find(x) bb = find(j) par[bb] = aa if j > i: aa = find(j - k) bb = find(j) par[bb] = aa tot = n aa = set(par) co = [[(0) for i in range(26)] for j in range(n)] for i in range(n): co[par[i]][s[i]] += 1 tot -= sum([max(i) for i in co]) print(tot)
IMPORT ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_DEF IF VAR VAR VAR RETURN VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR RETURN VAR 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 VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR IF VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR NUMBER VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR VAR VAR VAR VAR VAR NUMBER VAR FUNC_CALL 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.
for i in range(int(input())): [n, k] = list(map(int, input().split())) p = int(n / k) s = list(input()) total = 0 for j in range(1, int((k + 1) / 2) + 1): dom = {} for l in range(p): dom[s[l * k + j - 1]] = dom.get(s[l * k + j - 1], 0) + 1 dom[s[(l + 1) * k - j]] = dom.get(s[(l + 1) * k - j], 0) + 1 largest = max(dom.values()) if j == k + 1 - j: total += p - largest / 2 else: total += 2 * p - largest print(int(total))
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN LIST VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP FUNC_CALL VAR BIN_OP BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR DICT FOR VAR FUNC_CALL VAR VAR 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 ASSIGN VAR VAR BIN_OP BIN_OP BIN_OP VAR NUMBER VAR VAR BIN_OP FUNC_CALL VAR VAR BIN_OP BIN_OP BIN_OP VAR NUMBER VAR VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR IF VAR BIN_OP 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 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() z = 0 cnt = [([0] * 26) for i in range(k)] for i in range(n): c = s[i] cnt[i % k][ord(c) - ord("a")] += 1 cnts = [sum(x) for x in cnt] all = 0 for j in range(k // 2): mi = 10**9 for x in range(26): q = cnts[j] - cnt[j][x] + cnts[k - j - 1] - cnt[k - j - 1][x] mi = min(mi, q) all += mi if k % 2: mi = 10**9 for x in range(26): q = cnts[k // 2] - cnt[k // 2][x] mi = min(mi, q) all += mi print(all)
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 BIN_OP LIST NUMBER NUMBER VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR BIN_OP VAR VAR BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR STRING NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR VAR VAR VAR VAR VAR BIN_OP BIN_OP VAR VAR NUMBER VAR BIN_OP BIN_OP VAR VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR 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 q in range(t): n, k = map(int, input().split()) n //= k longword = input() lst = [longword[i * k : (i + 1) * k] for i in range(n)] ans = 0 for j in range(k // 2): temp = dict() maxc = 0 for i in range(n): if lst[i][j] in temp: temp[lst[i][j]] += 1 else: temp[lst[i][j]] = 1 if temp[lst[i][j]] > maxc: maxc = temp[lst[i][j]] letter = lst[i][j] if lst[i][k - j - 1] in temp: temp[lst[i][k - j - 1]] += 1 else: temp[lst[i][k - j - 1]] = 1 if temp[lst[i][k - j - 1]] > maxc: maxc = temp[lst[i][k - j - 1]] letter = lst[i][k - j - 1] for i in range(n): if lst[i][j] != letter: ans += 1 if lst[i][k - j - 1] != letter: ans += 1 if k % 2 == 1: temp = dict() maxc = 0 for i in range(n): if lst[i][k // 2] in temp: temp[lst[i][k // 2]] += 1 else: temp[lst[i][k // 2]] = 1 if temp[lst[i][k // 2]] > maxc: maxc = temp[lst[i][k // 2]] letter = lst[i][k // 2] for i in range(n): if lst[i][k // 2] != letter: ans += 1 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 VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR VAR BIN_OP VAR VAR BIN_OP BIN_OP VAR NUMBER VAR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR VAR VAR VAR VAR NUMBER ASSIGN VAR VAR VAR VAR NUMBER IF VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR IF VAR VAR BIN_OP BIN_OP VAR VAR NUMBER VAR VAR VAR VAR BIN_OP BIN_OP VAR VAR NUMBER NUMBER ASSIGN VAR VAR VAR BIN_OP BIN_OP VAR VAR NUMBER NUMBER IF VAR VAR VAR BIN_OP BIN_OP VAR VAR NUMBER VAR ASSIGN VAR VAR VAR VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR VAR VAR BIN_OP BIN_OP VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR VAR NUMBER IF VAR VAR BIN_OP BIN_OP VAR VAR NUMBER VAR VAR NUMBER IF BIN_OP VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR BIN_OP VAR NUMBER VAR VAR VAR VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR VAR BIN_OP VAR NUMBER NUMBER IF VAR VAR VAR BIN_OP VAR NUMBER VAR ASSIGN VAR VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR BIN_OP VAR NUMBER 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 def input(): return sys.stdin.readline().rstrip("\r\n") def maps(): return [int(i) for i in input().split()] maxx = 1 << 60 def naive(s, k): st = "wudiduw" A = 0 cnt = 0 for i in range(0, n, k): f = s[i : i + k] for j in range(k): if f[j] != st[j]: A += 1 cnt += 1 for _ in range(*maps()): n, k = maps() s = input() d = {i: ([0] * 26) for i in range(k)} if n // k % 2: x = n // k // 2 st = x * k end = st + k - 1 tot = n // k else: st = maxx end = maxx tot = n // k ans = 0 i = 0 while i < n: d[i % k][ord(s[i]) - 97] += 1 i += 1 for i in range(k // 2): tt = [(i + j) for i, j in zip(d[i], d[k - i - 1])] ans += 2 * tot - max(tt) if k % 2: ans += tot - max(d[k // 2]) print(ans)
IMPORT FUNC_DEF RETURN FUNC_CALL FUNC_CALL VAR STRING FUNC_DEF RETURN FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP NUMBER NUMBER FUNC_DEF ASSIGN VAR STRING ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR VAR ASSIGN VAR VAR VAR BIN_OP VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR VAR BIN_OP LIST NUMBER NUMBER VAR FUNC_CALL VAR VAR IF BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR VAR BIN_OP VAR VAR BIN_OP FUNC_CALL VAR VAR VAR NUMBER NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR VAR VAR VAR FUNC_CALL VAR VAR VAR VAR BIN_OP BIN_OP VAR VAR NUMBER VAR BIN_OP BIN_OP NUMBER VAR FUNC_CALL VAR VAR IF BIN_OP VAR NUMBER VAR BIN_OP 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.
t = int(input()) for i in range(1, t + 1): n, k = map(int, input().split()) w = [x for x in input()] answer = 0 for i in range((k - 1) // 2 + 1): letters = {} if i == k // 2: length = n // k for j in range(n // k): lettersIdxs = [j * k + i] for idx in lettersIdxs: letter = w[idx] if letter not in letters: letters[letter] = 0 letters[letter] += 1 else: length = n // k * 2 for j in range(n // k): lettersIdxs = [j * k + i, n - j * k - i - 1] for idx in lettersIdxs: letter = w[idx] if letter not in letters: letters[letter] = 0 letters[letter] += 1 cur_max = 0 for o in letters.values(): cur_max = max(cur_max, o) answer += length - cur_max print(answer)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR VAR FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP BIN_OP BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR DICT IF VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR VAR ASSIGN VAR LIST BIN_OP BIN_OP VAR VAR VAR FOR VAR VAR ASSIGN VAR VAR VAR IF VAR VAR ASSIGN VAR VAR NUMBER VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR VAR ASSIGN VAR LIST BIN_OP BIN_OP VAR VAR VAR BIN_OP BIN_OP BIN_OP VAR BIN_OP VAR VAR VAR NUMBER FOR VAR VAR ASSIGN VAR VAR VAR IF VAR VAR ASSIGN VAR VAR NUMBER VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL 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.
for _ in range(int(input())): n, k = map(int, input().split()) s = input() ans = 0 for i in range((k + 1) // 2): d = {} mx = -1 sec = k - i - 1 for j in range(i, n, k): d[s[sec]] = d.get(s[sec], 0) + 1 if j != sec: d[s[j]] = d.get(s[j], 0) + 1 sec += k sm = 0 for p in d: mx = max(mx, d[p]) sm += d[p] ans += sm - mx 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 BIN_OP VAR NUMBER NUMBER ASSIGN VAR DICT ASSIGN VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR VAR VAR BIN_OP FUNC_CALL VAR VAR VAR NUMBER NUMBER IF VAR VAR ASSIGN VAR VAR VAR BIN_OP FUNC_CALL VAR VAR VAR NUMBER NUMBER VAR VAR ASSIGN VAR NUMBER FOR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR 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.
def makeLetterList(s, i): alphabet[ord(s[i]) - 97] += 1 letterReached[i] = True if i + k < n and letterReached[i + k] == False: makeLetterList(s, i + k) if i - k >= 0 and letterReached[i - k] == False: makeLetterList(s, i - k) if n - 1 - i >= 0 and letterReached[n - i - 1] == False: makeLetterList(s, n - i - 1) for _ in range(int(input())): n, k = map(int, input().split()) s = input() letterReached = [False] * n count = 0 for i in range(n): if not letterReached[i]: alphabet = [0] * 26 for j in range(i, n, k): if not letterReached[j]: alphabet[ord(s[j]) - 97] += 1 letterReached[j] = True if not letterReached[n - 1 - j]: alphabet[ord(s[n - 1 - j]) - 97] += 1 letterReached[n - 1 - j] = True count += sum(alphabet) - max(alphabet) print(count)
FUNC_DEF VAR BIN_OP FUNC_CALL VAR VAR VAR NUMBER NUMBER ASSIGN VAR VAR NUMBER IF BIN_OP VAR VAR VAR VAR BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR VAR BIN_OP VAR VAR IF BIN_OP VAR VAR NUMBER VAR BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR VAR BIN_OP VAR VAR IF BIN_OP BIN_OP VAR NUMBER VAR NUMBER VAR BIN_OP BIN_OP VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR NUMBER 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 LIST NUMBER VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR ASSIGN VAR BIN_OP LIST NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR VAR VAR IF VAR VAR VAR BIN_OP FUNC_CALL VAR VAR VAR NUMBER NUMBER ASSIGN VAR VAR NUMBER IF VAR BIN_OP BIN_OP VAR NUMBER VAR VAR BIN_OP FUNC_CALL VAR VAR BIN_OP BIN_OP VAR NUMBER VAR NUMBER NUMBER ASSIGN VAR BIN_OP BIN_OP VAR NUMBER 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.
t = int(input()) sol = [] while t: t -= 1 pas = 0 n, k = tuple(map(int, input().split())) s = input() d = [i for i in range(k)] subu = [set([i]) for i in range(k)] dic = [{} for j in range(k)] i = 0 j = k - 1 poz1 = 0 poz2 = n - 1 while poz1 < poz2: subu[i].add(j) subu[j].add(i) i += 1 j -= 1 if i == k: i = 0 if j == -1: j = k - 1 poz1 += 1 poz2 -= 1 pr = 0 vis = [(True) for i in range(k)] for i in s: if not dic[pr].get(i): dic[pr][i] = 0 dic[pr][i] += 1 pr += 1 if pr == k: pr = 0 h = 0 for i in range(k): if vis[i]: nr = 0 g = {} for j in subu[i]: vis[j] = False nr += 1 for a in dic[j].keys(): if g.get(a): g[a] += dic[j][a] else: g[a] = dic[j][a] mini = 0 for a in g.keys(): mini = max(g[a], mini) h += n // k * nr - mini sol.append(h) for i in sol: print(i)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST WHILE VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR LIST VAR VAR FUNC_CALL VAR VAR ASSIGN VAR DICT VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER WHILE VAR VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR NUMBER VAR NUMBER IF VAR VAR ASSIGN VAR NUMBER IF VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR FOR VAR VAR IF FUNC_CALL VAR VAR VAR ASSIGN VAR VAR VAR NUMBER VAR VAR VAR NUMBER VAR NUMBER IF VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR ASSIGN VAR NUMBER ASSIGN VAR DICT FOR VAR VAR VAR ASSIGN VAR VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR BIN_OP BIN_OP BIN_OP VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR FOR VAR VAR EXPR FUNC_CALL VAR VAR
A bracket sequence is called regular if it is possible to obtain correct arithmetic expression by inserting characters «+» and «1» into this sequence. For example, sequences «(())()», «()» and «(()(()))» are regular, while «)(», «(()» and «(()))(» are not. One day Johnny got bracket sequence. He decided to remove some of the brackets from it in order to obtain a regular bracket sequence. What is the maximum length of a regular bracket sequence which can be obtained? Input Input consists of a single line with non-empty string of «(» and «)» characters. Its length does not exceed 106. Output Output the maximum possible length of a regular bracket sequence. Examples Input (()))( Output 4 Input ((()()) Output 6
s = input() c = 0 r = 0 n = len(s) for x in s: if x == "(": c += 1 elif c == 0: r += 1 else: c -= 1 print(n - (r + c))
ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR FOR VAR VAR IF VAR STRING VAR NUMBER IF VAR NUMBER VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR BIN_OP VAR VAR
A bracket sequence is called regular if it is possible to obtain correct arithmetic expression by inserting characters «+» and «1» into this sequence. For example, sequences «(())()», «()» and «(()(()))» are regular, while «)(», «(()» and «(()))(» are not. One day Johnny got bracket sequence. He decided to remove some of the brackets from it in order to obtain a regular bracket sequence. What is the maximum length of a regular bracket sequence which can be obtained? Input Input consists of a single line with non-empty string of «(» and «)» characters. Its length does not exceed 106. Output Output the maximum possible length of a regular bracket sequence. Examples Input (()))( Output 4 Input ((()()) Output 6
s = input() stack = [] cnt = 0 m = 0 for i in s: if i == "(": stack.append(i) elif i == ")": if len(stack) != 0: stack.pop() cnt += 2 print(cnt)
ASSIGN VAR FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR IF VAR STRING EXPR FUNC_CALL VAR VAR IF VAR STRING IF FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR VAR
A bracket sequence is called regular if it is possible to obtain correct arithmetic expression by inserting characters «+» and «1» into this sequence. For example, sequences «(())()», «()» and «(()(()))» are regular, while «)(», «(()» and «(()))(» are not. One day Johnny got bracket sequence. He decided to remove some of the brackets from it in order to obtain a regular bracket sequence. What is the maximum length of a regular bracket sequence which can be obtained? Input Input consists of a single line with non-empty string of «(» and «)» characters. Its length does not exceed 106. Output Output the maximum possible length of a regular bracket sequence. Examples Input (()))( Output 4 Input ((()()) Output 6
n = input() l = [] c = 0 for i in n: if i == "(": l.append("(") elif len(l) != 0: l.pop() c = c + 1 print(c * 2)
ASSIGN VAR FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR NUMBER FOR VAR VAR IF VAR STRING EXPR FUNC_CALL VAR STRING IF FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR ASSIGN VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER
A bracket sequence is called regular if it is possible to obtain correct arithmetic expression by inserting characters «+» and «1» into this sequence. For example, sequences «(())()», «()» and «(()(()))» are regular, while «)(», «(()» and «(()))(» are not. One day Johnny got bracket sequence. He decided to remove some of the brackets from it in order to obtain a regular bracket sequence. What is the maximum length of a regular bracket sequence which can be obtained? Input Input consists of a single line with non-empty string of «(» and «)» characters. Its length does not exceed 106. Output Output the maximum possible length of a regular bracket sequence. Examples Input (()))( Output 4 Input ((()()) Output 6
import sys b = sys.stdin.readline() bb = len(b) res = 0 for i in range(len(b)): if b[i] == "(": res += 1 elif b[i] == ")": res -= 1 if res < 0: bb -= 1 res = 0 print(bb - res - 1)
IMPORT ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR STRING VAR NUMBER IF VAR VAR STRING VAR NUMBER IF VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER EXPR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR NUMBER
A bracket sequence is called regular if it is possible to obtain correct arithmetic expression by inserting characters «+» and «1» into this sequence. For example, sequences «(())()», «()» and «(()(()))» are regular, while «)(», «(()» and «(()))(» are not. One day Johnny got bracket sequence. He decided to remove some of the brackets from it in order to obtain a regular bracket sequence. What is the maximum length of a regular bracket sequence which can be obtained? Input Input consists of a single line with non-empty string of «(» and «)» characters. Its length does not exceed 106. Output Output the maximum possible length of a regular bracket sequence. Examples Input (()))( Output 4 Input ((()()) Output 6
class Stack: def __init__(self): self.items = [] def isEmpty(self): return self.items == [] def push(self, item): self.items.append(item) def pop(self): return self.items.pop() def peek(self): return self.items[len(self.items) - 1] def size(self): return len(self.items) s = Stack() nb = 0 ch = str(input()) for i in range(len(ch)): if ch[i] in "(": s.push(ch[i]) elif ch[i] in ")": if s.isEmpty(): continue else: s.pop() nb += 2 print(nb)
CLASS_DEF FUNC_DEF ASSIGN VAR LIST FUNC_DEF RETURN VAR LIST FUNC_DEF EXPR FUNC_CALL VAR VAR FUNC_DEF RETURN FUNC_CALL VAR FUNC_DEF RETURN VAR BIN_OP FUNC_CALL VAR VAR NUMBER FUNC_DEF RETURN FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR STRING EXPR FUNC_CALL VAR VAR VAR IF VAR VAR STRING IF FUNC_CALL VAR EXPR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR VAR