description
stringlengths 171
4k
| code
stringlengths 94
3.98k
| normalized_code
stringlengths 57
4.99k
|
|---|---|---|
Nadeko's birthday is approaching! As she decorated the room for the party, a long garland of Dianthus-shaped paper pieces was placed on a prominent part of the wall. Brother Koyomi will like it!
Still unsatisfied with the garland, Nadeko decided to polish it again. The garland has n pieces numbered from 1 to n from left to right, and the i-th piece has a colour s_{i}, denoted by a lowercase English letter. Nadeko will repaint at most m of the pieces to give each of them an arbitrary new colour (still denoted by a lowercase English letter). After this work, she finds out all subsegments of the garland containing pieces of only colour c β Brother Koyomi's favourite one, and takes the length of the longest among them to be the Koyomity of the garland.
For instance, let's say the garland is represented by "kooomo", and Brother Koyomi's favourite colour is "o". Among all subsegments containing pieces of "o" only, "ooo" is the longest, with a length of 3. Thus the Koyomity of this garland equals 3.
But problem arises as Nadeko is unsure about Brother Koyomi's favourite colour, and has swaying ideas on the amount of work to do. She has q plans on this, each of which can be expressed as a pair of an integer m_{i} and a lowercase letter c_{i}, meanings of which are explained above. You are to find out the maximum Koyomity achievable after repainting the garland according to each plan.
-----Input-----
The first line of input contains a positive integer n (1 β€ n β€ 1 500) β the length of the garland.
The second line contains n lowercase English letters s_1s_2... s_{n} as a string β the initial colours of paper pieces on the garland.
The third line contains a positive integer q (1 β€ q β€ 200 000) β the number of plans Nadeko has.
The next q lines describe one plan each: the i-th among them contains an integer m_{i} (1 β€ m_{i} β€ n) β the maximum amount of pieces to repaint, followed by a space, then by a lowercase English letter c_{i} β Koyomi's possible favourite colour.
-----Output-----
Output q lines: for each work plan, output one line containing an integer β the largest Koyomity achievable after repainting the garland according to it.
-----Examples-----
Input
6
koyomi
3
1 o
4 o
4 m
Output
3
6
5
Input
15
yamatonadeshiko
10
1 a
2 a
3 a
4 a
5 a
1 b
2 b
3 b
4 b
5 b
Output
3
4
5
7
8
1
2
3
4
5
Input
10
aaaaaaaaaa
2
10 b
10 z
Output
10
10
-----Note-----
In the first sample, there are three plans: In the first plan, at most 1 piece can be repainted. Repainting the "y" piece to become "o" results in "kooomi", whose Koyomity of 3 is the best achievable; In the second plan, at most 4 pieces can be repainted, and "oooooo" results in a Koyomity of 6; In the third plan, at most 4 pieces can be repainted, and "mmmmmi" and "kmmmmm" both result in a Koyomity of 5.
|
n = int(input())
s = input()
dp = [([-1] * (n + 1)) for x in range(26)]
for c in range(26):
for l in range(n):
n_c = 0
for r in range(l, n):
if s[r] == chr(ord("a") + c):
n_c += 1
dp[c][r - l + 1 - n_c] = max(dp[c][r - l + 1 - n_c], r - l + 1)
all_res = []
for i in range(int(input())):
m, c = input().split()
m = int(m)
idx = ord(c) - ord("a")
all_res.append(dp[idx][m] if dp[idx][m] != -1 else n)
print("\n".join(map(str, all_res)))
|
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER VAR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR IF VAR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR STRING VAR VAR NUMBER ASSIGN VAR VAR BIN_OP BIN_OP BIN_OP VAR VAR NUMBER VAR FUNC_CALL VAR VAR VAR BIN_OP BIN_OP BIN_OP VAR VAR NUMBER VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR VAR VAR VAR NUMBER VAR VAR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL STRING FUNC_CALL VAR VAR VAR
|
Nadeko's birthday is approaching! As she decorated the room for the party, a long garland of Dianthus-shaped paper pieces was placed on a prominent part of the wall. Brother Koyomi will like it!
Still unsatisfied with the garland, Nadeko decided to polish it again. The garland has n pieces numbered from 1 to n from left to right, and the i-th piece has a colour s_{i}, denoted by a lowercase English letter. Nadeko will repaint at most m of the pieces to give each of them an arbitrary new colour (still denoted by a lowercase English letter). After this work, she finds out all subsegments of the garland containing pieces of only colour c β Brother Koyomi's favourite one, and takes the length of the longest among them to be the Koyomity of the garland.
For instance, let's say the garland is represented by "kooomo", and Brother Koyomi's favourite colour is "o". Among all subsegments containing pieces of "o" only, "ooo" is the longest, with a length of 3. Thus the Koyomity of this garland equals 3.
But problem arises as Nadeko is unsure about Brother Koyomi's favourite colour, and has swaying ideas on the amount of work to do. She has q plans on this, each of which can be expressed as a pair of an integer m_{i} and a lowercase letter c_{i}, meanings of which are explained above. You are to find out the maximum Koyomity achievable after repainting the garland according to each plan.
-----Input-----
The first line of input contains a positive integer n (1 β€ n β€ 1 500) β the length of the garland.
The second line contains n lowercase English letters s_1s_2... s_{n} as a string β the initial colours of paper pieces on the garland.
The third line contains a positive integer q (1 β€ q β€ 200 000) β the number of plans Nadeko has.
The next q lines describe one plan each: the i-th among them contains an integer m_{i} (1 β€ m_{i} β€ n) β the maximum amount of pieces to repaint, followed by a space, then by a lowercase English letter c_{i} β Koyomi's possible favourite colour.
-----Output-----
Output q lines: for each work plan, output one line containing an integer β the largest Koyomity achievable after repainting the garland according to it.
-----Examples-----
Input
6
koyomi
3
1 o
4 o
4 m
Output
3
6
5
Input
15
yamatonadeshiko
10
1 a
2 a
3 a
4 a
5 a
1 b
2 b
3 b
4 b
5 b
Output
3
4
5
7
8
1
2
3
4
5
Input
10
aaaaaaaaaa
2
10 b
10 z
Output
10
10
-----Note-----
In the first sample, there are three plans: In the first plan, at most 1 piece can be repainted. Repainting the "y" piece to become "o" results in "kooomi", whose Koyomity of 3 is the best achievable; In the second plan, at most 4 pieces can be repainted, and "oooooo" results in a Koyomity of 6; In the third plan, at most 4 pieces can be repainted, and "mmmmmi" and "kmmmmm" both result in a Koyomity of 5.
|
import sys
input = sys.stdin.buffer.readline
n = int(input())
s = str(input().strip())[2:-1]
ans = [([0] * (n + 1)) for i in range(26)]
segs = [[[0, -1]] for i in range(26)]
for i in range(n):
c = ord(s[i]) - ord("a")
if i > 0 and s[i] != s[i - 1]:
segs[c].append([i, i])
else:
segs[c][-1][1] = i
for c in range(26):
for start in range(len(segs[c])):
lst_seg = start
tot = segs[c][start][1] - segs[c][start][0] + 1
left = 0
l = segs[c][start][0]
for m in range(1, n + 1):
if segs[c][lst_seg][1] + left + 1 < n:
left += 1
tot += 1
if (
lst_seg != len(segs[c]) - 1
and segs[c][lst_seg][1] + left == segs[c][lst_seg + 1][0] - 1
):
lst_seg += 1
tot += segs[c][lst_seg][1] - segs[c][lst_seg][0] + 1
left = 0
elif start != 0 and l > segs[c][start - 1][1] + 2:
l -= 1
tot += 1
ans[c][m] = max(ans[c][m], tot)
q = int(input())
for i in range(q):
m, c = input().split()
m = int(str(m)[2:-1])
c = ord(str(c)[2:-1]) - ord("a")
print(ans[c][m])
|
IMPORT ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL FUNC_CALL VAR NUMBER NUMBER ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER VAR FUNC_CALL VAR NUMBER ASSIGN VAR LIST LIST NUMBER NUMBER VAR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR VAR FUNC_CALL VAR STRING IF VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR LIST VAR VAR ASSIGN VAR VAR NUMBER NUMBER VAR FOR VAR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR NUMBER VAR VAR VAR NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER IF BIN_OP BIN_OP VAR VAR VAR NUMBER VAR NUMBER VAR VAR NUMBER VAR NUMBER IF VAR BIN_OP FUNC_CALL VAR VAR VAR NUMBER BIN_OP VAR VAR VAR NUMBER VAR BIN_OP VAR VAR BIN_OP VAR NUMBER NUMBER NUMBER VAR NUMBER VAR BIN_OP BIN_OP VAR VAR VAR NUMBER VAR VAR VAR NUMBER NUMBER ASSIGN VAR NUMBER IF VAR NUMBER VAR BIN_OP VAR VAR BIN_OP VAR NUMBER NUMBER NUMBER VAR NUMBER VAR NUMBER ASSIGN VAR VAR VAR FUNC_CALL VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER NUMBER FUNC_CALL VAR STRING EXPR FUNC_CALL VAR VAR VAR VAR
|
Nadeko's birthday is approaching! As she decorated the room for the party, a long garland of Dianthus-shaped paper pieces was placed on a prominent part of the wall. Brother Koyomi will like it!
Still unsatisfied with the garland, Nadeko decided to polish it again. The garland has n pieces numbered from 1 to n from left to right, and the i-th piece has a colour s_{i}, denoted by a lowercase English letter. Nadeko will repaint at most m of the pieces to give each of them an arbitrary new colour (still denoted by a lowercase English letter). After this work, she finds out all subsegments of the garland containing pieces of only colour c β Brother Koyomi's favourite one, and takes the length of the longest among them to be the Koyomity of the garland.
For instance, let's say the garland is represented by "kooomo", and Brother Koyomi's favourite colour is "o". Among all subsegments containing pieces of "o" only, "ooo" is the longest, with a length of 3. Thus the Koyomity of this garland equals 3.
But problem arises as Nadeko is unsure about Brother Koyomi's favourite colour, and has swaying ideas on the amount of work to do. She has q plans on this, each of which can be expressed as a pair of an integer m_{i} and a lowercase letter c_{i}, meanings of which are explained above. You are to find out the maximum Koyomity achievable after repainting the garland according to each plan.
-----Input-----
The first line of input contains a positive integer n (1 β€ n β€ 1 500) β the length of the garland.
The second line contains n lowercase English letters s_1s_2... s_{n} as a string β the initial colours of paper pieces on the garland.
The third line contains a positive integer q (1 β€ q β€ 200 000) β the number of plans Nadeko has.
The next q lines describe one plan each: the i-th among them contains an integer m_{i} (1 β€ m_{i} β€ n) β the maximum amount of pieces to repaint, followed by a space, then by a lowercase English letter c_{i} β Koyomi's possible favourite colour.
-----Output-----
Output q lines: for each work plan, output one line containing an integer β the largest Koyomity achievable after repainting the garland according to it.
-----Examples-----
Input
6
koyomi
3
1 o
4 o
4 m
Output
3
6
5
Input
15
yamatonadeshiko
10
1 a
2 a
3 a
4 a
5 a
1 b
2 b
3 b
4 b
5 b
Output
3
4
5
7
8
1
2
3
4
5
Input
10
aaaaaaaaaa
2
10 b
10 z
Output
10
10
-----Note-----
In the first sample, there are three plans: In the first plan, at most 1 piece can be repainted. Repainting the "y" piece to become "o" results in "kooomi", whose Koyomity of 3 is the best achievable; In the second plan, at most 4 pieces can be repainted, and "oooooo" results in a Koyomity of 6; In the third plan, at most 4 pieces can be repainted, and "mmmmmi" and "kmmmmm" both result in a Koyomity of 5.
|
from sys import stdin, stdout
def main():
n = int(stdin.readline())
s = stdin.readline()
d = {chr(i): [(0) for j in range(n + 1)] for i in range(97, 123)}
for i in range(97, 123):
for j in range(n):
dp = 1 if s[j] == chr(i) else 0
d[chr(i)][1 - dp] = max(d[chr(i)][1 - dp], 1)
for u in range(j + 1, n):
if s[u] == chr(i):
dp += 1
d[chr(i)][u - j + 1 - dp] = max(d[chr(i)][u - j + 1 - dp], u - j + 1)
q = int(stdin.readline())
for i in range(q):
m, c = stdin.readline().split()
m = int(m)
res = d[c][m]
if res == 0:
res = n
stdout.write(str(res) + "\n")
main()
|
FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP NUMBER VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR BIN_OP NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR IF VAR VAR FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP BIN_OP VAR VAR NUMBER VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR BIN_OP BIN_OP BIN_OP VAR VAR NUMBER VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR IF VAR NUMBER ASSIGN VAR VAR EXPR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR STRING EXPR FUNC_CALL VAR
|
Nadeko's birthday is approaching! As she decorated the room for the party, a long garland of Dianthus-shaped paper pieces was placed on a prominent part of the wall. Brother Koyomi will like it!
Still unsatisfied with the garland, Nadeko decided to polish it again. The garland has n pieces numbered from 1 to n from left to right, and the i-th piece has a colour s_{i}, denoted by a lowercase English letter. Nadeko will repaint at most m of the pieces to give each of them an arbitrary new colour (still denoted by a lowercase English letter). After this work, she finds out all subsegments of the garland containing pieces of only colour c β Brother Koyomi's favourite one, and takes the length of the longest among them to be the Koyomity of the garland.
For instance, let's say the garland is represented by "kooomo", and Brother Koyomi's favourite colour is "o". Among all subsegments containing pieces of "o" only, "ooo" is the longest, with a length of 3. Thus the Koyomity of this garland equals 3.
But problem arises as Nadeko is unsure about Brother Koyomi's favourite colour, and has swaying ideas on the amount of work to do. She has q plans on this, each of which can be expressed as a pair of an integer m_{i} and a lowercase letter c_{i}, meanings of which are explained above. You are to find out the maximum Koyomity achievable after repainting the garland according to each plan.
-----Input-----
The first line of input contains a positive integer n (1 β€ n β€ 1 500) β the length of the garland.
The second line contains n lowercase English letters s_1s_2... s_{n} as a string β the initial colours of paper pieces on the garland.
The third line contains a positive integer q (1 β€ q β€ 200 000) β the number of plans Nadeko has.
The next q lines describe one plan each: the i-th among them contains an integer m_{i} (1 β€ m_{i} β€ n) β the maximum amount of pieces to repaint, followed by a space, then by a lowercase English letter c_{i} β Koyomi's possible favourite colour.
-----Output-----
Output q lines: for each work plan, output one line containing an integer β the largest Koyomity achievable after repainting the garland according to it.
-----Examples-----
Input
6
koyomi
3
1 o
4 o
4 m
Output
3
6
5
Input
15
yamatonadeshiko
10
1 a
2 a
3 a
4 a
5 a
1 b
2 b
3 b
4 b
5 b
Output
3
4
5
7
8
1
2
3
4
5
Input
10
aaaaaaaaaa
2
10 b
10 z
Output
10
10
-----Note-----
In the first sample, there are three plans: In the first plan, at most 1 piece can be repainted. Repainting the "y" piece to become "o" results in "kooomi", whose Koyomity of 3 is the best achievable; In the second plan, at most 4 pieces can be repainted, and "oooooo" results in a Koyomity of 6; In the third plan, at most 4 pieces can be repainted, and "mmmmmi" and "kmmmmm" both result in a Koyomity of 5.
|
import sys
n = int(sys.stdin.readline().strip())
s = sys.stdin.readline().strip()
ans = [([-1] * (n + 1)) for x in range(26)]
for c in range(26):
for l in range(n):
nrOfC = 0
for r in range(l, n):
if s[r] == chr(97 + c):
nrOfC += 1
ans[c][r - l + 1 - nrOfC] = max(ans[c][r - l + 1 - nrOfC], r - l + 1)
for i in range(int(sys.stdin.readline().strip())):
m, c = sys.stdin.readline().strip().split()
m = int(m)
print(ans[ord(c) - 97][m] if ans[ord(c) - 97][m] != -1 else n)
|
IMPORT ASSIGN VAR FUNC_CALL VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER VAR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR IF VAR VAR FUNC_CALL VAR BIN_OP NUMBER VAR VAR NUMBER ASSIGN VAR VAR BIN_OP BIN_OP BIN_OP VAR VAR NUMBER VAR FUNC_CALL VAR VAR VAR BIN_OP BIN_OP BIN_OP VAR VAR NUMBER VAR BIN_OP BIN_OP VAR VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER VAR NUMBER VAR BIN_OP FUNC_CALL VAR VAR NUMBER VAR VAR
|
Nadeko's birthday is approaching! As she decorated the room for the party, a long garland of Dianthus-shaped paper pieces was placed on a prominent part of the wall. Brother Koyomi will like it!
Still unsatisfied with the garland, Nadeko decided to polish it again. The garland has n pieces numbered from 1 to n from left to right, and the i-th piece has a colour s_{i}, denoted by a lowercase English letter. Nadeko will repaint at most m of the pieces to give each of them an arbitrary new colour (still denoted by a lowercase English letter). After this work, she finds out all subsegments of the garland containing pieces of only colour c β Brother Koyomi's favourite one, and takes the length of the longest among them to be the Koyomity of the garland.
For instance, let's say the garland is represented by "kooomo", and Brother Koyomi's favourite colour is "o". Among all subsegments containing pieces of "o" only, "ooo" is the longest, with a length of 3. Thus the Koyomity of this garland equals 3.
But problem arises as Nadeko is unsure about Brother Koyomi's favourite colour, and has swaying ideas on the amount of work to do. She has q plans on this, each of which can be expressed as a pair of an integer m_{i} and a lowercase letter c_{i}, meanings of which are explained above. You are to find out the maximum Koyomity achievable after repainting the garland according to each plan.
-----Input-----
The first line of input contains a positive integer n (1 β€ n β€ 1 500) β the length of the garland.
The second line contains n lowercase English letters s_1s_2... s_{n} as a string β the initial colours of paper pieces on the garland.
The third line contains a positive integer q (1 β€ q β€ 200 000) β the number of plans Nadeko has.
The next q lines describe one plan each: the i-th among them contains an integer m_{i} (1 β€ m_{i} β€ n) β the maximum amount of pieces to repaint, followed by a space, then by a lowercase English letter c_{i} β Koyomi's possible favourite colour.
-----Output-----
Output q lines: for each work plan, output one line containing an integer β the largest Koyomity achievable after repainting the garland according to it.
-----Examples-----
Input
6
koyomi
3
1 o
4 o
4 m
Output
3
6
5
Input
15
yamatonadeshiko
10
1 a
2 a
3 a
4 a
5 a
1 b
2 b
3 b
4 b
5 b
Output
3
4
5
7
8
1
2
3
4
5
Input
10
aaaaaaaaaa
2
10 b
10 z
Output
10
10
-----Note-----
In the first sample, there are three plans: In the first plan, at most 1 piece can be repainted. Repainting the "y" piece to become "o" results in "kooomi", whose Koyomity of 3 is the best achievable; In the second plan, at most 4 pieces can be repainted, and "oooooo" results in a Koyomity of 6; In the third plan, at most 4 pieces can be repainted, and "mmmmmi" and "kmmmmm" both result in a Koyomity of 5.
|
from sys import stdin
n = int(stdin.readline())
s = list(stdin.readline())[:-1]
cnt = [([0] * 26) for i in range(n + 1)]
for j in range(26):
x = chr(j + 97)
for i in range(n):
if s[i] == x:
cnt[i + 1][j] += 1
for i in range(1, n + 1):
cnt[i][j] += cnt[i - 1][j]
d = [([0] * (n + 1)) for i in range(26)]
for j in range(26):
for r in range(1, n + 1):
for l in range(1, r + 1):
dx = r - l + 1 - (cnt[r][j] - cnt[l - 1][j])
d[j][dx] = max(d[j][dx], r - l + 1)
ans = [([0] * (n + 1)) for i in range(26)]
for j in range(26):
ans[j][0] = d[j][0]
for i in range(1, n + 1):
ans[j][i] = max(ans[j][i - 1], d[j][i])
q = int(stdin.readline())
for _ in range(q):
m, c = stdin.readline().split()
m = int(m)
print(ans[ord(c) - ord("a")][m])
|
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR BIN_OP VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER VAR VAR VAR VAR BIN_OP VAR NUMBER VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER VAR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR VAR NUMBER BIN_OP VAR VAR VAR VAR BIN_OP VAR NUMBER VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR VAR VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER VAR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER ASSIGN VAR VAR NUMBER VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR VAR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER VAR VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR STRING VAR
|
Nadeko's birthday is approaching! As she decorated the room for the party, a long garland of Dianthus-shaped paper pieces was placed on a prominent part of the wall. Brother Koyomi will like it!
Still unsatisfied with the garland, Nadeko decided to polish it again. The garland has n pieces numbered from 1 to n from left to right, and the i-th piece has a colour s_{i}, denoted by a lowercase English letter. Nadeko will repaint at most m of the pieces to give each of them an arbitrary new colour (still denoted by a lowercase English letter). After this work, she finds out all subsegments of the garland containing pieces of only colour c β Brother Koyomi's favourite one, and takes the length of the longest among them to be the Koyomity of the garland.
For instance, let's say the garland is represented by "kooomo", and Brother Koyomi's favourite colour is "o". Among all subsegments containing pieces of "o" only, "ooo" is the longest, with a length of 3. Thus the Koyomity of this garland equals 3.
But problem arises as Nadeko is unsure about Brother Koyomi's favourite colour, and has swaying ideas on the amount of work to do. She has q plans on this, each of which can be expressed as a pair of an integer m_{i} and a lowercase letter c_{i}, meanings of which are explained above. You are to find out the maximum Koyomity achievable after repainting the garland according to each plan.
-----Input-----
The first line of input contains a positive integer n (1 β€ n β€ 1 500) β the length of the garland.
The second line contains n lowercase English letters s_1s_2... s_{n} as a string β the initial colours of paper pieces on the garland.
The third line contains a positive integer q (1 β€ q β€ 200 000) β the number of plans Nadeko has.
The next q lines describe one plan each: the i-th among them contains an integer m_{i} (1 β€ m_{i} β€ n) β the maximum amount of pieces to repaint, followed by a space, then by a lowercase English letter c_{i} β Koyomi's possible favourite colour.
-----Output-----
Output q lines: for each work plan, output one line containing an integer β the largest Koyomity achievable after repainting the garland according to it.
-----Examples-----
Input
6
koyomi
3
1 o
4 o
4 m
Output
3
6
5
Input
15
yamatonadeshiko
10
1 a
2 a
3 a
4 a
5 a
1 b
2 b
3 b
4 b
5 b
Output
3
4
5
7
8
1
2
3
4
5
Input
10
aaaaaaaaaa
2
10 b
10 z
Output
10
10
-----Note-----
In the first sample, there are three plans: In the first plan, at most 1 piece can be repainted. Repainting the "y" piece to become "o" results in "kooomi", whose Koyomity of 3 is the best achievable; In the second plan, at most 4 pieces can be repainted, and "oooooo" results in a Koyomity of 6; In the third plan, at most 4 pieces can be repainted, and "mmmmmi" and "kmmmmm" both result in a Koyomity of 5.
|
import sys
input = sys.stdin.readline
N = int(input())
S = list(map(lambda x: ord(x) - ord("a"), list(input())[:-1]))
res = [([0] * (N + 1)) for _ in range(26)]
for c in range(26):
for l in range(N):
x = 0
for r in range(l + 1, N + 1):
x += S[r - 1] != c
res[c][x] = max(res[c][x], r - l)
for i in range(N):
res[c][i + 1] = max(res[c][i + 1], res[c][i])
for _ in range(int(input())):
x, s = input().split()
x = int(x)
s = ord(s) - ord("a")
print(res[s][x])
|
IMPORT ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR STRING FUNC_CALL VAR FUNC_CALL VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER VAR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR VAR BIN_OP VAR NUMBER VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR VAR VAR BIN_OP VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER VAR VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR VAR VAR VAR
|
You are given two integer arrays a and b of length n.
You can reverse at most one subarray (continuous subsegment) of the array a.
Your task is to reverse such a subarray that the sum β_{i=1}^n a_i β
b_i is maximized.
Input
The first line contains one integer n (1 β€ n β€ 5000).
The second line contains n integers a_1, a_2, ..., a_n (1 β€ a_i β€ 10^7).
The third line contains n integers b_1, b_2, ..., b_n (1 β€ b_i β€ 10^7).
Output
Print single integer β maximum possible sum after reversing at most one subarray (continuous subsegment) of a.
Examples
Input
5
2 3 2 1 3
1 3 2 4 2
Output
29
Input
2
13 37
2 4
Output
174
Input
6
1 8 7 6 3 6
5 9 6 8 8 6
Output
235
Note
In the first example, you can reverse the subarray [4, 5]. Then a = [2, 3, 2, 3, 1] and 2 β
1 + 3 β
3 + 2 β
2 + 3 β
4 + 1 β
2 = 29.
In the second example, you don't need to use the reverse operation. 13 β
2 + 37 β
4 = 174.
In the third example, you can reverse the subarray [3, 5]. Then a = [1, 8, 3, 6, 7, 6] and 1 β
5 + 8 β
9 + 3 β
6 + 6 β
8 + 7 β
8 + 6 β
6 = 235.
|
def calc(l, r):
global max_diff
diff = 0
while l >= 0 and r < n:
diff += A[l] * (B[r] - B[l]) + A[r] * (B[l] - B[r])
max_diff = max(max_diff, diff)
l -= 1
r += 1
n = int(input())
A = tuple(map(int, input().split()))
B = tuple(map(int, input().split()))
res = sum(a * b for a, b in zip(A, B))
max_diff = 0
for i in range(n):
calc(i, i)
calc(i, i + 1)
print(res + max_diff)
|
FUNC_DEF ASSIGN VAR NUMBER WHILE VAR NUMBER VAR VAR VAR BIN_OP BIN_OP VAR VAR BIN_OP VAR VAR VAR VAR BIN_OP VAR VAR BIN_OP VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR VAR
|
You are given two integer arrays a and b of length n.
You can reverse at most one subarray (continuous subsegment) of the array a.
Your task is to reverse such a subarray that the sum β_{i=1}^n a_i β
b_i is maximized.
Input
The first line contains one integer n (1 β€ n β€ 5000).
The second line contains n integers a_1, a_2, ..., a_n (1 β€ a_i β€ 10^7).
The third line contains n integers b_1, b_2, ..., b_n (1 β€ b_i β€ 10^7).
Output
Print single integer β maximum possible sum after reversing at most one subarray (continuous subsegment) of a.
Examples
Input
5
2 3 2 1 3
1 3 2 4 2
Output
29
Input
2
13 37
2 4
Output
174
Input
6
1 8 7 6 3 6
5 9 6 8 8 6
Output
235
Note
In the first example, you can reverse the subarray [4, 5]. Then a = [2, 3, 2, 3, 1] and 2 β
1 + 3 β
3 + 2 β
2 + 3 β
4 + 1 β
2 = 29.
In the second example, you don't need to use the reverse operation. 13 β
2 + 37 β
4 = 174.
In the third example, you can reverse the subarray [3, 5]. Then a = [1, 8, 3, 6, 7, 6] and 1 β
5 + 8 β
9 + 3 β
6 + 6 β
8 + 7 β
8 + 6 β
6 = 235.
|
n = int(input())
a = list(map(int, input().split()))
b = list(map(int, input().split()))
ans, final = 0, 0
for i in range(n):
ans += a[i] * b[i]
temp = ans
final = ans
for i in range(n - 1):
ans = temp
left = i
right = i + 1
while left >= 0 and right <= n - 1:
ans += (a[left] - a[right]) * (b[right] - b[left])
final = max(ans, final)
left -= 1
right += 1
for i in range(n):
ans = temp
left = i
right = i
while left >= 0 and right <= n - 1:
ans += (a[left] - a[right]) * (b[right] - b[left])
final = max(ans, final)
left -= 1
right += 1
print(final)
|
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR VAR BIN_OP VAR VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR NUMBER WHILE VAR NUMBER VAR BIN_OP VAR NUMBER VAR BIN_OP BIN_OP VAR VAR VAR VAR BIN_OP VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR WHILE VAR NUMBER VAR BIN_OP VAR NUMBER VAR BIN_OP BIN_OP VAR VAR VAR VAR BIN_OP VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR
|
You are given two integer arrays a and b of length n.
You can reverse at most one subarray (continuous subsegment) of the array a.
Your task is to reverse such a subarray that the sum β_{i=1}^n a_i β
b_i is maximized.
Input
The first line contains one integer n (1 β€ n β€ 5000).
The second line contains n integers a_1, a_2, ..., a_n (1 β€ a_i β€ 10^7).
The third line contains n integers b_1, b_2, ..., b_n (1 β€ b_i β€ 10^7).
Output
Print single integer β maximum possible sum after reversing at most one subarray (continuous subsegment) of a.
Examples
Input
5
2 3 2 1 3
1 3 2 4 2
Output
29
Input
2
13 37
2 4
Output
174
Input
6
1 8 7 6 3 6
5 9 6 8 8 6
Output
235
Note
In the first example, you can reverse the subarray [4, 5]. Then a = [2, 3, 2, 3, 1] and 2 β
1 + 3 β
3 + 2 β
2 + 3 β
4 + 1 β
2 = 29.
In the second example, you don't need to use the reverse operation. 13 β
2 + 37 β
4 = 174.
In the third example, you can reverse the subarray [3, 5]. Then a = [1, 8, 3, 6, 7, 6] and 1 β
5 + 8 β
9 + 3 β
6 + 6 β
8 + 7 β
8 + 6 β
6 = 235.
|
import sys
def get_ints():
return list(map(int, sys.stdin.readline().strip().split()))
N = int(input())
A = get_ints()
B = get_ints()
total = 0
for i in range(N):
total += A[i] * B[i]
ans = total
for i in range(N):
x, y = i - 1, i + 1
temp_total = total
while 0 <= x < N and 0 <= y < N:
temp_total -= (A[x] - A[y]) * (B[x] - B[y])
ans = max(ans, temp_total)
x -= 1
y += 1
x, y = i, i + 1
temp_total = total
while 0 <= x < N and 0 <= y < N:
temp_total -= (A[x] - A[y]) * (B[x] - B[y])
ans = max(ans, temp_total)
x -= 1
y += 1
print(ans)
|
IMPORT FUNC_DEF RETURN FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR BIN_OP VAR VAR VAR VAR ASSIGN VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR WHILE NUMBER VAR VAR NUMBER VAR VAR VAR BIN_OP BIN_OP VAR VAR VAR VAR BIN_OP VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR NUMBER VAR NUMBER ASSIGN VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR WHILE NUMBER VAR VAR NUMBER VAR VAR VAR BIN_OP BIN_OP VAR VAR VAR VAR BIN_OP VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR
|
You are given two integer arrays a and b of length n.
You can reverse at most one subarray (continuous subsegment) of the array a.
Your task is to reverse such a subarray that the sum β_{i=1}^n a_i β
b_i is maximized.
Input
The first line contains one integer n (1 β€ n β€ 5000).
The second line contains n integers a_1, a_2, ..., a_n (1 β€ a_i β€ 10^7).
The third line contains n integers b_1, b_2, ..., b_n (1 β€ b_i β€ 10^7).
Output
Print single integer β maximum possible sum after reversing at most one subarray (continuous subsegment) of a.
Examples
Input
5
2 3 2 1 3
1 3 2 4 2
Output
29
Input
2
13 37
2 4
Output
174
Input
6
1 8 7 6 3 6
5 9 6 8 8 6
Output
235
Note
In the first example, you can reverse the subarray [4, 5]. Then a = [2, 3, 2, 3, 1] and 2 β
1 + 3 β
3 + 2 β
2 + 3 β
4 + 1 β
2 = 29.
In the second example, you don't need to use the reverse operation. 13 β
2 + 37 β
4 = 174.
In the third example, you can reverse the subarray [3, 5]. Then a = [1, 8, 3, 6, 7, 6] and 1 β
5 + 8 β
9 + 3 β
6 + 6 β
8 + 7 β
8 + 6 β
6 = 235.
|
t = 1
for i in range(t):
n = int(input())
a = [int(x) for x in input().split()]
b = [int(x) for x in input().split()]
ans = 0
temp = 0
for i in range(n):
temp += a[i] * b[i]
ans = temp
temp1 = temp
for i in range(n):
x = i - 1
y = i + 1
temp = temp1
while x >= 0 and y < n:
temp += (a[x] - a[y]) * b[y] + (a[y] - a[x]) * b[x]
ans = max(ans, temp)
x -= 1
y += 1
for i in range(n - 1):
x = i - 1
y = i + 2
temp = temp1
temp += (a[i] - a[i + 1]) * b[i + 1] + (a[i + 1] - a[i]) * b[i]
ans = max(ans, temp)
while x >= 0 and y < n:
temp = temp + (a[x] - a[y]) * b[y] + (a[y] - a[x]) * b[x]
ans = max(ans, temp)
x -= 1
y += 1
print(ans)
|
ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR BIN_OP VAR VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR VAR WHILE VAR NUMBER VAR VAR VAR BIN_OP BIN_OP BIN_OP VAR VAR VAR VAR VAR VAR BIN_OP BIN_OP VAR VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR BIN_OP BIN_OP BIN_OP VAR VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER BIN_OP BIN_OP VAR BIN_OP VAR NUMBER VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR WHILE VAR NUMBER VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR BIN_OP BIN_OP VAR VAR VAR VAR VAR VAR BIN_OP BIN_OP VAR VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR
|
You are given two integer arrays a and b of length n.
You can reverse at most one subarray (continuous subsegment) of the array a.
Your task is to reverse such a subarray that the sum β_{i=1}^n a_i β
b_i is maximized.
Input
The first line contains one integer n (1 β€ n β€ 5000).
The second line contains n integers a_1, a_2, ..., a_n (1 β€ a_i β€ 10^7).
The third line contains n integers b_1, b_2, ..., b_n (1 β€ b_i β€ 10^7).
Output
Print single integer β maximum possible sum after reversing at most one subarray (continuous subsegment) of a.
Examples
Input
5
2 3 2 1 3
1 3 2 4 2
Output
29
Input
2
13 37
2 4
Output
174
Input
6
1 8 7 6 3 6
5 9 6 8 8 6
Output
235
Note
In the first example, you can reverse the subarray [4, 5]. Then a = [2, 3, 2, 3, 1] and 2 β
1 + 3 β
3 + 2 β
2 + 3 β
4 + 1 β
2 = 29.
In the second example, you don't need to use the reverse operation. 13 β
2 + 37 β
4 = 174.
In the third example, you can reverse the subarray [3, 5]. Then a = [1, 8, 3, 6, 7, 6] and 1 β
5 + 8 β
9 + 3 β
6 + 6 β
8 + 7 β
8 + 6 β
6 = 235.
|
n = int(input())
a = list(map(int, input().split()))
b = list(map(int, input().split()))
ans = sum([(a[i] * b[i]) for i in range(n)])
def go(l, r):
diff = 0
mx = 0
while l >= 0 and r < n:
diff += a[l] * (b[r] - b[l])
diff += a[r] * (b[l] - b[r])
mx = max(mx, diff)
l -= 1
r += 1
return mx
mx = 0
for i in range(n - 1):
mx = max(mx, go(i, i), go(i, i + 1))
ans += mx
print(ans)
|
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR VAR FUNC_CALL VAR VAR FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR NUMBER VAR VAR VAR BIN_OP VAR VAR BIN_OP VAR VAR VAR VAR VAR BIN_OP VAR VAR BIN_OP VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR NUMBER VAR NUMBER RETURN VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR EXPR FUNC_CALL VAR VAR
|
You are given two integer arrays a and b of length n.
You can reverse at most one subarray (continuous subsegment) of the array a.
Your task is to reverse such a subarray that the sum β_{i=1}^n a_i β
b_i is maximized.
Input
The first line contains one integer n (1 β€ n β€ 5000).
The second line contains n integers a_1, a_2, ..., a_n (1 β€ a_i β€ 10^7).
The third line contains n integers b_1, b_2, ..., b_n (1 β€ b_i β€ 10^7).
Output
Print single integer β maximum possible sum after reversing at most one subarray (continuous subsegment) of a.
Examples
Input
5
2 3 2 1 3
1 3 2 4 2
Output
29
Input
2
13 37
2 4
Output
174
Input
6
1 8 7 6 3 6
5 9 6 8 8 6
Output
235
Note
In the first example, you can reverse the subarray [4, 5]. Then a = [2, 3, 2, 3, 1] and 2 β
1 + 3 β
3 + 2 β
2 + 3 β
4 + 1 β
2 = 29.
In the second example, you don't need to use the reverse operation. 13 β
2 + 37 β
4 = 174.
In the third example, you can reverse the subarray [3, 5]. Then a = [1, 8, 3, 6, 7, 6] and 1 β
5 + 8 β
9 + 3 β
6 + 6 β
8 + 7 β
8 + 6 β
6 = 235.
|
import sys
input = sys.stdin.readline
def solve():
n = int(input())
a = list(map(int, input().split()))
b = list(map(int, input().split()))
init = 0
for i in range(n):
init += a[i] * b[i]
dp1 = [(0) for j in range(n)]
dp2 = [(0) for j in range(n)]
m = 0
for i in range(1, n):
a1, a2 = a[i], a[i - 1]
b1, b2 = b[i], b[i - 1]
val = 0 - a1 * b1 - a2 * b2 + a1 * b2 + b1 * a2
dp2[i] = val
m = max(m, val)
ch = 0
for i in range(2, n):
for j in range(i - 1):
if (i + j) % 2 == 0:
a1, a2 = a[j], a[i]
b1, b2 = b[j], b[i]
dp1[(i + j + 1) // 2] = dp1[(i + j + 1) // 2] + (a1 - a2) * (b2 - b1)
m = max(m, dp1[(i + j + 1) // 2])
else:
a1, a2 = a[j], a[i]
b1, b2 = b[j], b[i]
dp2[(i + j + 1) // 2] = dp2[(i + j + 1) // 2] + (a1 - a2) * (b2 - b1)
m = max(m, dp2[(i + j + 1) // 2])
print(str(m + init))
solve()
|
IMPORT ASSIGN VAR VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR BIN_OP VAR VAR VAR VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP BIN_OP BIN_OP BIN_OP NUMBER BIN_OP VAR VAR BIN_OP VAR VAR BIN_OP VAR VAR BIN_OP VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER IF BIN_OP BIN_OP VAR VAR NUMBER NUMBER ASSIGN VAR VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR VAR NUMBER NUMBER BIN_OP VAR BIN_OP BIN_OP BIN_OP VAR VAR NUMBER NUMBER BIN_OP BIN_OP VAR VAR BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR BIN_OP BIN_OP BIN_OP VAR VAR NUMBER NUMBER ASSIGN VAR VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR VAR NUMBER NUMBER BIN_OP VAR BIN_OP BIN_OP BIN_OP VAR VAR NUMBER NUMBER BIN_OP BIN_OP VAR VAR BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR BIN_OP BIN_OP BIN_OP VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR
|
You are given two integer arrays a and b of length n.
You can reverse at most one subarray (continuous subsegment) of the array a.
Your task is to reverse such a subarray that the sum β_{i=1}^n a_i β
b_i is maximized.
Input
The first line contains one integer n (1 β€ n β€ 5000).
The second line contains n integers a_1, a_2, ..., a_n (1 β€ a_i β€ 10^7).
The third line contains n integers b_1, b_2, ..., b_n (1 β€ b_i β€ 10^7).
Output
Print single integer β maximum possible sum after reversing at most one subarray (continuous subsegment) of a.
Examples
Input
5
2 3 2 1 3
1 3 2 4 2
Output
29
Input
2
13 37
2 4
Output
174
Input
6
1 8 7 6 3 6
5 9 6 8 8 6
Output
235
Note
In the first example, you can reverse the subarray [4, 5]. Then a = [2, 3, 2, 3, 1] and 2 β
1 + 3 β
3 + 2 β
2 + 3 β
4 + 1 β
2 = 29.
In the second example, you don't need to use the reverse operation. 13 β
2 + 37 β
4 = 174.
In the third example, you can reverse the subarray [3, 5]. Then a = [1, 8, 3, 6, 7, 6] and 1 β
5 + 8 β
9 + 3 β
6 + 6 β
8 + 7 β
8 + 6 β
6 = 235.
|
n = int(input())
a = [0] * 5111
a = list(map(int, input().split()))
b = list(map(int, input().split()))
ans = 0
pre = [0] * 5111
suf = [0] * 5111
for i in range(n):
if i == 0:
pre[i] = a[i] * b[i]
else:
pre[i] = pre[i - 1] + a[i] * b[i]
for j in range(n - 1, -1, -1):
suf[j] = suf[j + 1] + a[j] * b[j]
for i in range(n):
maxx = 0
maxj = 0
summ = 0
t = min(i, n - i - 1) + 1
for j in range(t):
summ += (a[i - j] - a[i + j]) * (b[i + j] - b[i - j])
if summ > maxx:
maxx = summ
maxj = j
zs = pre[i - maxj - 1] if i - maxj - 1 >= 0 else 0
ys = suf[i + maxj + 1]
ms = pre[i + maxj] - zs + maxx
ans = max(ans, zs + ys + ms)
if i < n - 1:
summ = 0
maxx = 0
maxj = 0
t = min(i + 1, n - i - 2) + 1
for j in range(t):
summ += (a[i - j] - a[i + j + 1]) * (b[i + 1 + j] - b[i - j])
if summ > maxx:
maxx = summ
maxj = j
zs = pre[i - maxj - 1] if i - maxj - 1 >= 0 else 0
ys = suf[i + maxj + 2]
ms = pre[i + maxj + 1] - zs + maxx
ans = max(ans, zs + ys + ms)
print(ans)
|
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER NUMBER ASSIGN VAR FUNC_CALL 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 NUMBER ASSIGN VAR BIN_OP LIST NUMBER NUMBER ASSIGN VAR BIN_OP LIST NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR NUMBER ASSIGN VAR VAR BIN_OP VAR VAR VAR VAR ASSIGN VAR VAR BIN_OP VAR BIN_OP VAR NUMBER BIN_OP VAR VAR VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR VAR BIN_OP VAR BIN_OP VAR NUMBER BIN_OP VAR VAR VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR VAR BIN_OP BIN_OP VAR BIN_OP VAR VAR VAR BIN_OP VAR VAR BIN_OP VAR BIN_OP VAR VAR VAR BIN_OP VAR VAR IF VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER NUMBER VAR BIN_OP BIN_OP VAR VAR NUMBER NUMBER ASSIGN VAR VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR BIN_OP VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR VAR IF VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP BIN_OP VAR VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR VAR BIN_OP BIN_OP VAR BIN_OP VAR VAR VAR BIN_OP BIN_OP VAR VAR NUMBER BIN_OP VAR BIN_OP BIN_OP VAR NUMBER VAR VAR BIN_OP VAR VAR IF VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER NUMBER VAR BIN_OP BIN_OP VAR VAR NUMBER NUMBER ASSIGN VAR VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR BIN_OP BIN_OP VAR VAR NUMBER VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR VAR EXPR FUNC_CALL VAR VAR
|
You are given two integer arrays a and b of length n.
You can reverse at most one subarray (continuous subsegment) of the array a.
Your task is to reverse such a subarray that the sum β_{i=1}^n a_i β
b_i is maximized.
Input
The first line contains one integer n (1 β€ n β€ 5000).
The second line contains n integers a_1, a_2, ..., a_n (1 β€ a_i β€ 10^7).
The third line contains n integers b_1, b_2, ..., b_n (1 β€ b_i β€ 10^7).
Output
Print single integer β maximum possible sum after reversing at most one subarray (continuous subsegment) of a.
Examples
Input
5
2 3 2 1 3
1 3 2 4 2
Output
29
Input
2
13 37
2 4
Output
174
Input
6
1 8 7 6 3 6
5 9 6 8 8 6
Output
235
Note
In the first example, you can reverse the subarray [4, 5]. Then a = [2, 3, 2, 3, 1] and 2 β
1 + 3 β
3 + 2 β
2 + 3 β
4 + 1 β
2 = 29.
In the second example, you don't need to use the reverse operation. 13 β
2 + 37 β
4 = 174.
In the third example, you can reverse the subarray [3, 5]. Then a = [1, 8, 3, 6, 7, 6] and 1 β
5 + 8 β
9 + 3 β
6 + 6 β
8 + 7 β
8 + 6 β
6 = 235.
|
from sys import stdin
input = stdin.readline
_int = lambda: int(input())
_mult = lambda: map(int, input().split())
_list = lambda: list(map(int, input().split()))
def solve():
n = _int()
a = _list()
b = _list()
ans = 0
org = 0
for i in range(n):
org += a[i] * b[i]
ans = org
for k in range(n):
cur = org
p = k, k + 1
temp = traverse(p, cur, org, ans, n, a, b)
ans = max(ans, temp)
curr = a[k]
p = k - 1, k + 1
temp = traverse(p, cur, org, ans, n, a, b)
ans = max(ans, temp)
print(ans)
def traverse(p, cur, org, ans, n, a, b):
while check(p, n):
i, j = p
cur += (a[i] - a[j]) * (b[j] - b[i])
ans = max(ans, cur)
p = i - 1, j + 1
return ans
def check(p, n):
i, j = p
return 0 <= i < j and i < j < n
solve()
|
ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR 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 FUNC_DEF ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR BIN_OP VAR VAR VAR VAR ASSIGN VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR FUNC_DEF WHILE FUNC_CALL VAR VAR VAR ASSIGN VAR VAR VAR VAR BIN_OP BIN_OP VAR VAR VAR VAR BIN_OP VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER RETURN VAR FUNC_DEF ASSIGN VAR VAR VAR RETURN NUMBER VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR
|
You are given two integer arrays a and b of length n.
You can reverse at most one subarray (continuous subsegment) of the array a.
Your task is to reverse such a subarray that the sum β_{i=1}^n a_i β
b_i is maximized.
Input
The first line contains one integer n (1 β€ n β€ 5000).
The second line contains n integers a_1, a_2, ..., a_n (1 β€ a_i β€ 10^7).
The third line contains n integers b_1, b_2, ..., b_n (1 β€ b_i β€ 10^7).
Output
Print single integer β maximum possible sum after reversing at most one subarray (continuous subsegment) of a.
Examples
Input
5
2 3 2 1 3
1 3 2 4 2
Output
29
Input
2
13 37
2 4
Output
174
Input
6
1 8 7 6 3 6
5 9 6 8 8 6
Output
235
Note
In the first example, you can reverse the subarray [4, 5]. Then a = [2, 3, 2, 3, 1] and 2 β
1 + 3 β
3 + 2 β
2 + 3 β
4 + 1 β
2 = 29.
In the second example, you don't need to use the reverse operation. 13 β
2 + 37 β
4 = 174.
In the third example, you can reverse the subarray [3, 5]. Then a = [1, 8, 3, 6, 7, 6] and 1 β
5 + 8 β
9 + 3 β
6 + 6 β
8 + 7 β
8 + 6 β
6 = 235.
|
n = int(input())
a = list(map(int, input().split()))
b = list(map(int, input().split()))
prod = m = sum(a[i] * b[i] for i in range(n))
for i in range(n):
def g(l, r=i + 1, d=prod):
global m
while l >= 0 and r < n:
d = d - (a[l] - a[r]) * (b[l] - b[r])
m = max(m, d)
l = l - 1
r = r + 1
g(i - 1)
g(i)
print(m)
|
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR FUNC_DEF BIN_OP VAR NUMBER VAR WHILE VAR NUMBER VAR VAR ASSIGN VAR BIN_OP VAR BIN_OP BIN_OP VAR VAR VAR VAR BIN_OP VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR
|
You are given two integer arrays a and b of length n.
You can reverse at most one subarray (continuous subsegment) of the array a.
Your task is to reverse such a subarray that the sum β_{i=1}^n a_i β
b_i is maximized.
Input
The first line contains one integer n (1 β€ n β€ 5000).
The second line contains n integers a_1, a_2, ..., a_n (1 β€ a_i β€ 10^7).
The third line contains n integers b_1, b_2, ..., b_n (1 β€ b_i β€ 10^7).
Output
Print single integer β maximum possible sum after reversing at most one subarray (continuous subsegment) of a.
Examples
Input
5
2 3 2 1 3
1 3 2 4 2
Output
29
Input
2
13 37
2 4
Output
174
Input
6
1 8 7 6 3 6
5 9 6 8 8 6
Output
235
Note
In the first example, you can reverse the subarray [4, 5]. Then a = [2, 3, 2, 3, 1] and 2 β
1 + 3 β
3 + 2 β
2 + 3 β
4 + 1 β
2 = 29.
In the second example, you don't need to use the reverse operation. 13 β
2 + 37 β
4 = 174.
In the third example, you can reverse the subarray [3, 5]. Then a = [1, 8, 3, 6, 7, 6] and 1 β
5 + 8 β
9 + 3 β
6 + 6 β
8 + 7 β
8 + 6 β
6 = 235.
|
n = int(input())
A = list(map(int, input().split()))
B = list(map(int, input().split()))
cum = 0
for a, b in zip(A, B):
cum += a * b
ans = cum
for i in range(n):
count = cum
for j in range(1, min(i, n - 1 - i) + 1):
count += (A[i - j] - A[i + j]) * (B[i + j] - B[i - j])
if ans < count:
ans = count
for i in range(1, n):
count = cum
for j in range(1, min(i, n - i) + 1):
count += (A[i - j] - A[i + j - 1]) * (B[i + j - 1] - B[i - j])
if ans < count:
ans = count
print(ans)
|
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR VAR FUNC_CALL VAR VAR VAR VAR BIN_OP VAR VAR ASSIGN VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP FUNC_CALL VAR VAR BIN_OP BIN_OP VAR NUMBER VAR NUMBER VAR BIN_OP BIN_OP VAR BIN_OP VAR VAR VAR BIN_OP VAR VAR BIN_OP VAR BIN_OP VAR VAR VAR BIN_OP VAR VAR IF VAR VAR ASSIGN VAR VAR FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP FUNC_CALL VAR VAR BIN_OP VAR VAR NUMBER VAR BIN_OP BIN_OP VAR BIN_OP VAR VAR VAR BIN_OP BIN_OP VAR VAR NUMBER BIN_OP VAR BIN_OP BIN_OP VAR VAR NUMBER VAR BIN_OP VAR VAR IF VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR
|
You are given two integer arrays a and b of length n.
You can reverse at most one subarray (continuous subsegment) of the array a.
Your task is to reverse such a subarray that the sum β_{i=1}^n a_i β
b_i is maximized.
Input
The first line contains one integer n (1 β€ n β€ 5000).
The second line contains n integers a_1, a_2, ..., a_n (1 β€ a_i β€ 10^7).
The third line contains n integers b_1, b_2, ..., b_n (1 β€ b_i β€ 10^7).
Output
Print single integer β maximum possible sum after reversing at most one subarray (continuous subsegment) of a.
Examples
Input
5
2 3 2 1 3
1 3 2 4 2
Output
29
Input
2
13 37
2 4
Output
174
Input
6
1 8 7 6 3 6
5 9 6 8 8 6
Output
235
Note
In the first example, you can reverse the subarray [4, 5]. Then a = [2, 3, 2, 3, 1] and 2 β
1 + 3 β
3 + 2 β
2 + 3 β
4 + 1 β
2 = 29.
In the second example, you don't need to use the reverse operation. 13 β
2 + 37 β
4 = 174.
In the third example, you can reverse the subarray [3, 5]. Then a = [1, 8, 3, 6, 7, 6] and 1 β
5 + 8 β
9 + 3 β
6 + 6 β
8 + 7 β
8 + 6 β
6 = 235.
|
import sys
def solve(N, A, B):
s = 0
for i in range(N):
s += A[i] * B[i]
maxs = s + 0
for i in range(N - 1):
x = s + 0
swaps = min(i + 1, N - i - 1)
for j in range(swaps):
x += (A[i - j] - A[i + j + 1]) * (B[i + j + 1] - B[i - j])
if x > maxs:
maxs = x + 0
for i in range(1, N - 1):
x = s + 0
swaps = min(i, N - i - 1)
for j in range(swaps):
x += (A[i - j - 1] - A[i + j + 1]) * (B[i + j + 1] - B[i - j - 1])
if x > maxs:
maxs = x + 0
return maxs
def run():
out = ""
T = int(input())
A = [int(x) for x in input().split()]
B = [int(x) for x in input().split()]
print(solve(T, A, B))
run()
|
IMPORT FUNC_DEF ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR BIN_OP VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP BIN_OP VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR BIN_OP BIN_OP VAR BIN_OP VAR VAR VAR BIN_OP BIN_OP VAR VAR NUMBER BIN_OP VAR BIN_OP BIN_OP VAR VAR NUMBER VAR BIN_OP VAR VAR IF VAR VAR ASSIGN VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR BIN_OP BIN_OP VAR BIN_OP BIN_OP VAR VAR NUMBER VAR BIN_OP BIN_OP VAR VAR NUMBER BIN_OP VAR BIN_OP BIN_OP VAR VAR NUMBER VAR BIN_OP BIN_OP VAR VAR NUMBER IF VAR VAR ASSIGN VAR BIN_OP VAR NUMBER RETURN VAR FUNC_DEF ASSIGN VAR STRING ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR
|
You are given two integer arrays a and b of length n.
You can reverse at most one subarray (continuous subsegment) of the array a.
Your task is to reverse such a subarray that the sum β_{i=1}^n a_i β
b_i is maximized.
Input
The first line contains one integer n (1 β€ n β€ 5000).
The second line contains n integers a_1, a_2, ..., a_n (1 β€ a_i β€ 10^7).
The third line contains n integers b_1, b_2, ..., b_n (1 β€ b_i β€ 10^7).
Output
Print single integer β maximum possible sum after reversing at most one subarray (continuous subsegment) of a.
Examples
Input
5
2 3 2 1 3
1 3 2 4 2
Output
29
Input
2
13 37
2 4
Output
174
Input
6
1 8 7 6 3 6
5 9 6 8 8 6
Output
235
Note
In the first example, you can reverse the subarray [4, 5]. Then a = [2, 3, 2, 3, 1] and 2 β
1 + 3 β
3 + 2 β
2 + 3 β
4 + 1 β
2 = 29.
In the second example, you don't need to use the reverse operation. 13 β
2 + 37 β
4 = 174.
In the third example, you can reverse the subarray [3, 5]. Then a = [1, 8, 3, 6, 7, 6] and 1 β
5 + 8 β
9 + 3 β
6 + 6 β
8 + 7 β
8 + 6 β
6 = 235.
|
import sys
input = sys.stdin.readline
n = int(input())
a = list(map(int, input().split()))
b = list(map(int, input().split()))
p = sum([(a[i] * b[i]) for i in range(n)])
rez = p
for i in range(0, n - 1):
x = i
y = i + 1
dx = 0
while x >= 0 and y < n:
dx -= (a[y] - a[x]) * (b[y] - b[x])
rez = max(rez, p + dx)
x -= 1
y += 1
for i in range(1, n - 1):
x = i - 1
y = i + 1
dx = 0
while x >= 0 and y < n:
dx -= (a[y] - a[x]) * (b[y] - b[x])
rez = max(rez, p + dx)
x -= 1
y += 1
print(rez)
|
IMPORT ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER WHILE VAR NUMBER VAR VAR VAR BIN_OP BIN_OP VAR VAR VAR VAR BIN_OP VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER WHILE VAR NUMBER VAR VAR VAR BIN_OP BIN_OP VAR VAR VAR VAR BIN_OP VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR
|
You are given two integer arrays a and b of length n.
You can reverse at most one subarray (continuous subsegment) of the array a.
Your task is to reverse such a subarray that the sum β_{i=1}^n a_i β
b_i is maximized.
Input
The first line contains one integer n (1 β€ n β€ 5000).
The second line contains n integers a_1, a_2, ..., a_n (1 β€ a_i β€ 10^7).
The third line contains n integers b_1, b_2, ..., b_n (1 β€ b_i β€ 10^7).
Output
Print single integer β maximum possible sum after reversing at most one subarray (continuous subsegment) of a.
Examples
Input
5
2 3 2 1 3
1 3 2 4 2
Output
29
Input
2
13 37
2 4
Output
174
Input
6
1 8 7 6 3 6
5 9 6 8 8 6
Output
235
Note
In the first example, you can reverse the subarray [4, 5]. Then a = [2, 3, 2, 3, 1] and 2 β
1 + 3 β
3 + 2 β
2 + 3 β
4 + 1 β
2 = 29.
In the second example, you don't need to use the reverse operation. 13 β
2 + 37 β
4 = 174.
In the third example, you can reverse the subarray [3, 5]. Then a = [1, 8, 3, 6, 7, 6] and 1 β
5 + 8 β
9 + 3 β
6 + 6 β
8 + 7 β
8 + 6 β
6 = 235.
|
n = int(input())
a = [int(i) for i in input().split()]
b = [int(i) for i in input().split()]
base_sum = sum([(a * b) for a, b in zip(a, b)])
max_possible_sum = base_sum
for num in range(1, 2 * n - 2):
if num % 2 == 0:
left = num // 2 - 1
right = num // 2 + 1
else:
left = (num - 1) // 2
right = (num + 1) // 2
local_max_possible_sum = base_sum
while left >= 0 and right < n:
local_max_possible_sum -= (a[left] - a[right]) * (b[left] - b[right])
max_possible_sum = max(max_possible_sum, local_max_possible_sum)
left -= 1
right += 1
print(max_possible_sum)
|
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP BIN_OP NUMBER VAR NUMBER IF BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR WHILE VAR NUMBER VAR VAR VAR BIN_OP BIN_OP VAR VAR VAR VAR BIN_OP VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR
|
You are given two integer arrays a and b of length n.
You can reverse at most one subarray (continuous subsegment) of the array a.
Your task is to reverse such a subarray that the sum β_{i=1}^n a_i β
b_i is maximized.
Input
The first line contains one integer n (1 β€ n β€ 5000).
The second line contains n integers a_1, a_2, ..., a_n (1 β€ a_i β€ 10^7).
The third line contains n integers b_1, b_2, ..., b_n (1 β€ b_i β€ 10^7).
Output
Print single integer β maximum possible sum after reversing at most one subarray (continuous subsegment) of a.
Examples
Input
5
2 3 2 1 3
1 3 2 4 2
Output
29
Input
2
13 37
2 4
Output
174
Input
6
1 8 7 6 3 6
5 9 6 8 8 6
Output
235
Note
In the first example, you can reverse the subarray [4, 5]. Then a = [2, 3, 2, 3, 1] and 2 β
1 + 3 β
3 + 2 β
2 + 3 β
4 + 1 β
2 = 29.
In the second example, you don't need to use the reverse operation. 13 β
2 + 37 β
4 = 174.
In the third example, you can reverse the subarray [3, 5]. Then a = [1, 8, 3, 6, 7, 6] and 1 β
5 + 8 β
9 + 3 β
6 + 6 β
8 + 7 β
8 + 6 β
6 = 235.
|
import sys
def I():
return int(sys.stdin.readline().rstrip())
def MI():
return map(int, sys.stdin.readline().rstrip().split())
def LI():
return list(map(int, sys.stdin.readline().rstrip().split()))
def LI2():
return list(map(int, sys.stdin.readline().rstrip()))
def S():
return sys.stdin.readline().rstrip()
def LS():
return list(sys.stdin.readline().rstrip().split())
def LS2():
return list(sys.stdin.readline().rstrip())
def main():
n = I()
A = LI()
B = LI()
ans = 0
for a, b in zip(A, B):
ans += a * b
base = ans
for i in range(n - 1):
cur = 0
M = 0
for j in range(i, -1, -1):
k = 2 * i - j + 1
if k >= n:
break
a0, a1 = A[j], A[k]
b0, b1 = B[j], B[k]
cur -= (a0 - a1) * (b0 - b1)
M = max(M, cur)
ans = max(ans, base + M)
for i in range(n - 2):
cur = 0
M = 0
for j in range(i, -1, -1):
k = 2 * i - j + 2
if k >= n:
break
a0, a1 = A[j], A[k]
b0, b1 = B[j], B[k]
cur -= (a0 - a1) * (b0 - b1)
M = max(M, cur)
ans = max(ans, base + M)
print(ans)
main()
|
IMPORT FUNC_DEF RETURN FUNC_CALL VAR FUNC_CALL FUNC_CALL VAR FUNC_DEF RETURN 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 VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FUNC_DEF RETURN FUNC_CALL FUNC_CALL VAR FUNC_DEF RETURN FUNC_CALL VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR FUNC_DEF RETURN FUNC_CALL VAR FUNC_CALL FUNC_CALL VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR VAR FUNC_CALL VAR VAR VAR VAR BIN_OP VAR VAR ASSIGN VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR NUMBER NUMBER ASSIGN VAR BIN_OP BIN_OP BIN_OP NUMBER VAR VAR NUMBER IF VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR VAR BIN_OP BIN_OP VAR VAR BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR NUMBER NUMBER ASSIGN VAR BIN_OP BIN_OP BIN_OP NUMBER VAR VAR NUMBER IF VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR VAR BIN_OP BIN_OP VAR VAR BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR
|
You are given two integer arrays a and b of length n.
You can reverse at most one subarray (continuous subsegment) of the array a.
Your task is to reverse such a subarray that the sum β_{i=1}^n a_i β
b_i is maximized.
Input
The first line contains one integer n (1 β€ n β€ 5000).
The second line contains n integers a_1, a_2, ..., a_n (1 β€ a_i β€ 10^7).
The third line contains n integers b_1, b_2, ..., b_n (1 β€ b_i β€ 10^7).
Output
Print single integer β maximum possible sum after reversing at most one subarray (continuous subsegment) of a.
Examples
Input
5
2 3 2 1 3
1 3 2 4 2
Output
29
Input
2
13 37
2 4
Output
174
Input
6
1 8 7 6 3 6
5 9 6 8 8 6
Output
235
Note
In the first example, you can reverse the subarray [4, 5]. Then a = [2, 3, 2, 3, 1] and 2 β
1 + 3 β
3 + 2 β
2 + 3 β
4 + 1 β
2 = 29.
In the second example, you don't need to use the reverse operation. 13 β
2 + 37 β
4 = 174.
In the third example, you can reverse the subarray [3, 5]. Then a = [1, 8, 3, 6, 7, 6] and 1 β
5 + 8 β
9 + 3 β
6 + 6 β
8 + 7 β
8 + 6 β
6 = 235.
|
import sys
input = sys.stdin.readline
n = int(input())
a = list(map(int, input().split()))
b = list(map(int, input().split()))
num = 0
for i in range(n):
num += a[i] * b[i]
ans = 0
for i in range(1, n - 1):
ruiseki = 0
for j in range(1, min(i, n - 1 - i) + 1):
ruiseki += (a[i - j] - a[i + j]) * (b[i + j] - b[i - j])
ans = max(ans, ruiseki)
for i in range(n - 1):
ruiseki = 0
for j in range(1, min(i + 1, n - 1 - i) + 1):
ruiseki += (a[i - j + 1] - a[i + j]) * (b[i + j] - b[i - j + 1])
ans = max(ans, ruiseki)
print(ans + num)
|
IMPORT ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR BIN_OP VAR VAR VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP FUNC_CALL VAR VAR BIN_OP BIN_OP VAR NUMBER VAR NUMBER VAR BIN_OP BIN_OP VAR BIN_OP VAR VAR VAR BIN_OP VAR VAR BIN_OP VAR BIN_OP VAR VAR VAR BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP BIN_OP VAR NUMBER VAR NUMBER VAR BIN_OP BIN_OP VAR BIN_OP BIN_OP VAR VAR NUMBER VAR BIN_OP VAR VAR BIN_OP VAR BIN_OP VAR VAR VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR
|
You are given two integer arrays a and b of length n.
You can reverse at most one subarray (continuous subsegment) of the array a.
Your task is to reverse such a subarray that the sum β_{i=1}^n a_i β
b_i is maximized.
Input
The first line contains one integer n (1 β€ n β€ 5000).
The second line contains n integers a_1, a_2, ..., a_n (1 β€ a_i β€ 10^7).
The third line contains n integers b_1, b_2, ..., b_n (1 β€ b_i β€ 10^7).
Output
Print single integer β maximum possible sum after reversing at most one subarray (continuous subsegment) of a.
Examples
Input
5
2 3 2 1 3
1 3 2 4 2
Output
29
Input
2
13 37
2 4
Output
174
Input
6
1 8 7 6 3 6
5 9 6 8 8 6
Output
235
Note
In the first example, you can reverse the subarray [4, 5]. Then a = [2, 3, 2, 3, 1] and 2 β
1 + 3 β
3 + 2 β
2 + 3 β
4 + 1 β
2 = 29.
In the second example, you don't need to use the reverse operation. 13 β
2 + 37 β
4 = 174.
In the third example, you can reverse the subarray [3, 5]. Then a = [1, 8, 3, 6, 7, 6] and 1 β
5 + 8 β
9 + 3 β
6 + 6 β
8 + 7 β
8 + 6 β
6 = 235.
|
I = lambda: [*map(int, input().split())]
(n,) = I()
a = I()
b = I()
m = p = sum(a[i] * b[i] for i in range(n))
for i in range(n):
def g(l, d=p, r=i + 1):
global m
while l >= 0 and r < n:
d -= (a[l] - a[r]) * (b[l] - b[r])
m = max(m, d)
l -= 1
r += 1
g(i - 1)
g(i)
print(m)
|
ASSIGN VAR LIST FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR FUNC_DEF VAR BIN_OP VAR NUMBER WHILE VAR NUMBER VAR VAR VAR BIN_OP BIN_OP VAR VAR VAR VAR BIN_OP VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR
|
You are given two integer arrays a and b of length n.
You can reverse at most one subarray (continuous subsegment) of the array a.
Your task is to reverse such a subarray that the sum β_{i=1}^n a_i β
b_i is maximized.
Input
The first line contains one integer n (1 β€ n β€ 5000).
The second line contains n integers a_1, a_2, ..., a_n (1 β€ a_i β€ 10^7).
The third line contains n integers b_1, b_2, ..., b_n (1 β€ b_i β€ 10^7).
Output
Print single integer β maximum possible sum after reversing at most one subarray (continuous subsegment) of a.
Examples
Input
5
2 3 2 1 3
1 3 2 4 2
Output
29
Input
2
13 37
2 4
Output
174
Input
6
1 8 7 6 3 6
5 9 6 8 8 6
Output
235
Note
In the first example, you can reverse the subarray [4, 5]. Then a = [2, 3, 2, 3, 1] and 2 β
1 + 3 β
3 + 2 β
2 + 3 β
4 + 1 β
2 = 29.
In the second example, you don't need to use the reverse operation. 13 β
2 + 37 β
4 = 174.
In the third example, you can reverse the subarray [3, 5]. Then a = [1, 8, 3, 6, 7, 6] and 1 β
5 + 8 β
9 + 3 β
6 + 6 β
8 + 7 β
8 + 6 β
6 = 235.
|
N = int(input())
A = list(map(int, input().split()))
B = list(map(int, input().split()))
Base = 0
for i in range(N):
Base += A[i] * B[i]
Ans = Base
for i in range(1, N - 1):
Value = Base
L = i - 1
R = i + 1
while L >= 0 and R < N:
Value += (A[L] - A[R]) * (B[R] - B[L])
L -= 1
R += 1
if Value > Ans:
Ans = Value
for i in range(N - 1):
Value = Base
L = i
R = i + 1
while L >= 0 and R < N:
Value += (A[L] - A[R]) * (B[R] - B[L])
if Value > Ans:
Ans = Value
L -= 1
R += 1
print(Ans)
|
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR BIN_OP VAR VAR VAR VAR ASSIGN VAR VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER WHILE VAR NUMBER VAR VAR VAR BIN_OP BIN_OP VAR VAR VAR VAR BIN_OP VAR VAR VAR VAR VAR NUMBER VAR NUMBER IF VAR VAR ASSIGN VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR NUMBER WHILE VAR NUMBER VAR VAR VAR BIN_OP BIN_OP VAR VAR VAR VAR BIN_OP VAR VAR VAR VAR IF VAR VAR ASSIGN VAR VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR
|
You are given two integer arrays a and b of length n.
You can reverse at most one subarray (continuous subsegment) of the array a.
Your task is to reverse such a subarray that the sum β_{i=1}^n a_i β
b_i is maximized.
Input
The first line contains one integer n (1 β€ n β€ 5000).
The second line contains n integers a_1, a_2, ..., a_n (1 β€ a_i β€ 10^7).
The third line contains n integers b_1, b_2, ..., b_n (1 β€ b_i β€ 10^7).
Output
Print single integer β maximum possible sum after reversing at most one subarray (continuous subsegment) of a.
Examples
Input
5
2 3 2 1 3
1 3 2 4 2
Output
29
Input
2
13 37
2 4
Output
174
Input
6
1 8 7 6 3 6
5 9 6 8 8 6
Output
235
Note
In the first example, you can reverse the subarray [4, 5]. Then a = [2, 3, 2, 3, 1] and 2 β
1 + 3 β
3 + 2 β
2 + 3 β
4 + 1 β
2 = 29.
In the second example, you don't need to use the reverse operation. 13 β
2 + 37 β
4 = 174.
In the third example, you can reverse the subarray [3, 5]. Then a = [1, 8, 3, 6, 7, 6] and 1 β
5 + 8 β
9 + 3 β
6 + 6 β
8 + 7 β
8 + 6 β
6 = 235.
|
import sys
input = sys.stdin.buffer.readline
I = lambda: list(map(int, input().split()))
(n,) = I()
a = I()
b = I()
ans = an = sum(a[i] * b[i] for i in range(n))
for i in range(n):
l = r = i
temp = an
while l >= 0 and r < n:
temp -= (a[l] - a[r]) * (b[l] - b[r])
ans = max(ans, temp)
l -= 1
r += 1
l = r = i
r += 1
temp = an
while l >= 0 and r < n:
temp -= (a[l] - a[r]) * (b[l] - b[r])
ans = max(ans, temp)
l -= 1
r += 1
print(ans)
|
IMPORT ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR WHILE VAR NUMBER VAR VAR VAR BIN_OP BIN_OP VAR VAR VAR VAR BIN_OP VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR NUMBER VAR NUMBER ASSIGN VAR VAR VAR VAR NUMBER ASSIGN VAR VAR WHILE VAR NUMBER VAR VAR VAR BIN_OP BIN_OP VAR VAR VAR VAR BIN_OP VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR
|
You are given two integer arrays a and b of length n.
You can reverse at most one subarray (continuous subsegment) of the array a.
Your task is to reverse such a subarray that the sum β_{i=1}^n a_i β
b_i is maximized.
Input
The first line contains one integer n (1 β€ n β€ 5000).
The second line contains n integers a_1, a_2, ..., a_n (1 β€ a_i β€ 10^7).
The third line contains n integers b_1, b_2, ..., b_n (1 β€ b_i β€ 10^7).
Output
Print single integer β maximum possible sum after reversing at most one subarray (continuous subsegment) of a.
Examples
Input
5
2 3 2 1 3
1 3 2 4 2
Output
29
Input
2
13 37
2 4
Output
174
Input
6
1 8 7 6 3 6
5 9 6 8 8 6
Output
235
Note
In the first example, you can reverse the subarray [4, 5]. Then a = [2, 3, 2, 3, 1] and 2 β
1 + 3 β
3 + 2 β
2 + 3 β
4 + 1 β
2 = 29.
In the second example, you don't need to use the reverse operation. 13 β
2 + 37 β
4 = 174.
In the third example, you can reverse the subarray [3, 5]. Then a = [1, 8, 3, 6, 7, 6] and 1 β
5 + 8 β
9 + 3 β
6 + 6 β
8 + 7 β
8 + 6 β
6 = 235.
|
import sys
input = sys.stdin.readline
n = int(input())
A = list(map(int, input().split()))
B = list(map(int, input().split()))
C = []
S = 0
for i in range(n):
C.append(A[i] * B[i])
S += C[i]
ANS = S
for i in range(n):
XS = S
ANS = max(ANS, XS)
for j in range(i + 1):
if i - j >= 0 and i + j < n:
XS += (A[i + j] - A[i - j]) * (B[i - j] - B[i + j])
ANS = max(ANS, XS)
else:
break
if i >= 1:
XS = S - C[i - 1] - C[i] + A[i - 1] * B[i] + A[i] * B[i - 1]
ANS = max(ANS, XS)
for j in range(1, i):
if i - 1 - j >= 0 and i + j < n:
XS += (A[i + j] - A[i - j - 1]) * (B[i - j - 1] - B[i + j])
ANS = max(ANS, XS)
else:
break
print(ANS)
|
IMPORT ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR VAR VAR VAR ASSIGN VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER IF BIN_OP VAR VAR NUMBER BIN_OP VAR VAR VAR VAR BIN_OP BIN_OP VAR BIN_OP VAR VAR VAR BIN_OP VAR VAR BIN_OP VAR BIN_OP VAR VAR VAR BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR IF VAR NUMBER ASSIGN VAR BIN_OP BIN_OP BIN_OP BIN_OP VAR VAR BIN_OP VAR NUMBER VAR VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR FOR VAR FUNC_CALL VAR NUMBER VAR IF BIN_OP BIN_OP VAR NUMBER VAR NUMBER BIN_OP VAR VAR VAR VAR BIN_OP BIN_OP VAR BIN_OP VAR VAR VAR BIN_OP BIN_OP VAR VAR NUMBER BIN_OP VAR BIN_OP BIN_OP VAR VAR NUMBER VAR BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR
|
You are given two integer arrays a and b of length n.
You can reverse at most one subarray (continuous subsegment) of the array a.
Your task is to reverse such a subarray that the sum β_{i=1}^n a_i β
b_i is maximized.
Input
The first line contains one integer n (1 β€ n β€ 5000).
The second line contains n integers a_1, a_2, ..., a_n (1 β€ a_i β€ 10^7).
The third line contains n integers b_1, b_2, ..., b_n (1 β€ b_i β€ 10^7).
Output
Print single integer β maximum possible sum after reversing at most one subarray (continuous subsegment) of a.
Examples
Input
5
2 3 2 1 3
1 3 2 4 2
Output
29
Input
2
13 37
2 4
Output
174
Input
6
1 8 7 6 3 6
5 9 6 8 8 6
Output
235
Note
In the first example, you can reverse the subarray [4, 5]. Then a = [2, 3, 2, 3, 1] and 2 β
1 + 3 β
3 + 2 β
2 + 3 β
4 + 1 β
2 = 29.
In the second example, you don't need to use the reverse operation. 13 β
2 + 37 β
4 = 174.
In the third example, you can reverse the subarray [3, 5]. Then a = [1, 8, 3, 6, 7, 6] and 1 β
5 + 8 β
9 + 3 β
6 + 6 β
8 + 7 β
8 + 6 β
6 = 235.
|
n = int(input())
array1 = list(map(int, input().split()))
array2 = list(map(int, input().split()))
totalSum = sum([(array2[i] * array1[i]) for i in range(n)])
maxSum = totalSum
for i in range(n):
start = i
end = i
curSum = totalSum
while start >= 0 and end < n:
curSum += (array1[start] - array1[end]) * (array2[end] - array2[start])
start -= 1
end += 1
maxSum = max(maxSum, curSum)
for i in range(n - 1):
start = i
end = i + 1
curSum = totalSum
while start >= 0 and end < n:
curSum += (array1[start] - array1[end]) * (array2[end] - array2[start])
start -= 1
end += 1
maxSum = max(maxSum, curSum)
print(maxSum)
|
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR WHILE VAR NUMBER VAR VAR VAR BIN_OP BIN_OP VAR VAR VAR VAR BIN_OP VAR VAR VAR VAR VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR VAR WHILE VAR NUMBER VAR VAR VAR BIN_OP BIN_OP VAR VAR VAR VAR BIN_OP VAR VAR VAR VAR VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR
|
You are given two integer arrays a and b of length n.
You can reverse at most one subarray (continuous subsegment) of the array a.
Your task is to reverse such a subarray that the sum β_{i=1}^n a_i β
b_i is maximized.
Input
The first line contains one integer n (1 β€ n β€ 5000).
The second line contains n integers a_1, a_2, ..., a_n (1 β€ a_i β€ 10^7).
The third line contains n integers b_1, b_2, ..., b_n (1 β€ b_i β€ 10^7).
Output
Print single integer β maximum possible sum after reversing at most one subarray (continuous subsegment) of a.
Examples
Input
5
2 3 2 1 3
1 3 2 4 2
Output
29
Input
2
13 37
2 4
Output
174
Input
6
1 8 7 6 3 6
5 9 6 8 8 6
Output
235
Note
In the first example, you can reverse the subarray [4, 5]. Then a = [2, 3, 2, 3, 1] and 2 β
1 + 3 β
3 + 2 β
2 + 3 β
4 + 1 β
2 = 29.
In the second example, you don't need to use the reverse operation. 13 β
2 + 37 β
4 = 174.
In the third example, you can reverse the subarray [3, 5]. Then a = [1, 8, 3, 6, 7, 6] and 1 β
5 + 8 β
9 + 3 β
6 + 6 β
8 + 7 β
8 + 6 β
6 = 235.
|
n = int(input())
a = [*map(int, input().split())]
b = [*map(int, input().split())]
def sum(l1, l2):
x = 0
for i in range(len(l1)):
x += l1[i] * l2[i]
return x
m = s = sum(a, b)
for i in range(n):
for x in (i - 1, i):
temp = s
k = i + 1
while x >= 0 and k < n:
temp -= (a[x] - a[k]) * (b[x] - b[k])
m = max(m, temp)
x -= 1
k += 1
print(m)
|
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FUNC_DEF ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR BIN_OP VAR VAR VAR VAR RETURN VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR FOR VAR FUNC_CALL VAR VAR FOR VAR BIN_OP VAR NUMBER VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR NUMBER WHILE VAR NUMBER VAR VAR VAR BIN_OP BIN_OP VAR VAR VAR VAR BIN_OP VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR
|
You are given two integer arrays a and b of length n.
You can reverse at most one subarray (continuous subsegment) of the array a.
Your task is to reverse such a subarray that the sum β_{i=1}^n a_i β
b_i is maximized.
Input
The first line contains one integer n (1 β€ n β€ 5000).
The second line contains n integers a_1, a_2, ..., a_n (1 β€ a_i β€ 10^7).
The third line contains n integers b_1, b_2, ..., b_n (1 β€ b_i β€ 10^7).
Output
Print single integer β maximum possible sum after reversing at most one subarray (continuous subsegment) of a.
Examples
Input
5
2 3 2 1 3
1 3 2 4 2
Output
29
Input
2
13 37
2 4
Output
174
Input
6
1 8 7 6 3 6
5 9 6 8 8 6
Output
235
Note
In the first example, you can reverse the subarray [4, 5]. Then a = [2, 3, 2, 3, 1] and 2 β
1 + 3 β
3 + 2 β
2 + 3 β
4 + 1 β
2 = 29.
In the second example, you don't need to use the reverse operation. 13 β
2 + 37 β
4 = 174.
In the third example, you can reverse the subarray [3, 5]. Then a = [1, 8, 3, 6, 7, 6] and 1 β
5 + 8 β
9 + 3 β
6 + 6 β
8 + 7 β
8 + 6 β
6 = 235.
|
def main():
N = int(input())
A = list(map(int, input().split()))
B = list(map(int, input().split()))
def computePref(A, B):
cur = 0
pref = []
for a, b in zip(A, B):
cur += a * b
pref.append(cur)
return pref
pref = computePref(A, B)
suf = list(reversed(computePref(reversed(A), reversed(B))))
def compute(even):
ans = 0
for i in range(N):
l = i - even
r = i
cur = 0
while l >= 0 and r < N:
cur += A[l] * B[r]
if l != r:
cur += A[r] * B[l]
cand = cur
if l:
cand += pref[l - 1]
if r < N - 1:
cand += suf[r + 1]
ans = max(ans, cand)
l -= 1
r += 1
return ans
print(max(compute(True), compute(False)))
main()
|
FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR LIST FOR VAR VAR FUNC_CALL VAR VAR VAR VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR RETURN VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR FUNC_DEF ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR VAR ASSIGN VAR NUMBER WHILE VAR NUMBER VAR VAR VAR BIN_OP VAR VAR VAR VAR IF VAR VAR VAR BIN_OP VAR VAR VAR VAR ASSIGN VAR VAR IF VAR VAR VAR BIN_OP VAR NUMBER IF VAR BIN_OP VAR NUMBER VAR VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR NUMBER VAR NUMBER RETURN VAR EXPR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR
|
You are given two integer arrays a and b of length n.
You can reverse at most one subarray (continuous subsegment) of the array a.
Your task is to reverse such a subarray that the sum β_{i=1}^n a_i β
b_i is maximized.
Input
The first line contains one integer n (1 β€ n β€ 5000).
The second line contains n integers a_1, a_2, ..., a_n (1 β€ a_i β€ 10^7).
The third line contains n integers b_1, b_2, ..., b_n (1 β€ b_i β€ 10^7).
Output
Print single integer β maximum possible sum after reversing at most one subarray (continuous subsegment) of a.
Examples
Input
5
2 3 2 1 3
1 3 2 4 2
Output
29
Input
2
13 37
2 4
Output
174
Input
6
1 8 7 6 3 6
5 9 6 8 8 6
Output
235
Note
In the first example, you can reverse the subarray [4, 5]. Then a = [2, 3, 2, 3, 1] and 2 β
1 + 3 β
3 + 2 β
2 + 3 β
4 + 1 β
2 = 29.
In the second example, you don't need to use the reverse operation. 13 β
2 + 37 β
4 = 174.
In the third example, you can reverse the subarray [3, 5]. Then a = [1, 8, 3, 6, 7, 6] and 1 β
5 + 8 β
9 + 3 β
6 + 6 β
8 + 7 β
8 + 6 β
6 = 235.
|
def solvecaso():
n = int(input())
a = list(map(int, input().split()))
b = list(map(int, input().split()))
min = 0
for i in range(n):
min += a[i] * b[i]
max = 0
for i in range(n):
ini = i
fin = ini + 1
accum = 0
while ini >= 0 and fin < n:
accum += (a[ini] - a[fin]) * (b[fin] - b[ini])
if accum > max:
max = accum
ini -= 1
fin += 1
ini = i
fin = ini + 2
accum = 0
while ini >= 0 and fin < n:
accum += (a[ini] - a[fin]) * (b[fin] - b[ini])
if accum > max:
max = accum
ini -= 1
fin += 1
print(min + max)
solvecaso()
|
FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR BIN_OP VAR VAR VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER WHILE VAR NUMBER VAR VAR VAR BIN_OP BIN_OP VAR VAR VAR VAR BIN_OP VAR VAR VAR VAR IF VAR VAR ASSIGN VAR VAR VAR NUMBER VAR NUMBER ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER WHILE VAR NUMBER VAR VAR VAR BIN_OP BIN_OP VAR VAR VAR VAR BIN_OP VAR VAR VAR VAR IF VAR VAR ASSIGN VAR VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR
|
You are given two integer arrays a and b of length n.
You can reverse at most one subarray (continuous subsegment) of the array a.
Your task is to reverse such a subarray that the sum β_{i=1}^n a_i β
b_i is maximized.
Input
The first line contains one integer n (1 β€ n β€ 5000).
The second line contains n integers a_1, a_2, ..., a_n (1 β€ a_i β€ 10^7).
The third line contains n integers b_1, b_2, ..., b_n (1 β€ b_i β€ 10^7).
Output
Print single integer β maximum possible sum after reversing at most one subarray (continuous subsegment) of a.
Examples
Input
5
2 3 2 1 3
1 3 2 4 2
Output
29
Input
2
13 37
2 4
Output
174
Input
6
1 8 7 6 3 6
5 9 6 8 8 6
Output
235
Note
In the first example, you can reverse the subarray [4, 5]. Then a = [2, 3, 2, 3, 1] and 2 β
1 + 3 β
3 + 2 β
2 + 3 β
4 + 1 β
2 = 29.
In the second example, you don't need to use the reverse operation. 13 β
2 + 37 β
4 = 174.
In the third example, you can reverse the subarray [3, 5]. Then a = [1, 8, 3, 6, 7, 6] and 1 β
5 + 8 β
9 + 3 β
6 + 6 β
8 + 7 β
8 + 6 β
6 = 235.
|
import sys
def main():
n = int(input())
a = readIntArr()
b = readIntArr()
default = 0
for i in range(n):
default += a[i] * b[i]
def swapDiff(l, r):
return (a[r] - a[l]) * (b[l] - b[r])
maxDelta = 0
for center in range(n):
temp = 0
temp2 = 0
l1, r1 = center - 1, center + 1
while l1 >= 0 and r1 < n:
temp = temp + swapDiff(l1, r1)
maxDelta = max(maxDelta, temp)
l1 -= 1
r1 += 1
l2, r2 = center, center + 1
while l2 >= 0 and r2 < n:
temp2 = temp2 + swapDiff(l2, r2)
maxDelta = max(maxDelta, temp2)
l2 -= 1
r2 += 1
print(default + maxDelta)
return
input = sys.stdin.buffer.readline
def oneLineArrayPrint(arr):
print(" ".join([str(x) for x in arr]))
def multiLineArrayPrint(arr):
print("\n".join([str(x) for x in arr]))
def multiLineArrayOfArraysPrint(arr):
print("\n".join([" ".join([str(x) for x in y]) for y in arr]))
def readIntArr():
return [int(x) for x in input().split()]
def makeArr(defaultValFactory, dimensionArr):
dv = defaultValFactory
da = dimensionArr
if len(da) == 1:
return [dv() for _ in range(da[0])]
else:
return [makeArr(dv, da[1:]) for _ in range(da[0])]
def queryInteractive(x, y):
print("? {} {}".format(x, y))
sys.stdout.flush()
return int(input())
def answerInteractive(ans):
print("! {}".format(ans))
sys.stdout.flush()
inf = float("inf")
MOD = 10**9 + 7
for _abc in range(1):
main()
|
IMPORT FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR BIN_OP VAR VAR VAR VAR FUNC_DEF RETURN BIN_OP BIN_OP VAR VAR VAR VAR BIN_OP VAR VAR VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER WHILE VAR NUMBER VAR VAR ASSIGN VAR BIN_OP VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR NUMBER VAR NUMBER ASSIGN VAR VAR VAR BIN_OP VAR NUMBER WHILE VAR NUMBER VAR VAR ASSIGN VAR BIN_OP VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR VAR RETURN ASSIGN VAR VAR FUNC_DEF EXPR FUNC_CALL VAR FUNC_CALL STRING FUNC_CALL VAR VAR VAR VAR FUNC_DEF EXPR FUNC_CALL VAR FUNC_CALL STRING FUNC_CALL VAR VAR VAR VAR FUNC_DEF EXPR FUNC_CALL VAR FUNC_CALL STRING FUNC_CALL STRING FUNC_CALL VAR VAR VAR VAR VAR VAR FUNC_DEF RETURN FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR FUNC_DEF ASSIGN VAR VAR ASSIGN VAR VAR IF FUNC_CALL VAR VAR NUMBER RETURN FUNC_CALL VAR VAR FUNC_CALL VAR VAR NUMBER RETURN FUNC_CALL VAR VAR VAR NUMBER VAR FUNC_CALL VAR VAR NUMBER FUNC_DEF EXPR FUNC_CALL VAR FUNC_CALL STRING VAR VAR EXPR FUNC_CALL VAR RETURN FUNC_CALL VAR FUNC_CALL VAR FUNC_DEF EXPR FUNC_CALL VAR FUNC_CALL STRING VAR EXPR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR STRING ASSIGN VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR
|
You are given two integer arrays a and b of length n.
You can reverse at most one subarray (continuous subsegment) of the array a.
Your task is to reverse such a subarray that the sum β_{i=1}^n a_i β
b_i is maximized.
Input
The first line contains one integer n (1 β€ n β€ 5000).
The second line contains n integers a_1, a_2, ..., a_n (1 β€ a_i β€ 10^7).
The third line contains n integers b_1, b_2, ..., b_n (1 β€ b_i β€ 10^7).
Output
Print single integer β maximum possible sum after reversing at most one subarray (continuous subsegment) of a.
Examples
Input
5
2 3 2 1 3
1 3 2 4 2
Output
29
Input
2
13 37
2 4
Output
174
Input
6
1 8 7 6 3 6
5 9 6 8 8 6
Output
235
Note
In the first example, you can reverse the subarray [4, 5]. Then a = [2, 3, 2, 3, 1] and 2 β
1 + 3 β
3 + 2 β
2 + 3 β
4 + 1 β
2 = 29.
In the second example, you don't need to use the reverse operation. 13 β
2 + 37 β
4 = 174.
In the third example, you can reverse the subarray [3, 5]. Then a = [1, 8, 3, 6, 7, 6] and 1 β
5 + 8 β
9 + 3 β
6 + 6 β
8 + 7 β
8 + 6 β
6 = 235.
|
def readline():
return map(int, input().split())
def main():
n = int(input())
a = list(readline())
b = list(readline())
max_diff = 0
for begin in range(n):
for end in (begin, begin + 1):
index_sum = begin + end
diff = 0
for i in range(end, min(n, index_sum + 1)):
diff -= (a[i] - a[index_sum - i]) * (b[i] - b[index_sum - i])
max_diff = max(diff, max_diff)
print(sum(ai * bi for ai, bi in zip(a, b)) + max_diff)
main()
|
FUNC_DEF RETURN FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR FOR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR BIN_OP BIN_OP VAR VAR VAR BIN_OP VAR VAR BIN_OP VAR VAR VAR BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP FUNC_CALL VAR BIN_OP VAR VAR VAR VAR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR
|
You are given two integer arrays a and b of length n.
You can reverse at most one subarray (continuous subsegment) of the array a.
Your task is to reverse such a subarray that the sum β_{i=1}^n a_i β
b_i is maximized.
Input
The first line contains one integer n (1 β€ n β€ 5000).
The second line contains n integers a_1, a_2, ..., a_n (1 β€ a_i β€ 10^7).
The third line contains n integers b_1, b_2, ..., b_n (1 β€ b_i β€ 10^7).
Output
Print single integer β maximum possible sum after reversing at most one subarray (continuous subsegment) of a.
Examples
Input
5
2 3 2 1 3
1 3 2 4 2
Output
29
Input
2
13 37
2 4
Output
174
Input
6
1 8 7 6 3 6
5 9 6 8 8 6
Output
235
Note
In the first example, you can reverse the subarray [4, 5]. Then a = [2, 3, 2, 3, 1] and 2 β
1 + 3 β
3 + 2 β
2 + 3 β
4 + 1 β
2 = 29.
In the second example, you don't need to use the reverse operation. 13 β
2 + 37 β
4 = 174.
In the third example, you can reverse the subarray [3, 5]. Then a = [1, 8, 3, 6, 7, 6] and 1 β
5 + 8 β
9 + 3 β
6 + 6 β
8 + 7 β
8 + 6 β
6 = 235.
|
def func(l, r, d):
global ans
while l >= 0 and r < n:
d -= (a[l] - a[r]) * (b[l] - b[r])
ans = max(ans, d)
l -= 1
r += 1
n = int(input())
a = list(map(int, input().split()))
b = list(map(int, input().split()))
ans = 0
p = 0
for i in range(n):
ans += a[i] * b[i]
p += a[i] * b[i]
for i in range(n):
func(i - 1, i + 1, p)
func(i, i + 1, p)
print(ans)
|
FUNC_DEF WHILE VAR NUMBER VAR VAR VAR BIN_OP BIN_OP VAR VAR VAR VAR BIN_OP VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR BIN_OP VAR VAR VAR VAR VAR BIN_OP VAR VAR VAR VAR FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR VAR
|
You are given two integer arrays a and b of length n.
You can reverse at most one subarray (continuous subsegment) of the array a.
Your task is to reverse such a subarray that the sum β_{i=1}^n a_i β
b_i is maximized.
Input
The first line contains one integer n (1 β€ n β€ 5000).
The second line contains n integers a_1, a_2, ..., a_n (1 β€ a_i β€ 10^7).
The third line contains n integers b_1, b_2, ..., b_n (1 β€ b_i β€ 10^7).
Output
Print single integer β maximum possible sum after reversing at most one subarray (continuous subsegment) of a.
Examples
Input
5
2 3 2 1 3
1 3 2 4 2
Output
29
Input
2
13 37
2 4
Output
174
Input
6
1 8 7 6 3 6
5 9 6 8 8 6
Output
235
Note
In the first example, you can reverse the subarray [4, 5]. Then a = [2, 3, 2, 3, 1] and 2 β
1 + 3 β
3 + 2 β
2 + 3 β
4 + 1 β
2 = 29.
In the second example, you don't need to use the reverse operation. 13 β
2 + 37 β
4 = 174.
In the third example, you can reverse the subarray [3, 5]. Then a = [1, 8, 3, 6, 7, 6] and 1 β
5 + 8 β
9 + 3 β
6 + 6 β
8 + 7 β
8 + 6 β
6 = 235.
|
n = int(input())
a = list(map(int, input().split()))
b = list(map(int, input().split()))
pre = [0] * (n + 2)
suf = [0] * (n + 2)
for i in range(n):
pre[i + 1] = pre[i] + a[i] * b[i]
for i in range(n, 0, -1):
suf[i] = suf[i + 1] + a[i - 1] * b[i - 1]
ans = pre[n]
tmp = ans
for pivot in range(n):
tmp = pre[n]
l = pivot - 1
r = pivot + 1
while l >= 0 and r < n:
tmp -= (a[l] - a[r]) * (b[l] - b[r])
ans = max(ans, tmp)
l -= 1
r += 1
l = pivot
r = pivot + 1
tmp = pre[n]
while l >= 0 and r < n:
tmp -= (a[l] - a[r]) * (b[l] - b[r])
ans = max(ans, tmp)
l -= 1
r += 1
print(ans)
|
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR NUMBER BIN_OP VAR VAR BIN_OP VAR VAR VAR VAR FOR VAR FUNC_CALL VAR VAR NUMBER NUMBER ASSIGN VAR VAR BIN_OP VAR BIN_OP VAR NUMBER BIN_OP VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER WHILE VAR NUMBER VAR VAR VAR BIN_OP BIN_OP VAR VAR VAR VAR BIN_OP VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR NUMBER VAR NUMBER ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR WHILE VAR NUMBER VAR VAR VAR BIN_OP BIN_OP VAR VAR VAR VAR BIN_OP VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR
|
You are given two integer arrays a and b of length n.
You can reverse at most one subarray (continuous subsegment) of the array a.
Your task is to reverse such a subarray that the sum β_{i=1}^n a_i β
b_i is maximized.
Input
The first line contains one integer n (1 β€ n β€ 5000).
The second line contains n integers a_1, a_2, ..., a_n (1 β€ a_i β€ 10^7).
The third line contains n integers b_1, b_2, ..., b_n (1 β€ b_i β€ 10^7).
Output
Print single integer β maximum possible sum after reversing at most one subarray (continuous subsegment) of a.
Examples
Input
5
2 3 2 1 3
1 3 2 4 2
Output
29
Input
2
13 37
2 4
Output
174
Input
6
1 8 7 6 3 6
5 9 6 8 8 6
Output
235
Note
In the first example, you can reverse the subarray [4, 5]. Then a = [2, 3, 2, 3, 1] and 2 β
1 + 3 β
3 + 2 β
2 + 3 β
4 + 1 β
2 = 29.
In the second example, you don't need to use the reverse operation. 13 β
2 + 37 β
4 = 174.
In the third example, you can reverse the subarray [3, 5]. Then a = [1, 8, 3, 6, 7, 6] and 1 β
5 + 8 β
9 + 3 β
6 + 6 β
8 + 7 β
8 + 6 β
6 = 235.
|
from itertools import accumulate
from sys import stdin
input = stdin.readline
rn = lambda: int(input())
rns = lambda: map(int, input().split())
rl = lambda: list(map(int, input().split()))
rs = lambda: input()
YN = lambda x: print("YES") if x else print("NO")
mod = 10**9 + 7
n = rn()
a = rl()
b = rl()
prods = [(a[i] * b[i]) for i in range(n)]
pre = list(accumulate(prods))
suff = list(accumulate(prods[::-1]))[::-1]
ans = pre[-1]
change = 0
for i in range(2 * (n - 1) - 1):
if i == 0 or i == 1:
change = max(change, (a[i + 1] - a[0]) * (b[0] - b[i + 1]))
else:
acc = 0
x = i % 2
y = i // 2
while y >= 0 and x < n - 1 - y:
acc += (a[y] - a[y + x + 1]) * (b[y + x + 1] - b[y])
change = max(change, acc)
y -= 1
x += 2
print(ans + change)
|
ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR 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 ASSIGN VAR VAR FUNC_CALL VAR STRING FUNC_CALL VAR STRING ASSIGN VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR BIN_OP VAR VAR VAR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP BIN_OP NUMBER BIN_OP VAR NUMBER NUMBER IF VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER VAR NUMBER BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER WHILE VAR NUMBER VAR BIN_OP BIN_OP VAR NUMBER VAR VAR BIN_OP BIN_OP VAR VAR VAR BIN_OP BIN_OP VAR VAR NUMBER BIN_OP VAR BIN_OP BIN_OP VAR VAR NUMBER VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR VAR
|
You are given two integer arrays a and b of length n.
You can reverse at most one subarray (continuous subsegment) of the array a.
Your task is to reverse such a subarray that the sum β_{i=1}^n a_i β
b_i is maximized.
Input
The first line contains one integer n (1 β€ n β€ 5000).
The second line contains n integers a_1, a_2, ..., a_n (1 β€ a_i β€ 10^7).
The third line contains n integers b_1, b_2, ..., b_n (1 β€ b_i β€ 10^7).
Output
Print single integer β maximum possible sum after reversing at most one subarray (continuous subsegment) of a.
Examples
Input
5
2 3 2 1 3
1 3 2 4 2
Output
29
Input
2
13 37
2 4
Output
174
Input
6
1 8 7 6 3 6
5 9 6 8 8 6
Output
235
Note
In the first example, you can reverse the subarray [4, 5]. Then a = [2, 3, 2, 3, 1] and 2 β
1 + 3 β
3 + 2 β
2 + 3 β
4 + 1 β
2 = 29.
In the second example, you don't need to use the reverse operation. 13 β
2 + 37 β
4 = 174.
In the third example, you can reverse the subarray [3, 5]. Then a = [1, 8, 3, 6, 7, 6] and 1 β
5 + 8 β
9 + 3 β
6 + 6 β
8 + 7 β
8 + 6 β
6 = 235.
|
from sys import stderr, stdin
input = stdin.readline
def dbp(*args, **kwargs):
print(*args, file=stderr, **kwargs)
def get_int_list():
return [int(x) for x in input().strip().split()]
def slow_solve(alist, blist):
n = len(alist)
ans = sum(alist[i] * blist[i] for i in range(n))
for l in range(n + 1):
for r in range(l, n + 1):
alist[l:r] = alist[l:r][::-1]
newans = sum(alist[i] * blist[i] for i in range(n))
ans = max(ans, newans)
alist[l:r] = alist[l:r][::-1]
return ans
def med_solve(al, bl):
n = len(al)
psl = [(al[i] * bl[i]) for i in range(n)]
for i in range(1, n):
psl[i] += psl[i - 1]
dbp(al)
dbp(bl)
dbp(psl)
ans = psl[-1]
for midx in range(n):
dbp("odds")
ext = min(midx, n - midx - 1)
midsum = al[midx] * bl[midx]
for x in range(1, ext + 1):
l = midx - x
r = midx + x
dbp("lmr:", l, midx, r)
midsum += al[l] * bl[r] + al[r] * bl[l]
newtot = psl[l - 1] if l > 0 else 0
newtot += midsum + psl[-1] - psl[r]
dbp(
"newtot:",
newtot,
"from:",
psl[l - 1] if l > 0 else "*",
midsum,
psl[-1] - psl[r],
)
ans = max(newtot, ans)
dbp("evens")
if midx == n - 1:
continue
ext = min(midx, max(0, n - midx - 2))
midsum = 0
for x in range(ext + 1):
l = midx - x
r = midx + 1 + x
dbp("lmr:", l, midx, r)
midsum += al[l] * bl[r] + al[r] * bl[l]
newtot = psl[l - 1] if l > 0 else 0
newtot += midsum + psl[-1] - psl[r]
dbp(
"newtot:",
newtot,
"from:",
psl[l - 1] if l > 0 else "*",
midsum,
psl[-1] - psl[r],
)
ans = max(newtot, ans)
return ans
def fast_solve(al, bl):
n = len(al)
ss = ans = sum(al[i] * bl[i] for i in range(n))
for midx in range(n):
l = midx - 1
r = midx + 1
cs = ss
while l >= 0 and r < n:
cs -= (al[l] - al[r]) * (bl[l] - bl[r])
ans = max(ans, cs)
l -= 1
r += 1
l = midx
r = midx + 1
cs = ss
while l >= 0 and r < n:
cs -= (al[l] - al[r]) * (bl[l] - bl[r])
ans = max(ans, cs)
l -= 1
r += 1
return ans
def do_thing():
n = int(input())
alist = get_int_list()
blist = get_int_list()
assert n == len(alist) == len(blist)
r = fast_solve(alist, blist)
return r
print(do_thing())
|
ASSIGN VAR VAR FUNC_DEF EXPR FUNC_CALL VAR VAR VAR VAR FUNC_DEF RETURN FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR NUMBER RETURN VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR VAR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR NUMBER VAR VAR VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR VAR VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR STRING VAR VAR VAR VAR BIN_OP BIN_OP VAR VAR VAR VAR BIN_OP VAR VAR VAR VAR ASSIGN VAR VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER VAR BIN_OP BIN_OP VAR VAR NUMBER VAR VAR EXPR FUNC_CALL VAR STRING VAR STRING VAR NUMBER VAR BIN_OP VAR NUMBER STRING VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR STRING IF VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR NUMBER BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR STRING VAR VAR VAR VAR BIN_OP BIN_OP VAR VAR VAR VAR BIN_OP VAR VAR VAR VAR ASSIGN VAR VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER VAR BIN_OP BIN_OP VAR VAR NUMBER VAR VAR EXPR FUNC_CALL VAR STRING VAR STRING VAR NUMBER VAR BIN_OP VAR NUMBER STRING VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR RETURN VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR VAR WHILE VAR NUMBER VAR VAR VAR BIN_OP BIN_OP VAR VAR VAR VAR BIN_OP VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR NUMBER VAR NUMBER ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR VAR WHILE VAR NUMBER VAR VAR VAR BIN_OP BIN_OP VAR VAR VAR VAR BIN_OP VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR NUMBER VAR NUMBER RETURN VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR RETURN VAR EXPR FUNC_CALL VAR FUNC_CALL VAR
|
You are given two integer arrays a and b of length n.
You can reverse at most one subarray (continuous subsegment) of the array a.
Your task is to reverse such a subarray that the sum β_{i=1}^n a_i β
b_i is maximized.
Input
The first line contains one integer n (1 β€ n β€ 5000).
The second line contains n integers a_1, a_2, ..., a_n (1 β€ a_i β€ 10^7).
The third line contains n integers b_1, b_2, ..., b_n (1 β€ b_i β€ 10^7).
Output
Print single integer β maximum possible sum after reversing at most one subarray (continuous subsegment) of a.
Examples
Input
5
2 3 2 1 3
1 3 2 4 2
Output
29
Input
2
13 37
2 4
Output
174
Input
6
1 8 7 6 3 6
5 9 6 8 8 6
Output
235
Note
In the first example, you can reverse the subarray [4, 5]. Then a = [2, 3, 2, 3, 1] and 2 β
1 + 3 β
3 + 2 β
2 + 3 β
4 + 1 β
2 = 29.
In the second example, you don't need to use the reverse operation. 13 β
2 + 37 β
4 = 174.
In the third example, you can reverse the subarray [3, 5]. Then a = [1, 8, 3, 6, 7, 6] and 1 β
5 + 8 β
9 + 3 β
6 + 6 β
8 + 7 β
8 + 6 β
6 = 235.
|
n = int(input())
a = list(map(int, input().split()))
b = list(map(int, input().split()))
old = sum(x * y for x, y in zip(a, b))
ans = old
for i in range(n):
d = 0
s = old
while i - d >= 0 and i + d < n:
s -= (a[i + d] - a[i - d]) * (b[i + d] - b[i - d])
d += 1
ans = max(ans, s)
d = 0
s = old
while i - d >= 0 and i + d + 1 < n:
s -= (a[i + d + 1] - a[i - d]) * (b[i + d + 1] - b[i - d])
d += 1
ans = max(ans, s)
print(ans)
|
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR VAR WHILE BIN_OP VAR VAR NUMBER BIN_OP VAR VAR VAR VAR BIN_OP BIN_OP VAR BIN_OP VAR VAR VAR BIN_OP VAR VAR BIN_OP VAR BIN_OP VAR VAR VAR BIN_OP VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR VAR WHILE BIN_OP VAR VAR NUMBER BIN_OP BIN_OP VAR VAR NUMBER VAR VAR BIN_OP BIN_OP VAR BIN_OP BIN_OP VAR VAR NUMBER VAR BIN_OP VAR VAR BIN_OP VAR BIN_OP BIN_OP VAR VAR NUMBER VAR BIN_OP VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR
|
You are given two integer arrays a and b of length n.
You can reverse at most one subarray (continuous subsegment) of the array a.
Your task is to reverse such a subarray that the sum β_{i=1}^n a_i β
b_i is maximized.
Input
The first line contains one integer n (1 β€ n β€ 5000).
The second line contains n integers a_1, a_2, ..., a_n (1 β€ a_i β€ 10^7).
The third line contains n integers b_1, b_2, ..., b_n (1 β€ b_i β€ 10^7).
Output
Print single integer β maximum possible sum after reversing at most one subarray (continuous subsegment) of a.
Examples
Input
5
2 3 2 1 3
1 3 2 4 2
Output
29
Input
2
13 37
2 4
Output
174
Input
6
1 8 7 6 3 6
5 9 6 8 8 6
Output
235
Note
In the first example, you can reverse the subarray [4, 5]. Then a = [2, 3, 2, 3, 1] and 2 β
1 + 3 β
3 + 2 β
2 + 3 β
4 + 1 β
2 = 29.
In the second example, you don't need to use the reverse operation. 13 β
2 + 37 β
4 = 174.
In the third example, you can reverse the subarray [3, 5]. Then a = [1, 8, 3, 6, 7, 6] and 1 β
5 + 8 β
9 + 3 β
6 + 6 β
8 + 7 β
8 + 6 β
6 = 235.
|
import sys
input = sys.stdin.readline
N = int(input())
A = list(map(int, input().split()))
B = list(map(int, input().split()))
tmp = sum([(A[i] * B[i]) for i in range(N)])
ans = tmp
for i in range(N):
l, r = i - 1, i + 1
res = tmp
while 0 <= l and r < N:
delta = -(A[r] - A[l]) * (B[r] - B[l])
res += delta
ans = max(ans, res)
l -= 1
r += 1
l, r = i, i + 1
res = tmp
while 0 <= l and r < N:
delta = -(A[r] - A[l]) * (B[r] - B[l])
res += delta
ans = max(ans, res)
l -= 1
r += 1
print(ans)
|
IMPORT ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR WHILE NUMBER VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR VAR BIN_OP VAR VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR NUMBER VAR NUMBER ASSIGN VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR WHILE NUMBER VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR VAR BIN_OP VAR VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR NUMBER 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
input = sys.stdin.readline
for _ in range(int(input())):
s = list(map(int, list(input().strip())))
ans = len(s)
for i in range(10):
for j in range(10):
idx = 0
isSwapped = False
delete = 0
while idx < len(s):
match = i if isSwapped else j
while idx < len(s) and s[idx] != match:
delete += 1
idx += 1
idx += 1
isSwapped = not isSwapped
if (len(s) - delete) % 2 == 0:
ans = min(ans, delete)
for i in range(10):
ans = min(ans, len(s) - s.count(i))
print(ans)
|
IMPORT ASSIGN VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL 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 NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR WHILE VAR FUNC_CALL VAR VAR VAR VAR VAR VAR NUMBER VAR NUMBER VAR NUMBER ASSIGN VAR VAR IF BIN_OP BIN_OP FUNC_CALL VAR VAR VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR FOR VAR FUNC_CALL VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP FUNC_CALL VAR 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.
|
from sys import stdin
for _ in range(int(stdin.readline())):
s = stdin.readline()
n = len(s)
if len(s) > 2:
m = 2
else:
print(0)
continue
for i in range(10):
m = max(m, s.count(str(i)))
s1 = s[0]
for i in s[1:n]:
if i != s1[-1]:
s1 += i
s = s1
for i in "0123456789":
if i in s:
j = s.index(i)
d1 = {}
d2 = {}
for c in "0123456789":
d1[c] = 0
temp = s[j + 1 :]
for k in temp:
if k != i:
d2[k] = 1
else:
for c in d2.keys():
d1[c] += 1
d2 = {}
m = max(m, max(d1.values()) * 2)
j = n - s[::-1].index(i)
d1 = {}
d2 = {}
for c in "0123456789":
d1[c] = 0
temp = s[:j]
for k in temp:
if k != i:
d2[k] = 1
else:
for c in d2.keys():
d1[c] += 1
d2 = {}
m = max(m, max(d1.values()) * 2)
print(n - m - 1)
|
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER EXPR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR VAR NUMBER FOR VAR VAR NUMBER VAR IF VAR VAR NUMBER VAR VAR ASSIGN VAR VAR FOR VAR STRING IF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR DICT ASSIGN VAR DICT FOR VAR STRING ASSIGN VAR VAR NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER FOR VAR VAR IF VAR VAR ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR DICT ASSIGN VAR FUNC_CALL VAR VAR BIN_OP FUNC_CALL VAR FUNC_CALL VAR NUMBER ASSIGN VAR BIN_OP VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR DICT ASSIGN VAR DICT FOR VAR STRING ASSIGN VAR VAR NUMBER ASSIGN VAR VAR VAR FOR VAR VAR IF VAR VAR ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR DICT ASSIGN VAR FUNC_CALL VAR VAR BIN_OP FUNC_CALL VAR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR BIN_OP BIN_OP 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 _ in range(int(input())):
S = input()
ans = 0
for i in range(10):
for j in range(10):
c = str(i)
l = 0
for k in S:
if k == c:
l += 1
if c == str(j):
c = str(i)
else:
c = str(j)
if l % 2 and i != j:
l -= 1
ans = max(ans, l)
print(len(S) - ans)
|
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR 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 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 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 i in range(t):
s = input()
n = len(s)
ans = 2
for j in range(10):
for k in range(10):
l = [str(j), str(k)]
c = 0
a = 0
for p in s:
if p == l[a]:
c += 1
a ^= 1
if c % 2 and l[0] != l[1]:
ans = max(ans, c - 1)
else:
ans = max(ans, c)
print(n - 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 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 VAR IF VAR VAR VAR VAR NUMBER VAR NUMBER IF BIN_OP VAR NUMBER VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL 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.
|
def calc(a, b, s):
t = 0
for i in range(len(s)):
if t & 1 and s[i] == b:
t += 1
elif t & 1 == 0 and s[i] == a:
t += 1
return len(s) - t // 2 * 2
for _ in range(int(input())):
s = input()
ans = 1000000
for i in range(0, 10):
temp = s.count(str(i)[0])
if temp & 1 == 0:
temp -= 1
ans = min(ans, len(s) - temp)
ans = min(ans, calc("0", str(i), s))
for i in range(10, 100):
s1 = str(i)
a = s1[0]
b = s1[1]
ans = min(ans, calc(a, b, s))
print(ans)
|
FUNC_DEF ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF BIN_OP VAR NUMBER VAR VAR VAR VAR NUMBER IF BIN_OP VAR NUMBER NUMBER VAR VAR VAR VAR NUMBER RETURN BIN_OP FUNC_CALL VAR VAR BIN_OP BIN_OP VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER IF BIN_OP VAR NUMBER NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR STRING FUNC_CALL VAR VAR VAR FOR VAR FUNC_CALL VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL 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())
def find(s, i, j):
res = 0
for ch in s:
if ch == i:
res += 1
i, j = j, i
if i != j and res % 2 == 1:
res -= 1
return res
for _ in range(t):
s = input()
correct = 0
for i in range(10):
for j in range(10):
correct = max(correct, find(s, str(i), str(j)))
print(len(s) - correct)
|
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 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 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.
|
import gc
def calc(arr):
counts = []
for i in range(10):
for j in range(10):
s = i + j
target = i
count = 0
for a in arr:
if int(a) == target:
target = s - target
else:
count += 1
if not i == target:
count += 1
counts.append(count)
return min(counts)
def main():
n = int(input())
arr = []
for _ in range(n):
arr.append(calc(input()))
gc.collect()
for a in arr:
print(a)
main()
|
IMPORT FUNC_DEF ASSIGN VAR LIST FOR VAR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR VAR ASSIGN VAR NUMBER FOR VAR VAR IF FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR NUMBER IF VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR RETURN FUNC_CALL VAR VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR EXPR FUNC_CALL VAR FOR VAR VAR EXPR 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.
|
t = int(input())
while t > 0:
a = list(str(input()))
d = {}
d["0"] = []
d["1"] = []
d["2"] = []
d["3"] = []
d["4"] = []
d["5"] = []
d["6"] = []
d["7"] = []
d["8"] = []
d["9"] = []
for i in range(len(a)):
d[a[i]].append(i)
co = 0
g = d.keys()
for i in d.keys():
co = max(co, len(d[i]))
for i in range(9):
a1 = d[str(i)]
if len(a1) == 0:
continue
for j in range(i + 1, 10):
a1 = d[str(i)]
a2 = d[str(j)]
if len(a2) == 0:
continue
count = 0
if a1[0] > a2[0]:
a1d = []
for k in range(len(a1)):
a1d.append([a1[k], 1])
a2d = []
for k in range(len(a2)):
a2d.append([a2[k], 0])
c = list(a1d)
c.extend(a2d)
c.sort(key=lambda x: x[0])
ini = 0
for k in range(len(a1d) + len(a2d)):
if ini == c[k][1]:
count += 1
if ini == 0:
ini = 1
else:
ini = 0
if count % 2 != 0:
count = count - 1
else:
a1d = []
for k in range(len(a1)):
a1d.append([a1[k], 1])
a2d = []
for k in range(len(a2)):
a2d.append([a2[k], 0])
c = list(a1d)
c.extend(a2d)
c.sort(key=lambda x: x[0])
ini = 1
for k in range(len(a1d) + len(a2d)):
if ini == c[k][1]:
count += 1
if ini == 0:
ini = 1
else:
ini = 0
if count % 2 != 0:
count = count - 1
if count > co:
co = count
print(len(a) - co)
t = t - 1
|
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR WHILE VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR DICT ASSIGN VAR STRING LIST ASSIGN VAR STRING LIST ASSIGN VAR STRING LIST ASSIGN VAR STRING LIST ASSIGN VAR STRING LIST ASSIGN VAR STRING LIST ASSIGN VAR STRING LIST ASSIGN VAR STRING LIST ASSIGN VAR STRING LIST ASSIGN VAR STRING LIST FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR FOR VAR FUNC_CALL VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER IF VAR NUMBER VAR NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR LIST VAR VAR NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR LIST VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR VAR IF VAR VAR VAR NUMBER VAR NUMBER IF VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER IF BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR LIST VAR VAR NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR LIST VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR VAR IF VAR VAR VAR NUMBER VAR NUMBER IF VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER IF BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR NUMBER IF VAR VAR ASSIGN 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())
for case in range(t):
s = list(map(int, list(input())))
maxCounter = 0
for i in range(10):
for j in range(10):
prev = -1
counter = 0
for k in s:
if (k == i or k == j) and (i != j and k != prev or i == j):
counter += 1
prev = k
if i != j:
if counter % 2 == 1:
counter -= 1
if counter > maxCounter:
maxCounter = counter
print(len(s) - maxCounter)
|
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 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 VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR NUMBER ASSIGN VAR VAR IF VAR VAR IF BIN_OP VAR NUMBER NUMBER VAR NUMBER IF VAR VAR ASSIGN 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 = input().strip()
dp = [0] * 10
for c in s:
dp[ord(c) - ord("0")] += 1
ans = max(dp)
for i in range(10):
for j in range(i + 1, 10):
a, b = str(i), str(j)
f = None
cnt = 0
for c in s:
if c == a:
if f == None:
f = True
cnt = 1
elif f == False:
cnt += 1
f = not f
elif c == b:
if f == None:
f = False
cnt = 1
elif f == True:
cnt += 1
f = not f
ans = max(ans, cnt - cnt % 2)
print(len(s) - ans)
|
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER NUMBER FOR VAR VAR VAR BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR STRING NUMBER ASSIGN VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR NONE ASSIGN VAR NUMBER FOR VAR VAR IF VAR VAR IF VAR NONE ASSIGN VAR NUMBER ASSIGN VAR NUMBER IF VAR NUMBER VAR NUMBER ASSIGN VAR VAR IF VAR VAR IF VAR NONE ASSIGN VAR NUMBER ASSIGN VAR NUMBER IF VAR NUMBER 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 sol(s, x, y):
c = 0
for i in s:
if int(i) == x:
c += 1
x, y = y, x
if x != y and c & 1:
c -= 1
return c
for i in range(int(input())):
ans = 0
s = input()
n = len(s)
for x in range(10):
for y in range(10):
ans = max(ans, sol(s, x, y))
print(n - ans)
|
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 VAR NUMBER RETURN VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR NUMBER 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 FUNC_CALL VAR 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.
|
T = int(input())
for _ in range(0, T):
s = input()
n = len(s)
pre = [0] * 10
for i in range(0, len(s)):
pre[ord(s[i]) - 48] += 1
ans = n - 2
for i in range(10):
ans = min(ans, n - pre[i])
for i in range(10):
for j in range(10):
k1 = str(i)
k2 = str(j)
c = 0
turn = 0
for k in range(0, len(s)):
if turn == 0 and s[k] == k1:
turn = 1
c += 1
elif turn == 1 and s[k] == k2:
turn = 0
c += 1
if c % 2 != 0:
c -= 1
ans = min(ans, n - c)
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 BIN_OP LIST NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR VAR BIN_OP FUNC_CALL VAR VAR VAR NUMBER NUMBER ASSIGN VAR BIN_OP 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 FOR VAR FUNC_CALL VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR IF VAR NUMBER VAR VAR VAR ASSIGN VAR NUMBER VAR NUMBER IF VAR NUMBER VAR VAR VAR ASSIGN VAR NUMBER VAR NUMBER IF BIN_OP VAR NUMBER 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.
|
for _ in range(int(input())):
s = input()
x = [0] * 10
for i in s:
x[int(i)] += 1
mmm = max(x)
lll = [i for i in range(10) if x[i] > mmm / 2]
pat = []
for i in lll:
for j in lll:
if i != j:
pat.append((i, j))
if len(pat) < 1:
print(len(s) - mmm)
continue
sol = [0] * len(pat)
for i in range(len(s)):
for j in range(len(pat)):
if int(s[i]) == pat[j][sol[j] % 2]:
sol[j] += 1
print(len(s) - max(max(sol) // 2 * 2, mmm))
|
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER NUMBER FOR VAR VAR VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR NUMBER VAR VAR BIN_OP VAR NUMBER ASSIGN VAR LIST FOR VAR VAR FOR VAR VAR IF VAR VAR EXPR FUNC_CALL VAR VAR VAR IF FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP LIST NUMBER FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR VAR VAR VAR BIN_OP VAR VAR NUMBER VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR BIN_OP BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER 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 func(i, j, s):
p = [i, j]
need = 0
count = 0
for k in s:
if k == p[need]:
count += 1
need ^= 1
if i != j and count % 2 == 1:
count -= 1
return count
for _ in range(int(input())):
s = input()
ans = 2
for i in range(10):
for j in range(10):
ans = max(ans, func(str(i), str(j), s))
print(len(s) - ans)
|
FUNC_DEF ASSIGN VAR LIST VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR IF VAR VAR VAR VAR NUMBER VAR NUMBER IF VAR VAR BIN_OP VAR NUMBER NUMBER VAR NUMBER RETURN VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR 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 FUNC_CALL VAR 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 = list(input())
n = len(s)
ans = n - 2
lis = [str(i) for i in range(10)]
for i in lis:
for j in lis:
cnt = 0
swt = 1
for k in range(n):
if swt:
if s[k] == i:
swt = 0
else:
cnt += 1
elif s[k] == j:
swt = 1
else:
cnt += 1
if i != j and (n - cnt) % 2 != 0:
cnt += 1
ans = min(cnt, ans)
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 VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR NUMBER FOR VAR VAR FOR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR IF VAR VAR VAR ASSIGN VAR NUMBER VAR NUMBER IF VAR VAR VAR ASSIGN VAR NUMBER VAR NUMBER IF VAR VAR BIN_OP BIN_OP VAR VAR NUMBER NUMBER 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 numOfChars(s, c):
ans = 0
for k in s:
if k == c:
ans += 1
return ans
def calcMaxSubseq(s, charOne, charTwo):
maxL = 0
counter = 0
for c in s:
if counter == 0 and c == charOne:
counter = 1
maxL += 1
elif counter == 1 and c == charTwo:
counter = 0
maxL += 1
if counter == 1:
return maxL - 1
else:
return maxL
t = int(input())
for i in range(t):
s = input()
l = len(s)
maxL = 0
print
for j in range(10):
maxL = max(maxL, numOfChars(s, str(j)))
for k in range(j + 1, 10):
maxL = max(maxL, calcMaxSubseq(s, str(j), str(k)))
maxL = max(maxL, calcMaxSubseq(s, str(k), str(j)))
print(l - maxL)
|
FUNC_DEF ASSIGN VAR NUMBER FOR VAR VAR IF VAR VAR VAR NUMBER RETURN VAR FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR IF VAR NUMBER VAR VAR ASSIGN VAR NUMBER VAR NUMBER IF VAR NUMBER VAR VAR ASSIGN VAR NUMBER VAR NUMBER IF VAR NUMBER RETURN BIN_OP VAR NUMBER RETURN VAR 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 EXPR VAR FOR VAR FUNC_CALL VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR 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.
|
t = int(input())
for _ in range(t):
s = input()
if len(s) == 2:
print(0)
continue
d = {}
d1 = {}
for i in range(10):
for j in range(10):
if i == j:
d1[str(i)] = 0
else:
d[str(i) + str(j)] = 0
n = len(s)
for j in d:
f = 0
for i in range(n):
if s[i] == j[0] and f == 0:
f = 1
if s[i] == j[1] and f == 1:
d[j] += 1
f = 0
for j in d1:
for i in s:
if i == j:
d1[j] += 1
p = max(2 * max(d.values()), max(d1.values()))
print(n - p)
|
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR IF FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR NUMBER ASSIGN VAR DICT ASSIGN VAR DICT FOR VAR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER IF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR FOR VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER IF VAR VAR VAR NUMBER VAR NUMBER VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR FOR VAR VAR IF VAR VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP NUMBER FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL 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] * 10
for x in s:
a[int(x)] += 1
a = [x for x in a if x]
a.sort(reverse=True)
if len(a) == 1:
print(0)
else:
ans = a[0]
for x in range(10):
for y in range(10):
if x != y:
sm = 0
b = [str(x), str(y)]
i = 0
j = 0
while j < n:
if s[j] == b[i]:
sm += 1
i ^= 1
j += 1
ans = max(ans, 2 * (sm // 2))
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 BIN_OP LIST NUMBER NUMBER FOR VAR VAR VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR NUMBER IF FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR NUMBER ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER IF VAR VAR ASSIGN VAR NUMBER ASSIGN VAR LIST FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR IF VAR VAR VAR VAR VAR NUMBER VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP NUMBER 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.
|
import sys
input = sys.stdin.readline
(T,) = map(int, input().split())
for _ in range(T):
s = input().strip()
R = 0
for i in range(10):
for j in range(10):
tmp = 0
x = [i, j]
r = 0
for c in s:
c = int(c)
if x[tmp] == c:
tmp = 1 - tmp
r += 1
if i != j:
r = r // 2 * 2
R = max(R, r)
print(len(s) - R)
|
IMPORT ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR LIST VAR VAR ASSIGN VAR NUMBER FOR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR IF VAR VAR VAR ASSIGN VAR BIN_OP NUMBER VAR VAR NUMBER IF VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR NUMBER 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.
|
import sys
input = sys.stdin.readline
def inp():
return int(input())
def inlt():
return list(map(int, input().split()))
def insr():
s = input()
return s[: len(s) - 1]
def invr():
return map(int, input().split())
T = inp()
for t in range(T):
S = insr()
if len(S) < 3:
print(0)
else:
maxx = 0
for i in range(100):
curr = str(i)
if i < 10:
curr = "0" + curr
fl = True
tmp = 0
for j in range(len(S)):
if fl:
if S[j] == curr[0]:
fl = False
elif S[j] == curr[1]:
fl = True
tmp += 2
maxx = max(tmp, maxx)
for i in range(10):
tmp = S.count(str(i))
maxx = max(tmp, maxx)
print(len(S) - maxx)
|
IMPORT ASSIGN VAR VAR FUNC_DEF RETURN FUNC_CALL VAR FUNC_CALL VAR FUNC_DEF RETURN FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR RETURN VAR BIN_OP FUNC_CALL VAR VAR NUMBER FUNC_DEF RETURN FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR IF FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR IF VAR NUMBER ASSIGN VAR BIN_OP STRING VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR IF VAR VAR VAR NUMBER ASSIGN VAR NUMBER IF VAR VAR 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 FUNC_CALL VAR VAR 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.
|
import sys
input = lambda: sys.stdin.readline().rstrip()
for _ in range(int(input())):
a = [int(i) for i in input()]
n = len(a)
ans = 0
p = [0, 0]
for i in range(0, 10):
for j in range(0, 10):
cnt = 0
cur = [i, j]
ind = 0
for k in a:
if k == cur[ind]:
cnt += 1
ind ^= 1
if i != j:
cnt -= cnt & 1
ans = max(ans, cnt)
print(n - ans)
|
IMPORT ASSIGN VAR FUNC_CALL FUNC_CALL VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR LIST NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR LIST VAR VAR ASSIGN VAR NUMBER FOR VAR VAR IF VAR VAR VAR VAR NUMBER VAR NUMBER IF 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.
|
def solve_util(s, x, y):
res = 0
for c in s:
if c == x:
res += 1
x, y = y, x
if x != y and res % 2 == 1:
return res - 1
return res
def solve(s):
ans = 0
a = [str(i) for i in range(10)]
for x in a:
for y in a:
ans = max(ans, solve_util(s, x, y))
return ans
def read():
t = int(input())
for i in range(t):
s = input()
ans = solve(s)
print(len(s) - ans)
read()
|
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 RETURN BIN_OP VAR NUMBER RETURN VAR FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR NUMBER FOR VAR VAR FOR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR VAR RETURN VAR FUNC_DEF 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 EXPR FUNC_CALL VAR BIN_OP FUNC_CALL VAR 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.
|
def count(s, a):
b = 0
cur = 0
for i in s:
if i == a[0]:
b = 1
elif b and i == a[1]:
cur += 1
b = 0
return cur
t = int(input())
for k in range(t):
s = input()
mx = 0
for i in range(10):
for j in range(10):
if i == j:
mx = max(mx, s.count(str(i)))
else:
s1 = str(i) + str(j)
mx = max(mx, 2 * count(s, s1))
print(len(s) - mx)
|
FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR IF VAR VAR NUMBER ASSIGN VAR NUMBER IF VAR VAR VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER RETURN VAR ASSIGN VAR FUNC_CALL 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 IF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP NUMBER 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 = input()
ans = 1000000
for i in range(10):
temp = 0
for j in s:
if j != str(i):
temp += 1
ans = min(ans, temp)
for i in range(10):
for j in range(10):
temp = 0
l = True
for k in s:
if l:
if k != str(i):
temp += 1
else:
l = False
elif k != str(j):
temp += 1
else:
l = True
if (len(s) - temp) % 2 != 0:
ans = min(ans, temp + 1)
else:
ans = min(ans, temp)
print(ans)
|
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR IF VAR FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR FOR VAR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR IF VAR IF VAR FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR NUMBER IF VAR FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR NUMBER IF BIN_OP BIN_OP FUNC_CALL VAR VAR VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP 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 main(x, y):
c = x
count = 0
for i in range(n):
if s[i] == c:
count += 1
if c == x:
c = y
else:
c = x
if count & 1:
if x == y:
return count
return count - 1
return count
a = "0123456789"
for _ in range(int(input())):
s = input()
n = len(s)
ans = 0
for i in a:
for j in a:
ans = max(ans, main(i, j))
print(n - ans)
|
FUNC_DEF ASSIGN VAR VAR ASSIGN VAR NUMBER 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 IF VAR VAR RETURN VAR RETURN BIN_OP VAR NUMBER RETURN VAR ASSIGN VAR STRING 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 FOR VAR VAR FOR VAR VAR ASSIGN VAR FUNC_CALL VAR 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.
|
def alt(a, i, j):
c = 0
now = i
for k in range(len(a)):
if a[k] == i and now == i:
now = j
elif a[k] == j and now == j:
now = i
c += 1
return c * 2
def main():
for _ in range(int(input())):
a = list(input())
x = [i for i in range(10)]
m = 0
for i in range(10):
for j in range(10):
if i != j:
m = max(m, alt(a, str(i), str(j)))
else:
m = max(m, a.count(str(i)))
print(len(a) - m)
main()
|
FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR VAR ASSIGN VAR VAR IF VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR NUMBER RETURN BIN_OP VAR NUMBER FUNC_DEF FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR VAR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER 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 FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP FUNC_CALL VAR 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.
|
possibles = []
for i in range(10):
for j in range(10):
possibles.append(str(i) + str(j))
for _ in range(int(input())):
s = input()
max = 2
for st in possibles:
length = 0
j = 0
for digit in s:
if digit == st[j]:
length += 1
if j == 0:
j += 1
if st[0] == st[1] and length > max:
max = length
else:
j -= 1
if length > max:
max = length
print(len(s) - max)
|
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 FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR IF VAR VAR VAR VAR NUMBER IF VAR NUMBER VAR NUMBER IF VAR NUMBER VAR NUMBER VAR VAR ASSIGN VAR VAR VAR NUMBER IF VAR VAR ASSIGN 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 = input()
we = len(s)
d = {}
l = []
for i in s:
if i not in d:
d[i] = 1
else:
d[i] += 1
for i in d:
l.append(d[i])
l_pair = []
for i in d:
for j in d:
if i != j:
ss = i + j
l_pair.append(ss)
for i in range(len(l_pair)):
cnt = 0
flag = 0
for j in range(we):
if flag == 0 and s[j] == l_pair[i][0]:
flag = 1
elif flag == 1 and s[j] == l_pair[i][1]:
cnt += 1
flag = 0
l.append(cnt * 2)
an = max(max(l), 2)
print(we - an)
|
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR DICT ASSIGN VAR LIST FOR VAR VAR IF VAR VAR ASSIGN VAR VAR NUMBER VAR VAR NUMBER FOR VAR VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR LIST FOR VAR VAR FOR VAR VAR IF VAR VAR ASSIGN VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR NUMBER VAR VAR VAR VAR NUMBER ASSIGN VAR NUMBER IF VAR NUMBER VAR VAR VAR VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR 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.
|
def checker(a, b, arr):
fr = [-1, -1]
for i in range(len(arr)):
if arr[i] == a or arr[i] == b:
if fr[0] == -1 and fr[1] == -1:
if arr[i] == b:
a, b = b, a
fr[0] = 1
fr[1] = 0
elif arr[i] == a and fr[0] == fr[1]:
fr[0] += 1
elif arr[i] == b and fr[1] == fr[0] - 1:
fr[1] += 1
an = max(min(fr) * 2, ar[0][0])
return an
test = int(input())
for _ in range(test):
arr = list(input())
if len(arr) == 1 or len(arr) == 2:
print(0)
else:
freq = {}
maxf = 0
for i in range(len(arr)):
freq[arr[i]] = freq.get(arr[i], 0) + 1
maxf = max(maxf, freq[arr[i]])
if len(freq) == len(arr):
print(len(arr) - 2)
elif len(freq) == 1:
print(0)
else:
maxnn = 0
ar = []
for k, v in freq.items():
ar.append((v, k))
for i in range(10):
for j in range(i + 1, 10):
nn = checker(str(i), str(j), arr)
maxnn = max(maxnn, nn)
print(len(arr) - max(maxf, maxnn))
|
FUNC_DEF ASSIGN VAR LIST NUMBER NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR VAR VAR IF VAR NUMBER NUMBER VAR NUMBER NUMBER IF VAR VAR VAR ASSIGN VAR VAR VAR VAR ASSIGN VAR NUMBER NUMBER ASSIGN VAR NUMBER NUMBER IF VAR VAR VAR VAR NUMBER VAR NUMBER VAR NUMBER NUMBER IF VAR VAR VAR VAR NUMBER BIN_OP VAR NUMBER NUMBER VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER VAR NUMBER NUMBER RETURN VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR IF FUNC_CALL VAR VAR NUMBER FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR NUMBER ASSIGN VAR DICT ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR BIN_OP FUNC_CALL VAR VAR VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR IF FUNC_CALL VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER IF FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR LIST FOR VAR VAR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR VAR FOR VAR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP FUNC_CALL VAR 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.
|
import sys
INP = lambda: sys.stdin.readline().strip()
INT = lambda: int(INP())
MAP = lambda: map(int, INP().split())
ARR = lambda: [int(i) for i in INP()]
def JOIN(arr, x=" "):
return x.join([str(i) for i in arr])
def EXIT(x="NO"):
print(x)
exit()
def do(f, arr, a, b):
xx = 0
for x in arr:
if x == f:
if x == a:
f = b
else:
f = a
xx += 1
return xx // 2 * 2
def solve():
arr = [int(x) for x in list(INP())]
c = {x: (0) for x in arr}
for x in arr:
c[x] += 1
cs = sorted(list(c.items()), key=lambda x: x[1], reverse=True)
if len(cs) < 2:
return 0
ans = cs[0][1]
l = len(cs)
for i in range(l):
for j in range(i + 1, l):
ans = max(ans, do(cs[i][0], arr, cs[i][0], cs[j][0]))
ans = max(ans, do(cs[j][0], arr, cs[i][0], cs[j][0]))
return len(arr) - ans
for _ in range(INT()):
print(solve())
|
IMPORT ASSIGN VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR FUNC_DEF STRING RETURN FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR FUNC_DEF STRING EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_DEF ASSIGN VAR NUMBER FOR VAR VAR IF VAR VAR IF VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR VAR NUMBER RETURN BIN_OP BIN_OP VAR NUMBER NUMBER FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR NUMBER VAR VAR FOR VAR VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER NUMBER IF FUNC_CALL VAR VAR NUMBER RETURN NUMBER ASSIGN VAR VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR NUMBER VAR VAR VAR NUMBER VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR NUMBER VAR VAR VAR NUMBER VAR VAR NUMBER RETURN BIN_OP FUNC_CALL VAR VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR EXPR 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.
|
for _ in range(int(input())):
s = str(input())
l = list(s)
if len(list(set(l))) == 1:
print(0)
else:
l1 = [0] * 10
l2 = [-1] * 10
for i in range(len(s)):
l1[int(s[i])] += 1
l2[int(s[i])] = 1
ans = min(len(s) - max(l1), len(s) - 2)
l3 = []
for i in range(10):
if l2[i] == 1:
l3.append(i)
mv = 0
for i in range(len(l3)):
for j in range(len(l3)):
f = 0
x1 = 0
y1 = 0
for k in range(len(s)):
if f == 0 and int(s[k]) == l3[i]:
f = 1
x1 += 1
elif f == 1 and int(s[k]) == l3[j]:
f = 0
y1 += 1
mv = max(mv, 2 * min(x1, y1))
print(min(ans, len(s) - mv))
|
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 IF FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER NUMBER ASSIGN VAR BIN_OP LIST NUMBER NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR NUMBER IF VAR VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR NUMBER FUNC_CALL VAR VAR VAR VAR VAR ASSIGN VAR NUMBER VAR NUMBER IF VAR NUMBER FUNC_CALL VAR VAR VAR VAR VAR ASSIGN VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP NUMBER FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR 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.
|
from sys import stdin, stdout
def input():
return stdin.readline().strip()
T = int(input())
def ans1(S):
return max(S.count(i) for i in "0123456789")
def ans2(S):
ret = float("-inf")
for i in "0123456789":
for j in "0123456789":
ret = max(ret, ans3(S, i, j))
return ret
def ans3(S, a, b):
na = 0
nb = 0
for c in S:
if na == nb and c == a:
na += 1
elif na == nb + 1 and c == b:
nb += 1
return 2 * nb
def ans(S):
return len(S) - max(ans1(S), ans2(S))
for _ in range(T):
S = input()
print(ans(S))
|
FUNC_DEF RETURN FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_DEF RETURN FUNC_CALL VAR FUNC_CALL VAR VAR VAR STRING FUNC_DEF ASSIGN VAR FUNC_CALL VAR STRING FOR VAR STRING FOR VAR STRING ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR VAR RETURN VAR FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR IF VAR VAR VAR VAR VAR NUMBER IF VAR BIN_OP VAR NUMBER VAR VAR VAR NUMBER RETURN BIN_OP NUMBER VAR FUNC_DEF RETURN BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL VAR 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 isGood(st):
if len(st) <= 2:
return True
even = st[0]
odd = st[1]
for i, c in enumerate(st):
if i % 2 == 0:
if c != even:
return False
elif c != odd:
return False
if len(st) % 2 != 0 and even != odd:
return False
return True
for t in range(T):
s = input()
matrix = [[[-1, 0] for i in range(10)] for j in range(10)]
d = {i: (0) for i in range(10)}
for c in s:
cint = int(c)
d[cint] += 1
for i, l in enumerate(matrix[cint]):
if i == cint:
continue
if l[0] != cint:
l[0] = cint
l[1] += 1
for i in range(10):
if i == cint:
continue
l = matrix[i][cint]
if l[0] != cint and l[0] != -1:
l[0] = cint
l[1] += 1
max_o = -1
for i in range(10):
for j in range(10):
max_o = max(max_o, matrix[i][j][1])
if max_o % 2 != 0:
max_o -= 1
max_c = max([d[x] for x in range(10)])
to_sub = 0
if max_o > max_c:
to_sub = len(s) - max_o
else:
to_sub = len(s) - max_c
print(min(len(s) - 2, to_sub))
|
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_DEF IF FUNC_CALL VAR VAR NUMBER RETURN NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER FOR VAR VAR FUNC_CALL VAR VAR IF BIN_OP VAR NUMBER NUMBER IF VAR VAR RETURN NUMBER IF VAR VAR RETURN NUMBER IF BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER VAR VAR RETURN NUMBER RETURN NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR LIST NUMBER NUMBER VAR FUNC_CALL VAR NUMBER VAR FUNC_CALL VAR NUMBER ASSIGN VAR VAR NUMBER VAR FUNC_CALL VAR NUMBER FOR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR NUMBER FOR VAR VAR FUNC_CALL VAR VAR VAR IF VAR VAR IF VAR NUMBER VAR ASSIGN VAR NUMBER VAR VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER IF VAR VAR ASSIGN VAR VAR VAR VAR IF VAR NUMBER VAR VAR NUMBER NUMBER ASSIGN VAR NUMBER VAR VAR NUMBER 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 NUMBER IF BIN_OP VAR NUMBER NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER IF VAR VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER 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
def main():
for _ in range(int(input())):
s = input()
max_ = 0
for i in range(10):
for j in range(10):
max_ = max(max_, solve(s, str(i), str(j)))
print(len(s) - max_)
def solve(s1, x, y):
greed = 0
for char in s1:
if char == x:
greed += 1
z = x
x = y
y = z
if greed % 2 == 1 and x != y:
greed -= 1
return greed
main()
|
IMPORT FUNC_DEF FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR 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 FUNC_CALL VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR VAR FUNC_DEF ASSIGN VAR NUMBER FOR VAR VAR IF VAR VAR VAR NUMBER ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR IF BIN_OP VAR NUMBER NUMBER VAR VAR VAR NUMBER RETURN 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.
|
def ct(s, a, b):
tm = [a, b]
id = 0
res = 0
for n in s:
if n == tm[id]:
res += 1
id ^= 1
if res % 2 and a != b:
res -= 1
return len(s) - res
def solv():
s = list(map(int, input()))
res = 10**10
p = 0
q = 0
for n in range(10):
for k in range(10):
v = ct(s, n, k)
if v < res:
res = v
p = n
q = k
print(res)
for _ in range(int(input())):
solv()
|
FUNC_DEF ASSIGN VAR LIST VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR IF VAR VAR VAR VAR NUMBER VAR NUMBER IF BIN_OP VAR NUMBER VAR VAR VAR NUMBER RETURN BIN_OP FUNC_CALL VAR VAR VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL VAR ASSIGN VAR BIN_OP NUMBER NUMBER ASSIGN 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 IF VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR 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.
|
for _ in range(int(input())):
napis = input()
count = [[(0) for _ in range(10)] for _ in range(10)]
for x in napis:
d = int(x)
for p in range(10):
count[d][p] = count[p][d] + 1
print(
len(napis)
- max(
max([max(i) for i in count]) // 2 * 2, max([count[i][i] for i in range(10)])
)
)
|
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR NUMBER VAR FUNC_CALL VAR NUMBER FOR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR NUMBER ASSIGN VAR VAR VAR BIN_OP VAR VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR BIN_OP BIN_OP FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR NUMBER NUMBER FUNC_CALL VAR VAR VAR VAR VAR FUNC_CALL 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):
stro = input()
cntm = 0
for too in range(10):
for ga in range(10):
if too != ga:
f = 0
cnt = 0
for c in stro:
if f == 0 and int(c) == too:
cnt += 1
f = 1
elif f == 1 and int(c) == ga:
cnt += 1
f = 0
if cnt % 2 != 0:
cnt -= 1
else:
cnt = stro.count(str(too))
if cnt > cntm:
cntm = cnt
print(len(stro) - cntm)
|
ASSIGN VAR FUNC_CALL 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 IF VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR IF VAR NUMBER FUNC_CALL VAR VAR VAR VAR NUMBER ASSIGN VAR NUMBER IF VAR NUMBER FUNC_CALL VAR VAR VAR VAR NUMBER ASSIGN VAR NUMBER IF BIN_OP VAR NUMBER NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR ASSIGN 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.
|
from sys import stdin
for _ in range(int(stdin.readline())):
t = stdin.readline().strip()
array = [[(0) for i in range(10)] for j in range(10)]
flag = [[(0) for i in range(10)] for j in range(10)]
ans = 0
freq = [0] * 10
for i in t:
x = int(i)
freq[x] += 1
for j in range(10):
flag[x][j] = 1
for j in range(10):
if flag[j][x] == 1:
array[j][x] += 1
flag[j][x] = 0
ans = max(freq)
for i in range(10):
for j in range(10):
if i == j:
continue
ans = max(ans, 2 * array[i][j])
print(len(t) - ans)
|
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR NUMBER VAR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR NUMBER VAR FUNC_CALL VAR NUMBER ASSIGN 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 ASSIGN VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER IF VAR VAR VAR NUMBER VAR VAR VAR NUMBER ASSIGN 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 FUNC_CALL VAR VAR BIN_OP NUMBER 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 searchPattern(s, a, b):
check = 0
count = 0
for letter in s:
if not check and letter == a:
count += 1
check = 1
elif check and letter == b:
count += 1
check = 0
if a != b and count % 2 == 1:
count -= 1
return count
def minDeletes(s):
maxCount = 0
sSet = set()
for c in s:
sSet.add(c)
for i in sSet:
for j in sSet:
maxCount = max(maxCount, searchPattern(s, i, j))
return len(s) - maxCount
tests = int(input())
while tests:
s = input()
print(minDeletes(s))
tests -= 1
|
FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR IF VAR VAR VAR VAR NUMBER ASSIGN VAR NUMBER IF VAR VAR VAR VAR NUMBER ASSIGN VAR NUMBER IF VAR VAR BIN_OP VAR NUMBER NUMBER VAR NUMBER RETURN VAR FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR FOR VAR VAR EXPR FUNC_CALL VAR VAR FOR VAR VAR FOR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR VAR RETURN BIN_OP FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR WHILE VAR ASSIGN VAR FUNC_CALL VAR EXPR 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.
|
for _ in range(int(input())):
alpha = [0] * 10
s = [i for i in input()]
n = len(s)
ans = 200000.0 + 1
for i in range(10):
for j in range(10):
turn, cnt = 0, 0
for x in s:
if int(x) == i and turn == 0:
turn += 1
turn %= 2
cnt += 1
elif int(x) == j and turn == 1:
turn += 1
turn %= 2
cnt += 1
if cnt % 2 == 1 and i != j:
cnt -= 1
ans = min(ans, n - cnt)
print(ans)
|
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER NUMBER ASSIGN VAR VAR VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER ASSIGN VAR VAR NUMBER NUMBER FOR VAR VAR IF FUNC_CALL VAR VAR VAR VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER IF FUNC_CALL VAR VAR VAR VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER IF BIN_OP VAR NUMBER NUMBER VAR VAR 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.
|
for _ in range(int(input())):
s = input()
n = len(s)
cnt = [0] * 10
ind = [[] for i in range(10)]
for i in range(n):
cnt[int(s[i])] += 1
ind[int(s[i])] += [i]
ans = n - max(cnt)
for i in range(10):
for j in range(i + 1, 10):
l = 0
r = 0
cur = []
a = 0
fl = -1
for k in range(cnt[i] + cnt[j]):
if l == cnt[i]:
cur.append(ind[j][r])
r += 1
if fl == 0:
a += 1
fl = 1
elif r == cnt[j]:
cur.append(ind[i][l])
if fl == 1:
a += 1
fl = 0
l += 1
elif ind[i][l] < ind[j][r]:
cur.append(ind[i][l])
l += 1
if fl == 1:
a += 1
fl = 0
else:
cur.append(ind[j][r])
r += 1
if fl == 0:
a += 1
fl = 1
a += 1
ans = min(ans, n - a + a % 2)
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 BIN_OP LIST NUMBER NUMBER ASSIGN VAR LIST VAR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR NUMBER VAR FUNC_CALL VAR VAR VAR LIST VAR ASSIGN VAR BIN_OP 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 LIST ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR IF VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR VAR NUMBER IF VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER IF VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR IF VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER VAR NUMBER IF VAR VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR VAR NUMBER IF VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR VAR NUMBER IF VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR 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.
|
for i in " " * int(input()):
s = input()
countL = [0] * 10
for i in s:
countL[int(i)] += 1
mx = max(countL)
for i in range(10):
for j in range(10):
if i == j:
continue
frontcount = 0
count = 0
for k in s:
if int(k) == i and frontcount == count:
frontcount += 1
if int(k) == j and frontcount == count + 1:
count += 1
if mx < count * 2:
mx = count * 2
print(len(s) - mx)
|
FOR VAR BIN_OP STRING FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER NUMBER FOR VAR VAR VAR FUNC_CALL 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 FOR VAR VAR IF FUNC_CALL VAR VAR VAR VAR VAR VAR NUMBER IF FUNC_CALL VAR VAR VAR VAR BIN_OP VAR NUMBER VAR NUMBER IF VAR BIN_OP VAR NUMBER ASSIGN 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.
|
t = int(input())
for _ in range(t):
s = list(map(int, input().strip()))
n = len(s)
rm = n - max([s.count(i) for i in range(10)])
for i in range(10):
for j in range(10):
sw = 1
add = 0
for k in range(n):
if sw:
if s[k] == i:
add += 1
sw ^= 1
elif s[k] == j:
add += 1
sw ^= 1
add -= add % 2
rm = min(rm, n - add)
print(rm)
|
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR 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 VAR VAR VAR VAR NUMBER VAR NUMBER IF VAR VAR VAR VAR NUMBER VAR NUMBER VAR BIN_OP 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 = int(input())
for _ in range(t):
st = input()
sp = list(set(st))
moc = 0
mm = 0
for i in sp:
for j in sp:
if i == j:
coc = st.count(i)
mm = max(mm, coc)
continue
bo = False
coc = 0
for s in st:
if not bo and s == i:
bo = True
elif bo and s == j:
bo = False
coc += 1
moc = max(moc, coc)
print(len(st) - max(2 * moc, mm))
|
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR FOR VAR VAR IF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER 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 EXPR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR BIN_OP NUMBER 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 _ in range(t):
s = input().rstrip()
l = len(s)
b = 9999999
for i in range(10):
k = s.count(str(i))
b = min(b, l - k)
b_count = 0
for i in range(10):
for j in range(10):
count = 0
last = str(j)
for c in s:
if (c == str(j) or c == str(i)) and c != last:
count += 1
last = c
b_count = max(count, b_count)
bb = b_count // 2 * 2
print(min(b, l - bb))
|
IMPORT ASSIGN 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 ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR ASSIGN VAR NUMBER 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 VAR VAR NUMBER ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR FUNC_CALL 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.
|
w = int(input())
for _ in range(w):
A = list(map(int, list(input())))
MAX = 0
for i in range(10):
for j in range(10):
cur = i
l = 0
for k in A:
if k == cur:
if cur == i:
cur = j
else:
cur = i
l += 1
MAX = max(MAX, l)
if MAX % 2 == 0:
print(len(A) - MAX)
else:
dic = [0] * 10
for i in A:
dic[i] += 1
if MAX == max(dic):
print(len(A) - MAX)
else:
print(len(A) - MAX + 1)
|
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 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 IF VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR IF BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP LIST NUMBER NUMBER FOR VAR VAR VAR VAR NUMBER IF VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP BIN_OP 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.
|
def solve():
s = input()
ans = 0
for i in range(10):
for j in range(10):
x, y = str(i), str(j)
l = 0
for ch in s:
if ch == x:
l += 1
x, y = y, x
if l % 2 != 0 and x != y:
l -= 1
ans = max(ans, l)
print(len(s) - ans)
for case in range(int(input())):
solve()
|
FUNC_DEF ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR VAR IF VAR VAR VAR NUMBER ASSIGN VAR VAR VAR VAR IF BIN_OP VAR NUMBER NUMBER VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR 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.
|
t = int(input())
def swap(x, y):
return y, x
for _ in range(t):
s = input()
mx = 0
for i in range(10):
for j in range(10):
cur = 0
x, y = i, j
for a in s:
if int(a) == x:
cur += 1
x, y = swap(x, y)
if cur % 2 == 1 and i != j:
cur -= 1
mx = max(mx, cur)
print(len(s) - mx)
|
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_DEF RETURN VAR 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 VAR VAR VAR FOR VAR VAR IF FUNC_CALL VAR VAR VAR VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR VAR IF BIN_OP VAR NUMBER 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())
while t:
s = input()
arr = [0] * 10
ans = 10000000
for i in s:
arr[int(i)] += 1
for i in range(10):
for j in range(i):
if not arr[i] or not arr[j]:
continue
cnt = 0
cntt = 0
for k in s:
if cnt % 2 == 0 and int(k) == i or cnt % 2 == 1 and int(k) == j:
cnt += 1
if cntt % 2 == 0 and int(k) == j or cntt % 2 == 1 and int(k) == i:
cntt += 1
ans = min(ans, len(s) - cnt + cnt % 2)
ans = min(ans, len(s) - cntt + cntt % 2)
for i in arr:
ans = min(ans, len(s) - i)
print(ans)
t -= 1
|
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR WHILE VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER NUMBER ASSIGN VAR NUMBER FOR VAR VAR VAR FUNC_CALL VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR IF BIN_OP VAR NUMBER NUMBER FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER NUMBER FUNC_CALL VAR VAR VAR VAR NUMBER IF BIN_OP VAR NUMBER NUMBER FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER NUMBER FUNC_CALL VAR VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER FOR VAR VAR 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.
|
t = int(input())
while t > 0:
num = input()
ans = 0
sum = 0
for i in range(0, 10):
for j in range(0, 10):
state = 0
sum = 0
c1 = str(i)
c2 = str(j)
for k in num:
if c1 != c2:
if state == 0 and c1 == k:
sum += 1
state = 1
elif state == 1 and c2 == k:
sum += 1
state = 0
else:
sum += num.count(c1)
break
if state == 1 and sum >= 3:
sum -= 1
ans = max(ans, sum)
print(len(num) - ans)
t -= 1
|
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR WHILE VAR NUMBER ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FOR VAR VAR IF VAR VAR IF VAR NUMBER VAR VAR VAR NUMBER ASSIGN VAR NUMBER IF VAR NUMBER VAR VAR VAR NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR IF VAR NUMBER VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL 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.
|
for _ in range(int(input())):
s = input()
o = max(s.count(i) for i in {*s})
l = [[(0) for i in range(10)] for i in range(10)]
for i in "0123456789":
k = 0
q = {}
for m in range(10):
q[m] = set()
for j in s:
if j == i:
k += 1
elif k not in q[int(j)] and k > 0:
l[int(i)][int(j)] += 1
q[int(j)].add(k)
p = 0
for i in l:
p = max(p, max(i))
print(len(s) - max(p * 2, o))
|
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR NUMBER VAR FUNC_CALL VAR NUMBER FOR VAR STRING ASSIGN VAR NUMBER ASSIGN VAR DICT FOR VAR FUNC_CALL VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR FOR VAR VAR IF VAR VAR VAR NUMBER IF VAR VAR FUNC_CALL VAR VAR VAR NUMBER VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR ASSIGN VAR NUMBER FOR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER 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
input = stdin.readline
def answer():
count = [0] * 10
for i in s:
count[int(i)] += 1
ans = n - max(count)
for v1 in range(10):
for v2 in range(10):
x, take = 0, 0
for i in range(n):
if take == 0:
if int(s[i]) == v1:
x += 1
take = 1
elif int(s[i]) == v2:
x += 1
take = 0
x -= take
ans = min(ans, n - x)
return ans
for T in range(int(input())):
s = input().strip()
n = len(s)
print(answer())
|
ASSIGN VAR VAR FUNC_DEF ASSIGN VAR BIN_OP LIST NUMBER NUMBER FOR VAR VAR VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR BIN_OP VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER ASSIGN VAR VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR NUMBER IF FUNC_CALL VAR VAR VAR VAR VAR NUMBER ASSIGN VAR NUMBER IF FUNC_CALL VAR VAR VAR VAR VAR NUMBER ASSIGN VAR NUMBER VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR RETURN VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR EXPR 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.
|
t = int(input())
for _ in range(t):
st = input()
ans = 2000001
l = len(st)
s = set(st)
for j in s:
c = st.count(j)
if l - c < ans:
ans = l - c
arr = []
for j in s:
arr.append(j)
for i in range(len(arr)):
for j in range(i + 1, len(arr)):
count = 0
last = 0
for k in range(l):
if last == 0:
if st[k] == arr[i]:
count += 1
last = 1
elif st[k] == arr[j]:
count += 1
last = 0
if count % 2 == 0:
if l - count < ans:
ans = l - count
count = 0
last = 1
for k in range(l):
if last == 0:
if st[k] == arr[i]:
count += 1
last = 1
elif st[k] == arr[j]:
count += 1
last = 0
if count % 2 == 0:
if l - count < ans:
ans = l - count
print(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 FUNC_CALL VAR VAR FOR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR IF BIN_OP VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR LIST FOR VAR VAR EXPR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR NUMBER IF VAR VAR VAR VAR VAR NUMBER ASSIGN VAR NUMBER IF VAR VAR VAR VAR VAR NUMBER ASSIGN VAR NUMBER IF BIN_OP VAR NUMBER NUMBER IF BIN_OP VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR NUMBER IF VAR VAR VAR VAR VAR NUMBER ASSIGN VAR NUMBER IF VAR VAR VAR VAR VAR NUMBER ASSIGN VAR NUMBER IF BIN_OP VAR NUMBER NUMBER IF BIN_OP VAR VAR VAR ASSIGN 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 minErase(s):
n = len(s)
maxCnt = 0
for i in range(10):
for j in range(10):
currChar = i
cnt = 0
for c in s:
if int(c) == currChar:
cnt += 1
currChar = j if currChar == i else i
maxCnt = max(maxCnt, cnt - 1 if i != j and cnt % 2 != 0 else cnt)
ans = n - maxCnt
print(ans)
t = int(input())
for _ in range(t):
s = input()
minErase(s)
|
FUNC_DEF ASSIGN VAR FUNC_CALL VAR 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 FUNC_CALL VAR VAR VAR VAR NUMBER ASSIGN VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR BIN_OP VAR NUMBER NUMBER BIN_OP VAR NUMBER VAR ASSIGN VAR BIN_OP VAR VAR EXPR FUNC_CALL 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.
|
def comb(a, x, y):
flag = True
count = 0
for ele in a:
if flag:
if ele == x:
flag = False
elif ele == y:
count += 1
flag = True
return len(a) - 2 * count
T = int(input())
for case in range(T):
a = input()
ans = float("inf")
for i in range(0, 10):
for j in range(0, 10):
if i == j:
continue
else:
ans = min(ans, comb(a, str(i), str(j)))
for i in range(10):
count = a.count(str(i))
ans = min(ans, len(a) - count)
print(ans)
|
FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR IF VAR IF VAR VAR ASSIGN VAR NUMBER IF VAR VAR VAR NUMBER ASSIGN VAR NUMBER RETURN BIN_OP FUNC_CALL VAR VAR 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 FUNC_CALL VAR STRING FOR VAR FUNC_CALL VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER NUMBER IF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR 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.
|
import sys
T = int(sys.stdin.readline().strip())
for t in range(0, T):
s = sys.stdin.readline().strip()
l = len(s)
S = [int(s[i]) for i in range(0, l)]
ans = l
for i in range(0, 10):
for j in range(0, 10):
if i == j:
ans = min(ans, l - S.count(i))
else:
v = True
c = 0
for k in range(0, l):
if v == True and S[k] == i:
v = False
elif v == False and S[k] == j:
v = True
c = c + 1
ans = min(ans, l - 2 * c)
print(ans)
|
IMPORT ASSIGN VAR FUNC_CALL VAR FUNC_CALL FUNC_CALL VAR FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR FUNC_CALL VAR NUMBER VAR ASSIGN 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 BIN_OP VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR NUMBER VAR VAR VAR ASSIGN VAR NUMBER IF VAR NUMBER VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER 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.
|
def fun(s, a, b):
ct = 0
for i in s:
if i == a:
a, b = b, a
ct += 1
if ct % 2 != 0:
return ct - 1
return ct
for _ in range(int(input())):
s = input()
n = len(s)
d = {}
maxi = 0
for i in range(10):
for j in range(10):
if i == j:
x = s.count(str(i))
else:
x = fun(s, str(i), str(j))
if maxi < x:
maxi = x
print(n - maxi)
|
FUNC_DEF ASSIGN VAR NUMBER FOR VAR VAR IF VAR VAR ASSIGN VAR VAR VAR VAR VAR NUMBER IF BIN_OP VAR NUMBER NUMBER RETURN BIN_OP 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 ASSIGN VAR DICT ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER IF VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR IF VAR VAR ASSIGN 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.
|
def main():
t = int(input())
for i in range(t):
s = input()
n = len(s)
ar = [[0, ""] for i in range(100)]
al = "0123456789"
ar2 = [0] * 10
for i in s:
ar2[int(i)] += 1
for j in al:
ind = int("".join(sorted([i, j])))
if ar[ind][1] == "" or ar[ind][1] != i:
ar[ind][1] = i
ar[ind][0] += 1
for i in range(100):
ar[i][0] -= ar[i][0] & 1
print(n - max(max(ar2), max(ar, key=lambda x: x[0])[0]))
main()
|
FUNC_DEF 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 LIST NUMBER STRING VAR FUNC_CALL VAR NUMBER ASSIGN VAR STRING ASSIGN VAR BIN_OP LIST NUMBER NUMBER FOR VAR VAR VAR FUNC_CALL VAR VAR NUMBER FOR VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL STRING FUNC_CALL VAR LIST VAR VAR IF VAR VAR NUMBER STRING VAR VAR NUMBER VAR ASSIGN VAR VAR NUMBER VAR VAR VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR VAR NUMBER BIN_OP VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR BIN_OP VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR NUMBER 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.
|
from sys import stdin, stdout
for _ in range(int(input())):
s = input().strip()
n = len(s)
ans = 0
for i in range(10):
i = str(i)
for j in range(10):
j = str(j)
c1, c2 = 0, 0
for k in s:
if k == i and c1 == c2:
c1 += 1
elif k == j and c1 > c2:
c2 += 1
c3 = 0
if i == j:
c3 = c2 + c1
ans = max(c2 * 2, c3, ans)
print(n - ans)
|
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 NUMBER FOR VAR FUNC_CALL VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR VAR NUMBER NUMBER FOR VAR VAR IF VAR VAR VAR VAR VAR NUMBER IF VAR VAR VAR VAR VAR NUMBER ASSIGN VAR NUMBER IF VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER 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)
if n == 1 or n == 2:
print(0)
else:
dic = {}
for i in range(n):
if s[i] in dic:
dic[s[i]] += 1
else:
dic[s[i]] = 1
mx = 0
for i in dic.keys():
mx = max(mx, dic[i])
ans = n - max(2, mx)
for i in range(10):
for j in range(10):
if i != j:
fst = i
cnt = 0
for k in range(n):
if int(s[k]) != fst:
cnt += 1
elif fst == i:
fst = j
else:
fst = i
if (n - cnt) % 2 == 1:
cnt += 1
ans = min(ans, 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 IF VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR NUMBER ASSIGN VAR DICT FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR FUNC_CALL VAR NUMBER VAR FOR VAR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER IF VAR VAR ASSIGN VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR VAR VAR VAR NUMBER IF VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR IF BIN_OP BIN_OP VAR VAR NUMBER NUMBER 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.
|
ans = []
for i in range(10):
for j in range(10):
ans.append(str(i) + str(j))
for case in range(int(input())):
st = input()
n = len(st)
ans1 = 0
for s in ans:
count = 0
f = s[0]
for i in st:
if i == f:
count += 1
if f == s[0]:
f = s[1]
else:
f = s[0]
if s[0] != s[1] and count % 2 == 1:
count -= 1
ans1 = max(ans1, count)
print(len(st) - ans1)
|
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 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 FOR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR VAR NUMBER FOR VAR VAR IF VAR VAR VAR NUMBER IF VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER IF VAR NUMBER VAR NUMBER BIN_OP VAR NUMBER 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 = input().strip()
n = len(s)
freq = [(0) for i in range(10)]
for ch in s:
freq[ord(ch) - ord("0")] += 1
freq.sort()
maxx = 0
for i in range(10):
for j in range(i + 1, 10):
seq = 0
last = ""
for ch in s:
if ch == str(i) or ch == str(j):
if ch == last:
continue
else:
seq += 1
last = ch
if seq % 2 == 1:
seq -= 1
if seq > maxx:
maxx = seq
maxx = max(maxx, freq[-1])
print(n - maxx)
|
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 NUMBER VAR FUNC_CALL VAR NUMBER FOR VAR VAR VAR BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR STRING NUMBER EXPR FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR STRING FOR VAR VAR IF VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR IF VAR VAR VAR NUMBER ASSIGN VAR VAR IF BIN_OP VAR NUMBER NUMBER VAR NUMBER IF VAR VAR ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR VAR 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.
|
n = int(input())
for i in range(n):
s = input()
l = len(s)
d = dict()
for i in "0123456789":
d[i] = 0
for i in s:
d[i] = d[i] + 1
maxi = 0
for i in "0123456789":
if d[i] > maxi:
maxi = d[i]
maxi1 = 0
for i in "0123456789":
for j in "0123456789":
if i == j:
continue
count = 0
flag = 1
for k in s:
if k == i and flag == 1:
flag = 0
if k == j and flag == 0:
count = count + 1
flag = 1
if count * 2 >= maxi1:
maxi1 = count * 2
print(min(l - maxi, l - maxi1))
|
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 FOR VAR STRING ASSIGN VAR VAR NUMBER FOR VAR VAR ASSIGN VAR VAR BIN_OP VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR STRING IF VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR NUMBER FOR VAR STRING FOR VAR STRING IF VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR IF VAR VAR VAR NUMBER ASSIGN VAR NUMBER IF VAR VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER IF BIN_OP VAR NUMBER VAR ASSIGN VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR VAR BIN_OP VAR VAR
|
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.