description
stringlengths
171
4k
code
stringlengths
94
3.98k
normalized_code
stringlengths
57
4.99k
Let's call left cyclic shift of some string $t_1 t_2 t_3 \dots t_{n - 1} t_n$ as string $t_2 t_3 \dots t_{n - 1} t_n t_1$. Analogically, let's call right cyclic shift of string $t$ as string $t_n t_1 t_2 t_3 \dots t_{n - 1}$. Let's say string $t$ is good if its left cyclic shift is equal to its right cyclic shift. You are given string $s$ which consists of digits 0–9. What is the minimum number of characters you need to erase from $s$ to make it good? -----Input----- The first line contains single integer $t$ ($1 \le t \le 1000$) — the number of test cases. Next $t$ lines contains test cases — one per line. The first and only line of each test case contains string $s$ ($2 \le |s| \le 2 \cdot 10^5$). Each character $s_i$ is digit 0–9. It's guaranteed that the total length of strings doesn't exceed $2 \cdot 10^5$. -----Output----- For each test case, print the minimum number of characters you need to erase from $s$ to make it good. -----Example----- Input 3 95831 100120013 252525252525 Output 3 5 0 -----Note----- In the first test case, you can erase any $3$ characters, for example, the $1$-st, the $3$-rd, and the $4$-th. You'll get string 51 and it is good. In the second test case, we can erase all characters except 0: the remaining string is 0000 and it's good. In the third test case, the given string $s$ is already good.
def solve(s): ans = 0 for i in range(0, 10): c1 = str(i) for j in range(0, 10): c2 = str(j) state = 0 sum = 0 for k in range(len(s)): if state == 0: if c1 == s[k]: state = 1 elif c2 == s[k]: sum += 2 state = 0 if c1 == c2 and state == 1: sum += 1 ans = max(ans, sum) print(len(s) - ans) t = int(input()) for _ in range(t): s = input() solve(s)
FUNC_DEF ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR NUMBER IF VAR VAR VAR ASSIGN VAR NUMBER IF VAR VAR VAR VAR NUMBER ASSIGN VAR NUMBER IF VAR VAR VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR
Let's call left cyclic shift of some string $t_1 t_2 t_3 \dots t_{n - 1} t_n$ as string $t_2 t_3 \dots t_{n - 1} t_n t_1$. Analogically, let's call right cyclic shift of string $t$ as string $t_n t_1 t_2 t_3 \dots t_{n - 1}$. Let's say string $t$ is good if its left cyclic shift is equal to its right cyclic shift. You are given string $s$ which consists of digits 0–9. What is the minimum number of characters you need to erase from $s$ to make it good? -----Input----- The first line contains single integer $t$ ($1 \le t \le 1000$) — the number of test cases. Next $t$ lines contains test cases — one per line. The first and only line of each test case contains string $s$ ($2 \le |s| \le 2 \cdot 10^5$). Each character $s_i$ is digit 0–9. It's guaranteed that the total length of strings doesn't exceed $2 \cdot 10^5$. -----Output----- For each test case, print the minimum number of characters you need to erase from $s$ to make it good. -----Example----- Input 3 95831 100120013 252525252525 Output 3 5 0 -----Note----- In the first test case, you can erase any $3$ characters, for example, the $1$-st, the $3$-rd, and the $4$-th. You'll get string 51 and it is good. In the second test case, we can erase all characters except 0: the remaining string is 0000 and it's good. In the third test case, the given string $s$ is already good.
for _ in range(int(input())): s = input() n = len(s) s = [int(s[i]) for i in range(n)] ans = 2 for a in range(10): for b in range(10): temp = 0 sign = "a" for i in range(n): if sign == "a": if s[i] == a: temp += 1 sign = "b" elif s[i] == b: temp += 1 sign = "a" if a == b: ans = max(ans, temp) else: temp -= temp % 2 ans = max(ans, temp) print(n - ans)
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR STRING FOR VAR FUNC_CALL VAR VAR IF VAR STRING IF VAR VAR VAR VAR NUMBER ASSIGN VAR STRING IF VAR VAR VAR VAR NUMBER ASSIGN VAR STRING IF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR
Let's call left cyclic shift of some string $t_1 t_2 t_3 \dots t_{n - 1} t_n$ as string $t_2 t_3 \dots t_{n - 1} t_n t_1$. Analogically, let's call right cyclic shift of string $t$ as string $t_n t_1 t_2 t_3 \dots t_{n - 1}$. Let's say string $t$ is good if its left cyclic shift is equal to its right cyclic shift. You are given string $s$ which consists of digits 0–9. What is the minimum number of characters you need to erase from $s$ to make it good? -----Input----- The first line contains single integer $t$ ($1 \le t \le 1000$) — the number of test cases. Next $t$ lines contains test cases — one per line. The first and only line of each test case contains string $s$ ($2 \le |s| \le 2 \cdot 10^5$). Each character $s_i$ is digit 0–9. It's guaranteed that the total length of strings doesn't exceed $2 \cdot 10^5$. -----Output----- For each test case, print the minimum number of characters you need to erase from $s$ to make it good. -----Example----- Input 3 95831 100120013 252525252525 Output 3 5 0 -----Note----- In the first test case, you can erase any $3$ characters, for example, the $1$-st, the $3$-rd, and the $4$-th. You'll get string 51 and it is good. In the second test case, we can erase all characters except 0: the remaining string is 0000 and it's good. In the third test case, the given string $s$ is already good.
t = int(input()) for _ in range(t): s = input() d = {} for i in range(len(s)): d[s[i]] = 0 for i in range(len(s)): d[s[i]] += 1 mini = 1000000000.0 maxi = 0 for key in d: maxi = max(maxi, d[key]) o = len(s) mini = min(mini, o - maxi) for i in range(10): for j in range(10): if i != j: p = [] for k in s: if int(k) == i or int(k) == j: p.append(k) l = str("".join(p)) z = 2 * max(l.count(str(i) + str(j)), l.count(str(j) + str(i))) mini = min(mini, o - z) print(mini)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR DICT FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR FOR VAR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER IF VAR VAR ASSIGN VAR LIST FOR VAR VAR IF FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL STRING VAR ASSIGN VAR BIN_OP NUMBER FUNC_CALL VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR
Let's call left cyclic shift of some string $t_1 t_2 t_3 \dots t_{n - 1} t_n$ as string $t_2 t_3 \dots t_{n - 1} t_n t_1$. Analogically, let's call right cyclic shift of string $t$ as string $t_n t_1 t_2 t_3 \dots t_{n - 1}$. Let's say string $t$ is good if its left cyclic shift is equal to its right cyclic shift. You are given string $s$ which consists of digits 0–9. What is the minimum number of characters you need to erase from $s$ to make it good? -----Input----- The first line contains single integer $t$ ($1 \le t \le 1000$) — the number of test cases. Next $t$ lines contains test cases — one per line. The first and only line of each test case contains string $s$ ($2 \le |s| \le 2 \cdot 10^5$). Each character $s_i$ is digit 0–9. It's guaranteed that the total length of strings doesn't exceed $2 \cdot 10^5$. -----Output----- For each test case, print the minimum number of characters you need to erase from $s$ to make it good. -----Example----- Input 3 95831 100120013 252525252525 Output 3 5 0 -----Note----- In the first test case, you can erase any $3$ characters, for example, the $1$-st, the $3$-rd, and the $4$-th. You'll get string 51 and it is good. In the second test case, we can erase all characters except 0: the remaining string is 0000 and it's good. In the third test case, the given string $s$ is already good.
import sys input = sys.stdin.readline T = int(input()) for testcase in range(1, T + 1): s = list(input()) if s[-1] == "\n": s.pop() n = len(s) for i in range(n): s[i] = int(s[i]) d = [0] * 10 for e in s: d[e] += 1 res = n - max(d) for num in range(100): p, q = divmod(num, 10) if p == q: continue flag = True tmp = 0 for j in range(n): if flag and s[j] == p: flag = False elif not flag and s[j] == q: tmp += 2 flag = True res = min(res, n - tmp) print(res)
IMPORT ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR IF VAR NUMBER STRING EXPR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP LIST NUMBER NUMBER FOR VAR VAR VAR VAR NUMBER ASSIGN VAR BIN_OP VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR NUMBER IF VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR ASSIGN VAR NUMBER IF VAR VAR VAR VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR
Let's call left cyclic shift of some string $t_1 t_2 t_3 \dots t_{n - 1} t_n$ as string $t_2 t_3 \dots t_{n - 1} t_n t_1$. Analogically, let's call right cyclic shift of string $t$ as string $t_n t_1 t_2 t_3 \dots t_{n - 1}$. Let's say string $t$ is good if its left cyclic shift is equal to its right cyclic shift. You are given string $s$ which consists of digits 0–9. What is the minimum number of characters you need to erase from $s$ to make it good? -----Input----- The first line contains single integer $t$ ($1 \le t \le 1000$) — the number of test cases. Next $t$ lines contains test cases — one per line. The first and only line of each test case contains string $s$ ($2 \le |s| \le 2 \cdot 10^5$). Each character $s_i$ is digit 0–9. It's guaranteed that the total length of strings doesn't exceed $2 \cdot 10^5$. -----Output----- For each test case, print the minimum number of characters you need to erase from $s$ to make it good. -----Example----- Input 3 95831 100120013 252525252525 Output 3 5 0 -----Note----- In the first test case, you can erase any $3$ characters, for example, the $1$-st, the $3$-rd, and the $4$-th. You'll get string 51 and it is good. In the second test case, we can erase all characters except 0: the remaining string is 0000 and it's good. In the third test case, the given string $s$ is already good.
nos = "0123456789" son = "9876543210" for _ in range(int(input())): s = input().strip() pairs = [] d = {i: (0) for i in nos} for j in s: d[j] += 1 ans = max(d.values()) for i in range(len(nos)): for j in range(i + 1, len(nos)): pairs.append([nos[i], nos[j]]) for i in range(len(son)): for j in range(i + 1, len(son)): pairs.append([son[i], son[j]]) for i in pairs: last, cur = 0, 0 for j in s: if j == i[last]: cur += 1 last = 1 - last if last == 1: cur -= 1 ans = max(ans, cur) print(len(s) - ans)
ASSIGN VAR STRING ASSIGN VAR STRING FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR VAR NUMBER VAR VAR FOR VAR VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR EXPR FUNC_CALL VAR LIST VAR VAR VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR EXPR FUNC_CALL VAR LIST VAR VAR VAR VAR FOR VAR VAR ASSIGN VAR VAR NUMBER NUMBER FOR VAR VAR IF VAR VAR VAR VAR NUMBER ASSIGN VAR BIN_OP NUMBER VAR IF VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR VAR
Let's call left cyclic shift of some string $t_1 t_2 t_3 \dots t_{n - 1} t_n$ as string $t_2 t_3 \dots t_{n - 1} t_n t_1$. Analogically, let's call right cyclic shift of string $t$ as string $t_n t_1 t_2 t_3 \dots t_{n - 1}$. Let's say string $t$ is good if its left cyclic shift is equal to its right cyclic shift. You are given string $s$ which consists of digits 0–9. What is the minimum number of characters you need to erase from $s$ to make it good? -----Input----- The first line contains single integer $t$ ($1 \le t \le 1000$) — the number of test cases. Next $t$ lines contains test cases — one per line. The first and only line of each test case contains string $s$ ($2 \le |s| \le 2 \cdot 10^5$). Each character $s_i$ is digit 0–9. It's guaranteed that the total length of strings doesn't exceed $2 \cdot 10^5$. -----Output----- For each test case, print the minimum number of characters you need to erase from $s$ to make it good. -----Example----- Input 3 95831 100120013 252525252525 Output 3 5 0 -----Note----- In the first test case, you can erase any $3$ characters, for example, the $1$-st, the $3$-rd, and the $4$-th. You'll get string 51 and it is good. In the second test case, we can erase all characters except 0: the remaining string is 0000 and it's good. In the third test case, the given string $s$ is already good.
t = int(input()) for _ in range(t): l = input() ones = [(0) for i in range(10)] val = 0 for char in l: ones[ord(char) - ord("0")] += 1 val = max(val, ones[ord(char) - ord("0")]) for i in range(10): if ones[i] > val // 2: for j in range(10): if i != j and ones[j] > val // 2: a = False b = 0 for k in range(len(l)): if a == False and ord(l[k]) - ord("0") == i: a = True if a and ord(l[k]) - ord("0") == j: a = False b += 2 val = max(val, b) print(len(l) - val)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR VAR BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR STRING NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR STRING FOR VAR FUNC_CALL VAR NUMBER IF VAR VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER IF VAR VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR NUMBER BIN_OP FUNC_CALL VAR VAR VAR FUNC_CALL VAR STRING VAR ASSIGN VAR NUMBER IF VAR BIN_OP FUNC_CALL VAR VAR VAR FUNC_CALL VAR STRING VAR ASSIGN VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR VAR
Let's call left cyclic shift of some string $t_1 t_2 t_3 \dots t_{n - 1} t_n$ as string $t_2 t_3 \dots t_{n - 1} t_n t_1$. Analogically, let's call right cyclic shift of string $t$ as string $t_n t_1 t_2 t_3 \dots t_{n - 1}$. Let's say string $t$ is good if its left cyclic shift is equal to its right cyclic shift. You are given string $s$ which consists of digits 0–9. What is the minimum number of characters you need to erase from $s$ to make it good? -----Input----- The first line contains single integer $t$ ($1 \le t \le 1000$) — the number of test cases. Next $t$ lines contains test cases — one per line. The first and only line of each test case contains string $s$ ($2 \le |s| \le 2 \cdot 10^5$). Each character $s_i$ is digit 0–9. It's guaranteed that the total length of strings doesn't exceed $2 \cdot 10^5$. -----Output----- For each test case, print the minimum number of characters you need to erase from $s$ to make it good. -----Example----- Input 3 95831 100120013 252525252525 Output 3 5 0 -----Note----- In the first test case, you can erase any $3$ characters, for example, the $1$-st, the $3$-rd, and the $4$-th. You'll get string 51 and it is good. In the second test case, we can erase all characters except 0: the remaining string is 0000 and it's good. In the third test case, the given string $s$ is already good.
for _ in range(int(input())): s = list(input()) n = len(s) ss = "0123456789" d = [] dd = [] for i in ss: c = 0 for j in s: if i == j: c += 1 dd.append(c) for i in ss: for j in ss: c = 1 cou = 0 for k in range(n): if c != 0 and s[k] == i: c ^= 1 elif c == 0 and s[k] == j: c ^= 1 cou += 1 d.append(cou) ans = max(max(dd), 2 * max(d)) print(n - ans)
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR STRING ASSIGN VAR LIST ASSIGN VAR LIST FOR VAR VAR ASSIGN VAR NUMBER FOR VAR VAR IF VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR FOR VAR VAR FOR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR NUMBER VAR VAR VAR VAR NUMBER IF VAR NUMBER VAR VAR VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR BIN_OP NUMBER FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR
Let's call left cyclic shift of some string $t_1 t_2 t_3 \dots t_{n - 1} t_n$ as string $t_2 t_3 \dots t_{n - 1} t_n t_1$. Analogically, let's call right cyclic shift of string $t$ as string $t_n t_1 t_2 t_3 \dots t_{n - 1}$. Let's say string $t$ is good if its left cyclic shift is equal to its right cyclic shift. You are given string $s$ which consists of digits 0–9. What is the minimum number of characters you need to erase from $s$ to make it good? -----Input----- The first line contains single integer $t$ ($1 \le t \le 1000$) — the number of test cases. Next $t$ lines contains test cases — one per line. The first and only line of each test case contains string $s$ ($2 \le |s| \le 2 \cdot 10^5$). Each character $s_i$ is digit 0–9. It's guaranteed that the total length of strings doesn't exceed $2 \cdot 10^5$. -----Output----- For each test case, print the minimum number of characters you need to erase from $s$ to make it good. -----Example----- Input 3 95831 100120013 252525252525 Output 3 5 0 -----Note----- In the first test case, you can erase any $3$ characters, for example, the $1$-st, the $3$-rd, and the $4$-th. You'll get string 51 and it is good. In the second test case, we can erase all characters except 0: the remaining string is 0000 and it's good. In the third test case, the given string $s$ is already good.
for _ in range(int(input())): s = input() n = len(s) a = [(0) for i in range(10)] m = 0 for i in s: a[int(i)] += 1 m = max(m, a[int(i)]) maxx = 0 for i in range(10): if a[i] > 0: for j in range(10): if a[j] > 0: if i == j: continue sl = str(i) + str(j) ind = 0 p = 0 le = 0 while p < n: if s[p] == sl[ind]: le += 1 ind = (ind + 1) % 2 while s[p] != sl[ind]: p += 1 if p == n: break else: p += 1 if le % 2 != 0: le -= 1 maxx = max(maxx, le) print(min(n - m, n - maxx))
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER IF VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER IF VAR VAR NUMBER IF VAR VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR IF VAR VAR VAR VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR NUMBER NUMBER WHILE VAR VAR VAR VAR VAR NUMBER IF VAR VAR VAR NUMBER IF BIN_OP VAR NUMBER NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR VAR BIN_OP VAR VAR
Let's call left cyclic shift of some string $t_1 t_2 t_3 \dots t_{n - 1} t_n$ as string $t_2 t_3 \dots t_{n - 1} t_n t_1$. Analogically, let's call right cyclic shift of string $t$ as string $t_n t_1 t_2 t_3 \dots t_{n - 1}$. Let's say string $t$ is good if its left cyclic shift is equal to its right cyclic shift. You are given string $s$ which consists of digits 0–9. What is the minimum number of characters you need to erase from $s$ to make it good? -----Input----- The first line contains single integer $t$ ($1 \le t \le 1000$) — the number of test cases. Next $t$ lines contains test cases — one per line. The first and only line of each test case contains string $s$ ($2 \le |s| \le 2 \cdot 10^5$). Each character $s_i$ is digit 0–9. It's guaranteed that the total length of strings doesn't exceed $2 \cdot 10^5$. -----Output----- For each test case, print the minimum number of characters you need to erase from $s$ to make it good. -----Example----- Input 3 95831 100120013 252525252525 Output 3 5 0 -----Note----- In the first test case, you can erase any $3$ characters, for example, the $1$-st, the $3$-rd, and the $4$-th. You'll get string 51 and it is good. In the second test case, we can erase all characters except 0: the remaining string is 0000 and it's good. In the third test case, the given string $s$ is already good.
t = int(input()) for i in range(t): s = input() best = max(len(s) - 2, 0) if best == 0: print(0) continue counter = [0] * 10 for char in s: counter[int(char)] += 1 onedigitbest = len(s) - max(counter) if onedigitbest < best: best = onedigitbest for j in range(10): if not counter[j] or 2 * counter[j] <= len(s) - best: continue for k in range(j + 1, 10): if not counter[k] or 2 * counter[k] <= len(s) - best: continue a = s.index(str(j)) b = s.index(str(k)) if a < b: start = str(j) else: start = str(k) total = 0 for char in s: if char == start: total += 1 if start == str(j): start = str(k) else: start = str(j) if total % 2 == 1: total -= 1 if len(s) - total < best: best = len(s) - total print(best)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER NUMBER FOR VAR VAR VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR VAR IF VAR VAR ASSIGN VAR VAR FOR VAR FUNC_CALL VAR NUMBER IF VAR VAR BIN_OP NUMBER VAR VAR BIN_OP FUNC_CALL VAR VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER IF VAR VAR BIN_OP NUMBER VAR VAR BIN_OP FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR VAR IF VAR VAR VAR NUMBER IF VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR IF BIN_OP VAR NUMBER NUMBER VAR NUMBER IF BIN_OP FUNC_CALL VAR VAR VAR VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR
Let's call left cyclic shift of some string $t_1 t_2 t_3 \dots t_{n - 1} t_n$ as string $t_2 t_3 \dots t_{n - 1} t_n t_1$. Analogically, let's call right cyclic shift of string $t$ as string $t_n t_1 t_2 t_3 \dots t_{n - 1}$. Let's say string $t$ is good if its left cyclic shift is equal to its right cyclic shift. You are given string $s$ which consists of digits 0–9. What is the minimum number of characters you need to erase from $s$ to make it good? -----Input----- The first line contains single integer $t$ ($1 \le t \le 1000$) — the number of test cases. Next $t$ lines contains test cases — one per line. The first and only line of each test case contains string $s$ ($2 \le |s| \le 2 \cdot 10^5$). Each character $s_i$ is digit 0–9. It's guaranteed that the total length of strings doesn't exceed $2 \cdot 10^5$. -----Output----- For each test case, print the minimum number of characters you need to erase from $s$ to make it good. -----Example----- Input 3 95831 100120013 252525252525 Output 3 5 0 -----Note----- In the first test case, you can erase any $3$ characters, for example, the $1$-st, the $3$-rd, and the $4$-th. You'll get string 51 and it is good. In the second test case, we can erase all characters except 0: the remaining string is 0000 and it's good. In the third test case, the given string $s$ is already good.
for _ in range(int(input())): s = input() n = len(s) ar = [0] * 10 mx = 0 if n <= 2: print(0) continue for i in range(10): for j in range(10): f = True cnt = 0 for k in range(n): if f: if int(s[k]) == i: f = not f cnt += 1 elif int(s[k]) == j: f = not f cnt += 1 if i != j and not f: cnt -= 1 mx = max(mx, cnt) print(n - mx)
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER NUMBER ASSIGN VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR IF FUNC_CALL VAR VAR VAR VAR ASSIGN VAR VAR VAR NUMBER IF FUNC_CALL VAR VAR VAR VAR ASSIGN VAR VAR VAR NUMBER IF VAR VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR
Let's call left cyclic shift of some string $t_1 t_2 t_3 \dots t_{n - 1} t_n$ as string $t_2 t_3 \dots t_{n - 1} t_n t_1$. Analogically, let's call right cyclic shift of string $t$ as string $t_n t_1 t_2 t_3 \dots t_{n - 1}$. Let's say string $t$ is good if its left cyclic shift is equal to its right cyclic shift. You are given string $s$ which consists of digits 0–9. What is the minimum number of characters you need to erase from $s$ to make it good? -----Input----- The first line contains single integer $t$ ($1 \le t \le 1000$) — the number of test cases. Next $t$ lines contains test cases — one per line. The first and only line of each test case contains string $s$ ($2 \le |s| \le 2 \cdot 10^5$). Each character $s_i$ is digit 0–9. It's guaranteed that the total length of strings doesn't exceed $2 \cdot 10^5$. -----Output----- For each test case, print the minimum number of characters you need to erase from $s$ to make it good. -----Example----- Input 3 95831 100120013 252525252525 Output 3 5 0 -----Note----- In the first test case, you can erase any $3$ characters, for example, the $1$-st, the $3$-rd, and the $4$-th. You'll get string 51 and it is good. In the second test case, we can erase all characters except 0: the remaining string is 0000 and it's good. In the third test case, the given string $s$ is already good.
t = int(input()) for _ in range(t): s = input() N = len(s) ans = max(0, N - 2) d = dict() d2 = dict() d3 = dict() for x in s: if x in d: d[x] += 1 else: d[x] = 1 for x in d: ans = min(ans, N - d[x]) for i in range(0, 100): k = str(i) if len(k) == 1: k = "0" + k a = k[0] b = k[1] flag = False count = 0 for x in s: if flag: if b == x: flag = False count += 1 else: continue elif a == x: flag = True else: continue if N - count * 2 < ans: ans = N - count * 2 print(ans)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FOR VAR VAR IF VAR VAR VAR VAR NUMBER ASSIGN VAR VAR NUMBER FOR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR VAR FOR VAR FUNC_CALL VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR NUMBER ASSIGN VAR BIN_OP STRING VAR ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR IF VAR IF VAR VAR ASSIGN VAR NUMBER VAR NUMBER IF VAR VAR ASSIGN VAR NUMBER IF BIN_OP VAR BIN_OP VAR NUMBER VAR ASSIGN VAR BIN_OP VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR
Let's call left cyclic shift of some string $t_1 t_2 t_3 \dots t_{n - 1} t_n$ as string $t_2 t_3 \dots t_{n - 1} t_n t_1$. Analogically, let's call right cyclic shift of string $t$ as string $t_n t_1 t_2 t_3 \dots t_{n - 1}$. Let's say string $t$ is good if its left cyclic shift is equal to its right cyclic shift. You are given string $s$ which consists of digits 0–9. What is the minimum number of characters you need to erase from $s$ to make it good? -----Input----- The first line contains single integer $t$ ($1 \le t \le 1000$) — the number of test cases. Next $t$ lines contains test cases — one per line. The first and only line of each test case contains string $s$ ($2 \le |s| \le 2 \cdot 10^5$). Each character $s_i$ is digit 0–9. It's guaranteed that the total length of strings doesn't exceed $2 \cdot 10^5$. -----Output----- For each test case, print the minimum number of characters you need to erase from $s$ to make it good. -----Example----- Input 3 95831 100120013 252525252525 Output 3 5 0 -----Note----- In the first test case, you can erase any $3$ characters, for example, the $1$-st, the $3$-rd, and the $4$-th. You'll get string 51 and it is good. In the second test case, we can erase all characters except 0: the remaining string is 0000 and it's good. In the third test case, the given string $s$ is already good.
t = int(input()) for x in range(t): testcase = str(input()) K = len(testcase) best = K - 2 for i in range(10): for j in range(10): digits = i, j nextch = i count = 0 for ch in testcase: if int(ch) != nextch: count += 1 elif nextch == i: nextch = j elif nextch == j: nextch = i if nextch == j and i != j: count += 1 best = min(best, count) print(best)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR NUMBER FOR VAR VAR IF FUNC_CALL VAR VAR VAR VAR NUMBER IF VAR VAR ASSIGN VAR VAR IF VAR VAR ASSIGN VAR VAR IF VAR VAR VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR
Let's call left cyclic shift of some string $t_1 t_2 t_3 \dots t_{n - 1} t_n$ as string $t_2 t_3 \dots t_{n - 1} t_n t_1$. Analogically, let's call right cyclic shift of string $t$ as string $t_n t_1 t_2 t_3 \dots t_{n - 1}$. Let's say string $t$ is good if its left cyclic shift is equal to its right cyclic shift. You are given string $s$ which consists of digits 0–9. What is the minimum number of characters you need to erase from $s$ to make it good? -----Input----- The first line contains single integer $t$ ($1 \le t \le 1000$) — the number of test cases. Next $t$ lines contains test cases — one per line. The first and only line of each test case contains string $s$ ($2 \le |s| \le 2 \cdot 10^5$). Each character $s_i$ is digit 0–9. It's guaranteed that the total length of strings doesn't exceed $2 \cdot 10^5$. -----Output----- For each test case, print the minimum number of characters you need to erase from $s$ to make it good. -----Example----- Input 3 95831 100120013 252525252525 Output 3 5 0 -----Note----- In the first test case, you can erase any $3$ characters, for example, the $1$-st, the $3$-rd, and the $4$-th. You'll get string 51 and it is good. In the second test case, we can erase all characters except 0: the remaining string is 0000 and it's good. In the third test case, the given string $s$ is already good.
def count_maximum_length(string: str, i: str, j: str) -> int: last = None collection = i, j result = 0 for char in string: if last is None: if char in collection: first = char last = collection.index(char) result += 1 continue if char == collection[1 - last]: last = 1 - last result += 1 if first == collection[last] and i != j: result -= 1 return result def solve() -> int: string = input() characters = tuple(set(string)) result = [] for i in range(len(characters)): for j in range(i, len(characters)): mlen = count_maximum_length(string, characters[i], characters[j]) result.append(mlen) return len(string) - max(result) print("\n".join(map(str, (solve() for _ in range(int(input()))))))
FUNC_DEF VAR VAR VAR ASSIGN VAR NONE ASSIGN VAR VAR VAR ASSIGN VAR NUMBER FOR VAR VAR IF VAR NONE IF VAR VAR ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR NUMBER IF VAR VAR BIN_OP NUMBER VAR ASSIGN VAR BIN_OP NUMBER VAR VAR NUMBER IF VAR VAR VAR VAR VAR VAR NUMBER RETURN VAR VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR RETURN BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL STRING FUNC_CALL VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR
Let's call left cyclic shift of some string $t_1 t_2 t_3 \dots t_{n - 1} t_n$ as string $t_2 t_3 \dots t_{n - 1} t_n t_1$. Analogically, let's call right cyclic shift of string $t$ as string $t_n t_1 t_2 t_3 \dots t_{n - 1}$. Let's say string $t$ is good if its left cyclic shift is equal to its right cyclic shift. You are given string $s$ which consists of digits 0–9. What is the minimum number of characters you need to erase from $s$ to make it good? -----Input----- The first line contains single integer $t$ ($1 \le t \le 1000$) — the number of test cases. Next $t$ lines contains test cases — one per line. The first and only line of each test case contains string $s$ ($2 \le |s| \le 2 \cdot 10^5$). Each character $s_i$ is digit 0–9. It's guaranteed that the total length of strings doesn't exceed $2 \cdot 10^5$. -----Output----- For each test case, print the minimum number of characters you need to erase from $s$ to make it good. -----Example----- Input 3 95831 100120013 252525252525 Output 3 5 0 -----Note----- In the first test case, you can erase any $3$ characters, for example, the $1$-st, the $3$-rd, and the $4$-th. You'll get string 51 and it is good. In the second test case, we can erase all characters except 0: the remaining string is 0000 and it's good. In the third test case, the given string $s$ is already good.
def fun(o, p): c = c2 = c1 = c0 = 0 for i in range(n): if s[i] == p: c1 = 1 if c1 == 1: if s[i] == o: c += 1 c1 = 0 for i in range(n): if s[i] == o: c2 = 1 if c2 == 1: if s[i] == p: c0 += 1 c2 = 0 return max(c, c0) for _ in range(int(input())): s = input() l = [] for i in range(10): l.append([i, 0]) for i in s: l[int(i)][1] += 1 l.sort(key=lambda x: x[1], reverse=True) k = l[1][1] n = len(s) su = 0 for i in range(10): for j in range(i + 1, 10): su = max(su, fun(str(l[i][0]), str(l[j][0]))) if l[1][1] == 0: print(0) else: print(min(n - 2 * su, n - l[0][1]))
FUNC_DEF ASSIGN VAR VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR ASSIGN VAR NUMBER IF VAR NUMBER IF VAR VAR VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR ASSIGN VAR NUMBER IF VAR NUMBER IF VAR VAR VAR VAR NUMBER ASSIGN VAR NUMBER RETURN FUNC_CALL VAR VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR LIST VAR NUMBER FOR VAR VAR VAR FUNC_CALL VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR NUMBER NUMBER ASSIGN VAR VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR NUMBER FUNC_CALL VAR VAR VAR NUMBER IF VAR NUMBER NUMBER NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR BIN_OP NUMBER VAR BIN_OP VAR VAR NUMBER NUMBER
Let's call left cyclic shift of some string $t_1 t_2 t_3 \dots t_{n - 1} t_n$ as string $t_2 t_3 \dots t_{n - 1} t_n t_1$. Analogically, let's call right cyclic shift of string $t$ as string $t_n t_1 t_2 t_3 \dots t_{n - 1}$. Let's say string $t$ is good if its left cyclic shift is equal to its right cyclic shift. You are given string $s$ which consists of digits 0–9. What is the minimum number of characters you need to erase from $s$ to make it good? -----Input----- The first line contains single integer $t$ ($1 \le t \le 1000$) — the number of test cases. Next $t$ lines contains test cases — one per line. The first and only line of each test case contains string $s$ ($2 \le |s| \le 2 \cdot 10^5$). Each character $s_i$ is digit 0–9. It's guaranteed that the total length of strings doesn't exceed $2 \cdot 10^5$. -----Output----- For each test case, print the minimum number of characters you need to erase from $s$ to make it good. -----Example----- Input 3 95831 100120013 252525252525 Output 3 5 0 -----Note----- In the first test case, you can erase any $3$ characters, for example, the $1$-st, the $3$-rd, and the $4$-th. You'll get string 51 and it is good. In the second test case, we can erase all characters except 0: the remaining string is 0000 and it's good. In the third test case, the given string $s$ is already good.
def getmax(s): reps = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0] matrix = ( [ 0, [-1, 0], [-1, 0], [-1, 0], [-1, 0], [-1, 0], [-1, 0], [-1, 0], [-1, 0], [-1, 0], ], [ [-1, 0], 0, [-1, 0], [-1, 0], [-1, 0], [-1, 0], [-1, 0], [-1, 0], [-1, 0], [-1, 0], ], [ [-1, 0], [-1, 0], 0, [-1, 0], [-1, 0], [-1, 0], [-1, 0], [-1, 0], [-1, 0], [-1, 0], ], [ [-1, 0], [-1, 0], [-1, 0], 0, [-1, 0], [-1, 0], [-1, 0], [-1, 0], [-1, 0], [-1, 0], ], [ [-1, 0], [-1, 0], [-1, 0], [-1, 0], 0, [-1, 0], [-1, 0], [-1, 0], [-1, 0], [-1, 0], ], [ [-1, 0], [-1, 0], [-1, 0], [-1, 0], [-1, 0], 0, [-1, 0], [-1, 0], [-1, 0], [-1, 0], ], [ [-1, 0], [-1, 0], [-1, 0], [-1, 0], [-1, 0], [-1, 0], 0, [-1, 0], [-1, 0], [-1, 0], ], [ [-1, 0], [-1, 0], [-1, 0], [-1, 0], [-1, 0], [-1, 0], [-1, 0], 0, [-1, 0], [-1, 0], ], [ [-1, 0], [-1, 0], [-1, 0], [-1, 0], [-1, 0], [-1, 0], [-1, 0], [-1, 0], 0, [-1, 0], ], [ [-1, 0], [-1, 0], [-1, 0], [-1, 0], [-1, 0], [-1, 0], [-1, 0], [-1, 0], [-1, 0], 0, ], ) maxpair = 0 for i in range(len(s)): x = int(s[i]) reps[x] += 1 for y in range(10): if x != y: current = matrix[y][x][0] if current == y: matrix[y][x][0] = x matrix[y][x][1] += 1 if matrix[y][x][1] > maxpair: maxpair = matrix[y][x][1] current = matrix[x][y][0] if current != x: matrix[x][y][0] = x if matrix[x][y][1] > maxpair: maxpair = matrix[x][y][1] maxrep = max(reps) maxpair = maxpair * 2 return maxpair if maxpair > maxrep else maxrep n = int(input()) for t in range(n): s = input() print(len(s) - getmax(s))
FUNC_DEF ASSIGN VAR LIST NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER ASSIGN VAR LIST NUMBER LIST NUMBER NUMBER LIST NUMBER NUMBER LIST NUMBER NUMBER LIST NUMBER NUMBER LIST NUMBER NUMBER LIST NUMBER NUMBER LIST NUMBER NUMBER LIST NUMBER NUMBER LIST NUMBER NUMBER LIST LIST NUMBER NUMBER NUMBER LIST NUMBER NUMBER LIST NUMBER NUMBER LIST NUMBER NUMBER LIST NUMBER NUMBER LIST NUMBER NUMBER LIST NUMBER NUMBER LIST NUMBER NUMBER LIST NUMBER NUMBER LIST LIST NUMBER NUMBER LIST NUMBER NUMBER NUMBER LIST NUMBER NUMBER LIST NUMBER NUMBER LIST NUMBER NUMBER LIST NUMBER NUMBER LIST NUMBER NUMBER LIST NUMBER NUMBER LIST NUMBER NUMBER LIST LIST NUMBER NUMBER LIST NUMBER NUMBER LIST NUMBER NUMBER NUMBER LIST NUMBER NUMBER LIST NUMBER NUMBER LIST NUMBER NUMBER LIST NUMBER NUMBER LIST NUMBER NUMBER LIST NUMBER NUMBER LIST LIST NUMBER NUMBER LIST NUMBER NUMBER LIST NUMBER NUMBER LIST NUMBER NUMBER NUMBER LIST NUMBER NUMBER LIST NUMBER NUMBER LIST NUMBER NUMBER LIST NUMBER NUMBER LIST NUMBER NUMBER LIST LIST NUMBER NUMBER LIST NUMBER NUMBER LIST NUMBER NUMBER LIST NUMBER NUMBER LIST NUMBER NUMBER NUMBER LIST NUMBER NUMBER LIST NUMBER NUMBER LIST NUMBER NUMBER LIST NUMBER NUMBER LIST LIST NUMBER NUMBER LIST NUMBER NUMBER LIST NUMBER NUMBER LIST NUMBER NUMBER LIST NUMBER NUMBER LIST NUMBER NUMBER NUMBER LIST NUMBER NUMBER LIST NUMBER NUMBER LIST NUMBER NUMBER LIST LIST NUMBER NUMBER LIST NUMBER NUMBER LIST NUMBER NUMBER LIST NUMBER NUMBER LIST NUMBER NUMBER LIST NUMBER NUMBER LIST NUMBER NUMBER NUMBER LIST NUMBER NUMBER LIST NUMBER NUMBER LIST LIST NUMBER NUMBER LIST NUMBER NUMBER LIST NUMBER NUMBER LIST NUMBER NUMBER LIST NUMBER NUMBER LIST NUMBER NUMBER LIST NUMBER NUMBER LIST NUMBER NUMBER NUMBER LIST NUMBER NUMBER LIST LIST NUMBER NUMBER LIST NUMBER NUMBER LIST NUMBER NUMBER LIST NUMBER NUMBER LIST NUMBER NUMBER LIST NUMBER NUMBER LIST NUMBER NUMBER LIST NUMBER NUMBER LIST NUMBER NUMBER NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER IF VAR VAR ASSIGN VAR VAR VAR VAR NUMBER IF VAR VAR ASSIGN VAR VAR VAR NUMBER VAR VAR VAR VAR NUMBER NUMBER IF VAR VAR VAR NUMBER VAR ASSIGN VAR VAR VAR VAR NUMBER ASSIGN VAR VAR VAR VAR NUMBER IF VAR VAR ASSIGN VAR VAR VAR NUMBER VAR IF VAR VAR VAR NUMBER VAR ASSIGN VAR VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR NUMBER RETURN VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR EXPR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR VAR
Let's call left cyclic shift of some string $t_1 t_2 t_3 \dots t_{n - 1} t_n$ as string $t_2 t_3 \dots t_{n - 1} t_n t_1$. Analogically, let's call right cyclic shift of string $t$ as string $t_n t_1 t_2 t_3 \dots t_{n - 1}$. Let's say string $t$ is good if its left cyclic shift is equal to its right cyclic shift. You are given string $s$ which consists of digits 0–9. What is the minimum number of characters you need to erase from $s$ to make it good? -----Input----- The first line contains single integer $t$ ($1 \le t \le 1000$) — the number of test cases. Next $t$ lines contains test cases — one per line. The first and only line of each test case contains string $s$ ($2 \le |s| \le 2 \cdot 10^5$). Each character $s_i$ is digit 0–9. It's guaranteed that the total length of strings doesn't exceed $2 \cdot 10^5$. -----Output----- For each test case, print the minimum number of characters you need to erase from $s$ to make it good. -----Example----- Input 3 95831 100120013 252525252525 Output 3 5 0 -----Note----- In the first test case, you can erase any $3$ characters, for example, the $1$-st, the $3$-rd, and the $4$-th. You'll get string 51 and it is good. In the second test case, we can erase all characters except 0: the remaining string is 0000 and it's good. In the third test case, the given string $s$ is already good.
def do(s, x, y): cnt = 0 for c in s: if int(c) == x: cnt += 1 x, y = y, x if x != y and cnt % 2 == 1: cnt -= 1 return cnt def solve(): s = input() res = 0 for i in range(10): for j in range(10): res = max(res, do(s, i, j)) print(len(s) - res) tc = int(input()) for _ in range(tc): solve()
FUNC_DEF ASSIGN VAR NUMBER FOR VAR VAR IF FUNC_CALL VAR VAR VAR VAR NUMBER ASSIGN VAR VAR VAR VAR IF VAR VAR BIN_OP VAR NUMBER NUMBER VAR NUMBER RETURN VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR
Let's call left cyclic shift of some string $t_1 t_2 t_3 \dots t_{n - 1} t_n$ as string $t_2 t_3 \dots t_{n - 1} t_n t_1$. Analogically, let's call right cyclic shift of string $t$ as string $t_n t_1 t_2 t_3 \dots t_{n - 1}$. Let's say string $t$ is good if its left cyclic shift is equal to its right cyclic shift. You are given string $s$ which consists of digits 0–9. What is the minimum number of characters you need to erase from $s$ to make it good? -----Input----- The first line contains single integer $t$ ($1 \le t \le 1000$) — the number of test cases. Next $t$ lines contains test cases — one per line. The first and only line of each test case contains string $s$ ($2 \le |s| \le 2 \cdot 10^5$). Each character $s_i$ is digit 0–9. It's guaranteed that the total length of strings doesn't exceed $2 \cdot 10^5$. -----Output----- For each test case, print the minimum number of characters you need to erase from $s$ to make it good. -----Example----- Input 3 95831 100120013 252525252525 Output 3 5 0 -----Note----- In the first test case, you can erase any $3$ characters, for example, the $1$-st, the $3$-rd, and the $4$-th. You'll get string 51 and it is good. In the second test case, we can erase all characters except 0: the remaining string is 0000 and it's good. In the third test case, the given string $s$ is already good.
import sys t = sys.stdin.readline() while t: t = int(t) s = input() n = len(s) ans = n for i in range(100): if i < 10: a, b = "0", str(i) else: b = str(i % 10) a = str(i // 10) j = 0 flag = True gd_str = 0 while j < n: if flag: while j < n and s[j] != a: j += 1 flag = False else: while j < n and s[j] != b: j += 1 continue if j < n and s[j] == b: gd_str += 2 flag = True j += 1 ans = min(ans, n - gd_str) hm = {} for i in range(n): try: hm[s[i]] += 1 except: hm[s[i]] = 1 print(min(n - max(hm.values()), ans)) t -= 1
IMPORT ASSIGN VAR FUNC_CALL VAR WHILE VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FOR VAR FUNC_CALL VAR NUMBER IF VAR NUMBER ASSIGN VAR VAR STRING FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR IF VAR WHILE VAR VAR VAR VAR VAR VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR VAR VAR VAR VAR NUMBER IF VAR VAR VAR VAR VAR VAR NUMBER ASSIGN VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR ASSIGN VAR DICT FOR VAR FUNC_CALL VAR VAR VAR VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR NUMBER
Let's call left cyclic shift of some string $t_1 t_2 t_3 \dots t_{n - 1} t_n$ as string $t_2 t_3 \dots t_{n - 1} t_n t_1$. Analogically, let's call right cyclic shift of string $t$ as string $t_n t_1 t_2 t_3 \dots t_{n - 1}$. Let's say string $t$ is good if its left cyclic shift is equal to its right cyclic shift. You are given string $s$ which consists of digits 0–9. What is the minimum number of characters you need to erase from $s$ to make it good? -----Input----- The first line contains single integer $t$ ($1 \le t \le 1000$) — the number of test cases. Next $t$ lines contains test cases — one per line. The first and only line of each test case contains string $s$ ($2 \le |s| \le 2 \cdot 10^5$). Each character $s_i$ is digit 0–9. It's guaranteed that the total length of strings doesn't exceed $2 \cdot 10^5$. -----Output----- For each test case, print the minimum number of characters you need to erase from $s$ to make it good. -----Example----- Input 3 95831 100120013 252525252525 Output 3 5 0 -----Note----- In the first test case, you can erase any $3$ characters, for example, the $1$-st, the $3$-rd, and the $4$-th. You'll get string 51 and it is good. In the second test case, we can erase all characters except 0: the remaining string is 0000 and it's good. In the third test case, the given string $s$ is already good.
t = int(input()) for i in range(t): s = str(input()) n = len(s) matrix = [([0] * 10) for j in range(10)] for j in range(10): for k in range(10): if j != k: str1 = str(j) + str(k) a = 0 x = 0 for l in range(n): if a % 2 == 0: if s[l] == str1[0]: x += 1 a += 1 elif s[l] == str1[1]: x += 1 a += 1 if x % 2 == 1: x -= 1 matrix[j][k] = x lis = [0] * 10 for j in range(n): lis[int(s[j])] += 1 ans = n - max(lis) for j in range(10): temp = max(matrix[j]) ans = min(ans, n - temp) print(ans)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER NUMBER VAR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER IF VAR VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF BIN_OP VAR NUMBER NUMBER IF VAR VAR VAR NUMBER VAR NUMBER VAR NUMBER IF VAR VAR VAR NUMBER VAR NUMBER VAR NUMBER IF BIN_OP VAR NUMBER NUMBER VAR NUMBER ASSIGN VAR VAR VAR VAR ASSIGN VAR BIN_OP LIST NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR BIN_OP VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR
Let's call left cyclic shift of some string $t_1 t_2 t_3 \dots t_{n - 1} t_n$ as string $t_2 t_3 \dots t_{n - 1} t_n t_1$. Analogically, let's call right cyclic shift of string $t$ as string $t_n t_1 t_2 t_3 \dots t_{n - 1}$. Let's say string $t$ is good if its left cyclic shift is equal to its right cyclic shift. You are given string $s$ which consists of digits 0–9. What is the minimum number of characters you need to erase from $s$ to make it good? -----Input----- The first line contains single integer $t$ ($1 \le t \le 1000$) — the number of test cases. Next $t$ lines contains test cases — one per line. The first and only line of each test case contains string $s$ ($2 \le |s| \le 2 \cdot 10^5$). Each character $s_i$ is digit 0–9. It's guaranteed that the total length of strings doesn't exceed $2 \cdot 10^5$. -----Output----- For each test case, print the minimum number of characters you need to erase from $s$ to make it good. -----Example----- Input 3 95831 100120013 252525252525 Output 3 5 0 -----Note----- In the first test case, you can erase any $3$ characters, for example, the $1$-st, the $3$-rd, and the $4$-th. You'll get string 51 and it is good. In the second test case, we can erase all characters except 0: the remaining string is 0000 and it's good. In the third test case, the given string $s$ is already good.
def get_a(even, odd, a): flag = 0 b = [] for i in range(0, len(a)): if flag == 0 and a[i] == even: b.append(even) flag = 1 elif flag == 1 and a[i] == odd: b.append(odd) flag = 0 return b def process(n, a): a = list(a) m = 0 for i in range(0, 10): for j in range(0, 10): a1 = get_a(str(i), str(j), a) if len(a1) % 2 == 0 or i == j: m = max(m, len(a1)) return len(a) - m t = int(input()) for i in range(0, t): a = input() print(process(len(a), a))
FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR IF VAR NUMBER VAR VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER IF VAR NUMBER VAR VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER RETURN VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR IF BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR RETURN BIN_OP FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR
Let's call left cyclic shift of some string $t_1 t_2 t_3 \dots t_{n - 1} t_n$ as string $t_2 t_3 \dots t_{n - 1} t_n t_1$. Analogically, let's call right cyclic shift of string $t$ as string $t_n t_1 t_2 t_3 \dots t_{n - 1}$. Let's say string $t$ is good if its left cyclic shift is equal to its right cyclic shift. You are given string $s$ which consists of digits 0–9. What is the minimum number of characters you need to erase from $s$ to make it good? -----Input----- The first line contains single integer $t$ ($1 \le t \le 1000$) — the number of test cases. Next $t$ lines contains test cases — one per line. The first and only line of each test case contains string $s$ ($2 \le |s| \le 2 \cdot 10^5$). Each character $s_i$ is digit 0–9. It's guaranteed that the total length of strings doesn't exceed $2 \cdot 10^5$. -----Output----- For each test case, print the minimum number of characters you need to erase from $s$ to make it good. -----Example----- Input 3 95831 100120013 252525252525 Output 3 5 0 -----Note----- In the first test case, you can erase any $3$ characters, for example, the $1$-st, the $3$-rd, and the $4$-th. You'll get string 51 and it is good. In the second test case, we can erase all characters except 0: the remaining string is 0000 and it's good. In the third test case, the given string $s$ is already good.
for _ in range(int(input())): s = input() ans = 0 dic = [0] * 10 mx = 0 for i in s: dic[int(i)] += 1 if mx < dic[int(i)]: mx = dic[int(i)] ans = len(s) - mx s1 = "0" s2 = "0" flag = 0 while int(s1) <= 9 and int(s2) <= 9: for i in s: if flag & 1 and s2 == i or flag % 2 == 0 and s1 == i: flag += 1 if flag % 2 == 0: ans = min(len(s) - flag, ans) v = int(s1 + s2) + 1 s1 = str(v // 10) s2 = str(v % 10) flag = 0 print(ans)
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER NUMBER ASSIGN VAR NUMBER FOR VAR VAR VAR FUNC_CALL VAR VAR NUMBER IF VAR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR VAR ASSIGN VAR STRING ASSIGN VAR STRING ASSIGN VAR NUMBER WHILE FUNC_CALL VAR VAR NUMBER FUNC_CALL VAR VAR NUMBER FOR VAR VAR IF BIN_OP VAR NUMBER VAR VAR BIN_OP VAR NUMBER NUMBER VAR VAR VAR NUMBER IF BIN_OP VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR VAR VAR ASSIGN VAR BIN_OP FUNC_CALL VAR BIN_OP VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER EXPR FUNC_CALL VAR VAR
Let's call left cyclic shift of some string $t_1 t_2 t_3 \dots t_{n - 1} t_n$ as string $t_2 t_3 \dots t_{n - 1} t_n t_1$. Analogically, let's call right cyclic shift of string $t$ as string $t_n t_1 t_2 t_3 \dots t_{n - 1}$. Let's say string $t$ is good if its left cyclic shift is equal to its right cyclic shift. You are given string $s$ which consists of digits 0–9. What is the minimum number of characters you need to erase from $s$ to make it good? -----Input----- The first line contains single integer $t$ ($1 \le t \le 1000$) — the number of test cases. Next $t$ lines contains test cases — one per line. The first and only line of each test case contains string $s$ ($2 \le |s| \le 2 \cdot 10^5$). Each character $s_i$ is digit 0–9. It's guaranteed that the total length of strings doesn't exceed $2 \cdot 10^5$. -----Output----- For each test case, print the minimum number of characters you need to erase from $s$ to make it good. -----Example----- Input 3 95831 100120013 252525252525 Output 3 5 0 -----Note----- In the first test case, you can erase any $3$ characters, for example, the $1$-st, the $3$-rd, and the $4$-th. You'll get string 51 and it is good. In the second test case, we can erase all characters except 0: the remaining string is 0000 and it's good. In the third test case, the given string $s$ is already good.
def solve(a, b, s): res = 0 si = 1 for i in range(0, len(s)): if ord(s[i]) - ord("0") == a and si == 1: res += 1 si = -si elif ord(s[i]) - ord("0") == b and si == -1: res += 1 si = -si if res & 1 and res > 1 and a != b: res -= 1 return res tc = int(input()) while tc: s = input() max1 = 0 for i in range(0, 10): for j in range(0, 10): max1 = max(max1, solve(i, j, s)) print(len(s) - max1) tc -= 1
FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR IF BIN_OP FUNC_CALL VAR VAR VAR FUNC_CALL VAR STRING VAR VAR NUMBER VAR NUMBER ASSIGN VAR VAR IF BIN_OP FUNC_CALL VAR VAR VAR FUNC_CALL VAR STRING VAR VAR NUMBER VAR NUMBER ASSIGN VAR VAR IF BIN_OP VAR NUMBER VAR NUMBER VAR VAR VAR NUMBER RETURN VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR WHILE VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR VAR VAR NUMBER
Let's call left cyclic shift of some string $t_1 t_2 t_3 \dots t_{n - 1} t_n$ as string $t_2 t_3 \dots t_{n - 1} t_n t_1$. Analogically, let's call right cyclic shift of string $t$ as string $t_n t_1 t_2 t_3 \dots t_{n - 1}$. Let's say string $t$ is good if its left cyclic shift is equal to its right cyclic shift. You are given string $s$ which consists of digits 0–9. What is the minimum number of characters you need to erase from $s$ to make it good? -----Input----- The first line contains single integer $t$ ($1 \le t \le 1000$) — the number of test cases. Next $t$ lines contains test cases — one per line. The first and only line of each test case contains string $s$ ($2 \le |s| \le 2 \cdot 10^5$). Each character $s_i$ is digit 0–9. It's guaranteed that the total length of strings doesn't exceed $2 \cdot 10^5$. -----Output----- For each test case, print the minimum number of characters you need to erase from $s$ to make it good. -----Example----- Input 3 95831 100120013 252525252525 Output 3 5 0 -----Note----- In the first test case, you can erase any $3$ characters, for example, the $1$-st, the $3$-rd, and the $4$-th. You'll get string 51 and it is good. In the second test case, we can erase all characters except 0: the remaining string is 0000 and it's good. In the third test case, the given string $s$ is already good.
def fun(S, s): N = len(S) i = 0 flip = 1 cun = 0 while i < N: if flip: if S[i] == s[0]: i += 1 flip = 1 - flip continue elif S[i] == s[1]: flip = 1 - flip cun += 1 i += 1 continue i += 1 return cun for _ in range(int(input())): S = input() N = len(S) if N <= 2: print(0) continue Always = N - 2 Single = 0 for i in range(10): s = str(i) count = S.count(s) if count != -1: Single = max(Single, count) Double = 0 for i in range(0, 100): if i < 10: s = "0" + str(i) else: s = str(i) count = fun(S, s) Double = max(Double, count) X = min(Always, N - Single, N - 2 * Double) print(X)
FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR IF VAR IF VAR VAR VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP NUMBER VAR IF VAR VAR VAR NUMBER ASSIGN VAR BIN_OP NUMBER VAR VAR NUMBER VAR NUMBER VAR NUMBER RETURN VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR IF VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER NUMBER IF VAR NUMBER ASSIGN VAR BIN_OP STRING FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR BIN_OP VAR BIN_OP NUMBER VAR EXPR FUNC_CALL VAR VAR
Let's call left cyclic shift of some string $t_1 t_2 t_3 \dots t_{n - 1} t_n$ as string $t_2 t_3 \dots t_{n - 1} t_n t_1$. Analogically, let's call right cyclic shift of string $t$ as string $t_n t_1 t_2 t_3 \dots t_{n - 1}$. Let's say string $t$ is good if its left cyclic shift is equal to its right cyclic shift. You are given string $s$ which consists of digits 0–9. What is the minimum number of characters you need to erase from $s$ to make it good? -----Input----- The first line contains single integer $t$ ($1 \le t \le 1000$) — the number of test cases. Next $t$ lines contains test cases — one per line. The first and only line of each test case contains string $s$ ($2 \le |s| \le 2 \cdot 10^5$). Each character $s_i$ is digit 0–9. It's guaranteed that the total length of strings doesn't exceed $2 \cdot 10^5$. -----Output----- For each test case, print the minimum number of characters you need to erase from $s$ to make it good. -----Example----- Input 3 95831 100120013 252525252525 Output 3 5 0 -----Note----- In the first test case, you can erase any $3$ characters, for example, the $1$-st, the $3$-rd, and the $4$-th. You'll get string 51 and it is good. In the second test case, we can erase all characters except 0: the remaining string is 0000 and it's good. In the third test case, the given string $s$ is already good.
t = int(input()) for t1 in range(0, t): s = input() n = len(s) ans = n p = "0123456789" for i in p: for j in p: if i == j: continue f = 0 c = 0 for k in s: if k == i and f == 0: c = c + 1 f = 1 elif k == j and f == 1: c = c + 1 f = 0 c = c // 2 * 2 ans = min(ans, n - c) for i in p: ans = min(ans, n - s.count(i)) print(ans)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR VAR ASSIGN VAR STRING FOR VAR VAR FOR VAR VAR IF VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR IF VAR VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER IF VAR VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR FOR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR
Let's call left cyclic shift of some string $t_1 t_2 t_3 \dots t_{n - 1} t_n$ as string $t_2 t_3 \dots t_{n - 1} t_n t_1$. Analogically, let's call right cyclic shift of string $t$ as string $t_n t_1 t_2 t_3 \dots t_{n - 1}$. Let's say string $t$ is good if its left cyclic shift is equal to its right cyclic shift. You are given string $s$ which consists of digits 0–9. What is the minimum number of characters you need to erase from $s$ to make it good? -----Input----- The first line contains single integer $t$ ($1 \le t \le 1000$) — the number of test cases. Next $t$ lines contains test cases — one per line. The first and only line of each test case contains string $s$ ($2 \le |s| \le 2 \cdot 10^5$). Each character $s_i$ is digit 0–9. It's guaranteed that the total length of strings doesn't exceed $2 \cdot 10^5$. -----Output----- For each test case, print the minimum number of characters you need to erase from $s$ to make it good. -----Example----- Input 3 95831 100120013 252525252525 Output 3 5 0 -----Note----- In the first test case, you can erase any $3$ characters, for example, the $1$-st, the $3$-rd, and the $4$-th. You'll get string 51 and it is good. In the second test case, we can erase all characters except 0: the remaining string is 0000 and it's good. In the third test case, the given string $s$ is already good.
import sys input = sys.stdin.readline def ceil(x): if x != int(x): x = int(x) + 1 return x def swaparr(arr, a, b): temp = arr[a] arr[a] = arr[b] arr[b] = temp def gcd(a, b): if b == 0: return a return gcd(b, a % b) def nCr(n, k): if k > n - k: k = n - k res = 1 for i in range(k): res = res * (n - i) res = res / (i + 1) return int(res) def upper_bound(a, x, lo=0, hi=None): if hi == None: hi = len(a) while lo < hi: mid = (lo + hi) // 2 if a[mid] < x: lo = mid + 1 else: hi = mid return lo def primefs(n): primes = {} while n % 2 == 0 and n > 0: primes[2] = primes.get(2, 0) + 1 n = n // 2 for i in range(3, int(n**0.5) + 2, 2): while n % i == 0 and n > 0: primes[i] = primes.get(i, 0) + 1 n = n // i if n > 2: primes[n] = primes.get(n, 0) + 1 return primes def power(x, y, p): res = 1 x = x % p if x == 0: return 0 while y > 0: if y & 1 == 1: res = res * x % p y = y >> 1 x = x * x % p return res def swap(a, b): temp = a a = b b = temp return a, b def find(x, link): p = x while p != link[p]: p = link[p] while x != p: nex = link[x] link[x] = p x = nex return p def union(x, y, link, size): x = find(x, link) y = find(y, link) if size[x] < size[y]: x, y = swap(x, y) if x != y: size[x] += size[y] link[y] = x def sieve(n): prime = [(True) for i in range(n + 1)] p = 2 while p * p <= n: if prime[p] == True: for i in range(p * p, n + 1, p): prime[i] = False p += 1 return prime MAXN = int(100000.0 + 5) def spf_sieve(): spf[1] = 1 for i in range(2, MAXN): spf[i] = i for i in range(4, MAXN, 2): spf[i] = 2 for i in range(3, ceil(MAXN**0.5), 2): if spf[i] == i: for j in range(i * i, MAXN, i): if spf[j] == j: spf[j] = i def factoriazation(x): ret = {} while x != 1: ret[spf[x]] = ret.get(spf[x], 0) + 1 x = x // spf[x] return ret def int_array(): return list(map(int, input().strip().split())) def float_array(): return list(map(float, input().strip().split())) def str_array(): return input().strip().split() MOD = int(1000000000.0) + 7 CMOD = 998244353 INF = float("inf") NINF = -float("inf") for _ in range(int(input())): s = input().strip() n = len(s) dick = {} ans = INF for i in s: dick[i] = dick.get(i, 0) + 1 mx = NINF for i in dick: mx = max(mx, dick[i]) this = n - mx ans = min(ans, this) if len(dick) > 1: ans = min(ans, n - 2) for i in range(10): for j in range(i + 1, 10): this = [] for x in s: if x == str(i) or x == str(j): this.append(x) p = 0 prev = str(j) for x in this: if x != prev: p += 1 prev = x t = p // 2 p = 0 prev = str(i) for x in this: if x != prev: p += 1 prev = x p = max(p // 2, t) ans = min(ans, n - 2 * p) print(ans)
IMPORT ASSIGN VAR VAR FUNC_DEF IF VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER RETURN VAR FUNC_DEF ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR VAR ASSIGN VAR VAR VAR FUNC_DEF IF VAR NUMBER RETURN VAR RETURN FUNC_CALL VAR VAR BIN_OP VAR VAR FUNC_DEF IF VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR BIN_OP VAR NUMBER RETURN FUNC_CALL VAR VAR FUNC_DEF NUMBER NONE IF VAR NONE ASSIGN VAR FUNC_CALL VAR VAR WHILE VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR VAR RETURN VAR FUNC_DEF ASSIGN VAR DICT WHILE BIN_OP VAR NUMBER NUMBER VAR NUMBER ASSIGN VAR NUMBER BIN_OP FUNC_CALL VAR NUMBER NUMBER NUMBER ASSIGN VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER WHILE BIN_OP VAR VAR NUMBER VAR NUMBER ASSIGN VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR VAR IF VAR NUMBER ASSIGN VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER RETURN VAR FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR VAR IF VAR NUMBER RETURN NUMBER WHILE VAR NUMBER IF BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR RETURN VAR FUNC_DEF ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR RETURN VAR VAR FUNC_DEF ASSIGN VAR VAR WHILE VAR VAR VAR ASSIGN VAR VAR VAR WHILE VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR RETURN VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR IF VAR VAR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR IF VAR VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR FUNC_DEF ASSIGN VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER WHILE BIN_OP VAR VAR VAR IF VAR VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR VAR BIN_OP VAR NUMBER VAR ASSIGN VAR VAR NUMBER VAR NUMBER RETURN VAR ASSIGN VAR FUNC_CALL VAR BIN_OP NUMBER NUMBER FUNC_DEF ASSIGN VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR VAR FOR VAR FUNC_CALL VAR NUMBER VAR NUMBER ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER IF VAR VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR IF VAR VAR VAR ASSIGN VAR VAR VAR FUNC_DEF ASSIGN VAR DICT WHILE VAR NUMBER ASSIGN VAR VAR VAR BIN_OP FUNC_CALL VAR VAR VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR VAR VAR RETURN VAR FUNC_DEF RETURN FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR FUNC_DEF RETURN FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR FUNC_DEF RETURN FUNC_CALL FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP FUNC_CALL VAR NUMBER NUMBER ASSIGN VAR NUMBER 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 FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR DICT ASSIGN VAR VAR FOR VAR VAR ASSIGN VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER ASSIGN VAR VAR FOR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR IF FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR LIST FOR VAR VAR IF VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR FOR VAR VAR IF VAR VAR VAR NUMBER ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR FOR VAR VAR IF VAR VAR VAR NUMBER ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR BIN_OP NUMBER VAR EXPR FUNC_CALL VAR VAR
Let's call left cyclic shift of some string $t_1 t_2 t_3 \dots t_{n - 1} t_n$ as string $t_2 t_3 \dots t_{n - 1} t_n t_1$. Analogically, let's call right cyclic shift of string $t$ as string $t_n t_1 t_2 t_3 \dots t_{n - 1}$. Let's say string $t$ is good if its left cyclic shift is equal to its right cyclic shift. You are given string $s$ which consists of digits 0–9. What is the minimum number of characters you need to erase from $s$ to make it good? -----Input----- The first line contains single integer $t$ ($1 \le t \le 1000$) — the number of test cases. Next $t$ lines contains test cases — one per line. The first and only line of each test case contains string $s$ ($2 \le |s| \le 2 \cdot 10^5$). Each character $s_i$ is digit 0–9. It's guaranteed that the total length of strings doesn't exceed $2 \cdot 10^5$. -----Output----- For each test case, print the minimum number of characters you need to erase from $s$ to make it good. -----Example----- Input 3 95831 100120013 252525252525 Output 3 5 0 -----Note----- In the first test case, you can erase any $3$ characters, for example, the $1$-st, the $3$-rd, and the $4$-th. You'll get string 51 and it is good. In the second test case, we can erase all characters except 0: the remaining string is 0000 and it's good. In the third test case, the given string $s$ is already good.
z = input for _ in range(int(z())): l, a = z(), 0 for i in range(10): for j in range(10): t, t1, c = i, j, 0 for k in l: if int(k) == t: c += 1 t, t1 = t1, t if c % 2 and i != j: c -= 1 a = max(a, c) print(len(l) - a)
ASSIGN VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER ASSIGN VAR VAR VAR VAR VAR NUMBER FOR VAR VAR IF FUNC_CALL VAR VAR VAR VAR NUMBER ASSIGN VAR VAR VAR VAR IF BIN_OP VAR NUMBER VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR VAR
Let's call left cyclic shift of some string $t_1 t_2 t_3 \dots t_{n - 1} t_n$ as string $t_2 t_3 \dots t_{n - 1} t_n t_1$. Analogically, let's call right cyclic shift of string $t$ as string $t_n t_1 t_2 t_3 \dots t_{n - 1}$. Let's say string $t$ is good if its left cyclic shift is equal to its right cyclic shift. You are given string $s$ which consists of digits 0–9. What is the minimum number of characters you need to erase from $s$ to make it good? -----Input----- The first line contains single integer $t$ ($1 \le t \le 1000$) — the number of test cases. Next $t$ lines contains test cases — one per line. The first and only line of each test case contains string $s$ ($2 \le |s| \le 2 \cdot 10^5$). Each character $s_i$ is digit 0–9. It's guaranteed that the total length of strings doesn't exceed $2 \cdot 10^5$. -----Output----- For each test case, print the minimum number of characters you need to erase from $s$ to make it good. -----Example----- Input 3 95831 100120013 252525252525 Output 3 5 0 -----Note----- In the first test case, you can erase any $3$ characters, for example, the $1$-st, the $3$-rd, and the $4$-th. You'll get string 51 and it is good. In the second test case, we can erase all characters except 0: the remaining string is 0000 and it's good. In the third test case, the given string $s$ is already good.
t = int(input()) for _ in range(t): s = input() span = [] for i in range(10): for j in range(10): span.append(str(i) + str(j)) ans = 0 for i in range(100): toggle = 0 cnt = 0 prev = ans for j in range(len(s)): if s[j] == span[i][toggle]: cnt += 1 toggle = 1 - toggle if span[i][0] != span[i][1]: ans = max(ans, cnt // 2 * 2) else: ans = max(ans, cnt) if prev != ans: char = span[i] print(len(s) - ans)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR VAR VAR NUMBER ASSIGN VAR BIN_OP NUMBER VAR IF VAR VAR NUMBER VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR IF VAR VAR ASSIGN VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR VAR
Let's call left cyclic shift of some string $t_1 t_2 t_3 \dots t_{n - 1} t_n$ as string $t_2 t_3 \dots t_{n - 1} t_n t_1$. Analogically, let's call right cyclic shift of string $t$ as string $t_n t_1 t_2 t_3 \dots t_{n - 1}$. Let's say string $t$ is good if its left cyclic shift is equal to its right cyclic shift. You are given string $s$ which consists of digits 0–9. What is the minimum number of characters you need to erase from $s$ to make it good? -----Input----- The first line contains single integer $t$ ($1 \le t \le 1000$) — the number of test cases. Next $t$ lines contains test cases — one per line. The first and only line of each test case contains string $s$ ($2 \le |s| \le 2 \cdot 10^5$). Each character $s_i$ is digit 0–9. It's guaranteed that the total length of strings doesn't exceed $2 \cdot 10^5$. -----Output----- For each test case, print the minimum number of characters you need to erase from $s$ to make it good. -----Example----- Input 3 95831 100120013 252525252525 Output 3 5 0 -----Note----- In the first test case, you can erase any $3$ characters, for example, the $1$-st, the $3$-rd, and the $4$-th. You'll get string 51 and it is good. In the second test case, we can erase all characters except 0: the remaining string is 0000 and it's good. In the third test case, the given string $s$ is already good.
t = int(input()) for i in range(t): s = str(input()) l = len(s) a0 = a1 = a2 = a3 = a4 = a5 = a6 = a7 = a8 = a9 = 0 a = l - 2 for i in s: if i == "0": a0 = a0 + 1 elif i == "1": a1 = a1 + 1 elif i == "2": a2 = a2 + 1 elif i == "3": a3 = a3 + 1 elif i == "4": a4 = a4 + 1 elif i == "5": a5 = a5 + 1 elif i == "6": a6 = a6 + 1 elif i == "7": a7 = a7 + 1 elif i == "8": a8 = a8 + 1 else: a9 = a9 + 1 bm = max(a0, a1, a2, a3, a4, a5, a6, a7, a8, a9) b = l - bm m = 0 for i in range(10): for j in range(10): c = 0 if i != j: for x in s: y = int(x) if c % 2 == 0 and y == i: c = c + 1 elif c % 2 == 1 and y == j: c = c + 1 if c > m: m = c m = m // 2 m = m * 2 d = l - m print(max(min(a, b, d), 0))
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER FOR VAR VAR IF VAR STRING ASSIGN VAR BIN_OP VAR NUMBER IF VAR STRING ASSIGN VAR BIN_OP VAR NUMBER IF VAR STRING ASSIGN VAR BIN_OP VAR NUMBER IF VAR STRING ASSIGN VAR BIN_OP VAR NUMBER IF VAR STRING ASSIGN VAR BIN_OP VAR NUMBER IF VAR STRING ASSIGN VAR BIN_OP VAR NUMBER IF VAR STRING ASSIGN VAR BIN_OP VAR NUMBER IF VAR STRING ASSIGN VAR BIN_OP VAR NUMBER IF VAR STRING ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER IF VAR VAR FOR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR IF BIN_OP VAR NUMBER NUMBER VAR VAR ASSIGN VAR BIN_OP VAR NUMBER IF BIN_OP VAR NUMBER NUMBER VAR VAR ASSIGN VAR BIN_OP VAR NUMBER IF VAR VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR NUMBER
Let's call left cyclic shift of some string $t_1 t_2 t_3 \dots t_{n - 1} t_n$ as string $t_2 t_3 \dots t_{n - 1} t_n t_1$. Analogically, let's call right cyclic shift of string $t$ as string $t_n t_1 t_2 t_3 \dots t_{n - 1}$. Let's say string $t$ is good if its left cyclic shift is equal to its right cyclic shift. You are given string $s$ which consists of digits 0–9. What is the minimum number of characters you need to erase from $s$ to make it good? -----Input----- The first line contains single integer $t$ ($1 \le t \le 1000$) — the number of test cases. Next $t$ lines contains test cases — one per line. The first and only line of each test case contains string $s$ ($2 \le |s| \le 2 \cdot 10^5$). Each character $s_i$ is digit 0–9. It's guaranteed that the total length of strings doesn't exceed $2 \cdot 10^5$. -----Output----- For each test case, print the minimum number of characters you need to erase from $s$ to make it good. -----Example----- Input 3 95831 100120013 252525252525 Output 3 5 0 -----Note----- In the first test case, you can erase any $3$ characters, for example, the $1$-st, the $3$-rd, and the $4$-th. You'll get string 51 and it is good. In the second test case, we can erase all characters except 0: the remaining string is 0000 and it's good. In the third test case, the given string $s$ is already good.
t = int(input()) for _ in range(t): s = input() n = len(s) best = 0 for i in range(0, 10): a = chr(48 + i) for j in range(0, 10): b = chr(48 + j) curlen = 0 cur = 0 for c in s: if cur == 0: if c == a: curlen += 1 cur ^= 1 elif c == b: curlen += 1 cur ^= 1 best = max(best, curlen if a == b else curlen - curlen % 2) print(n - best)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP NUMBER VAR FOR VAR FUNC_CALL VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP NUMBER VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR IF VAR NUMBER IF VAR VAR VAR NUMBER VAR NUMBER IF VAR VAR VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR BIN_OP VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR VAR
Let's call left cyclic shift of some string $t_1 t_2 t_3 \dots t_{n - 1} t_n$ as string $t_2 t_3 \dots t_{n - 1} t_n t_1$. Analogically, let's call right cyclic shift of string $t$ as string $t_n t_1 t_2 t_3 \dots t_{n - 1}$. Let's say string $t$ is good if its left cyclic shift is equal to its right cyclic shift. You are given string $s$ which consists of digits 0–9. What is the minimum number of characters you need to erase from $s$ to make it good? -----Input----- The first line contains single integer $t$ ($1 \le t \le 1000$) — the number of test cases. Next $t$ lines contains test cases — one per line. The first and only line of each test case contains string $s$ ($2 \le |s| \le 2 \cdot 10^5$). Each character $s_i$ is digit 0–9. It's guaranteed that the total length of strings doesn't exceed $2 \cdot 10^5$. -----Output----- For each test case, print the minimum number of characters you need to erase from $s$ to make it good. -----Example----- Input 3 95831 100120013 252525252525 Output 3 5 0 -----Note----- In the first test case, you can erase any $3$ characters, for example, the $1$-st, the $3$-rd, and the $4$-th. You'll get string 51 and it is good. In the second test case, we can erase all characters except 0: the remaining string is 0000 and it's good. In the third test case, the given string $s$ is already good.
tt = int(input()) while tt != 0: n = input() d = {} for i in range(10): d[i] = 0 for i in range(len(n)): d[int(n[i])] += 1 maxi = 0 for i in d.keys(): maxi = max(maxi, d[i]) ans = len(n) - maxi for i in range(10): for j in range(10): if i == j: continue else: k = 0 t = 0 while k < len(n): f1, f2 = 0, 0 while k < len(n): if int(n[k]) == i: f1 = 1 break k += 1 if f1 == 1: while k < len(n): if int(n[k]) == j: f2 = 1 break k += 1 if f1 == 0 or f2 == 0: break else: t += 2 ans = min(ans, len(n) - t) print(ans) tt -= 1
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR WHILE VAR NUMBER ASSIGN VAR FUNC_CALL VAR ASSIGN VAR DICT FOR VAR FUNC_CALL VAR NUMBER ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR VAR FOR VAR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER IF VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR FUNC_CALL VAR VAR ASSIGN VAR VAR NUMBER NUMBER WHILE VAR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR VAR VAR ASSIGN VAR NUMBER VAR NUMBER IF VAR NUMBER WHILE VAR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR VAR VAR ASSIGN VAR NUMBER VAR NUMBER IF VAR NUMBER VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR NUMBER
Let's call left cyclic shift of some string $t_1 t_2 t_3 \dots t_{n - 1} t_n$ as string $t_2 t_3 \dots t_{n - 1} t_n t_1$. Analogically, let's call right cyclic shift of string $t$ as string $t_n t_1 t_2 t_3 \dots t_{n - 1}$. Let's say string $t$ is good if its left cyclic shift is equal to its right cyclic shift. You are given string $s$ which consists of digits 0–9. What is the minimum number of characters you need to erase from $s$ to make it good? -----Input----- The first line contains single integer $t$ ($1 \le t \le 1000$) — the number of test cases. Next $t$ lines contains test cases — one per line. The first and only line of each test case contains string $s$ ($2 \le |s| \le 2 \cdot 10^5$). Each character $s_i$ is digit 0–9. It's guaranteed that the total length of strings doesn't exceed $2 \cdot 10^5$. -----Output----- For each test case, print the minimum number of characters you need to erase from $s$ to make it good. -----Example----- Input 3 95831 100120013 252525252525 Output 3 5 0 -----Note----- In the first test case, you can erase any $3$ characters, for example, the $1$-st, the $3$-rd, and the $4$-th. You'll get string 51 and it is good. In the second test case, we can erase all characters except 0: the remaining string is 0000 and it's good. In the third test case, the given string $s$ is already good.
for i in range(int(input())): a = input() n = len(a) d = n for j in range(10): e = [0] * (9 - j) c = 0 g = 0 h = [0] * (9 - j) for k in range(n): y = int(a[k]) if y == j: if c > 0: for l in range(9 - j): e[l] += f[l] f = [0] * (9 - j) c += 1 elif c > 0 and y > j: z = y - j - 1 if f[z] == 0: f[z] = 1 elif c == 0 and y > j: z = y - j - 1 if h[z] == 0: h[z] = 1 if c > 0: for l in range(9 - j): e[l] += f[l] c0 = c for k in range(9 - j): if h[k] == 1 and c0 > 0 and f[k] == 0: e[k] += 1 c = max(c, 2 * e[k]) d = min(d, n - c) print(d)
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FOR VAR FUNC_CALL VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER BIN_OP NUMBER VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER BIN_OP NUMBER VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR IF VAR VAR IF VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP NUMBER VAR VAR VAR VAR VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP NUMBER VAR VAR NUMBER IF VAR NUMBER VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF VAR VAR NUMBER ASSIGN VAR VAR NUMBER IF VAR NUMBER VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF VAR VAR NUMBER ASSIGN VAR VAR NUMBER IF VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP NUMBER VAR VAR VAR VAR VAR ASSIGN VAR VAR FOR VAR FUNC_CALL VAR BIN_OP NUMBER VAR IF VAR VAR NUMBER VAR NUMBER VAR VAR NUMBER VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP NUMBER VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR
Let's call left cyclic shift of some string $t_1 t_2 t_3 \dots t_{n - 1} t_n$ as string $t_2 t_3 \dots t_{n - 1} t_n t_1$. Analogically, let's call right cyclic shift of string $t$ as string $t_n t_1 t_2 t_3 \dots t_{n - 1}$. Let's say string $t$ is good if its left cyclic shift is equal to its right cyclic shift. You are given string $s$ which consists of digits 0–9. What is the minimum number of characters you need to erase from $s$ to make it good? -----Input----- The first line contains single integer $t$ ($1 \le t \le 1000$) — the number of test cases. Next $t$ lines contains test cases — one per line. The first and only line of each test case contains string $s$ ($2 \le |s| \le 2 \cdot 10^5$). Each character $s_i$ is digit 0–9. It's guaranteed that the total length of strings doesn't exceed $2 \cdot 10^5$. -----Output----- For each test case, print the minimum number of characters you need to erase from $s$ to make it good. -----Example----- Input 3 95831 100120013 252525252525 Output 3 5 0 -----Note----- In the first test case, you can erase any $3$ characters, for example, the $1$-st, the $3$-rd, and the $4$-th. You'll get string 51 and it is good. In the second test case, we can erase all characters except 0: the remaining string is 0000 and it's good. In the third test case, the given string $s$ is already good.
for _ in range(int(input())): s = input() f = [(0) for _ in range(10)] for c in s: f[int(c)] += 1 ans = len(s) - max(f) for i in range(9): for j in range(i + 1, 10): am = False bm = -1 an = 0 x, y = -1, -1 for c in s: if not am and (c == str(i) or c == str(j)): if c == str(i): x, y = i, j else: x, y = j, i am = True bm = 1 continue if am: if bm == 1: if c == str(y): bm = 0 an += 1 continue if bm == 0: if c == str(x): bm = 1 continue ans = min(ans, len(s) - 2 * an) print(ans)
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR NUMBER FOR VAR VAR VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR NUMBER NUMBER FOR VAR VAR IF VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR IF VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER IF VAR IF VAR NUMBER IF VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER VAR NUMBER IF VAR NUMBER IF VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP FUNC_CALL VAR VAR BIN_OP NUMBER VAR EXPR FUNC_CALL VAR VAR
Let's call left cyclic shift of some string $t_1 t_2 t_3 \dots t_{n - 1} t_n$ as string $t_2 t_3 \dots t_{n - 1} t_n t_1$. Analogically, let's call right cyclic shift of string $t$ as string $t_n t_1 t_2 t_3 \dots t_{n - 1}$. Let's say string $t$ is good if its left cyclic shift is equal to its right cyclic shift. You are given string $s$ which consists of digits 0–9. What is the minimum number of characters you need to erase from $s$ to make it good? -----Input----- The first line contains single integer $t$ ($1 \le t \le 1000$) — the number of test cases. Next $t$ lines contains test cases — one per line. The first and only line of each test case contains string $s$ ($2 \le |s| \le 2 \cdot 10^5$). Each character $s_i$ is digit 0–9. It's guaranteed that the total length of strings doesn't exceed $2 \cdot 10^5$. -----Output----- For each test case, print the minimum number of characters you need to erase from $s$ to make it good. -----Example----- Input 3 95831 100120013 252525252525 Output 3 5 0 -----Note----- In the first test case, you can erase any $3$ characters, for example, the $1$-st, the $3$-rd, and the $4$-th. You'll get string 51 and it is good. In the second test case, we can erase all characters except 0: the remaining string is 0000 and it's good. In the third test case, the given string $s$ is already good.
t = int(input()) for i in range(t): s = input() v = len(s) ansf = 0 bq = [0] * 10 l = ["0", "1", "2", "3", "4", "5", "6", "7", "8", "9"] for j in range(len(s)): bq[int(s[j])] += 1 ansf = max(bq) for j in range(10): for k in range(10): if j != k: ans = 0 w = 0 while w < v: p = 0 q = 0 while w < v and s[w] != l[j]: w += 1 p = 1 if p == 1: w += 1 while w < v and s[w] != l[k]: w += 1 q = 1 if q == 1 and w < v: ans += 2 w += 1 else: if w < v: ans += 2 w += 1 elif w < v: w += 1 while w < v and s[w] != l[k]: w += 1 q = 1 if q == 1 and w < v: ans += 2 w += 1 else: if w < v: ans += 2 w += 1 ansf = max(ans, ansf) print(v - ansf)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER NUMBER ASSIGN VAR LIST STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER IF VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR VAR VAR VAR VAR VAR NUMBER ASSIGN VAR NUMBER IF VAR NUMBER VAR NUMBER WHILE VAR VAR VAR VAR VAR VAR VAR NUMBER ASSIGN VAR NUMBER IF VAR NUMBER VAR VAR VAR NUMBER VAR NUMBER IF VAR VAR VAR NUMBER VAR NUMBER IF VAR VAR VAR NUMBER WHILE VAR VAR VAR VAR VAR VAR VAR NUMBER ASSIGN VAR NUMBER IF VAR NUMBER VAR VAR VAR NUMBER VAR NUMBER IF VAR VAR VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR
Let's call left cyclic shift of some string $t_1 t_2 t_3 \dots t_{n - 1} t_n$ as string $t_2 t_3 \dots t_{n - 1} t_n t_1$. Analogically, let's call right cyclic shift of string $t$ as string $t_n t_1 t_2 t_3 \dots t_{n - 1}$. Let's say string $t$ is good if its left cyclic shift is equal to its right cyclic shift. You are given string $s$ which consists of digits 0–9. What is the minimum number of characters you need to erase from $s$ to make it good? -----Input----- The first line contains single integer $t$ ($1 \le t \le 1000$) — the number of test cases. Next $t$ lines contains test cases — one per line. The first and only line of each test case contains string $s$ ($2 \le |s| \le 2 \cdot 10^5$). Each character $s_i$ is digit 0–9. It's guaranteed that the total length of strings doesn't exceed $2 \cdot 10^5$. -----Output----- For each test case, print the minimum number of characters you need to erase from $s$ to make it good. -----Example----- Input 3 95831 100120013 252525252525 Output 3 5 0 -----Note----- In the first test case, you can erase any $3$ characters, for example, the $1$-st, the $3$-rd, and the $4$-th. You'll get string 51 and it is good. In the second test case, we can erase all characters except 0: the remaining string is 0000 and it's good. In the third test case, the given string $s$ is already good.
t = int(input()) for _ in range(t): s = input() l = [[(0) for i in range(10)] for i in range(10)] d = {} d = [(0) for i in range(10)] boolar = [[(False) for i in range(10)] for i in range(10)] for i in s: d[int(i)] += 1 x = int(i) for j in range(10): if boolar[j][x]: l[j][x] += 1 boolar[j][x] = False if x != j: boolar[x][j] = True m = 0 for i in range(10): for j in range(10): m = max(m, l[i][j]) a = len(s) - 2 * m b = len(s) - max(d) if max(d) == len(s): print(0) else: print(min(a, b))
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR NUMBER VAR FUNC_CALL VAR NUMBER ASSIGN VAR DICT ASSIGN VAR NUMBER VAR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR NUMBER VAR FUNC_CALL VAR NUMBER FOR VAR VAR VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR NUMBER IF VAR VAR VAR VAR VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER IF VAR VAR ASSIGN VAR VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR BIN_OP NUMBER VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR
Let's call left cyclic shift of some string $t_1 t_2 t_3 \dots t_{n - 1} t_n$ as string $t_2 t_3 \dots t_{n - 1} t_n t_1$. Analogically, let's call right cyclic shift of string $t$ as string $t_n t_1 t_2 t_3 \dots t_{n - 1}$. Let's say string $t$ is good if its left cyclic shift is equal to its right cyclic shift. You are given string $s$ which consists of digits 0–9. What is the minimum number of characters you need to erase from $s$ to make it good? -----Input----- The first line contains single integer $t$ ($1 \le t \le 1000$) — the number of test cases. Next $t$ lines contains test cases — one per line. The first and only line of each test case contains string $s$ ($2 \le |s| \le 2 \cdot 10^5$). Each character $s_i$ is digit 0–9. It's guaranteed that the total length of strings doesn't exceed $2 \cdot 10^5$. -----Output----- For each test case, print the minimum number of characters you need to erase from $s$ to make it good. -----Example----- Input 3 95831 100120013 252525252525 Output 3 5 0 -----Note----- In the first test case, you can erase any $3$ characters, for example, the $1$-st, the $3$-rd, and the $4$-th. You'll get string 51 and it is good. In the second test case, we can erase all characters except 0: the remaining string is 0000 and it's good. In the third test case, the given string $s$ is already good.
t = int(input()) for i in range(t): s = input() ans = 0 e = len(s) qr = 0 for j in range(10): for q in range(10): b = False r = 0 for i in range(len(s)): if j != q: if s[i] == str(j) and not b: b = True r += 1 if s[i] == str(q) and b: b = False r += 1 elif s[i] == str(j): r += 1 if j == q: ans = max(ans, r) else: r = r - r % 2 ans = max(ans, r) print(e - ans)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR IF VAR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR NUMBER VAR NUMBER IF VAR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR NUMBER VAR NUMBER IF VAR VAR FUNC_CALL VAR VAR VAR NUMBER IF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR
Let's call left cyclic shift of some string $t_1 t_2 t_3 \dots t_{n - 1} t_n$ as string $t_2 t_3 \dots t_{n - 1} t_n t_1$. Analogically, let's call right cyclic shift of string $t$ as string $t_n t_1 t_2 t_3 \dots t_{n - 1}$. Let's say string $t$ is good if its left cyclic shift is equal to its right cyclic shift. You are given string $s$ which consists of digits 0–9. What is the minimum number of characters you need to erase from $s$ to make it good? -----Input----- The first line contains single integer $t$ ($1 \le t \le 1000$) — the number of test cases. Next $t$ lines contains test cases — one per line. The first and only line of each test case contains string $s$ ($2 \le |s| \le 2 \cdot 10^5$). Each character $s_i$ is digit 0–9. It's guaranteed that the total length of strings doesn't exceed $2 \cdot 10^5$. -----Output----- For each test case, print the minimum number of characters you need to erase from $s$ to make it good. -----Example----- Input 3 95831 100120013 252525252525 Output 3 5 0 -----Note----- In the first test case, you can erase any $3$ characters, for example, the $1$-st, the $3$-rd, and the $4$-th. You'll get string 51 and it is good. In the second test case, we can erase all characters except 0: the remaining string is 0000 and it's good. In the third test case, the given string $s$ is already good.
for t in range(int(input())): s = input() n = len(s) index = [[] for i in range(10)] for i in range(len(s)): index[int(s[i])].append(i) ans = float("inf") for i in range(10): for j in range(10): if i == j: ans = min(ans, n - len(index[i])) else: i1 = j1 = 0 cnt = 0 last = -1 while True: if cnt % 2 == 0: if i1 == len(index[i]): break if index[i][i1] > last: last = index[i][i1] cnt += 1 i1 += 1 else: if j1 == len(index[j]): break if index[j][j1] > last: last = index[j][j1] cnt += 1 j1 += 1 if cnt % 2: cnt -= 1 ans = min(ans, n - cnt) print(ans)
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR LIST VAR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR STRING FOR VAR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER IF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE NUMBER IF BIN_OP VAR NUMBER NUMBER IF VAR FUNC_CALL VAR VAR VAR IF VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR NUMBER VAR NUMBER IF VAR FUNC_CALL VAR VAR VAR IF VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR NUMBER VAR NUMBER IF BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR
Let's call left cyclic shift of some string $t_1 t_2 t_3 \dots t_{n - 1} t_n$ as string $t_2 t_3 \dots t_{n - 1} t_n t_1$. Analogically, let's call right cyclic shift of string $t$ as string $t_n t_1 t_2 t_3 \dots t_{n - 1}$. Let's say string $t$ is good if its left cyclic shift is equal to its right cyclic shift. You are given string $s$ which consists of digits 0–9. What is the minimum number of characters you need to erase from $s$ to make it good? -----Input----- The first line contains single integer $t$ ($1 \le t \le 1000$) — the number of test cases. Next $t$ lines contains test cases — one per line. The first and only line of each test case contains string $s$ ($2 \le |s| \le 2 \cdot 10^5$). Each character $s_i$ is digit 0–9. It's guaranteed that the total length of strings doesn't exceed $2 \cdot 10^5$. -----Output----- For each test case, print the minimum number of characters you need to erase from $s$ to make it good. -----Example----- Input 3 95831 100120013 252525252525 Output 3 5 0 -----Note----- In the first test case, you can erase any $3$ characters, for example, the $1$-st, the $3$-rd, and the $4$-th. You'll get string 51 and it is good. In the second test case, we can erase all characters except 0: the remaining string is 0000 and it's good. In the third test case, the given string $s$ is already good.
t = input() t = int(t) def solve(s): s_list = list(map(int, s)) real_ans = 0 for i in range(10): for j in range(10): ans = 0 if i == j: for n in s_list: if n == i: ans += 1 else: chk = 0 for n in s_list: if n == i and chk == 0: chk = 1 ans += 1 elif n == j and chk == 1: chk = 0 ans += 1 else: pass if ans % 2 == 1: ans = 0 if ans > real_ans: real_ans = ans print(len(s) - real_ans) for i in range(t): s = input() solve(s)
ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER IF VAR VAR FOR VAR VAR IF VAR VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR IF VAR VAR VAR NUMBER ASSIGN VAR NUMBER VAR NUMBER IF VAR VAR VAR NUMBER ASSIGN VAR NUMBER VAR NUMBER IF BIN_OP VAR NUMBER NUMBER ASSIGN VAR NUMBER IF VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR
Let's call left cyclic shift of some string $t_1 t_2 t_3 \dots t_{n - 1} t_n$ as string $t_2 t_3 \dots t_{n - 1} t_n t_1$. Analogically, let's call right cyclic shift of string $t$ as string $t_n t_1 t_2 t_3 \dots t_{n - 1}$. Let's say string $t$ is good if its left cyclic shift is equal to its right cyclic shift. You are given string $s$ which consists of digits 0–9. What is the minimum number of characters you need to erase from $s$ to make it good? -----Input----- The first line contains single integer $t$ ($1 \le t \le 1000$) — the number of test cases. Next $t$ lines contains test cases — one per line. The first and only line of each test case contains string $s$ ($2 \le |s| \le 2 \cdot 10^5$). Each character $s_i$ is digit 0–9. It's guaranteed that the total length of strings doesn't exceed $2 \cdot 10^5$. -----Output----- For each test case, print the minimum number of characters you need to erase from $s$ to make it good. -----Example----- Input 3 95831 100120013 252525252525 Output 3 5 0 -----Note----- In the first test case, you can erase any $3$ characters, for example, the $1$-st, the $3$-rd, and the $4$-th. You'll get string 51 and it is good. In the second test case, we can erase all characters except 0: the remaining string is 0000 and it's good. In the third test case, the given string $s$ is already good.
for _ in range(int(input())): s = str(input()) n = len(s) m = n def check(x, y): res = 0 for k in s: if k == x: res += 1 x, y = y, x if res % 2 != 0: res -= 1 return res for i in range(10): for j in range(10): if i == j: m = min(n - s.count(str(i)), m) else: r = check(str(i), str(j)) m = min(n - r, m) print(m)
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_DEF ASSIGN VAR NUMBER FOR VAR VAR IF VAR VAR VAR NUMBER ASSIGN VAR VAR VAR VAR IF BIN_OP VAR NUMBER NUMBER VAR NUMBER RETURN VAR FOR VAR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER IF VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR VAR EXPR FUNC_CALL VAR VAR
Let's call left cyclic shift of some string $t_1 t_2 t_3 \dots t_{n - 1} t_n$ as string $t_2 t_3 \dots t_{n - 1} t_n t_1$. Analogically, let's call right cyclic shift of string $t$ as string $t_n t_1 t_2 t_3 \dots t_{n - 1}$. Let's say string $t$ is good if its left cyclic shift is equal to its right cyclic shift. You are given string $s$ which consists of digits 0–9. What is the minimum number of characters you need to erase from $s$ to make it good? -----Input----- The first line contains single integer $t$ ($1 \le t \le 1000$) — the number of test cases. Next $t$ lines contains test cases — one per line. The first and only line of each test case contains string $s$ ($2 \le |s| \le 2 \cdot 10^5$). Each character $s_i$ is digit 0–9. It's guaranteed that the total length of strings doesn't exceed $2 \cdot 10^5$. -----Output----- For each test case, print the minimum number of characters you need to erase from $s$ to make it good. -----Example----- Input 3 95831 100120013 252525252525 Output 3 5 0 -----Note----- In the first test case, you can erase any $3$ characters, for example, the $1$-st, the $3$-rd, and the $4$-th. You'll get string 51 and it is good. In the second test case, we can erase all characters except 0: the remaining string is 0000 and it's good. In the third test case, the given string $s$ is already good.
import sys readline = sys.stdin.readline readlines = sys.stdin.readlines ns = lambda: readline().rstrip() ni = lambda: int(readline().rstrip()) nm = lambda: map(int, readline().split()) nl = lambda: list(map(int, readline().split())) prn = lambda x: print(*x, sep="\n") def solve(): s = list(map(int, list(ns()))) ans = 0 for i in range(10): for j in range(10): a, b = i, j c = 0 for x in s: if x == a: a, b = b, a c += 1 if a != b: c &= ~1 if ans < c: ans = c print(len(s) - ans) return T = ni() for _ in range(T): solve()
IMPORT ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR STRING FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER ASSIGN VAR VAR VAR VAR ASSIGN VAR NUMBER FOR VAR VAR IF VAR VAR ASSIGN VAR VAR VAR VAR VAR NUMBER IF VAR VAR VAR NUMBER IF VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR VAR RETURN ASSIGN VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR
Let's call left cyclic shift of some string $t_1 t_2 t_3 \dots t_{n - 1} t_n$ as string $t_2 t_3 \dots t_{n - 1} t_n t_1$. Analogically, let's call right cyclic shift of string $t$ as string $t_n t_1 t_2 t_3 \dots t_{n - 1}$. Let's say string $t$ is good if its left cyclic shift is equal to its right cyclic shift. You are given string $s$ which consists of digits 0–9. What is the minimum number of characters you need to erase from $s$ to make it good? -----Input----- The first line contains single integer $t$ ($1 \le t \le 1000$) — the number of test cases. Next $t$ lines contains test cases — one per line. The first and only line of each test case contains string $s$ ($2 \le |s| \le 2 \cdot 10^5$). Each character $s_i$ is digit 0–9. It's guaranteed that the total length of strings doesn't exceed $2 \cdot 10^5$. -----Output----- For each test case, print the minimum number of characters you need to erase from $s$ to make it good. -----Example----- Input 3 95831 100120013 252525252525 Output 3 5 0 -----Note----- In the first test case, you can erase any $3$ characters, for example, the $1$-st, the $3$-rd, and the $4$-th. You'll get string 51 and it is good. In the second test case, we can erase all characters except 0: the remaining string is 0000 and it's good. In the third test case, the given string $s$ is already good.
for _ in range(int(input())): s = input() a = [0] * 10 n = len(s) for i in range(n): a[ord(s[i]) - ord("0")] += 1 ma = 0 y = 0 flag = 0 for i in range(10): for j in range(10): flag = 0 y = 0 for k in range(n): if y == 0 and i == ord(s[k]) - ord("0"): y = 1 elif j == ord(s[k]) - ord("0") and y == 1: y = 0 flag += 2 ma = max(ma, flag) for i in range(10): ma = max(ma, a[i]) print(n - ma)
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR VAR BIN_OP FUNC_CALL VAR VAR VAR FUNC_CALL VAR STRING NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR NUMBER VAR BIN_OP FUNC_CALL VAR VAR VAR FUNC_CALL VAR STRING ASSIGN VAR NUMBER IF VAR BIN_OP FUNC_CALL VAR VAR VAR FUNC_CALL VAR STRING VAR NUMBER ASSIGN VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR FOR VAR FUNC_CALL VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR
Let's call left cyclic shift of some string $t_1 t_2 t_3 \dots t_{n - 1} t_n$ as string $t_2 t_3 \dots t_{n - 1} t_n t_1$. Analogically, let's call right cyclic shift of string $t$ as string $t_n t_1 t_2 t_3 \dots t_{n - 1}$. Let's say string $t$ is good if its left cyclic shift is equal to its right cyclic shift. You are given string $s$ which consists of digits 0–9. What is the minimum number of characters you need to erase from $s$ to make it good? -----Input----- The first line contains single integer $t$ ($1 \le t \le 1000$) — the number of test cases. Next $t$ lines contains test cases — one per line. The first and only line of each test case contains string $s$ ($2 \le |s| \le 2 \cdot 10^5$). Each character $s_i$ is digit 0–9. It's guaranteed that the total length of strings doesn't exceed $2 \cdot 10^5$. -----Output----- For each test case, print the minimum number of characters you need to erase from $s$ to make it good. -----Example----- Input 3 95831 100120013 252525252525 Output 3 5 0 -----Note----- In the first test case, you can erase any $3$ characters, for example, the $1$-st, the $3$-rd, and the $4$-th. You'll get string 51 and it is good. In the second test case, we can erase all characters except 0: the remaining string is 0000 and it's good. In the third test case, the given string $s$ is already good.
import sys reader = (s.rstrip() for s in sys.stdin) input = reader.__next__ def calc(s, a, b): temp = 0 for i in s: if i == a: a, b = b, a temp += 1 if temp & 1: temp -= 1 return temp t = int(input()) for _ in range(t): s = [int(x) for x in input()] n = len(s) cnt = {} for x in range(10): cnt[x] = s.count(x) ans = n for x in range(10): for y in range(10): if x in cnt and y in cnt: if x == y: ans = min(ans, n - cnt[x]) else: temp1 = calc(s, x, y) temp2 = calc(s, y, x) temp = min(n - temp1, n - temp2) ans = min(ans, temp) print(ans)
IMPORT ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR FUNC_DEF ASSIGN VAR NUMBER FOR VAR VAR IF VAR VAR ASSIGN VAR VAR VAR VAR VAR NUMBER IF BIN_OP VAR NUMBER VAR NUMBER RETURN VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR DICT FOR VAR FUNC_CALL VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FOR VAR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER IF VAR VAR VAR VAR IF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR
Let's call left cyclic shift of some string $t_1 t_2 t_3 \dots t_{n - 1} t_n$ as string $t_2 t_3 \dots t_{n - 1} t_n t_1$. Analogically, let's call right cyclic shift of string $t$ as string $t_n t_1 t_2 t_3 \dots t_{n - 1}$. Let's say string $t$ is good if its left cyclic shift is equal to its right cyclic shift. You are given string $s$ which consists of digits 0–9. What is the minimum number of characters you need to erase from $s$ to make it good? -----Input----- The first line contains single integer $t$ ($1 \le t \le 1000$) — the number of test cases. Next $t$ lines contains test cases — one per line. The first and only line of each test case contains string $s$ ($2 \le |s| \le 2 \cdot 10^5$). Each character $s_i$ is digit 0–9. It's guaranteed that the total length of strings doesn't exceed $2 \cdot 10^5$. -----Output----- For each test case, print the minimum number of characters you need to erase from $s$ to make it good. -----Example----- Input 3 95831 100120013 252525252525 Output 3 5 0 -----Note----- In the first test case, you can erase any $3$ characters, for example, the $1$-st, the $3$-rd, and the $4$-th. You'll get string 51 and it is good. In the second test case, we can erase all characters except 0: the remaining string is 0000 and it's good. In the third test case, the given string $s$ is already good.
def solve(s): c = [([0] * 10) for _ in range(10)] front_appear = [([False] * 10) for _ in range(10)] s_cnt = [0] * 10 for sd in s: d = int(sd) s_cnt[d] += 1 for n in range(10): if front_appear[n][d]: if n != d: c[n][d] += 1 front_appear[n][d] = False front_appear[d][n] = True cand = len(s) - max(s_cnt) max_pair_cnt = 0 for i in range(10): for j in range(10): if i != j: max_pair_cnt = max(max_pair_cnt, c[i][j]) cand2 = len(s) - 2 * max_pair_cnt ans = min(cand, cand2) return ans t = int(input()) for _ in range(t): s = input() print(solve(s))
FUNC_DEF ASSIGN VAR BIN_OP LIST NUMBER NUMBER VAR FUNC_CALL VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER NUMBER VAR FUNC_CALL VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER NUMBER FOR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER IF VAR VAR VAR IF VAR VAR VAR VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER IF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR BIN_OP NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR VAR RETURN VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR
Let's call left cyclic shift of some string $t_1 t_2 t_3 \dots t_{n - 1} t_n$ as string $t_2 t_3 \dots t_{n - 1} t_n t_1$. Analogically, let's call right cyclic shift of string $t$ as string $t_n t_1 t_2 t_3 \dots t_{n - 1}$. Let's say string $t$ is good if its left cyclic shift is equal to its right cyclic shift. You are given string $s$ which consists of digits 0–9. What is the minimum number of characters you need to erase from $s$ to make it good? -----Input----- The first line contains single integer $t$ ($1 \le t \le 1000$) — the number of test cases. Next $t$ lines contains test cases — one per line. The first and only line of each test case contains string $s$ ($2 \le |s| \le 2 \cdot 10^5$). Each character $s_i$ is digit 0–9. It's guaranteed that the total length of strings doesn't exceed $2 \cdot 10^5$. -----Output----- For each test case, print the minimum number of characters you need to erase from $s$ to make it good. -----Example----- Input 3 95831 100120013 252525252525 Output 3 5 0 -----Note----- In the first test case, you can erase any $3$ characters, for example, the $1$-st, the $3$-rd, and the $4$-th. You'll get string 51 and it is good. In the second test case, we can erase all characters except 0: the remaining string is 0000 and it's good. In the third test case, the given string $s$ is already good.
T = int(input()) def f(x, y, a): correct = 0 for c in a: if c == x: correct += 1 x, y = y, x if x != y and correct % 2 == 1: correct -= 1 return correct while T: s = input() array = [int(x) for x in s] ans = 0 for x in range(10): for y in range(10): ans = max(ans, f(x, y, array)) print(len(s) - ans) T = T - 1
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_DEF ASSIGN VAR NUMBER FOR VAR VAR IF VAR VAR VAR NUMBER ASSIGN VAR VAR VAR VAR IF VAR VAR BIN_OP VAR NUMBER NUMBER VAR NUMBER RETURN VAR WHILE VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER
Let's call left cyclic shift of some string $t_1 t_2 t_3 \dots t_{n - 1} t_n$ as string $t_2 t_3 \dots t_{n - 1} t_n t_1$. Analogically, let's call right cyclic shift of string $t$ as string $t_n t_1 t_2 t_3 \dots t_{n - 1}$. Let's say string $t$ is good if its left cyclic shift is equal to its right cyclic shift. You are given string $s$ which consists of digits 0–9. What is the minimum number of characters you need to erase from $s$ to make it good? -----Input----- The first line contains single integer $t$ ($1 \le t \le 1000$) — the number of test cases. Next $t$ lines contains test cases — one per line. The first and only line of each test case contains string $s$ ($2 \le |s| \le 2 \cdot 10^5$). Each character $s_i$ is digit 0–9. It's guaranteed that the total length of strings doesn't exceed $2 \cdot 10^5$. -----Output----- For each test case, print the minimum number of characters you need to erase from $s$ to make it good. -----Example----- Input 3 95831 100120013 252525252525 Output 3 5 0 -----Note----- In the first test case, you can erase any $3$ characters, for example, the $1$-st, the $3$-rd, and the $4$-th. You'll get string 51 and it is good. In the second test case, we can erase all characters except 0: the remaining string is 0000 and it's good. In the third test case, the given string $s$ is already good.
t = int(input().split()[0]) for case in range(t): s = input().split()[0] best = len(s) for i in range(10): for j in range(10): if i == j: continue curr = 0 foundFirst = True if s.count(str(i)) == 0: continue l = s.index(str(i)) r = l + 1 while l < len(s): while r < len(s): if foundFirst == False and s[r] == str(i): foundFirst = True if foundFirst == True and s[r] == str(j): curr += 2 foundFirst = False break r += 1 l = r r += 1 if len(s) - curr < best: best = len(s) - curr for i in range(10): if len(s) - s.count(str(i)) < best: best = len(s) - s.count(str(i)) print(best)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER IF VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER IF FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR NUMBER WHILE VAR FUNC_CALL VAR VAR WHILE VAR FUNC_CALL VAR VAR IF VAR NUMBER VAR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER IF VAR NUMBER VAR VAR FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR NUMBER VAR NUMBER ASSIGN VAR VAR VAR NUMBER IF BIN_OP FUNC_CALL VAR VAR VAR VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR VAR FOR VAR FUNC_CALL VAR NUMBER IF BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR
Let's call left cyclic shift of some string $t_1 t_2 t_3 \dots t_{n - 1} t_n$ as string $t_2 t_3 \dots t_{n - 1} t_n t_1$. Analogically, let's call right cyclic shift of string $t$ as string $t_n t_1 t_2 t_3 \dots t_{n - 1}$. Let's say string $t$ is good if its left cyclic shift is equal to its right cyclic shift. You are given string $s$ which consists of digits 0–9. What is the minimum number of characters you need to erase from $s$ to make it good? -----Input----- The first line contains single integer $t$ ($1 \le t \le 1000$) — the number of test cases. Next $t$ lines contains test cases — one per line. The first and only line of each test case contains string $s$ ($2 \le |s| \le 2 \cdot 10^5$). Each character $s_i$ is digit 0–9. It's guaranteed that the total length of strings doesn't exceed $2 \cdot 10^5$. -----Output----- For each test case, print the minimum number of characters you need to erase from $s$ to make it good. -----Example----- Input 3 95831 100120013 252525252525 Output 3 5 0 -----Note----- In the first test case, you can erase any $3$ characters, for example, the $1$-st, the $3$-rd, and the $4$-th. You'll get string 51 and it is good. In the second test case, we can erase all characters except 0: the remaining string is 0000 and it's good. In the third test case, the given string $s$ is already good.
for _ in range(int(input())): s = input() f = {} for i in range(len(s)): if int(s[i]) not in f: f[int(s[i])] = 1 else: f[int(s[i])] += 1 m = max(f.values()) t = len(f) c, a = 0, 0 if m == 1: print(len(s) - 2) elif m == len(s): print(0) else: l = len(s) for i in range(10): for j in range(10): f = 1 for p in range(l): if int(s[p]) == i and f == 1: c += 1 f = 0 elif int(s[p]) == j and f == 0: c += 1 f = 1 if i != j and c % 2 == 1: c -= 1 a = max(a, c) c = 0 print(l - a)
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR DICT FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR NUMBER VAR FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR VAR NUMBER NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER IF VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR VAR VAR VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER IF FUNC_CALL VAR VAR VAR VAR VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER IF VAR VAR BIN_OP VAR NUMBER NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR VAR
Let's call left cyclic shift of some string $t_1 t_2 t_3 \dots t_{n - 1} t_n$ as string $t_2 t_3 \dots t_{n - 1} t_n t_1$. Analogically, let's call right cyclic shift of string $t$ as string $t_n t_1 t_2 t_3 \dots t_{n - 1}$. Let's say string $t$ is good if its left cyclic shift is equal to its right cyclic shift. You are given string $s$ which consists of digits 0–9. What is the minimum number of characters you need to erase from $s$ to make it good? -----Input----- The first line contains single integer $t$ ($1 \le t \le 1000$) — the number of test cases. Next $t$ lines contains test cases — one per line. The first and only line of each test case contains string $s$ ($2 \le |s| \le 2 \cdot 10^5$). Each character $s_i$ is digit 0–9. It's guaranteed that the total length of strings doesn't exceed $2 \cdot 10^5$. -----Output----- For each test case, print the minimum number of characters you need to erase from $s$ to make it good. -----Example----- Input 3 95831 100120013 252525252525 Output 3 5 0 -----Note----- In the first test case, you can erase any $3$ characters, for example, the $1$-st, the $3$-rd, and the $4$-th. You'll get string 51 and it is good. In the second test case, we can erase all characters except 0: the remaining string is 0000 and it's good. In the third test case, the given string $s$ is already good.
for t in range(int(input())): s = input() i = 0 n = len(s) if n <= 2: print(0) continue ans = 2 freq = {} maxF = 1 for c in s: if c not in freq: freq[c] = 1 else: freq[c] += 1 maxF = max(maxF, freq[c]) ans = max(ans, maxF) for i in range(0, 10): for j in range(0, 10): if i != j: x = str(i) y = str(j) val = 0 target = x for k in range(n): if s[k] == target: val += 1 if target == x: target = y else: target = x if val % 2 != 0: val -= 1 ans = max(ans, val) print(n - ans)
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR DICT ASSIGN VAR NUMBER FOR VAR VAR IF VAR VAR ASSIGN VAR VAR NUMBER VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FOR VAR FUNC_CALL VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER NUMBER IF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR NUMBER IF VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR IF BIN_OP VAR NUMBER NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR
Let's call left cyclic shift of some string $t_1 t_2 t_3 \dots t_{n - 1} t_n$ as string $t_2 t_3 \dots t_{n - 1} t_n t_1$. Analogically, let's call right cyclic shift of string $t$ as string $t_n t_1 t_2 t_3 \dots t_{n - 1}$. Let's say string $t$ is good if its left cyclic shift is equal to its right cyclic shift. You are given string $s$ which consists of digits 0–9. What is the minimum number of characters you need to erase from $s$ to make it good? -----Input----- The first line contains single integer $t$ ($1 \le t \le 1000$) — the number of test cases. Next $t$ lines contains test cases — one per line. The first and only line of each test case contains string $s$ ($2 \le |s| \le 2 \cdot 10^5$). Each character $s_i$ is digit 0–9. It's guaranteed that the total length of strings doesn't exceed $2 \cdot 10^5$. -----Output----- For each test case, print the minimum number of characters you need to erase from $s$ to make it good. -----Example----- Input 3 95831 100120013 252525252525 Output 3 5 0 -----Note----- In the first test case, you can erase any $3$ characters, for example, the $1$-st, the $3$-rd, and the $4$-th. You'll get string 51 and it is good. In the second test case, we can erase all characters except 0: the remaining string is 0000 and it's good. In the third test case, the given string $s$ is already good.
from sys import stdin, stdout for query in range(int(stdin.readline())): s = [int(x) for x in list(stdin.readline().strip())] n = len(s) a = [] best = 0 for x in range(10): for y in range(10): if x != y: a.append((x, y)) for x in range(10): count = 0 for y in s: if x == y: count += 1 best = max(best, count) for x in a: pointer = 0 count = 0 for y in s: if y == x[pointer]: count += 1 pointer = (pointer + 1) % 2 if count % 2 == 1: count -= 1 best = max(best, count) stdout.write(str(n - best) + "\n")
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR LIST ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER IF VAR VAR EXPR FUNC_CALL VAR VAR VAR FOR VAR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR IF VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR FOR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR IF VAR VAR VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR NUMBER NUMBER IF BIN_OP VAR NUMBER NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP FUNC_CALL VAR BIN_OP VAR VAR STRING
Let's call left cyclic shift of some string $t_1 t_2 t_3 \dots t_{n - 1} t_n$ as string $t_2 t_3 \dots t_{n - 1} t_n t_1$. Analogically, let's call right cyclic shift of string $t$ as string $t_n t_1 t_2 t_3 \dots t_{n - 1}$. Let's say string $t$ is good if its left cyclic shift is equal to its right cyclic shift. You are given string $s$ which consists of digits 0–9. What is the minimum number of characters you need to erase from $s$ to make it good? -----Input----- The first line contains single integer $t$ ($1 \le t \le 1000$) — the number of test cases. Next $t$ lines contains test cases — one per line. The first and only line of each test case contains string $s$ ($2 \le |s| \le 2 \cdot 10^5$). Each character $s_i$ is digit 0–9. It's guaranteed that the total length of strings doesn't exceed $2 \cdot 10^5$. -----Output----- For each test case, print the minimum number of characters you need to erase from $s$ to make it good. -----Example----- Input 3 95831 100120013 252525252525 Output 3 5 0 -----Note----- In the first test case, you can erase any $3$ characters, for example, the $1$-st, the $3$-rd, and the $4$-th. You'll get string 51 and it is good. In the second test case, we can erase all characters except 0: the remaining string is 0000 and it's good. In the third test case, the given string $s$ is already good.
from sys import stdin t = int(stdin.readline().strip()) for _ in range(t): S = stdin.readline().strip() mindelete = len(S) for i in range(10): for j in range(10): ichar = str(i) jchar = str(j) first = True delete = len(S) for c in S: if c == ichar and first: first = False elif c == jchar and not first: first = True delete -= 2 mindelete = min(mindelete, delete) for i in range(10): ichar = str(i) delete = 0 for c in S: if c != ichar: delete += 1 mindelete = min(mindelete, delete) print(mindelete)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR FOR VAR VAR IF VAR VAR VAR ASSIGN VAR NUMBER IF VAR VAR VAR ASSIGN VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR FOR VAR FUNC_CALL VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR VAR IF VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR
Let's call left cyclic shift of some string $t_1 t_2 t_3 \dots t_{n - 1} t_n$ as string $t_2 t_3 \dots t_{n - 1} t_n t_1$. Analogically, let's call right cyclic shift of string $t$ as string $t_n t_1 t_2 t_3 \dots t_{n - 1}$. Let's say string $t$ is good if its left cyclic shift is equal to its right cyclic shift. You are given string $s$ which consists of digits 0–9. What is the minimum number of characters you need to erase from $s$ to make it good? -----Input----- The first line contains single integer $t$ ($1 \le t \le 1000$) — the number of test cases. Next $t$ lines contains test cases — one per line. The first and only line of each test case contains string $s$ ($2 \le |s| \le 2 \cdot 10^5$). Each character $s_i$ is digit 0–9. It's guaranteed that the total length of strings doesn't exceed $2 \cdot 10^5$. -----Output----- For each test case, print the minimum number of characters you need to erase from $s$ to make it good. -----Example----- Input 3 95831 100120013 252525252525 Output 3 5 0 -----Note----- In the first test case, you can erase any $3$ characters, for example, the $1$-st, the $3$-rd, and the $4$-th. You'll get string 51 and it is good. In the second test case, we can erase all characters except 0: the remaining string is 0000 and it's good. In the third test case, the given string $s$ is already good.
def solve(): s = list(map(int, input())) n = len(s) c = [0] * 10 for i in s: c[i] += 1 ans = max(c) for u in range(10): for v in range(10): if u == v: continue res = [0, 0] for i in range(n): if s[i] == u: res[0] = max(res[0], res[1] + 1) elif s[i] == v: res[1] = max(res[1], res[0] + 1) ans = max(ans, max(res) - max(res) % 2) print(n - ans) t = int(input()) for _ in range(t): solve()
FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER NUMBER FOR VAR VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER IF VAR VAR ASSIGN VAR LIST NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR ASSIGN VAR NUMBER FUNC_CALL VAR VAR NUMBER BIN_OP VAR NUMBER NUMBER IF VAR VAR VAR ASSIGN VAR NUMBER FUNC_CALL VAR VAR NUMBER BIN_OP VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP FUNC_CALL VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR
Let's call left cyclic shift of some string $t_1 t_2 t_3 \dots t_{n - 1} t_n$ as string $t_2 t_3 \dots t_{n - 1} t_n t_1$. Analogically, let's call right cyclic shift of string $t$ as string $t_n t_1 t_2 t_3 \dots t_{n - 1}$. Let's say string $t$ is good if its left cyclic shift is equal to its right cyclic shift. You are given string $s$ which consists of digits 0–9. What is the minimum number of characters you need to erase from $s$ to make it good? -----Input----- The first line contains single integer $t$ ($1 \le t \le 1000$) — the number of test cases. Next $t$ lines contains test cases — one per line. The first and only line of each test case contains string $s$ ($2 \le |s| \le 2 \cdot 10^5$). Each character $s_i$ is digit 0–9. It's guaranteed that the total length of strings doesn't exceed $2 \cdot 10^5$. -----Output----- For each test case, print the minimum number of characters you need to erase from $s$ to make it good. -----Example----- Input 3 95831 100120013 252525252525 Output 3 5 0 -----Note----- In the first test case, you can erase any $3$ characters, for example, the $1$-st, the $3$-rd, and the $4$-th. You'll get string 51 and it is good. In the second test case, we can erase all characters except 0: the remaining string is 0000 and it's good. In the third test case, the given string $s$ is already good.
for _ in range(int(input())): arr = list(map(int, input())) ans = int(10000000000.0) l = len(arr) for i in range(10): for j in range(10): now = i cnt = 0 for k in range(l): if arr[k] == now: if now == i: now = j else: now = i else: cnt += 1 if now == i: ans = min(cnt, ans) print(ans)
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER ASSIGN VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR IF VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR VAR NUMBER IF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR
Let's call left cyclic shift of some string $t_1 t_2 t_3 \dots t_{n - 1} t_n$ as string $t_2 t_3 \dots t_{n - 1} t_n t_1$. Analogically, let's call right cyclic shift of string $t$ as string $t_n t_1 t_2 t_3 \dots t_{n - 1}$. Let's say string $t$ is good if its left cyclic shift is equal to its right cyclic shift. You are given string $s$ which consists of digits 0–9. What is the minimum number of characters you need to erase from $s$ to make it good? -----Input----- The first line contains single integer $t$ ($1 \le t \le 1000$) — the number of test cases. Next $t$ lines contains test cases — one per line. The first and only line of each test case contains string $s$ ($2 \le |s| \le 2 \cdot 10^5$). Each character $s_i$ is digit 0–9. It's guaranteed that the total length of strings doesn't exceed $2 \cdot 10^5$. -----Output----- For each test case, print the minimum number of characters you need to erase from $s$ to make it good. -----Example----- Input 3 95831 100120013 252525252525 Output 3 5 0 -----Note----- In the first test case, you can erase any $3$ characters, for example, the $1$-st, the $3$-rd, and the $4$-th. You'll get string 51 and it is good. In the second test case, we can erase all characters except 0: the remaining string is 0000 and it's good. In the third test case, the given string $s$ is already good.
import sys input = sys.stdin.readline for f in range(int(input())): s = input() s = s[0 : len(s) - 1] costs = [0] * 10 for i in range(10): costs[i] = [0] * 10 isf = [0] * 10 for i in range(10): isf[i] = [True] * 10 for c in s: c = int(c) for i in range(10): for j in range(10): if isf[i][j]: if c == i: isf[i][j] = False else: costs[i][j] += 1 elif c == j: isf[i][j] = True else: costs[i][j] += 1 mincost = 10000000000000000000000000000 for i in range(10): for j in range(10): if not isf[i][j] and i != j: costs[i][j] += 1 mincost = min(mincost, costs[i][j]) print(mincost)
IMPORT ASSIGN VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER ASSIGN VAR VAR BIN_OP LIST NUMBER NUMBER ASSIGN VAR BIN_OP LIST NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER ASSIGN VAR VAR BIN_OP LIST NUMBER NUMBER FOR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER IF VAR VAR VAR IF VAR VAR ASSIGN VAR VAR VAR NUMBER VAR VAR VAR NUMBER IF VAR VAR ASSIGN VAR VAR VAR NUMBER VAR VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER IF VAR VAR VAR VAR VAR VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR
Let's call left cyclic shift of some string $t_1 t_2 t_3 \dots t_{n - 1} t_n$ as string $t_2 t_3 \dots t_{n - 1} t_n t_1$. Analogically, let's call right cyclic shift of string $t$ as string $t_n t_1 t_2 t_3 \dots t_{n - 1}$. Let's say string $t$ is good if its left cyclic shift is equal to its right cyclic shift. You are given string $s$ which consists of digits 0–9. What is the minimum number of characters you need to erase from $s$ to make it good? -----Input----- The first line contains single integer $t$ ($1 \le t \le 1000$) — the number of test cases. Next $t$ lines contains test cases — one per line. The first and only line of each test case contains string $s$ ($2 \le |s| \le 2 \cdot 10^5$). Each character $s_i$ is digit 0–9. It's guaranteed that the total length of strings doesn't exceed $2 \cdot 10^5$. -----Output----- For each test case, print the minimum number of characters you need to erase from $s$ to make it good. -----Example----- Input 3 95831 100120013 252525252525 Output 3 5 0 -----Note----- In the first test case, you can erase any $3$ characters, for example, the $1$-st, the $3$-rd, and the $4$-th. You'll get string 51 and it is good. In the second test case, we can erase all characters except 0: the remaining string is 0000 and it's good. In the third test case, the given string $s$ is already good.
t = int(input()) while t: t -= 1 s = input() if len(list(set(s))) == 1: print("0") else: x = [ "00", "01", "02", "03", "04", "05", "06", "07", "08", "09", "11", "12", "13", "14", "15", "16", "17", "18", "19", "22", "23", "24", "25", "26", "27", "28", "29", "33", "34", "35", "36", "37", "38", "39", "44", "45", "46", "47", "48", "49", "55", "56", "57", "58", "59", "66", "67", "68", "69", "77", "78", "79", "88", "89", "99", ] y = [0] * len(x) z = [0] * len(x) w = [1] * len(x) extra = [0] * 10 ans = [0] * len(x) for i in range(len(s)): extra[int(s[i])] += 1 for j in range(len(x)): if s[i] == x[j][y[j] % 2]: y[j] += 1 if y[j] % 2 == 0: ans[j] += 1 if s[i] == x[j][w[j] % 2]: w[j] += 1 if w[j] % 2 == 1: z[j] += 1 m = max(max(ans), max(z)) if 2 * m >= max(extra): print(len(s) - 2 * m) else: print(len(s) - max(extra))
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR WHILE VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR IF FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR STRING 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 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 STRING STRING STRING ASSIGN VAR BIN_OP LIST NUMBER FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER NUMBER ASSIGN VAR BIN_OP LIST NUMBER FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR BIN_OP VAR VAR NUMBER VAR VAR NUMBER IF BIN_OP VAR VAR NUMBER NUMBER VAR VAR NUMBER IF VAR VAR VAR VAR BIN_OP VAR VAR NUMBER VAR VAR NUMBER IF BIN_OP VAR VAR NUMBER NUMBER VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR IF BIN_OP NUMBER VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR BIN_OP NUMBER VAR EXPR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR VAR
Let's call left cyclic shift of some string $t_1 t_2 t_3 \dots t_{n - 1} t_n$ as string $t_2 t_3 \dots t_{n - 1} t_n t_1$. Analogically, let's call right cyclic shift of string $t$ as string $t_n t_1 t_2 t_3 \dots t_{n - 1}$. Let's say string $t$ is good if its left cyclic shift is equal to its right cyclic shift. You are given string $s$ which consists of digits 0–9. What is the minimum number of characters you need to erase from $s$ to make it good? -----Input----- The first line contains single integer $t$ ($1 \le t \le 1000$) — the number of test cases. Next $t$ lines contains test cases — one per line. The first and only line of each test case contains string $s$ ($2 \le |s| \le 2 \cdot 10^5$). Each character $s_i$ is digit 0–9. It's guaranteed that the total length of strings doesn't exceed $2 \cdot 10^5$. -----Output----- For each test case, print the minimum number of characters you need to erase from $s$ to make it good. -----Example----- Input 3 95831 100120013 252525252525 Output 3 5 0 -----Note----- In the first test case, you can erase any $3$ characters, for example, the $1$-st, the $3$-rd, and the $4$-th. You'll get string 51 and it is good. In the second test case, we can erase all characters except 0: the remaining string is 0000 and it's good. In the third test case, the given string $s$ is already good.
for _ in range(int(input())): ch = str(input()) ans = 0 for i in range(0, 10): ct = 0 for c in ch: if int(c) == i: ct = ct + 1 ans = max(ans, ct) for j in range(0, 10): ct = 0 for c in ch: if ct % 2 == 0: if int(c) == i: ct = ct + 1 elif int(c) == j: ct = ct + 1 if ct % 2: ct = ct - 1 ans = max(ans, ct) print(len(ch) - ans)
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER NUMBER ASSIGN VAR NUMBER FOR VAR VAR IF FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR FOR VAR FUNC_CALL VAR NUMBER NUMBER ASSIGN VAR NUMBER FOR VAR VAR IF BIN_OP VAR NUMBER NUMBER IF FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER IF FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER IF BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR VAR
Let's call left cyclic shift of some string $t_1 t_2 t_3 \dots t_{n - 1} t_n$ as string $t_2 t_3 \dots t_{n - 1} t_n t_1$. Analogically, let's call right cyclic shift of string $t$ as string $t_n t_1 t_2 t_3 \dots t_{n - 1}$. Let's say string $t$ is good if its left cyclic shift is equal to its right cyclic shift. You are given string $s$ which consists of digits 0–9. What is the minimum number of characters you need to erase from $s$ to make it good? -----Input----- The first line contains single integer $t$ ($1 \le t \le 1000$) — the number of test cases. Next $t$ lines contains test cases — one per line. The first and only line of each test case contains string $s$ ($2 \le |s| \le 2 \cdot 10^5$). Each character $s_i$ is digit 0–9. It's guaranteed that the total length of strings doesn't exceed $2 \cdot 10^5$. -----Output----- For each test case, print the minimum number of characters you need to erase from $s$ to make it good. -----Example----- Input 3 95831 100120013 252525252525 Output 3 5 0 -----Note----- In the first test case, you can erase any $3$ characters, for example, the $1$-st, the $3$-rd, and the $4$-th. You'll get string 51 and it is good. In the second test case, we can erase all characters except 0: the remaining string is 0000 and it's good. In the third test case, the given string $s$ is already good.
def check(pat, strr): idx = 0 i = 0 ans = 0 while i < len(strr): if strr[i] == pat[0]: idx = 1 elif strr[i] == pat[1]: if idx > 0: idx -= 1 ans += 2 i += 1 return len(strr) - ans for vishal in range(int(input())): s = input() d = {} for i in s: try: d[i] += 1 except: d[i] = 1 ans = len(s) - 2 ans = min(ans, len(s) - max([d[j] for j in d.keys()])) for i in range(10): for j in range(10): if i == j: continue ans = min(ans, check(str(i) + str(j), s)) print(ans)
FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR FUNC_CALL VAR VAR IF VAR VAR VAR NUMBER ASSIGN VAR NUMBER IF VAR VAR VAR NUMBER IF VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER RETURN BIN_OP FUNC_CALL VAR VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR DICT FOR VAR VAR VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER IF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR
Let's call left cyclic shift of some string $t_1 t_2 t_3 \dots t_{n - 1} t_n$ as string $t_2 t_3 \dots t_{n - 1} t_n t_1$. Analogically, let's call right cyclic shift of string $t$ as string $t_n t_1 t_2 t_3 \dots t_{n - 1}$. Let's say string $t$ is good if its left cyclic shift is equal to its right cyclic shift. You are given string $s$ which consists of digits 0–9. What is the minimum number of characters you need to erase from $s$ to make it good? -----Input----- The first line contains single integer $t$ ($1 \le t \le 1000$) — the number of test cases. Next $t$ lines contains test cases — one per line. The first and only line of each test case contains string $s$ ($2 \le |s| \le 2 \cdot 10^5$). Each character $s_i$ is digit 0–9. It's guaranteed that the total length of strings doesn't exceed $2 \cdot 10^5$. -----Output----- For each test case, print the minimum number of characters you need to erase from $s$ to make it good. -----Example----- Input 3 95831 100120013 252525252525 Output 3 5 0 -----Note----- In the first test case, you can erase any $3$ characters, for example, the $1$-st, the $3$-rd, and the $4$-th. You'll get string 51 and it is good. In the second test case, we can erase all characters except 0: the remaining string is 0000 and it's good. In the third test case, the given string $s$ is already good.
def find(s, a, b): ans = 0 for c in s: if a == c: a, b = b, a ans += 1 if a != b and ans % 2 == 1: ans -= 1 return ans def slove(): s = input() ans = 0 for i in range(0, 10): for j in range(0, 10): ans = max(ans, find(s, str(i), str(j))) print(len(s) - ans) def main(): t = int(input()) while t > 0: slove() t -= 1 main()
FUNC_DEF ASSIGN VAR NUMBER FOR VAR VAR IF VAR VAR ASSIGN VAR VAR VAR VAR VAR NUMBER IF VAR VAR BIN_OP VAR NUMBER NUMBER VAR NUMBER RETURN VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR WHILE VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR
Let's call left cyclic shift of some string $t_1 t_2 t_3 \dots t_{n - 1} t_n$ as string $t_2 t_3 \dots t_{n - 1} t_n t_1$. Analogically, let's call right cyclic shift of string $t$ as string $t_n t_1 t_2 t_3 \dots t_{n - 1}$. Let's say string $t$ is good if its left cyclic shift is equal to its right cyclic shift. You are given string $s$ which consists of digits 0–9. What is the minimum number of characters you need to erase from $s$ to make it good? -----Input----- The first line contains single integer $t$ ($1 \le t \le 1000$) — the number of test cases. Next $t$ lines contains test cases — one per line. The first and only line of each test case contains string $s$ ($2 \le |s| \le 2 \cdot 10^5$). Each character $s_i$ is digit 0–9. It's guaranteed that the total length of strings doesn't exceed $2 \cdot 10^5$. -----Output----- For each test case, print the minimum number of characters you need to erase from $s$ to make it good. -----Example----- Input 3 95831 100120013 252525252525 Output 3 5 0 -----Note----- In the first test case, you can erase any $3$ characters, for example, the $1$-st, the $3$-rd, and the $4$-th. You'll get string 51 and it is good. In the second test case, we can erase all characters except 0: the remaining string is 0000 and it's good. In the third test case, the given string $s$ is already good.
t = int(input()) for _ in range(t): s = list(map(int, input())) best = 0 for d in range(10): c = s.count(d) best = max(best, c) for d1 in range(10): for d2 in range(10): if d1 == d2: continue find1 = True count = 0 for d in s: if find1 and d == d1: count += 1 find1 = not find1 elif not find1 and d == d2: count += 1 find1 = not find1 best = max(best, count - count % 2) print(len(s) - best)
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 VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FOR VAR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER IF VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR IF VAR VAR VAR VAR NUMBER ASSIGN VAR VAR IF VAR VAR VAR VAR NUMBER ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR VAR
Let's call left cyclic shift of some string $t_1 t_2 t_3 \dots t_{n - 1} t_n$ as string $t_2 t_3 \dots t_{n - 1} t_n t_1$. Analogically, let's call right cyclic shift of string $t$ as string $t_n t_1 t_2 t_3 \dots t_{n - 1}$. Let's say string $t$ is good if its left cyclic shift is equal to its right cyclic shift. You are given string $s$ which consists of digits 0–9. What is the minimum number of characters you need to erase from $s$ to make it good? -----Input----- The first line contains single integer $t$ ($1 \le t \le 1000$) — the number of test cases. Next $t$ lines contains test cases — one per line. The first and only line of each test case contains string $s$ ($2 \le |s| \le 2 \cdot 10^5$). Each character $s_i$ is digit 0–9. It's guaranteed that the total length of strings doesn't exceed $2 \cdot 10^5$. -----Output----- For each test case, print the minimum number of characters you need to erase from $s$ to make it good. -----Example----- Input 3 95831 100120013 252525252525 Output 3 5 0 -----Note----- In the first test case, you can erase any $3$ characters, for example, the $1$-st, the $3$-rd, and the $4$-th. You'll get string 51 and it is good. In the second test case, we can erase all characters except 0: the remaining string is 0000 and it's good. In the third test case, the given string $s$ is already good.
def solution(): a = list(map(int, list(input()))) n = len(a) answer = len(a) - 2 counter = [0] * 10 for ai in a: counter[ai] += 1 for i in range(10): answer = min(answer, n - counter[i]) for i in range(10): if counter[i] == 0: continue for j in range(i + 1, 10): if counter[j] == 0: continue cur_ans = 0 last = -1 for k in range(n): if a[k] == i: if last == i: cur_ans += 1 last = i elif a[k] == j: if last == j: cur_ans += 1 last = j else: cur_ans += 1 if (n - cur_ans) % 2 == 1: cur_ans += 1 answer = min(answer, cur_ans) print(answer) t = int(input()) for _ in range(t): solution()
FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER NUMBER FOR VAR VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR VAR FOR VAR FUNC_CALL VAR NUMBER IF VAR VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER IF VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR IF VAR VAR VAR NUMBER ASSIGN VAR VAR IF VAR VAR VAR IF VAR VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER IF BIN_OP BIN_OP VAR VAR NUMBER NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR
Let's call left cyclic shift of some string $t_1 t_2 t_3 \dots t_{n - 1} t_n$ as string $t_2 t_3 \dots t_{n - 1} t_n t_1$. Analogically, let's call right cyclic shift of string $t$ as string $t_n t_1 t_2 t_3 \dots t_{n - 1}$. Let's say string $t$ is good if its left cyclic shift is equal to its right cyclic shift. You are given string $s$ which consists of digits 0–9. What is the minimum number of characters you need to erase from $s$ to make it good? -----Input----- The first line contains single integer $t$ ($1 \le t \le 1000$) — the number of test cases. Next $t$ lines contains test cases — one per line. The first and only line of each test case contains string $s$ ($2 \le |s| \le 2 \cdot 10^5$). Each character $s_i$ is digit 0–9. It's guaranteed that the total length of strings doesn't exceed $2 \cdot 10^5$. -----Output----- For each test case, print the minimum number of characters you need to erase from $s$ to make it good. -----Example----- Input 3 95831 100120013 252525252525 Output 3 5 0 -----Note----- In the first test case, you can erase any $3$ characters, for example, the $1$-st, the $3$-rd, and the $4$-th. You'll get string 51 and it is good. In the second test case, we can erase all characters except 0: the remaining string is 0000 and it's good. In the third test case, the given string $s$ is already good.
import sys input = sys.stdin.readline def rle(lst): ans = [] cnt = 1 ini = lst[0] for i in range(1, len(lst)): if ini == lst[i]: cnt += 1 else: ans.append((ini, cnt)) cnt = 1 ini = lst[i] ans.append((ini, cnt)) return ans t = int(input()) for _ in range(t): s = list(input())[:-1] ans = len(s) for i in range(10): for j in range(i, 10): now = [] for k in range(len(s)): if s[k] == str(i) or s[k] == str(j): now.append(s[k]) if len(now) == 0: pass else: u = rle(now) if len(u) == 1: ans = min(ans, len(s) - len(now)) elif len(u) % 2 == 0: ans = min(ans, len(s) - len(u)) else: ans = min(ans, len(s) - len(u) + 1) print(ans)
IMPORT ASSIGN VAR VAR FUNC_DEF ASSIGN VAR LIST ASSIGN VAR NUMBER ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR IF VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR VAR VAR EXPR FUNC_CALL VAR 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 NUMBER ASSIGN VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR FUNC_CALL VAR VAR VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR IF FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR VAR IF BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR VAR
Let's call left cyclic shift of some string $t_1 t_2 t_3 \dots t_{n - 1} t_n$ as string $t_2 t_3 \dots t_{n - 1} t_n t_1$. Analogically, let's call right cyclic shift of string $t$ as string $t_n t_1 t_2 t_3 \dots t_{n - 1}$. Let's say string $t$ is good if its left cyclic shift is equal to its right cyclic shift. You are given string $s$ which consists of digits 0–9. What is the minimum number of characters you need to erase from $s$ to make it good? -----Input----- The first line contains single integer $t$ ($1 \le t \le 1000$) — the number of test cases. Next $t$ lines contains test cases — one per line. The first and only line of each test case contains string $s$ ($2 \le |s| \le 2 \cdot 10^5$). Each character $s_i$ is digit 0–9. It's guaranteed that the total length of strings doesn't exceed $2 \cdot 10^5$. -----Output----- For each test case, print the minimum number of characters you need to erase from $s$ to make it good. -----Example----- Input 3 95831 100120013 252525252525 Output 3 5 0 -----Note----- In the first test case, you can erase any $3$ characters, for example, the $1$-st, the $3$-rd, and the $4$-th. You'll get string 51 and it is good. In the second test case, we can erase all characters except 0: the remaining string is 0000 and it's good. In the third test case, the given string $s$ is already good.
T = int(input()) for t in range(T): s = input() ans = len(s) for a in range(10): for b in range(10): reqd = [str(a), str(b)] cost = 0 idx = 0 for i in range(len(s)): if s[i] != reqd[idx]: cost += 1 else: idx ^= 1 if idx != 0 and a != b: cost += 1 ans = min(cost, ans) print(ans)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER ASSIGN VAR LIST FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR VAR NUMBER VAR NUMBER IF VAR NUMBER VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR
Let's call left cyclic shift of some string $t_1 t_2 t_3 \dots t_{n - 1} t_n$ as string $t_2 t_3 \dots t_{n - 1} t_n t_1$. Analogically, let's call right cyclic shift of string $t$ as string $t_n t_1 t_2 t_3 \dots t_{n - 1}$. Let's say string $t$ is good if its left cyclic shift is equal to its right cyclic shift. You are given string $s$ which consists of digits 0–9. What is the minimum number of characters you need to erase from $s$ to make it good? -----Input----- The first line contains single integer $t$ ($1 \le t \le 1000$) — the number of test cases. Next $t$ lines contains test cases — one per line. The first and only line of each test case contains string $s$ ($2 \le |s| \le 2 \cdot 10^5$). Each character $s_i$ is digit 0–9. It's guaranteed that the total length of strings doesn't exceed $2 \cdot 10^5$. -----Output----- For each test case, print the minimum number of characters you need to erase from $s$ to make it good. -----Example----- Input 3 95831 100120013 252525252525 Output 3 5 0 -----Note----- In the first test case, you can erase any $3$ characters, for example, the $1$-st, the $3$-rd, and the $4$-th. You'll get string 51 and it is good. In the second test case, we can erase all characters except 0: the remaining string is 0000 and it's good. In the third test case, the given string $s$ is already good.
t = int(input()) def subseq(a, b, s): ans = 0 i = 0 flag = 0 while i < len(s): if flag == 0: if s[i] == a: ans += 1 flag += 1 elif s[i] == b: ans += 1 flag += 1 flag %= 2 i += 1 if ans % 2 == 0: return ans elif a == b: return ans else: return ans - 1 def solve(a): ans = 10**9 for i in range(10): for j in range(10): ans = min(ans, len(a) - subseq(str(i), str(j), a)) print(ans) while t: t -= 1 a = input() solve(a)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR FUNC_CALL VAR VAR IF VAR NUMBER IF VAR VAR VAR VAR NUMBER VAR NUMBER IF VAR VAR VAR VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER IF BIN_OP VAR NUMBER NUMBER RETURN VAR IF VAR VAR RETURN VAR RETURN BIN_OP VAR NUMBER FUNC_DEF ASSIGN VAR BIN_OP NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR WHILE VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR
Let's call left cyclic shift of some string $t_1 t_2 t_3 \dots t_{n - 1} t_n$ as string $t_2 t_3 \dots t_{n - 1} t_n t_1$. Analogically, let's call right cyclic shift of string $t$ as string $t_n t_1 t_2 t_3 \dots t_{n - 1}$. Let's say string $t$ is good if its left cyclic shift is equal to its right cyclic shift. You are given string $s$ which consists of digits 0–9. What is the minimum number of characters you need to erase from $s$ to make it good? -----Input----- The first line contains single integer $t$ ($1 \le t \le 1000$) — the number of test cases. Next $t$ lines contains test cases — one per line. The first and only line of each test case contains string $s$ ($2 \le |s| \le 2 \cdot 10^5$). Each character $s_i$ is digit 0–9. It's guaranteed that the total length of strings doesn't exceed $2 \cdot 10^5$. -----Output----- For each test case, print the minimum number of characters you need to erase from $s$ to make it good. -----Example----- Input 3 95831 100120013 252525252525 Output 3 5 0 -----Note----- In the first test case, you can erase any $3$ characters, for example, the $1$-st, the $3$-rd, and the $4$-th. You'll get string 51 and it is good. In the second test case, we can erase all characters except 0: the remaining string is 0000 and it's good. In the third test case, the given string $s$ is already good.
t = int(input()) for _ in range(t): list1 = list(map(int, input())) max_count = 0 for i in range(10): temp = list1.count(i) if temp > max_count: max_count = temp ans = 0 for i in range(10): for j in range(10): temp1 = i temp2 = j search = i count = 0 for val in list1: if val == search: if search == i: search = j else: search = i count += 1 if count > ans: ans = count print(min(len(list1) - 2 * ans, len(list1) - max_count))
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 VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR IF VAR VAR ASSIGN VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR NUMBER FOR VAR VAR IF VAR VAR IF VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR VAR NUMBER IF VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR BIN_OP NUMBER VAR BIN_OP FUNC_CALL VAR VAR VAR
Let's call left cyclic shift of some string $t_1 t_2 t_3 \dots t_{n - 1} t_n$ as string $t_2 t_3 \dots t_{n - 1} t_n t_1$. Analogically, let's call right cyclic shift of string $t$ as string $t_n t_1 t_2 t_3 \dots t_{n - 1}$. Let's say string $t$ is good if its left cyclic shift is equal to its right cyclic shift. You are given string $s$ which consists of digits 0–9. What is the minimum number of characters you need to erase from $s$ to make it good? -----Input----- The first line contains single integer $t$ ($1 \le t \le 1000$) — the number of test cases. Next $t$ lines contains test cases — one per line. The first and only line of each test case contains string $s$ ($2 \le |s| \le 2 \cdot 10^5$). Each character $s_i$ is digit 0–9. It's guaranteed that the total length of strings doesn't exceed $2 \cdot 10^5$. -----Output----- For each test case, print the minimum number of characters you need to erase from $s$ to make it good. -----Example----- Input 3 95831 100120013 252525252525 Output 3 5 0 -----Note----- In the first test case, you can erase any $3$ characters, for example, the $1$-st, the $3$-rd, and the $4$-th. You'll get string 51 and it is good. In the second test case, we can erase all characters except 0: the remaining string is 0000 and it's good. In the third test case, the given string $s$ is already good.
def solve(stt): leni = len(stt) ls = list(stt) maxi = -1 for i in range(0, 10): for j in range(0, 10): cr = str(i) + str(j) newcr = cr * (leni // 2) if leni % 2 == 1: newcr = newcr + str(i) lss = list(newcr) ans = 0 k1 = 0 k2 = 0 while k1 < leni: if ls[k1] == lss[k2]: k1 += 1 k2 += 1 ans += 1 else: k1 += 1 if i != j and ans % 2 == 1: ans = -1 maxi = max(maxi, ans) return leni - maxi for i in range(int(input())): s = input() print(solve(s))
FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR BIN_OP VAR NUMBER IF BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR IF VAR VAR VAR VAR VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER IF VAR VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR RETURN BIN_OP VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR
Let's call left cyclic shift of some string $t_1 t_2 t_3 \dots t_{n - 1} t_n$ as string $t_2 t_3 \dots t_{n - 1} t_n t_1$. Analogically, let's call right cyclic shift of string $t$ as string $t_n t_1 t_2 t_3 \dots t_{n - 1}$. Let's say string $t$ is good if its left cyclic shift is equal to its right cyclic shift. You are given string $s$ which consists of digits 0–9. What is the minimum number of characters you need to erase from $s$ to make it good? -----Input----- The first line contains single integer $t$ ($1 \le t \le 1000$) — the number of test cases. Next $t$ lines contains test cases — one per line. The first and only line of each test case contains string $s$ ($2 \le |s| \le 2 \cdot 10^5$). Each character $s_i$ is digit 0–9. It's guaranteed that the total length of strings doesn't exceed $2 \cdot 10^5$. -----Output----- For each test case, print the minimum number of characters you need to erase from $s$ to make it good. -----Example----- Input 3 95831 100120013 252525252525 Output 3 5 0 -----Note----- In the first test case, you can erase any $3$ characters, for example, the $1$-st, the $3$-rd, and the $4$-th. You'll get string 51 and it is good. In the second test case, we can erase all characters except 0: the remaining string is 0000 and it's good. In the third test case, the given string $s$ is already good.
def abc(s, x, y): m = 0 i = 0 while i < len(s): if s[i] == x: i += 1 while i < len(s) and s[i] != y: i += 1 if i < len(s): m += 1 i += 1 else: i += 1 return 2 * m t = int(input()) for _ in range(t): s = input() d = {} for i in s: try: d[i] += 1 except: d[i] = 1 ans = max(d.values()) for i in range(0, 10): for j in range(0, 10): ans = max(ans, abc(s, str(i), str(j))) print(len(s) - ans)
FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR NUMBER WHILE VAR FUNC_CALL VAR VAR VAR VAR VAR VAR NUMBER IF VAR FUNC_CALL VAR VAR VAR NUMBER VAR NUMBER VAR NUMBER RETURN BIN_OP NUMBER VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR DICT FOR VAR VAR VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR VAR
Let's call left cyclic shift of some string $t_1 t_2 t_3 \dots t_{n - 1} t_n$ as string $t_2 t_3 \dots t_{n - 1} t_n t_1$. Analogically, let's call right cyclic shift of string $t$ as string $t_n t_1 t_2 t_3 \dots t_{n - 1}$. Let's say string $t$ is good if its left cyclic shift is equal to its right cyclic shift. You are given string $s$ which consists of digits 0–9. What is the minimum number of characters you need to erase from $s$ to make it good? -----Input----- The first line contains single integer $t$ ($1 \le t \le 1000$) — the number of test cases. Next $t$ lines contains test cases — one per line. The first and only line of each test case contains string $s$ ($2 \le |s| \le 2 \cdot 10^5$). Each character $s_i$ is digit 0–9. It's guaranteed that the total length of strings doesn't exceed $2 \cdot 10^5$. -----Output----- For each test case, print the minimum number of characters you need to erase from $s$ to make it good. -----Example----- Input 3 95831 100120013 252525252525 Output 3 5 0 -----Note----- In the first test case, you can erase any $3$ characters, for example, the $1$-st, the $3$-rd, and the $4$-th. You'll get string 51 and it is good. In the second test case, we can erase all characters except 0: the remaining string is 0000 and it's good. In the third test case, the given string $s$ is already good.
digits = list("0123456789") def f(s): p = 0 n = len(s) for i in digits: for j in digits: xx = i m = 0 last = 0 for mm in range(n): if s[mm] == xx: last = mm m += 1 if xx == i: xx = j else: xx = i if s[last] == i and i != j: m -= 1 p = max(m, p) return len(s) - p n = int(input()) for i in range(n): x = input() print(f(x))
ASSIGN VAR FUNC_CALL VAR STRING FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR FOR VAR VAR FOR VAR VAR ASSIGN VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR ASSIGN VAR VAR VAR NUMBER IF VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR IF VAR VAR VAR VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR RETURN BIN_OP FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR
Let's call left cyclic shift of some string $t_1 t_2 t_3 \dots t_{n - 1} t_n$ as string $t_2 t_3 \dots t_{n - 1} t_n t_1$. Analogically, let's call right cyclic shift of string $t$ as string $t_n t_1 t_2 t_3 \dots t_{n - 1}$. Let's say string $t$ is good if its left cyclic shift is equal to its right cyclic shift. You are given string $s$ which consists of digits 0–9. What is the minimum number of characters you need to erase from $s$ to make it good? -----Input----- The first line contains single integer $t$ ($1 \le t \le 1000$) — the number of test cases. Next $t$ lines contains test cases — one per line. The first and only line of each test case contains string $s$ ($2 \le |s| \le 2 \cdot 10^5$). Each character $s_i$ is digit 0–9. It's guaranteed that the total length of strings doesn't exceed $2 \cdot 10^5$. -----Output----- For each test case, print the minimum number of characters you need to erase from $s$ to make it good. -----Example----- Input 3 95831 100120013 252525252525 Output 3 5 0 -----Note----- In the first test case, you can erase any $3$ characters, for example, the $1$-st, the $3$-rd, and the $4$-th. You'll get string 51 and it is good. In the second test case, we can erase all characters except 0: the remaining string is 0000 and it's good. In the third test case, the given string $s$ is already good.
t = int(input()) for _ in range(0, t): s = input() def test(arr, phase): ret = 0 iphase = phase for c in s: if arr[phase] != int(c): ret += 1 else: phase ^= 1 if iphase != phase and arr[0] != arr[1]: ret += 1 return ret ret = 100000000 for i in range(0, 10): for j in range(0, 10): ret = min(ret, test([i, j], 0)) print(ret)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR VAR FOR VAR VAR IF VAR VAR FUNC_CALL VAR VAR VAR NUMBER VAR NUMBER IF VAR VAR VAR NUMBER VAR NUMBER VAR NUMBER RETURN VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR LIST VAR VAR NUMBER EXPR FUNC_CALL VAR VAR
Let's call left cyclic shift of some string $t_1 t_2 t_3 \dots t_{n - 1} t_n$ as string $t_2 t_3 \dots t_{n - 1} t_n t_1$. Analogically, let's call right cyclic shift of string $t$ as string $t_n t_1 t_2 t_3 \dots t_{n - 1}$. Let's say string $t$ is good if its left cyclic shift is equal to its right cyclic shift. You are given string $s$ which consists of digits 0–9. What is the minimum number of characters you need to erase from $s$ to make it good? -----Input----- The first line contains single integer $t$ ($1 \le t \le 1000$) — the number of test cases. Next $t$ lines contains test cases — one per line. The first and only line of each test case contains string $s$ ($2 \le |s| \le 2 \cdot 10^5$). Each character $s_i$ is digit 0–9. It's guaranteed that the total length of strings doesn't exceed $2 \cdot 10^5$. -----Output----- For each test case, print the minimum number of characters you need to erase from $s$ to make it good. -----Example----- Input 3 95831 100120013 252525252525 Output 3 5 0 -----Note----- In the first test case, you can erase any $3$ characters, for example, the $1$-st, the $3$-rd, and the $4$-th. You'll get string 51 and it is good. In the second test case, we can erase all characters except 0: the remaining string is 0000 and it's good. In the third test case, the given string $s$ is already good.
import sys LI = lambda: list(map(int, sys.stdin.readline().strip("\n").split())) MI = lambda: map(int, sys.stdin.readline().strip("\n").split()) SI = lambda: sys.stdin.readline().strip("\n") II = lambda: int(sys.stdin.readline().strip("\n")) for _ in range(II()): s = SI() n = len(s) ans = n - s.count(max("0123456789", key=lambda c: s.count(c))) for i in range(10): for j in range(10): val = 0 p = str(i) for c in s: if c == str(j) and p == str(i) or c == str(i) and p == str(j): val += 1 p = c if val % 2: val -= 1 ans = min(ans, n - val) print(ans)
IMPORT ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR FUNC_CALL FUNC_CALL VAR STRING FOR VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR FUNC_CALL VAR FUNC_CALL VAR STRING FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR FOR VAR VAR IF VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR VAR IF BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR
Let's call left cyclic shift of some string $t_1 t_2 t_3 \dots t_{n - 1} t_n$ as string $t_2 t_3 \dots t_{n - 1} t_n t_1$. Analogically, let's call right cyclic shift of string $t$ as string $t_n t_1 t_2 t_3 \dots t_{n - 1}$. Let's say string $t$ is good if its left cyclic shift is equal to its right cyclic shift. You are given string $s$ which consists of digits 0–9. What is the minimum number of characters you need to erase from $s$ to make it good? -----Input----- The first line contains single integer $t$ ($1 \le t \le 1000$) — the number of test cases. Next $t$ lines contains test cases — one per line. The first and only line of each test case contains string $s$ ($2 \le |s| \le 2 \cdot 10^5$). Each character $s_i$ is digit 0–9. It's guaranteed that the total length of strings doesn't exceed $2 \cdot 10^5$. -----Output----- For each test case, print the minimum number of characters you need to erase from $s$ to make it good. -----Example----- Input 3 95831 100120013 252525252525 Output 3 5 0 -----Note----- In the first test case, you can erase any $3$ characters, for example, the $1$-st, the $3$-rd, and the $4$-th. You'll get string 51 and it is good. In the second test case, we can erase all characters except 0: the remaining string is 0000 and it's good. In the third test case, the given string $s$ is already good.
def good(s): maxx = 0 for i in range(10): for j in range(10): if i != j: first = False pair = 0 for dig in s: if not first and str(i) == dig: first = True elif first and str(j) == dig: pair += 1 first = False maxx = max(maxx, pair) l = [(0) for i in range(10)] for i in s: l[int(i)] += 1 ans = max(2 * maxx, max(l)) return len(s) - ans t = int(input()) for t in range(t): s = input() print(good(s))
FUNC_DEF ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER IF VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR IF VAR FUNC_CALL VAR VAR VAR ASSIGN VAR NUMBER IF VAR FUNC_CALL VAR VAR VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR NUMBER FOR VAR VAR VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP NUMBER VAR FUNC_CALL VAR VAR RETURN BIN_OP FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR
Let's call left cyclic shift of some string $t_1 t_2 t_3 \dots t_{n - 1} t_n$ as string $t_2 t_3 \dots t_{n - 1} t_n t_1$. Analogically, let's call right cyclic shift of string $t$ as string $t_n t_1 t_2 t_3 \dots t_{n - 1}$. Let's say string $t$ is good if its left cyclic shift is equal to its right cyclic shift. You are given string $s$ which consists of digits 0–9. What is the minimum number of characters you need to erase from $s$ to make it good? -----Input----- The first line contains single integer $t$ ($1 \le t \le 1000$) — the number of test cases. Next $t$ lines contains test cases — one per line. The first and only line of each test case contains string $s$ ($2 \le |s| \le 2 \cdot 10^5$). Each character $s_i$ is digit 0–9. It's guaranteed that the total length of strings doesn't exceed $2 \cdot 10^5$. -----Output----- For each test case, print the minimum number of characters you need to erase from $s$ to make it good. -----Example----- Input 3 95831 100120013 252525252525 Output 3 5 0 -----Note----- In the first test case, you can erase any $3$ characters, for example, the $1$-st, the $3$-rd, and the $4$-th. You'll get string 51 and it is good. In the second test case, we can erase all characters except 0: the remaining string is 0000 and it's good. In the third test case, the given string $s$ is already good.
from sys import stdin, stdout def good_string(s): dp = [[(0) for _ in range(10)] for _ in range(10)] cnt = [0] * 10 ans1 = 0 ans2 = 0 for c in s: cv = ord(c) - ord("0") cnt[cv] += 1 ans1 = max(ans1, cnt[cv]) for i in range(10): if cv == i: continue if dp[cv][i] % 2 == 0: dp[cv][i] += 1 if dp[i][cv] % 2 == 1: dp[i][cv] += 1 ans2 = max(dp[i][cv], ans2) return len(s) - max(ans1, ans2) t = int(stdin.readline()) for _ in range(t): s = stdin.readline().strip() ans = good_string(s) stdout.write(str(ans) + "\n")
FUNC_DEF ASSIGN VAR NUMBER VAR FUNC_CALL VAR NUMBER VAR FUNC_CALL VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR STRING VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR FOR VAR FUNC_CALL VAR NUMBER IF VAR VAR IF BIN_OP VAR VAR VAR NUMBER NUMBER VAR VAR VAR NUMBER IF BIN_OP VAR VAR VAR NUMBER NUMBER VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR RETURN BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR STRING
Let's call left cyclic shift of some string $t_1 t_2 t_3 \dots t_{n - 1} t_n$ as string $t_2 t_3 \dots t_{n - 1} t_n t_1$. Analogically, let's call right cyclic shift of string $t$ as string $t_n t_1 t_2 t_3 \dots t_{n - 1}$. Let's say string $t$ is good if its left cyclic shift is equal to its right cyclic shift. You are given string $s$ which consists of digits 0–9. What is the minimum number of characters you need to erase from $s$ to make it good? -----Input----- The first line contains single integer $t$ ($1 \le t \le 1000$) — the number of test cases. Next $t$ lines contains test cases — one per line. The first and only line of each test case contains string $s$ ($2 \le |s| \le 2 \cdot 10^5$). Each character $s_i$ is digit 0–9. It's guaranteed that the total length of strings doesn't exceed $2 \cdot 10^5$. -----Output----- For each test case, print the minimum number of characters you need to erase from $s$ to make it good. -----Example----- Input 3 95831 100120013 252525252525 Output 3 5 0 -----Note----- In the first test case, you can erase any $3$ characters, for example, the $1$-st, the $3$-rd, and the $4$-th. You'll get string 51 and it is good. In the second test case, we can erase all characters except 0: the remaining string is 0000 and it's good. In the third test case, the given string $s$ is already good.
import sys input = sys.stdin.readline ins = lambda: input().rstrip() ini = lambda: int(input().rstrip()) inm = lambda: map(int, input().rstrip().split()) inl = lambda: list(map(int, input().split())) out = lambda x, s="\n": print(s.join(map(str, x))) output = [] t = ini() for _ in range(t): s = ins() ans = 0 for i in range(10): for j in range(10): k = 1 count = 0 for n in s: if k == 1: if n == str(i): count += 1 k = -k elif n == str(j): count += 1 k = -k if i == j: ans = max(ans, count) else: count += -1 if count % 2 else 0 ans = max(ans, count) output.append(len(s) - ans) out(output)
IMPORT ASSIGN VAR VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR STRING FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR ASSIGN VAR LIST ASSIGN VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR IF VAR NUMBER IF VAR FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR VAR IF VAR FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR VAR IF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR
Let's call left cyclic shift of some string $t_1 t_2 t_3 \dots t_{n - 1} t_n$ as string $t_2 t_3 \dots t_{n - 1} t_n t_1$. Analogically, let's call right cyclic shift of string $t$ as string $t_n t_1 t_2 t_3 \dots t_{n - 1}$. Let's say string $t$ is good if its left cyclic shift is equal to its right cyclic shift. You are given string $s$ which consists of digits 0–9. What is the minimum number of characters you need to erase from $s$ to make it good? -----Input----- The first line contains single integer $t$ ($1 \le t \le 1000$) — the number of test cases. Next $t$ lines contains test cases — one per line. The first and only line of each test case contains string $s$ ($2 \le |s| \le 2 \cdot 10^5$). Each character $s_i$ is digit 0–9. It's guaranteed that the total length of strings doesn't exceed $2 \cdot 10^5$. -----Output----- For each test case, print the minimum number of characters you need to erase from $s$ to make it good. -----Example----- Input 3 95831 100120013 252525252525 Output 3 5 0 -----Note----- In the first test case, you can erase any $3$ characters, for example, the $1$-st, the $3$-rd, and the $4$-th. You'll get string 51 and it is good. In the second test case, we can erase all characters except 0: the remaining string is 0000 and it's good. In the third test case, the given string $s$ is already good.
from sys import gettrace, stdin if gettrace(): inputi = input else: def input(): return next(stdin)[:-1] def inputi(): return stdin.buffer.readline() def solve(): ss = list(map(int, list(input()))) best = 0 for i in range(10): for j in range(10): next = i count = 0 for s in ss: if s == next: next = i if next == j else j count += 1 if next == j and i != j: count -= 1 best = max(best, count) print(len(ss) - best) def main(): t = int(inputi()) for _ in range(t): solve() main()
IF FUNC_CALL VAR ASSIGN VAR VAR FUNC_DEF RETURN FUNC_CALL VAR VAR NUMBER FUNC_DEF RETURN FUNC_CALL VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER ASSIGN VAR VAR ASSIGN VAR NUMBER FOR VAR VAR IF VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR NUMBER IF VAR VAR VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP FUNC_CALL VAR 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
Let's call left cyclic shift of some string $t_1 t_2 t_3 \dots t_{n - 1} t_n$ as string $t_2 t_3 \dots t_{n - 1} t_n t_1$. Analogically, let's call right cyclic shift of string $t$ as string $t_n t_1 t_2 t_3 \dots t_{n - 1}$. Let's say string $t$ is good if its left cyclic shift is equal to its right cyclic shift. You are given string $s$ which consists of digits 0–9. What is the minimum number of characters you need to erase from $s$ to make it good? -----Input----- The first line contains single integer $t$ ($1 \le t \le 1000$) — the number of test cases. Next $t$ lines contains test cases — one per line. The first and only line of each test case contains string $s$ ($2 \le |s| \le 2 \cdot 10^5$). Each character $s_i$ is digit 0–9. It's guaranteed that the total length of strings doesn't exceed $2 \cdot 10^5$. -----Output----- For each test case, print the minimum number of characters you need to erase from $s$ to make it good. -----Example----- Input 3 95831 100120013 252525252525 Output 3 5 0 -----Note----- In the first test case, you can erase any $3$ characters, for example, the $1$-st, the $3$-rd, and the $4$-th. You'll get string 51 and it is good. In the second test case, we can erase all characters except 0: the remaining string is 0000 and it's good. In the third test case, the given string $s$ is already good.
case = int(input()) for _ in range(case): n = input() ln = len(n) ans = 1000000 for i in range(10): tmp = n.count(str(i)) tmp = ln - tmp if tmp < ans: ans = tmp for i in range(10): for j in range(10): if i == j: continue switch = 0 tmp = 0 for k in range(ln): if switch == 0: if n[k] == str(i): switch = 1 if switch == 1: if n[k] == str(j): switch = 0 tmp += 2 tmp = ln - tmp if tmp < ans: ans = tmp print(ans)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR VAR IF VAR VAR ASSIGN VAR VAR FOR VAR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER IF VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR NUMBER IF VAR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER IF VAR NUMBER IF VAR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP VAR VAR IF VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR
Let's call left cyclic shift of some string $t_1 t_2 t_3 \dots t_{n - 1} t_n$ as string $t_2 t_3 \dots t_{n - 1} t_n t_1$. Analogically, let's call right cyclic shift of string $t$ as string $t_n t_1 t_2 t_3 \dots t_{n - 1}$. Let's say string $t$ is good if its left cyclic shift is equal to its right cyclic shift. You are given string $s$ which consists of digits 0–9. What is the minimum number of characters you need to erase from $s$ to make it good? -----Input----- The first line contains single integer $t$ ($1 \le t \le 1000$) — the number of test cases. Next $t$ lines contains test cases — one per line. The first and only line of each test case contains string $s$ ($2 \le |s| \le 2 \cdot 10^5$). Each character $s_i$ is digit 0–9. It's guaranteed that the total length of strings doesn't exceed $2 \cdot 10^5$. -----Output----- For each test case, print the minimum number of characters you need to erase from $s$ to make it good. -----Example----- Input 3 95831 100120013 252525252525 Output 3 5 0 -----Note----- In the first test case, you can erase any $3$ characters, for example, the $1$-st, the $3$-rd, and the $4$-th. You'll get string 51 and it is good. In the second test case, we can erase all characters except 0: the remaining string is 0000 and it's good. In the third test case, the given string $s$ is already good.
def find(first, second, s): total = 0 last = -1 for i in range(len(s)): if s[i] == first: if last == -1: last = first total += 1 elif last == second: total += 1 last = first elif s[i] == second: if last == first: last = second total += 1 if total % 2 != 0: total -= 1 return total def solve(s, ans): max_len = 0 for i in range(10): x = s.count(i) max_len = max(max_len, x) for i in range(10): for j in range(10): if i != j: max_len = max(max_len, find(i, j, s)) ans.append(str(len(s) - max_len)) def main(): t = int(input()) ans = [] for i in range(t): s = list(map(int, input())) solve(s, ans) print("\n".join(ans)) main()
FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR VAR IF VAR NUMBER ASSIGN VAR VAR VAR NUMBER IF VAR VAR VAR NUMBER ASSIGN VAR VAR IF VAR VAR VAR IF VAR VAR ASSIGN VAR VAR VAR NUMBER IF BIN_OP VAR NUMBER NUMBER VAR NUMBER RETURN VAR FUNC_DEF ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FOR VAR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER IF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL STRING VAR EXPR FUNC_CALL VAR
This is the easy version of the problem. The only difference is the constraints on $n$ and $k$. You can make hacks only if all versions of the problem are solved. You have a string $s$, and you can do two types of operations on it: Delete the last character of the string. Duplicate the string: $s:=s+s$, where $+$ denotes concatenation. You can use each operation any number of times (possibly none). Your task is to find the lexicographically smallest string of length exactly $k$ that can be obtained by doing these operations on string $s$. A string $a$ is lexicographically smaller than a string $b$ if and only if one of the following holds: $a$ is a prefix of $b$, but $a\ne b$; In the first position where $a$ and $b$ differ, the string $a$ has a letter that appears earlier in the alphabet than the corresponding letter in $b$. -----Input----- The first line contains two integers $n$, $k$ ($1 \leq n, k \leq 5000$) — the length of the original string $s$ and the length of the desired string. The second line contains the string $s$, consisting of $n$ lowercase English letters. -----Output----- Print the lexicographically smallest string of length $k$ that can be obtained by doing the operations on string $s$. -----Examples----- Input 8 16 dbcadabc Output dbcadabcdbcadabc Input 4 5 abcd Output aaaaa -----Note----- In the first test, it is optimal to make one duplication: "dbcadabc" $\to$ "dbcadabcdbcadabc". In the second test it is optimal to delete the last $3$ characters, then duplicate the string $3$ times, then delete the last $3$ characters to make the string have length $k$. "abcd" $\to$ "abc" $\to$ "ab" $\to$ "a" $\to$ "aa" $\to$ "aaaa" $\to$ "aaaaaaaa" $\to$ "aaaaaaa" $\to$ "aaaaaa" $\to$ "aaaaa".
n, m = map(int, input().split()) s = input() now = len(s) for _ in range(1, n): if s < s[_:] or s.startswith(s[_:]): now = _ break i = 0 while m != 0: print(s[i], end="") i = i + 1 m = m - 1 if i == now: i = 0
ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR VAR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR NUMBER WHILE VAR NUMBER EXPR FUNC_CALL VAR VAR VAR STRING ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER IF VAR VAR ASSIGN VAR NUMBER
This is the easy version of the problem. The only difference is the constraints on $n$ and $k$. You can make hacks only if all versions of the problem are solved. You have a string $s$, and you can do two types of operations on it: Delete the last character of the string. Duplicate the string: $s:=s+s$, where $+$ denotes concatenation. You can use each operation any number of times (possibly none). Your task is to find the lexicographically smallest string of length exactly $k$ that can be obtained by doing these operations on string $s$. A string $a$ is lexicographically smaller than a string $b$ if and only if one of the following holds: $a$ is a prefix of $b$, but $a\ne b$; In the first position where $a$ and $b$ differ, the string $a$ has a letter that appears earlier in the alphabet than the corresponding letter in $b$. -----Input----- The first line contains two integers $n$, $k$ ($1 \leq n, k \leq 5000$) — the length of the original string $s$ and the length of the desired string. The second line contains the string $s$, consisting of $n$ lowercase English letters. -----Output----- Print the lexicographically smallest string of length $k$ that can be obtained by doing the operations on string $s$. -----Examples----- Input 8 16 dbcadabc Output dbcadabcdbcadabc Input 4 5 abcd Output aaaaa -----Note----- In the first test, it is optimal to make one duplication: "dbcadabc" $\to$ "dbcadabcdbcadabc". In the second test it is optimal to delete the last $3$ characters, then duplicate the string $3$ times, then delete the last $3$ characters to make the string have length $k$. "abcd" $\to$ "abc" $\to$ "ab" $\to$ "a" $\to$ "aa" $\to$ "aaaa" $\to$ "aaaaaaaa" $\to$ "aaaaaaa" $\to$ "aaaaaa" $\to$ "aaaaa".
def solve(N, K, A): tok_size = 1 ti = 0 i = 1 while i < N: if A[ti] > A[i]: ti = 0 tok_size = i + 1 elif A[ti] == A[i]: ti = (ti + 1) % tok_size else: break i += 1 tok = A[:tok_size] if len(tok) >= K: return tok[:K] else: d = K // len(tok) m = K % len(tok) return tok * d + tok[:m] N, K = tuple(map(int, input().split())) A = input() print(solve(N, K, A))
FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR IF VAR VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER IF VAR VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR NUMBER VAR VAR NUMBER ASSIGN VAR VAR VAR IF FUNC_CALL VAR VAR VAR RETURN VAR VAR ASSIGN VAR BIN_OP VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR FUNC_CALL VAR VAR RETURN BIN_OP BIN_OP VAR VAR VAR VAR ASSIGN VAR VAR FUNC_CALL 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
This is the easy version of the problem. The only difference is the constraints on $n$ and $k$. You can make hacks only if all versions of the problem are solved. You have a string $s$, and you can do two types of operations on it: Delete the last character of the string. Duplicate the string: $s:=s+s$, where $+$ denotes concatenation. You can use each operation any number of times (possibly none). Your task is to find the lexicographically smallest string of length exactly $k$ that can be obtained by doing these operations on string $s$. A string $a$ is lexicographically smaller than a string $b$ if and only if one of the following holds: $a$ is a prefix of $b$, but $a\ne b$; In the first position where $a$ and $b$ differ, the string $a$ has a letter that appears earlier in the alphabet than the corresponding letter in $b$. -----Input----- The first line contains two integers $n$, $k$ ($1 \leq n, k \leq 5000$) — the length of the original string $s$ and the length of the desired string. The second line contains the string $s$, consisting of $n$ lowercase English letters. -----Output----- Print the lexicographically smallest string of length $k$ that can be obtained by doing the operations on string $s$. -----Examples----- Input 8 16 dbcadabc Output dbcadabcdbcadabc Input 4 5 abcd Output aaaaa -----Note----- In the first test, it is optimal to make one duplication: "dbcadabc" $\to$ "dbcadabcdbcadabc". In the second test it is optimal to delete the last $3$ characters, then duplicate the string $3$ times, then delete the last $3$ characters to make the string have length $k$. "abcd" $\to$ "abc" $\to$ "ab" $\to$ "a" $\to$ "aa" $\to$ "aaaa" $\to$ "aaaaaaaa" $\to$ "aaaaaaa" $\to$ "aaaaaa" $\to$ "aaaaa".
n, k = map(int, input().split()) s = input() x = "" for i in range(n): if i == 0: x += s[i] r = k // n + 1 q = (s * r)[:k] else: r = k // len(x) + 1 p = (x * r)[:k] if p < q: break x += s[i] r = k // len(x) + 1 print((x * r)[:k])
ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR STRING FOR VAR FUNC_CALL VAR VAR IF VAR NUMBER VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR VAR IF VAR VAR VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR VAR VAR
This is the easy version of the problem. The only difference is the constraints on $n$ and $k$. You can make hacks only if all versions of the problem are solved. You have a string $s$, and you can do two types of operations on it: Delete the last character of the string. Duplicate the string: $s:=s+s$, where $+$ denotes concatenation. You can use each operation any number of times (possibly none). Your task is to find the lexicographically smallest string of length exactly $k$ that can be obtained by doing these operations on string $s$. A string $a$ is lexicographically smaller than a string $b$ if and only if one of the following holds: $a$ is a prefix of $b$, but $a\ne b$; In the first position where $a$ and $b$ differ, the string $a$ has a letter that appears earlier in the alphabet than the corresponding letter in $b$. -----Input----- The first line contains two integers $n$, $k$ ($1 \leq n, k \leq 5000$) — the length of the original string $s$ and the length of the desired string. The second line contains the string $s$, consisting of $n$ lowercase English letters. -----Output----- Print the lexicographically smallest string of length $k$ that can be obtained by doing the operations on string $s$. -----Examples----- Input 8 16 dbcadabc Output dbcadabcdbcadabc Input 4 5 abcd Output aaaaa -----Note----- In the first test, it is optimal to make one duplication: "dbcadabc" $\to$ "dbcadabcdbcadabc". In the second test it is optimal to delete the last $3$ characters, then duplicate the string $3$ times, then delete the last $3$ characters to make the string have length $k$. "abcd" $\to$ "abc" $\to$ "ab" $\to$ "a" $\to$ "aa" $\to$ "aaaa" $\to$ "aaaaaaaa" $\to$ "aaaaaaa" $\to$ "aaaaaa" $\to$ "aaaaa".
n, k = map(int, input().split()) s = input() ans = "" for i in range(n): st = s[: i + 1] while len(st) <= k: st += st if ans == "": ans = st[:k] else: ans = min(ans, st[:k]) print(ans)
ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR STRING FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR BIN_OP VAR NUMBER WHILE FUNC_CALL VAR VAR VAR VAR VAR IF VAR STRING ASSIGN VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR
This is the easy version of the problem. The only difference is the constraints on $n$ and $k$. You can make hacks only if all versions of the problem are solved. You have a string $s$, and you can do two types of operations on it: Delete the last character of the string. Duplicate the string: $s:=s+s$, where $+$ denotes concatenation. You can use each operation any number of times (possibly none). Your task is to find the lexicographically smallest string of length exactly $k$ that can be obtained by doing these operations on string $s$. A string $a$ is lexicographically smaller than a string $b$ if and only if one of the following holds: $a$ is a prefix of $b$, but $a\ne b$; In the first position where $a$ and $b$ differ, the string $a$ has a letter that appears earlier in the alphabet than the corresponding letter in $b$. -----Input----- The first line contains two integers $n$, $k$ ($1 \leq n, k \leq 5000$) — the length of the original string $s$ and the length of the desired string. The second line contains the string $s$, consisting of $n$ lowercase English letters. -----Output----- Print the lexicographically smallest string of length $k$ that can be obtained by doing the operations on string $s$. -----Examples----- Input 8 16 dbcadabc Output dbcadabcdbcadabc Input 4 5 abcd Output aaaaa -----Note----- In the first test, it is optimal to make one duplication: "dbcadabc" $\to$ "dbcadabcdbcadabc". In the second test it is optimal to delete the last $3$ characters, then duplicate the string $3$ times, then delete the last $3$ characters to make the string have length $k$. "abcd" $\to$ "abc" $\to$ "ab" $\to$ "a" $\to$ "aa" $\to$ "aaaa" $\to$ "aaaaaaaa" $\to$ "aaaaaaa" $\to$ "aaaaaa" $\to$ "aaaaa".
n, k = map(int, input().split()) s = input() unit = s[0] while True: i = 0 test = s + unit while i < len(s) and unit == test[i : i + len(unit)]: i += len(unit) while i < len(s) and unit > test[i : i + len(unit)]: i += 1 if i == len(unit): break unit = test[:i] print(unit * (k // len(unit)) + unit[: k % len(unit)])
ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR VAR NUMBER WHILE NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR VAR WHILE VAR FUNC_CALL VAR VAR VAR VAR VAR BIN_OP VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR WHILE VAR FUNC_CALL VAR VAR VAR VAR VAR BIN_OP VAR FUNC_CALL VAR VAR VAR NUMBER IF VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP BIN_OP VAR BIN_OP VAR FUNC_CALL VAR VAR VAR BIN_OP VAR FUNC_CALL VAR VAR
This is the easy version of the problem. The only difference is the constraints on $n$ and $k$. You can make hacks only if all versions of the problem are solved. You have a string $s$, and you can do two types of operations on it: Delete the last character of the string. Duplicate the string: $s:=s+s$, where $+$ denotes concatenation. You can use each operation any number of times (possibly none). Your task is to find the lexicographically smallest string of length exactly $k$ that can be obtained by doing these operations on string $s$. A string $a$ is lexicographically smaller than a string $b$ if and only if one of the following holds: $a$ is a prefix of $b$, but $a\ne b$; In the first position where $a$ and $b$ differ, the string $a$ has a letter that appears earlier in the alphabet than the corresponding letter in $b$. -----Input----- The first line contains two integers $n$, $k$ ($1 \leq n, k \leq 5000$) — the length of the original string $s$ and the length of the desired string. The second line contains the string $s$, consisting of $n$ lowercase English letters. -----Output----- Print the lexicographically smallest string of length $k$ that can be obtained by doing the operations on string $s$. -----Examples----- Input 8 16 dbcadabc Output dbcadabcdbcadabc Input 4 5 abcd Output aaaaa -----Note----- In the first test, it is optimal to make one duplication: "dbcadabc" $\to$ "dbcadabcdbcadabc". In the second test it is optimal to delete the last $3$ characters, then duplicate the string $3$ times, then delete the last $3$ characters to make the string have length $k$. "abcd" $\to$ "abc" $\to$ "ab" $\to$ "a" $\to$ "aa" $\to$ "aaaa" $\to$ "aaaaaaaa" $\to$ "aaaaaaa" $\to$ "aaaaaa" $\to$ "aaaaa".
def readline(): return map(int, input().split()) def main(): n, k = readline() s = list(map(ord, input())) if len(s) > k: s = s[:k] prefix = 0 i = 0 for i in range(1, len(s)): if s[i] > s[prefix]: s = s[:i] break elif s[i] == s[prefix]: prefix += 1 else: prefix = 0 if prefix: s = s[:-prefix] s *= (k - 1) // len(s) + 1 print("".join(map(chr, s[:k]))) main()
FUNC_DEF RETURN FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FUNC_DEF ASSIGN VAR VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL VAR IF FUNC_CALL VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR IF VAR VAR VAR VAR ASSIGN VAR VAR VAR IF VAR VAR VAR VAR VAR NUMBER ASSIGN VAR NUMBER IF VAR ASSIGN VAR VAR VAR VAR BIN_OP BIN_OP BIN_OP VAR NUMBER FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL STRING FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR
This is the easy version of the problem. The only difference is the constraints on $n$ and $k$. You can make hacks only if all versions of the problem are solved. You have a string $s$, and you can do two types of operations on it: Delete the last character of the string. Duplicate the string: $s:=s+s$, where $+$ denotes concatenation. You can use each operation any number of times (possibly none). Your task is to find the lexicographically smallest string of length exactly $k$ that can be obtained by doing these operations on string $s$. A string $a$ is lexicographically smaller than a string $b$ if and only if one of the following holds: $a$ is a prefix of $b$, but $a\ne b$; In the first position where $a$ and $b$ differ, the string $a$ has a letter that appears earlier in the alphabet than the corresponding letter in $b$. -----Input----- The first line contains two integers $n$, $k$ ($1 \leq n, k \leq 5000$) — the length of the original string $s$ and the length of the desired string. The second line contains the string $s$, consisting of $n$ lowercase English letters. -----Output----- Print the lexicographically smallest string of length $k$ that can be obtained by doing the operations on string $s$. -----Examples----- Input 8 16 dbcadabc Output dbcadabcdbcadabc Input 4 5 abcd Output aaaaa -----Note----- In the first test, it is optimal to make one duplication: "dbcadabc" $\to$ "dbcadabcdbcadabc". In the second test it is optimal to delete the last $3$ characters, then duplicate the string $3$ times, then delete the last $3$ characters to make the string have length $k$. "abcd" $\to$ "abc" $\to$ "ab" $\to$ "a" $\to$ "aa" $\to$ "aaaa" $\to$ "aaaaaaaa" $\to$ "aaaaaaa" $\to$ "aaaaaa" $\to$ "aaaaa".
def findAns(s, k): res = "z" * k for i in range(1, len(s) + 1): u = s[:i] while len(u) < k: u = u + u res = min(res, u[:k]) return res n, k = [int(x) for x in input().split(" ")] s = str(input()) print(findAns(s, k))
FUNC_DEF ASSIGN VAR BIN_OP STRING VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR VAR WHILE FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR RETURN VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR
This is the easy version of the problem. The only difference is the constraints on $n$ and $k$. You can make hacks only if all versions of the problem are solved. You have a string $s$, and you can do two types of operations on it: Delete the last character of the string. Duplicate the string: $s:=s+s$, where $+$ denotes concatenation. You can use each operation any number of times (possibly none). Your task is to find the lexicographically smallest string of length exactly $k$ that can be obtained by doing these operations on string $s$. A string $a$ is lexicographically smaller than a string $b$ if and only if one of the following holds: $a$ is a prefix of $b$, but $a\ne b$; In the first position where $a$ and $b$ differ, the string $a$ has a letter that appears earlier in the alphabet than the corresponding letter in $b$. -----Input----- The first line contains two integers $n$, $k$ ($1 \leq n, k \leq 5000$) — the length of the original string $s$ and the length of the desired string. The second line contains the string $s$, consisting of $n$ lowercase English letters. -----Output----- Print the lexicographically smallest string of length $k$ that can be obtained by doing the operations on string $s$. -----Examples----- Input 8 16 dbcadabc Output dbcadabcdbcadabc Input 4 5 abcd Output aaaaa -----Note----- In the first test, it is optimal to make one duplication: "dbcadabc" $\to$ "dbcadabcdbcadabc". In the second test it is optimal to delete the last $3$ characters, then duplicate the string $3$ times, then delete the last $3$ characters to make the string have length $k$. "abcd" $\to$ "abc" $\to$ "ab" $\to$ "a" $\to$ "aa" $\to$ "aaaa" $\to$ "aaaaaaaa" $\to$ "aaaaaaa" $\to$ "aaaaaa" $\to$ "aaaaa".
n, k = map(int, input().split()) s = input() ans = n for i in range(1, n): if ord(s[i]) < ord(s[0]): continue j = 0 x = i while i < n: if s[i] == s[j]: i += 1 j += 1 else: break if i == n: ans = x break if ord(s[j]) < ord(s[i]): ans = x break ans = s[:ans] m = len(ans) while m < k: m += m ans += ans print(ans[:k])
ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR VAR FOR VAR FUNC_CALL VAR NUMBER VAR IF FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR WHILE VAR VAR IF VAR VAR VAR VAR VAR NUMBER VAR NUMBER IF VAR VAR ASSIGN VAR VAR IF FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR WHILE VAR VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR
This is the easy version of the problem. The only difference is the constraints on $n$ and $k$. You can make hacks only if all versions of the problem are solved. You have a string $s$, and you can do two types of operations on it: Delete the last character of the string. Duplicate the string: $s:=s+s$, where $+$ denotes concatenation. You can use each operation any number of times (possibly none). Your task is to find the lexicographically smallest string of length exactly $k$ that can be obtained by doing these operations on string $s$. A string $a$ is lexicographically smaller than a string $b$ if and only if one of the following holds: $a$ is a prefix of $b$, but $a\ne b$; In the first position where $a$ and $b$ differ, the string $a$ has a letter that appears earlier in the alphabet than the corresponding letter in $b$. -----Input----- The first line contains two integers $n$, $k$ ($1 \leq n, k \leq 5000$) — the length of the original string $s$ and the length of the desired string. The second line contains the string $s$, consisting of $n$ lowercase English letters. -----Output----- Print the lexicographically smallest string of length $k$ that can be obtained by doing the operations on string $s$. -----Examples----- Input 8 16 dbcadabc Output dbcadabcdbcadabc Input 4 5 abcd Output aaaaa -----Note----- In the first test, it is optimal to make one duplication: "dbcadabc" $\to$ "dbcadabcdbcadabc". In the second test it is optimal to delete the last $3$ characters, then duplicate the string $3$ times, then delete the last $3$ characters to make the string have length $k$. "abcd" $\to$ "abc" $\to$ "ab" $\to$ "a" $\to$ "aa" $\to$ "aaaa" $\to$ "aaaaaaaa" $\to$ "aaaaaaa" $\to$ "aaaaaa" $\to$ "aaaaa".
def asmaller(a, b): if len(a) == len(b): return a < b if len(a) > len(b): return a < b + (len(a) - len(b)) * b[-1] n, k = map(int, input().split()) s = input() pref = s ans = "" for i in range(1, n): if ord(s[i]) > ord(s[0]): pref = s[:i] break elif ord(s[i]) == ord(s[0]): if asmaller(s[:i], s[i : 2 * i]): pref = s[:i] break elif s[:i] == s[i : 2 * i]: if 2 * i < n and asmaller(s[:i], s[2 * i :]): pref = s[:i] break t = len(pref) for i in range(k // t + 1): ans += pref c = len(ans) for j in range(c - k): ans = ans[:-1] print(ans)
FUNC_DEF IF FUNC_CALL VAR VAR FUNC_CALL VAR VAR RETURN VAR VAR IF FUNC_CALL VAR VAR FUNC_CALL VAR VAR RETURN VAR BIN_OP VAR BIN_OP BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR VAR ASSIGN VAR STRING FOR VAR FUNC_CALL VAR NUMBER VAR IF FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR VAR IF FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR NUMBER IF FUNC_CALL VAR VAR VAR VAR VAR BIN_OP NUMBER VAR ASSIGN VAR VAR VAR IF VAR VAR VAR VAR BIN_OP NUMBER VAR IF BIN_OP NUMBER VAR VAR FUNC_CALL VAR VAR VAR VAR BIN_OP NUMBER VAR ASSIGN VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR NUMBER VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR VAR ASSIGN VAR VAR NUMBER EXPR FUNC_CALL VAR VAR
This is the easy version of the problem. The only difference is the constraints on $n$ and $k$. You can make hacks only if all versions of the problem are solved. You have a string $s$, and you can do two types of operations on it: Delete the last character of the string. Duplicate the string: $s:=s+s$, where $+$ denotes concatenation. You can use each operation any number of times (possibly none). Your task is to find the lexicographically smallest string of length exactly $k$ that can be obtained by doing these operations on string $s$. A string $a$ is lexicographically smaller than a string $b$ if and only if one of the following holds: $a$ is a prefix of $b$, but $a\ne b$; In the first position where $a$ and $b$ differ, the string $a$ has a letter that appears earlier in the alphabet than the corresponding letter in $b$. -----Input----- The first line contains two integers $n$, $k$ ($1 \leq n, k \leq 5000$) — the length of the original string $s$ and the length of the desired string. The second line contains the string $s$, consisting of $n$ lowercase English letters. -----Output----- Print the lexicographically smallest string of length $k$ that can be obtained by doing the operations on string $s$. -----Examples----- Input 8 16 dbcadabc Output dbcadabcdbcadabc Input 4 5 abcd Output aaaaa -----Note----- In the first test, it is optimal to make one duplication: "dbcadabc" $\to$ "dbcadabcdbcadabc". In the second test it is optimal to delete the last $3$ characters, then duplicate the string $3$ times, then delete the last $3$ characters to make the string have length $k$. "abcd" $\to$ "abc" $\to$ "ab" $\to$ "a" $\to$ "aa" $\to$ "aaaa" $\to$ "aaaaaaaa" $\to$ "aaaaaaa" $\to$ "aaaaaa" $\to$ "aaaaa".
import sys n, q = map(int, sys.stdin.readline().split()) string = sys.stdin.readline().rstrip() p = 0 i = 1 while i < len(string): if string[i] < string[p]: i += 1 p = 0 elif string[i] == string[p]: i += 1 p += 1 else: break st = string[: i - p] l = len(st) answer = st * (q // l) answer += st[: q % l] print(answer)
IMPORT ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR VAR NUMBER ASSIGN VAR NUMBER IF VAR VAR VAR VAR VAR NUMBER VAR NUMBER ASSIGN VAR VAR BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR BIN_OP VAR VAR VAR VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR
This is the easy version of the problem. The only difference is the constraints on $n$ and $k$. You can make hacks only if all versions of the problem are solved. You have a string $s$, and you can do two types of operations on it: Delete the last character of the string. Duplicate the string: $s:=s+s$, where $+$ denotes concatenation. You can use each operation any number of times (possibly none). Your task is to find the lexicographically smallest string of length exactly $k$ that can be obtained by doing these operations on string $s$. A string $a$ is lexicographically smaller than a string $b$ if and only if one of the following holds: $a$ is a prefix of $b$, but $a\ne b$; In the first position where $a$ and $b$ differ, the string $a$ has a letter that appears earlier in the alphabet than the corresponding letter in $b$. -----Input----- The first line contains two integers $n$, $k$ ($1 \leq n, k \leq 5000$) — the length of the original string $s$ and the length of the desired string. The second line contains the string $s$, consisting of $n$ lowercase English letters. -----Output----- Print the lexicographically smallest string of length $k$ that can be obtained by doing the operations on string $s$. -----Examples----- Input 8 16 dbcadabc Output dbcadabcdbcadabc Input 4 5 abcd Output aaaaa -----Note----- In the first test, it is optimal to make one duplication: "dbcadabc" $\to$ "dbcadabcdbcadabc". In the second test it is optimal to delete the last $3$ characters, then duplicate the string $3$ times, then delete the last $3$ characters to make the string have length $k$. "abcd" $\to$ "abc" $\to$ "ab" $\to$ "a" $\to$ "aa" $\to$ "aaaa" $\to$ "aaaaaaaa" $\to$ "aaaaaaa" $\to$ "aaaaaa" $\to$ "aaaaa".
n, k = [int(x) for x in input().split()] s = input() lal = "" l = [] for i in range(n): lal += s[i] string = lal * (k // (i + 1)) leftover = s[: k % (i + 1)] l.append(string + leftover) print(min(l))
ASSIGN VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR STRING ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR BIN_OP VAR BIN_OP VAR NUMBER ASSIGN VAR VAR BIN_OP VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR
This is the easy version of the problem. The only difference is the constraints on $n$ and $k$. You can make hacks only if all versions of the problem are solved. You have a string $s$, and you can do two types of operations on it: Delete the last character of the string. Duplicate the string: $s:=s+s$, where $+$ denotes concatenation. You can use each operation any number of times (possibly none). Your task is to find the lexicographically smallest string of length exactly $k$ that can be obtained by doing these operations on string $s$. A string $a$ is lexicographically smaller than a string $b$ if and only if one of the following holds: $a$ is a prefix of $b$, but $a\ne b$; In the first position where $a$ and $b$ differ, the string $a$ has a letter that appears earlier in the alphabet than the corresponding letter in $b$. -----Input----- The first line contains two integers $n$, $k$ ($1 \leq n, k \leq 5000$) — the length of the original string $s$ and the length of the desired string. The second line contains the string $s$, consisting of $n$ lowercase English letters. -----Output----- Print the lexicographically smallest string of length $k$ that can be obtained by doing the operations on string $s$. -----Examples----- Input 8 16 dbcadabc Output dbcadabcdbcadabc Input 4 5 abcd Output aaaaa -----Note----- In the first test, it is optimal to make one duplication: "dbcadabc" $\to$ "dbcadabcdbcadabc". In the second test it is optimal to delete the last $3$ characters, then duplicate the string $3$ times, then delete the last $3$ characters to make the string have length $k$. "abcd" $\to$ "abc" $\to$ "ab" $\to$ "a" $\to$ "aa" $\to$ "aaaa" $\to$ "aaaaaaaa" $\to$ "aaaaaaa" $\to$ "aaaaaa" $\to$ "aaaaa".
def helper(s, k): while len(s) < k: s = s + s return s[:k] n, k = list(map(int, input().split())) s = input() inp = s l = [] while s != "": l.append(helper(s, k)) s = s[: len(s) - 1] l.sort() print(l[0])
FUNC_DEF WHILE FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR RETURN VAR VAR ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR VAR ASSIGN VAR LIST WHILE VAR STRING EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR NUMBER
This is the easy version of the problem. The only difference is the constraints on $n$ and $k$. You can make hacks only if all versions of the problem are solved. You have a string $s$, and you can do two types of operations on it: Delete the last character of the string. Duplicate the string: $s:=s+s$, where $+$ denotes concatenation. You can use each operation any number of times (possibly none). Your task is to find the lexicographically smallest string of length exactly $k$ that can be obtained by doing these operations on string $s$. A string $a$ is lexicographically smaller than a string $b$ if and only if one of the following holds: $a$ is a prefix of $b$, but $a\ne b$; In the first position where $a$ and $b$ differ, the string $a$ has a letter that appears earlier in the alphabet than the corresponding letter in $b$. -----Input----- The first line contains two integers $n$, $k$ ($1 \leq n, k \leq 5000$) — the length of the original string $s$ and the length of the desired string. The second line contains the string $s$, consisting of $n$ lowercase English letters. -----Output----- Print the lexicographically smallest string of length $k$ that can be obtained by doing the operations on string $s$. -----Examples----- Input 8 16 dbcadabc Output dbcadabcdbcadabc Input 4 5 abcd Output aaaaa -----Note----- In the first test, it is optimal to make one duplication: "dbcadabc" $\to$ "dbcadabcdbcadabc". In the second test it is optimal to delete the last $3$ characters, then duplicate the string $3$ times, then delete the last $3$ characters to make the string have length $k$. "abcd" $\to$ "abc" $\to$ "ab" $\to$ "a" $\to$ "aa" $\to$ "aaaa" $\to$ "aaaaaaaa" $\to$ "aaaaaaa" $\to$ "aaaaaa" $\to$ "aaaaa".
n, k = map(int, input().split()) s = input() res = 1 for i in range(n): if s[i] < s[i % res]: res = i + 1 elif s[i] > s[i % res]: break print((s[:res] * (k // res + 1))[:k])
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 IF VAR VAR VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR NUMBER IF VAR VAR VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR BIN_OP BIN_OP VAR VAR NUMBER VAR
This is the easy version of the problem. The only difference is the constraints on $n$ and $k$. You can make hacks only if all versions of the problem are solved. You have a string $s$, and you can do two types of operations on it: Delete the last character of the string. Duplicate the string: $s:=s+s$, where $+$ denotes concatenation. You can use each operation any number of times (possibly none). Your task is to find the lexicographically smallest string of length exactly $k$ that can be obtained by doing these operations on string $s$. A string $a$ is lexicographically smaller than a string $b$ if and only if one of the following holds: $a$ is a prefix of $b$, but $a\ne b$; In the first position where $a$ and $b$ differ, the string $a$ has a letter that appears earlier in the alphabet than the corresponding letter in $b$. -----Input----- The first line contains two integers $n$, $k$ ($1 \leq n, k \leq 5000$) — the length of the original string $s$ and the length of the desired string. The second line contains the string $s$, consisting of $n$ lowercase English letters. -----Output----- Print the lexicographically smallest string of length $k$ that can be obtained by doing the operations on string $s$. -----Examples----- Input 8 16 dbcadabc Output dbcadabcdbcadabc Input 4 5 abcd Output aaaaa -----Note----- In the first test, it is optimal to make one duplication: "dbcadabc" $\to$ "dbcadabcdbcadabc". In the second test it is optimal to delete the last $3$ characters, then duplicate the string $3$ times, then delete the last $3$ characters to make the string have length $k$. "abcd" $\to$ "abc" $\to$ "ab" $\to$ "a" $\to$ "aa" $\to$ "aaaa" $\to$ "aaaaaaaa" $\to$ "aaaaaaa" $\to$ "aaaaaa" $\to$ "aaaaa".
n, k = [int(x) for x in input().split()] s = input() s1 = list(s) finalans = "z" * k for i in range(0, n): leng = i + 1 s2 = "".join(s1[0 : i + 1]) cnt = k // leng ans = "" for i in range(0, cnt): ans += s2 if len(ans) < k: rem = k - len(ans) s3 = "".join(s1[0:rem]) ans += s3 if ans < finalans: finalans = ans print(finalans)
ASSIGN VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP STRING VAR FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL STRING VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR STRING FOR VAR FUNC_CALL VAR NUMBER VAR VAR VAR IF FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL STRING VAR NUMBER VAR VAR VAR IF VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR
This is the easy version of the problem. The only difference is the constraints on $n$ and $k$. You can make hacks only if all versions of the problem are solved. You have a string $s$, and you can do two types of operations on it: Delete the last character of the string. Duplicate the string: $s:=s+s$, where $+$ denotes concatenation. You can use each operation any number of times (possibly none). Your task is to find the lexicographically smallest string of length exactly $k$ that can be obtained by doing these operations on string $s$. A string $a$ is lexicographically smaller than a string $b$ if and only if one of the following holds: $a$ is a prefix of $b$, but $a\ne b$; In the first position where $a$ and $b$ differ, the string $a$ has a letter that appears earlier in the alphabet than the corresponding letter in $b$. -----Input----- The first line contains two integers $n$, $k$ ($1 \leq n, k \leq 5000$) — the length of the original string $s$ and the length of the desired string. The second line contains the string $s$, consisting of $n$ lowercase English letters. -----Output----- Print the lexicographically smallest string of length $k$ that can be obtained by doing the operations on string $s$. -----Examples----- Input 8 16 dbcadabc Output dbcadabcdbcadabc Input 4 5 abcd Output aaaaa -----Note----- In the first test, it is optimal to make one duplication: "dbcadabc" $\to$ "dbcadabcdbcadabc". In the second test it is optimal to delete the last $3$ characters, then duplicate the string $3$ times, then delete the last $3$ characters to make the string have length $k$. "abcd" $\to$ "abc" $\to$ "ab" $\to$ "a" $\to$ "aa" $\to$ "aaaa" $\to$ "aaaaaaaa" $\to$ "aaaaaaa" $\to$ "aaaaaa" $\to$ "aaaaa".
n, k = map(int, input().strip().split()) string = input() l = string * (k // n) + string[: k % n] for i in range(1, n): temp = string[:-i] l = min(l, temp * (k // (n - i)) + temp[: k % (n - i)]) print(l)
ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR BIN_OP BIN_OP VAR BIN_OP VAR VAR VAR BIN_OP VAR VAR FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR BIN_OP VAR BIN_OP VAR VAR VAR BIN_OP VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR
This is the easy version of the problem. The only difference is the constraints on $n$ and $k$. You can make hacks only if all versions of the problem are solved. You have a string $s$, and you can do two types of operations on it: Delete the last character of the string. Duplicate the string: $s:=s+s$, where $+$ denotes concatenation. You can use each operation any number of times (possibly none). Your task is to find the lexicographically smallest string of length exactly $k$ that can be obtained by doing these operations on string $s$. A string $a$ is lexicographically smaller than a string $b$ if and only if one of the following holds: $a$ is a prefix of $b$, but $a\ne b$; In the first position where $a$ and $b$ differ, the string $a$ has a letter that appears earlier in the alphabet than the corresponding letter in $b$. -----Input----- The first line contains two integers $n$, $k$ ($1 \leq n, k \leq 5000$) — the length of the original string $s$ and the length of the desired string. The second line contains the string $s$, consisting of $n$ lowercase English letters. -----Output----- Print the lexicographically smallest string of length $k$ that can be obtained by doing the operations on string $s$. -----Examples----- Input 8 16 dbcadabc Output dbcadabcdbcadabc Input 4 5 abcd Output aaaaa -----Note----- In the first test, it is optimal to make one duplication: "dbcadabc" $\to$ "dbcadabcdbcadabc". In the second test it is optimal to delete the last $3$ characters, then duplicate the string $3$ times, then delete the last $3$ characters to make the string have length $k$. "abcd" $\to$ "abc" $\to$ "ab" $\to$ "a" $\to$ "aa" $\to$ "aaaa" $\to$ "aaaaaaaa" $\to$ "aaaaaaa" $\to$ "aaaaaa" $\to$ "aaaaa".
n, k = map(int, input().split()) ss = input() mn = "z" * k for i in range(n): s = ss[: n - i] while True: if len(s * 2) > k: break else: s = s * 2 if len(s) == k: if s < mn: mn = s else: p = s * 2 q = p[:k] if q < mn: mn = q if k % 2 == 0: r = s[: k // 2] r = r * 2 if r < mn: mn = r print(mn)
ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR BIN_OP STRING VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR BIN_OP VAR VAR WHILE NUMBER IF FUNC_CALL VAR BIN_OP VAR NUMBER VAR ASSIGN VAR BIN_OP VAR NUMBER IF FUNC_CALL VAR VAR VAR IF VAR VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR IF VAR VAR ASSIGN VAR VAR IF BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER IF VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR
This is the easy version of the problem. The only difference is the constraints on $n$ and $k$. You can make hacks only if all versions of the problem are solved. You have a string $s$, and you can do two types of operations on it: Delete the last character of the string. Duplicate the string: $s:=s+s$, where $+$ denotes concatenation. You can use each operation any number of times (possibly none). Your task is to find the lexicographically smallest string of length exactly $k$ that can be obtained by doing these operations on string $s$. A string $a$ is lexicographically smaller than a string $b$ if and only if one of the following holds: $a$ is a prefix of $b$, but $a\ne b$; In the first position where $a$ and $b$ differ, the string $a$ has a letter that appears earlier in the alphabet than the corresponding letter in $b$. -----Input----- The first line contains two integers $n$, $k$ ($1 \leq n, k \leq 5000$) — the length of the original string $s$ and the length of the desired string. The second line contains the string $s$, consisting of $n$ lowercase English letters. -----Output----- Print the lexicographically smallest string of length $k$ that can be obtained by doing the operations on string $s$. -----Examples----- Input 8 16 dbcadabc Output dbcadabcdbcadabc Input 4 5 abcd Output aaaaa -----Note----- In the first test, it is optimal to make one duplication: "dbcadabc" $\to$ "dbcadabcdbcadabc". In the second test it is optimal to delete the last $3$ characters, then duplicate the string $3$ times, then delete the last $3$ characters to make the string have length $k$. "abcd" $\to$ "abc" $\to$ "ab" $\to$ "a" $\to$ "aa" $\to$ "aaaa" $\to$ "aaaaaaaa" $\to$ "aaaaaaa" $\to$ "aaaaaa" $\to$ "aaaaa".
n, k = map(int, input().split()) s = input() all_strings = [] for num in range(n, 0, -1): m = k // num new_s = s[:num] all_strings.append("".join(new_s for _ in range(m + 1))[:k]) print(min(all_strings))
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 VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL STRING VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR
This is the easy version of the problem. The only difference is the constraints on $n$ and $k$. You can make hacks only if all versions of the problem are solved. You have a string $s$, and you can do two types of operations on it: Delete the last character of the string. Duplicate the string: $s:=s+s$, where $+$ denotes concatenation. You can use each operation any number of times (possibly none). Your task is to find the lexicographically smallest string of length exactly $k$ that can be obtained by doing these operations on string $s$. A string $a$ is lexicographically smaller than a string $b$ if and only if one of the following holds: $a$ is a prefix of $b$, but $a\ne b$; In the first position where $a$ and $b$ differ, the string $a$ has a letter that appears earlier in the alphabet than the corresponding letter in $b$. -----Input----- The first line contains two integers $n$, $k$ ($1 \leq n, k \leq 5000$) — the length of the original string $s$ and the length of the desired string. The second line contains the string $s$, consisting of $n$ lowercase English letters. -----Output----- Print the lexicographically smallest string of length $k$ that can be obtained by doing the operations on string $s$. -----Examples----- Input 8 16 dbcadabc Output dbcadabcdbcadabc Input 4 5 abcd Output aaaaa -----Note----- In the first test, it is optimal to make one duplication: "dbcadabc" $\to$ "dbcadabcdbcadabc". In the second test it is optimal to delete the last $3$ characters, then duplicate the string $3$ times, then delete the last $3$ characters to make the string have length $k$. "abcd" $\to$ "abc" $\to$ "ab" $\to$ "a" $\to$ "aa" $\to$ "aaaa" $\to$ "aaaaaaaa" $\to$ "aaaaaaa" $\to$ "aaaaaa" $\to$ "aaaaa".
n, k = map(int, input().split()) s = input() p = -1 i = 0 while i < n: if s[i] > s[0]: p = i break elif s[i] == s[0]: j = i + 1 z = 1 while j < n and z < i and s[j] == s[z]: j += 1 z += 1 if j < n and z < i and s[j] > s[z]: p = i break i = j - 1 i += 1 if p != -1: s = s[:p] i = len(s) - 1 while i > 0 and s[i] == s[0]: i -= 1 s = s[: i + 1] print(s * (k // len(s)) + s[: k % len(s)])
ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR IF VAR VAR VAR NUMBER ASSIGN VAR VAR IF VAR VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR VAR VAR VAR VAR VAR VAR VAR NUMBER VAR NUMBER IF VAR VAR VAR VAR VAR VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR NUMBER VAR NUMBER IF VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER WHILE VAR NUMBER VAR VAR VAR NUMBER VAR NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP BIN_OP VAR BIN_OP VAR FUNC_CALL VAR VAR VAR BIN_OP VAR FUNC_CALL VAR VAR
This is the easy version of the problem. The only difference is the constraints on $n$ and $k$. You can make hacks only if all versions of the problem are solved. You have a string $s$, and you can do two types of operations on it: Delete the last character of the string. Duplicate the string: $s:=s+s$, where $+$ denotes concatenation. You can use each operation any number of times (possibly none). Your task is to find the lexicographically smallest string of length exactly $k$ that can be obtained by doing these operations on string $s$. A string $a$ is lexicographically smaller than a string $b$ if and only if one of the following holds: $a$ is a prefix of $b$, but $a\ne b$; In the first position where $a$ and $b$ differ, the string $a$ has a letter that appears earlier in the alphabet than the corresponding letter in $b$. -----Input----- The first line contains two integers $n$, $k$ ($1 \leq n, k \leq 5000$) — the length of the original string $s$ and the length of the desired string. The second line contains the string $s$, consisting of $n$ lowercase English letters. -----Output----- Print the lexicographically smallest string of length $k$ that can be obtained by doing the operations on string $s$. -----Examples----- Input 8 16 dbcadabc Output dbcadabcdbcadabc Input 4 5 abcd Output aaaaa -----Note----- In the first test, it is optimal to make one duplication: "dbcadabc" $\to$ "dbcadabcdbcadabc". In the second test it is optimal to delete the last $3$ characters, then duplicate the string $3$ times, then delete the last $3$ characters to make the string have length $k$. "abcd" $\to$ "abc" $\to$ "ab" $\to$ "a" $\to$ "aa" $\to$ "aaaa" $\to$ "aaaaaaaa" $\to$ "aaaaaaa" $\to$ "aaaaaa" $\to$ "aaaaa".
n, k = map(int, input().split(" ")) s = input() ss = 2 * s pos = n for i in range(1, n): if ss[:i] < ss[i : 2 * i]: pos = i break ans = s[:pos] * (1 + k // pos) print(ans[:k])
ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR ASSIGN VAR BIN_OP NUMBER VAR ASSIGN VAR VAR FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR VAR VAR VAR BIN_OP NUMBER VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR VAR BIN_OP NUMBER BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR VAR
This is the easy version of the problem. The only difference is the constraints on $n$ and $k$. You can make hacks only if all versions of the problem are solved. You have a string $s$, and you can do two types of operations on it: Delete the last character of the string. Duplicate the string: $s:=s+s$, where $+$ denotes concatenation. You can use each operation any number of times (possibly none). Your task is to find the lexicographically smallest string of length exactly $k$ that can be obtained by doing these operations on string $s$. A string $a$ is lexicographically smaller than a string $b$ if and only if one of the following holds: $a$ is a prefix of $b$, but $a\ne b$; In the first position where $a$ and $b$ differ, the string $a$ has a letter that appears earlier in the alphabet than the corresponding letter in $b$. -----Input----- The first line contains two integers $n$, $k$ ($1 \leq n, k \leq 5000$) — the length of the original string $s$ and the length of the desired string. The second line contains the string $s$, consisting of $n$ lowercase English letters. -----Output----- Print the lexicographically smallest string of length $k$ that can be obtained by doing the operations on string $s$. -----Examples----- Input 8 16 dbcadabc Output dbcadabcdbcadabc Input 4 5 abcd Output aaaaa -----Note----- In the first test, it is optimal to make one duplication: "dbcadabc" $\to$ "dbcadabcdbcadabc". In the second test it is optimal to delete the last $3$ characters, then duplicate the string $3$ times, then delete the last $3$ characters to make the string have length $k$. "abcd" $\to$ "abc" $\to$ "ab" $\to$ "a" $\to$ "aa" $\to$ "aaaa" $\to$ "aaaaaaaa" $\to$ "aaaaaaa" $\to$ "aaaaaa" $\to$ "aaaaa".
n, k = map(int, input().split()) s = input() print(min([(s[: i + 1] * (k // (i + 1) + 1))[:k] for i in range(n)]))
ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR BIN_OP VAR NUMBER BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER VAR VAR FUNC_CALL VAR VAR
This is the easy version of the problem. The only difference is the constraints on $n$ and $k$. You can make hacks only if all versions of the problem are solved. You have a string $s$, and you can do two types of operations on it: Delete the last character of the string. Duplicate the string: $s:=s+s$, where $+$ denotes concatenation. You can use each operation any number of times (possibly none). Your task is to find the lexicographically smallest string of length exactly $k$ that can be obtained by doing these operations on string $s$. A string $a$ is lexicographically smaller than a string $b$ if and only if one of the following holds: $a$ is a prefix of $b$, but $a\ne b$; In the first position where $a$ and $b$ differ, the string $a$ has a letter that appears earlier in the alphabet than the corresponding letter in $b$. -----Input----- The first line contains two integers $n$, $k$ ($1 \leq n, k \leq 5000$) — the length of the original string $s$ and the length of the desired string. The second line contains the string $s$, consisting of $n$ lowercase English letters. -----Output----- Print the lexicographically smallest string of length $k$ that can be obtained by doing the operations on string $s$. -----Examples----- Input 8 16 dbcadabc Output dbcadabcdbcadabc Input 4 5 abcd Output aaaaa -----Note----- In the first test, it is optimal to make one duplication: "dbcadabc" $\to$ "dbcadabcdbcadabc". In the second test it is optimal to delete the last $3$ characters, then duplicate the string $3$ times, then delete the last $3$ characters to make the string have length $k$. "abcd" $\to$ "abc" $\to$ "ab" $\to$ "a" $\to$ "aa" $\to$ "aaaa" $\to$ "aaaaaaaa" $\to$ "aaaaaaa" $\to$ "aaaaaa" $\to$ "aaaaa".
n, k = map(int, input().split()) s = input() ans = s[0] * k for i in range(1, n): c = i + 1 pr = s[0 : i + 1] while c < k: pr *= 2 c = len(pr) ans = min(ans, pr) print(ans[0:k])
ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR BIN_OP VAR NUMBER VAR FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR VAR NUMBER BIN_OP VAR NUMBER WHILE VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR NUMBER VAR
This is the easy version of the problem. The only difference is the constraints on $n$ and $k$. You can make hacks only if all versions of the problem are solved. You have a string $s$, and you can do two types of operations on it: Delete the last character of the string. Duplicate the string: $s:=s+s$, where $+$ denotes concatenation. You can use each operation any number of times (possibly none). Your task is to find the lexicographically smallest string of length exactly $k$ that can be obtained by doing these operations on string $s$. A string $a$ is lexicographically smaller than a string $b$ if and only if one of the following holds: $a$ is a prefix of $b$, but $a\ne b$; In the first position where $a$ and $b$ differ, the string $a$ has a letter that appears earlier in the alphabet than the corresponding letter in $b$. -----Input----- The first line contains two integers $n$, $k$ ($1 \leq n, k \leq 5000$) — the length of the original string $s$ and the length of the desired string. The second line contains the string $s$, consisting of $n$ lowercase English letters. -----Output----- Print the lexicographically smallest string of length $k$ that can be obtained by doing the operations on string $s$. -----Examples----- Input 8 16 dbcadabc Output dbcadabcdbcadabc Input 4 5 abcd Output aaaaa -----Note----- In the first test, it is optimal to make one duplication: "dbcadabc" $\to$ "dbcadabcdbcadabc". In the second test it is optimal to delete the last $3$ characters, then duplicate the string $3$ times, then delete the last $3$ characters to make the string have length $k$. "abcd" $\to$ "abc" $\to$ "ab" $\to$ "a" $\to$ "aa" $\to$ "aaaa" $\to$ "aaaaaaaa" $\to$ "aaaaaaa" $\to$ "aaaaaa" $\to$ "aaaaa".
import sys input = sys.stdin.readline n, k = map(int, input().split()) s = list(input().rstrip()) li = [] for i, t in enumerate(s): if t > s[0]: break elif t == s[0]: if str(s[0:i]) < str(s[i : i * 2]): break li.append(t) l = len(li) print("".join(li * (k // l) + li[0 : k % l]))
IMPORT ASSIGN VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST FOR VAR VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER IF VAR VAR NUMBER IF FUNC_CALL VAR VAR NUMBER VAR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL STRING BIN_OP BIN_OP VAR BIN_OP VAR VAR VAR NUMBER BIN_OP VAR VAR
This is the easy version of the problem. The only difference is the constraints on $n$ and $k$. You can make hacks only if all versions of the problem are solved. You have a string $s$, and you can do two types of operations on it: Delete the last character of the string. Duplicate the string: $s:=s+s$, where $+$ denotes concatenation. You can use each operation any number of times (possibly none). Your task is to find the lexicographically smallest string of length exactly $k$ that can be obtained by doing these operations on string $s$. A string $a$ is lexicographically smaller than a string $b$ if and only if one of the following holds: $a$ is a prefix of $b$, but $a\ne b$; In the first position where $a$ and $b$ differ, the string $a$ has a letter that appears earlier in the alphabet than the corresponding letter in $b$. -----Input----- The first line contains two integers $n$, $k$ ($1 \leq n, k \leq 5000$) — the length of the original string $s$ and the length of the desired string. The second line contains the string $s$, consisting of $n$ lowercase English letters. -----Output----- Print the lexicographically smallest string of length $k$ that can be obtained by doing the operations on string $s$. -----Examples----- Input 8 16 dbcadabc Output dbcadabcdbcadabc Input 4 5 abcd Output aaaaa -----Note----- In the first test, it is optimal to make one duplication: "dbcadabc" $\to$ "dbcadabcdbcadabc". In the second test it is optimal to delete the last $3$ characters, then duplicate the string $3$ times, then delete the last $3$ characters to make the string have length $k$. "abcd" $\to$ "abc" $\to$ "ab" $\to$ "a" $\to$ "aa" $\to$ "aaaa" $\to$ "aaaaaaaa" $\to$ "aaaaaaa" $\to$ "aaaaaa" $\to$ "aaaaa".
n, k = [int(i) for i in input().split(" ")] s = input() new = [] for i in range(1, n + 1): a = s[:i] while len(a) < k: a += a new.append(a[:k]) new.sort() print(new[0])
ASSIGN VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR VAR WHILE FUNC_CALL VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR NUMBER
This is the easy version of the problem. The only difference is the constraints on $n$ and $k$. You can make hacks only if all versions of the problem are solved. You have a string $s$, and you can do two types of operations on it: Delete the last character of the string. Duplicate the string: $s:=s+s$, where $+$ denotes concatenation. You can use each operation any number of times (possibly none). Your task is to find the lexicographically smallest string of length exactly $k$ that can be obtained by doing these operations on string $s$. A string $a$ is lexicographically smaller than a string $b$ if and only if one of the following holds: $a$ is a prefix of $b$, but $a\ne b$; In the first position where $a$ and $b$ differ, the string $a$ has a letter that appears earlier in the alphabet than the corresponding letter in $b$. -----Input----- The first line contains two integers $n$, $k$ ($1 \leq n, k \leq 5000$) — the length of the original string $s$ and the length of the desired string. The second line contains the string $s$, consisting of $n$ lowercase English letters. -----Output----- Print the lexicographically smallest string of length $k$ that can be obtained by doing the operations on string $s$. -----Examples----- Input 8 16 dbcadabc Output dbcadabcdbcadabc Input 4 5 abcd Output aaaaa -----Note----- In the first test, it is optimal to make one duplication: "dbcadabc" $\to$ "dbcadabcdbcadabc". In the second test it is optimal to delete the last $3$ characters, then duplicate the string $3$ times, then delete the last $3$ characters to make the string have length $k$. "abcd" $\to$ "abc" $\to$ "ab" $\to$ "a" $\to$ "aa" $\to$ "aaaa" $\to$ "aaaaaaaa" $\to$ "aaaaaaa" $\to$ "aaaaaa" $\to$ "aaaaa".
inp = input().split() length = int(inp[0]) required = int(inp[1]) s = input() count = 1 for i in range(1, length): if s[i] > s[i % count]: break if s[i] < s[i % count]: count = i + 1 for i in range(required): print(s[i % count], end="")
ASSIGN VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR VAR VAR BIN_OP VAR VAR IF VAR VAR VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR BIN_OP VAR VAR STRING
This is the easy version of the problem. The only difference is the constraints on $n$ and $k$. You can make hacks only if all versions of the problem are solved. You have a string $s$, and you can do two types of operations on it: Delete the last character of the string. Duplicate the string: $s:=s+s$, where $+$ denotes concatenation. You can use each operation any number of times (possibly none). Your task is to find the lexicographically smallest string of length exactly $k$ that can be obtained by doing these operations on string $s$. A string $a$ is lexicographically smaller than a string $b$ if and only if one of the following holds: $a$ is a prefix of $b$, but $a\ne b$; In the first position where $a$ and $b$ differ, the string $a$ has a letter that appears earlier in the alphabet than the corresponding letter in $b$. -----Input----- The first line contains two integers $n$, $k$ ($1 \leq n, k \leq 5000$) — the length of the original string $s$ and the length of the desired string. The second line contains the string $s$, consisting of $n$ lowercase English letters. -----Output----- Print the lexicographically smallest string of length $k$ that can be obtained by doing the operations on string $s$. -----Examples----- Input 8 16 dbcadabc Output dbcadabcdbcadabc Input 4 5 abcd Output aaaaa -----Note----- In the first test, it is optimal to make one duplication: "dbcadabc" $\to$ "dbcadabcdbcadabc". In the second test it is optimal to delete the last $3$ characters, then duplicate the string $3$ times, then delete the last $3$ characters to make the string have length $k$. "abcd" $\to$ "abc" $\to$ "ab" $\to$ "a" $\to$ "aa" $\to$ "aaaa" $\to$ "aaaaaaaa" $\to$ "aaaaaaa" $\to$ "aaaaaa" $\to$ "aaaaa".
n, k = map(int, input().split()) l = list(input()) m = "z" * 2 * k for i in range(n): c = "".join(l) * 100 l.pop() if c < m: m = c while len(m) < k: m += m print(m[:k])
ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR BIN_OP BIN_OP STRING NUMBER VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP FUNC_CALL STRING VAR NUMBER EXPR FUNC_CALL VAR IF VAR VAR ASSIGN VAR VAR WHILE FUNC_CALL VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR
This is the easy version of the problem. The only difference is the constraints on $n$ and $k$. You can make hacks only if all versions of the problem are solved. You have a string $s$, and you can do two types of operations on it: Delete the last character of the string. Duplicate the string: $s:=s+s$, where $+$ denotes concatenation. You can use each operation any number of times (possibly none). Your task is to find the lexicographically smallest string of length exactly $k$ that can be obtained by doing these operations on string $s$. A string $a$ is lexicographically smaller than a string $b$ if and only if one of the following holds: $a$ is a prefix of $b$, but $a\ne b$; In the first position where $a$ and $b$ differ, the string $a$ has a letter that appears earlier in the alphabet than the corresponding letter in $b$. -----Input----- The first line contains two integers $n$, $k$ ($1 \leq n, k \leq 5000$) — the length of the original string $s$ and the length of the desired string. The second line contains the string $s$, consisting of $n$ lowercase English letters. -----Output----- Print the lexicographically smallest string of length $k$ that can be obtained by doing the operations on string $s$. -----Examples----- Input 8 16 dbcadabc Output dbcadabcdbcadabc Input 4 5 abcd Output aaaaa -----Note----- In the first test, it is optimal to make one duplication: "dbcadabc" $\to$ "dbcadabcdbcadabc". In the second test it is optimal to delete the last $3$ characters, then duplicate the string $3$ times, then delete the last $3$ characters to make the string have length $k$. "abcd" $\to$ "abc" $\to$ "ab" $\to$ "a" $\to$ "aa" $\to$ "aaaa" $\to$ "aaaaaaaa" $\to$ "aaaaaaa" $\to$ "aaaaaa" $\to$ "aaaaa".
n, p = map(int, input().split()) s = list(input()) j = n i = 1 while i < n: if s[i] > s[0]: j = i break elif s[i] == s[0]: k = i l = 0 while k < n and s[k] == s[l]: k += 1 l += 1 if k >= n or s[k] > s[l]: j = i break else: i = k else: i += 1 print(("".join(s[:j]) * p)[:p])
ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR ASSIGN VAR NUMBER WHILE VAR VAR IF VAR VAR VAR NUMBER ASSIGN VAR VAR IF VAR VAR VAR NUMBER ASSIGN VAR VAR ASSIGN VAR NUMBER WHILE VAR VAR VAR VAR VAR VAR VAR NUMBER VAR NUMBER IF VAR VAR VAR VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP FUNC_CALL STRING VAR VAR VAR VAR
This is the easy version of the problem. The only difference is the constraints on $n$ and $k$. You can make hacks only if all versions of the problem are solved. You have a string $s$, and you can do two types of operations on it: Delete the last character of the string. Duplicate the string: $s:=s+s$, where $+$ denotes concatenation. You can use each operation any number of times (possibly none). Your task is to find the lexicographically smallest string of length exactly $k$ that can be obtained by doing these operations on string $s$. A string $a$ is lexicographically smaller than a string $b$ if and only if one of the following holds: $a$ is a prefix of $b$, but $a\ne b$; In the first position where $a$ and $b$ differ, the string $a$ has a letter that appears earlier in the alphabet than the corresponding letter in $b$. -----Input----- The first line contains two integers $n$, $k$ ($1 \leq n, k \leq 5000$) — the length of the original string $s$ and the length of the desired string. The second line contains the string $s$, consisting of $n$ lowercase English letters. -----Output----- Print the lexicographically smallest string of length $k$ that can be obtained by doing the operations on string $s$. -----Examples----- Input 8 16 dbcadabc Output dbcadabcdbcadabc Input 4 5 abcd Output aaaaa -----Note----- In the first test, it is optimal to make one duplication: "dbcadabc" $\to$ "dbcadabcdbcadabc". In the second test it is optimal to delete the last $3$ characters, then duplicate the string $3$ times, then delete the last $3$ characters to make the string have length $k$. "abcd" $\to$ "abc" $\to$ "ab" $\to$ "a" $\to$ "aa" $\to$ "aaaa" $\to$ "aaaaaaaa" $\to$ "aaaaaaa" $\to$ "aaaaaa" $\to$ "aaaaa".
n, m = input().split() n = int(n) m = int(m) s = str(input()) p = 1 for i in range(1, n): if s[i % p] < s[i]: break if s[i % p] > s[i]: p = i + 1 x = m // p y = m - x * p print(s[0:p] * x + s[0:y])
ASSIGN VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR BIN_OP VAR VAR VAR VAR IF VAR BIN_OP VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR BIN_OP BIN_OP VAR NUMBER VAR VAR VAR NUMBER VAR
This is the easy version of the problem. The only difference is the constraints on $n$ and $k$. You can make hacks only if all versions of the problem are solved. You have a string $s$, and you can do two types of operations on it: Delete the last character of the string. Duplicate the string: $s:=s+s$, where $+$ denotes concatenation. You can use each operation any number of times (possibly none). Your task is to find the lexicographically smallest string of length exactly $k$ that can be obtained by doing these operations on string $s$. A string $a$ is lexicographically smaller than a string $b$ if and only if one of the following holds: $a$ is a prefix of $b$, but $a\ne b$; In the first position where $a$ and $b$ differ, the string $a$ has a letter that appears earlier in the alphabet than the corresponding letter in $b$. -----Input----- The first line contains two integers $n$, $k$ ($1 \leq n, k \leq 5000$) — the length of the original string $s$ and the length of the desired string. The second line contains the string $s$, consisting of $n$ lowercase English letters. -----Output----- Print the lexicographically smallest string of length $k$ that can be obtained by doing the operations on string $s$. -----Examples----- Input 8 16 dbcadabc Output dbcadabcdbcadabc Input 4 5 abcd Output aaaaa -----Note----- In the first test, it is optimal to make one duplication: "dbcadabc" $\to$ "dbcadabcdbcadabc". In the second test it is optimal to delete the last $3$ characters, then duplicate the string $3$ times, then delete the last $3$ characters to make the string have length $k$. "abcd" $\to$ "abc" $\to$ "ab" $\to$ "a" $\to$ "aa" $\to$ "aaaa" $\to$ "aaaaaaaa" $\to$ "aaaaaaa" $\to$ "aaaaaa" $\to$ "aaaaa".
n, k = map(int, input().split()) str = input() ans = None for i in range(1, len(str) + 1): cnt = int(k / i) if cnt * i != k: cnt += 1 tmp = str[0:i] * cnt if i == 1: ans = tmp[:k] elif tmp < ans: ans = tmp[:k] print(ans)
ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NONE FOR VAR FUNC_CALL VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR IF BIN_OP VAR VAR VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER VAR VAR IF VAR NUMBER ASSIGN VAR VAR VAR IF VAR VAR ASSIGN VAR VAR VAR EXPR FUNC_CALL VAR VAR
This is the easy version of the problem. The only difference is the constraints on $n$ and $k$. You can make hacks only if all versions of the problem are solved. You have a string $s$, and you can do two types of operations on it: Delete the last character of the string. Duplicate the string: $s:=s+s$, where $+$ denotes concatenation. You can use each operation any number of times (possibly none). Your task is to find the lexicographically smallest string of length exactly $k$ that can be obtained by doing these operations on string $s$. A string $a$ is lexicographically smaller than a string $b$ if and only if one of the following holds: $a$ is a prefix of $b$, but $a\ne b$; In the first position where $a$ and $b$ differ, the string $a$ has a letter that appears earlier in the alphabet than the corresponding letter in $b$. -----Input----- The first line contains two integers $n$, $k$ ($1 \leq n, k \leq 5000$) — the length of the original string $s$ and the length of the desired string. The second line contains the string $s$, consisting of $n$ lowercase English letters. -----Output----- Print the lexicographically smallest string of length $k$ that can be obtained by doing the operations on string $s$. -----Examples----- Input 8 16 dbcadabc Output dbcadabcdbcadabc Input 4 5 abcd Output aaaaa -----Note----- In the first test, it is optimal to make one duplication: "dbcadabc" $\to$ "dbcadabcdbcadabc". In the second test it is optimal to delete the last $3$ characters, then duplicate the string $3$ times, then delete the last $3$ characters to make the string have length $k$. "abcd" $\to$ "abc" $\to$ "ab" $\to$ "a" $\to$ "aa" $\to$ "aaaa" $\to$ "aaaaaaaa" $\to$ "aaaaaaa" $\to$ "aaaaaa" $\to$ "aaaaa".
n, k = list(map(int, input().split())) stri = input() inp = stri l = [] while stri != "": stri1 = stri while len(stri1) < k: stri1 = stri1 + stri1 l.append(stri1[:k]) stri = stri[: len(stri) - 1] l.sort() print(l[0])
ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR VAR ASSIGN VAR LIST WHILE VAR STRING ASSIGN VAR VAR WHILE FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR NUMBER