description
stringlengths
171
4k
code
stringlengths
94
3.98k
normalized_code
stringlengths
57
4.99k
Chef came across a new online judge that has N problems, and decided that he wants to solve them. Chef takes A_{i} consecutive minutes to solve the i-th problem, and will take a break of B_{i} minutes immediately after solving it. That is, Chef will solve a problem, then take a break. Solve another problem, then take another break, and so on. Chef has K minutes of free time. If he chooses the problems and their order optimally, what is the maximum number of problems he can solve in this time? Note that a problem is considered solved if Chef finishes solving it by the K-th minute, even if the break time of the last problem extends past minute K. See the sample tests below for an example. ------ Input Format ------ - The first line of input will contain a single integer T, denoting the number of test cases. - Each test case consists of three lines of input. - The first line of each test case contains two space-separated integers N and K β€” the number of problems in the online judge and Chef's free time, respectively. - The second line of each test case contains N space-separated integers β€” the values A_{1}, A_{2}, \ldots, A_{N}. - The third line of each test case contains N space-separated integers β€” the values B_{1}, B_{2}, \ldots, B_{N}. ------ Output Format ------ For each test case, output on a new line the maximum number of problems that Chef can solve within K minutes. ------ Constraints ------ $1 ≀ T ≀ 2\cdot 10^{4}$ $1 ≀ N ≀ 2\cdot 10^{5}$ $1 ≀ K ≀ 10^{8}$ $1 ≀ A_{i} ≀ 10^{8}$ $0 ≀ B_{i} ≀ 10^{8}$ - The sum of $N$ over all test cases won't exceed $2\cdot 10^{5}$. ----- Sample Input 1 ------ 4 3 10 3 4 5 2 4 2 3 8 3 4 5 2 4 2 5 20 23 54 124 54 83 2 9 5 2 10 5 20 4 7 12 34 13 30 4 3 0 9 ----- Sample Output 1 ------ 2 1 0 2 ----- explanation 1 ------ Test case $1$: Chef can solve the first problem followed by the second problem. - $3$ minutes for the first problem - $2$ minutes of break time after solving it, for a total of $5$ minutes - $5$ minutes for the third problem, for a total of $10$ minutes - There's two minutes of break time left which goes beyond $10$ minutes, but that's ok: Chef finished solving $2$ problems within $10$ minutes. Test case $2$: The same times as the first sample, but with $K = 8$. Now, Chef can solve any one problem, but cannot solve a second one no matter what. Test case $3$: Chef can't solve anything within $20$ minutes. Test case $4$: Chef can solve the third problem followed by the first problem.
t = int(input()) for _ in range(t): q, t = map(int, input().split()) questions = [int(i) for i in input().split()] time = [int(i) for i in input().split()] d = {} for j in range(len(questions)): d[j] = [questions[j], time[j], questions[j] + time[j]] d = dict(sorted(d.items(), key=lambda x: x[1][2])) qs = 0 summ = 0 f = True mn = float("inf") mx_b = 0 key = 0 for k, v in d.items(): summ += v[2] mx_b = max(mx_b, v[1]) if summ - mx_b <= t: qs += 1 else: summ -= v[2] print(qs)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR DICT FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR VAR LIST VAR VAR VAR VAR BIN_OP VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR STRING ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR NUMBER IF BIN_OP VAR VAR VAR VAR NUMBER VAR VAR NUMBER EXPR FUNC_CALL VAR VAR
Chef came across a new online judge that has N problems, and decided that he wants to solve them. Chef takes A_{i} consecutive minutes to solve the i-th problem, and will take a break of B_{i} minutes immediately after solving it. That is, Chef will solve a problem, then take a break. Solve another problem, then take another break, and so on. Chef has K minutes of free time. If he chooses the problems and their order optimally, what is the maximum number of problems he can solve in this time? Note that a problem is considered solved if Chef finishes solving it by the K-th minute, even if the break time of the last problem extends past minute K. See the sample tests below for an example. ------ Input Format ------ - The first line of input will contain a single integer T, denoting the number of test cases. - Each test case consists of three lines of input. - The first line of each test case contains two space-separated integers N and K β€” the number of problems in the online judge and Chef's free time, respectively. - The second line of each test case contains N space-separated integers β€” the values A_{1}, A_{2}, \ldots, A_{N}. - The third line of each test case contains N space-separated integers β€” the values B_{1}, B_{2}, \ldots, B_{N}. ------ Output Format ------ For each test case, output on a new line the maximum number of problems that Chef can solve within K minutes. ------ Constraints ------ $1 ≀ T ≀ 2\cdot 10^{4}$ $1 ≀ N ≀ 2\cdot 10^{5}$ $1 ≀ K ≀ 10^{8}$ $1 ≀ A_{i} ≀ 10^{8}$ $0 ≀ B_{i} ≀ 10^{8}$ - The sum of $N$ over all test cases won't exceed $2\cdot 10^{5}$. ----- Sample Input 1 ------ 4 3 10 3 4 5 2 4 2 3 8 3 4 5 2 4 2 5 20 23 54 124 54 83 2 9 5 2 10 5 20 4 7 12 34 13 30 4 3 0 9 ----- Sample Output 1 ------ 2 1 0 2 ----- explanation 1 ------ Test case $1$: Chef can solve the first problem followed by the second problem. - $3$ minutes for the first problem - $2$ minutes of break time after solving it, for a total of $5$ minutes - $5$ minutes for the third problem, for a total of $10$ minutes - There's two minutes of break time left which goes beyond $10$ minutes, but that's ok: Chef finished solving $2$ problems within $10$ minutes. Test case $2$: The same times as the first sample, but with $K = 8$. Now, Chef can solve any one problem, but cannot solve a second one no matter what. Test case $3$: Chef can't solve anything within $20$ minutes. Test case $4$: Chef can solve the third problem followed by the first problem.
for _ in range(int(input())): n, m = map(int, input().split()) a = list(map(int, input().split())) b = list(map(int, input().split())) l = [] for f in range(n): l.append([a[f], b[f]]) l.sort(key=lambda x: (sum(x), x[0])) mx = 0 su = 0 i = 0 while i < n: if l[i][0] + l[i][1] + su <= m: su += l[i][0] + l[i][1] mx = max(mx, l[i][1]) i += 1 else: break x = [0] * n for j in range(n - 1, -1, -1): if j == n - 1: x[j] = l[j][0] else: x[j] = min(x[j + 1], l[j][0]) ans = i if i < n: rem = m - su if x[i] <= rem: ans += 1 elif rem + mx >= l[i][0] + l[i][1]: ans += 1 print(ans)
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR LIST VAR VAR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR IF BIN_OP BIN_OP VAR VAR NUMBER VAR VAR NUMBER VAR VAR VAR BIN_OP VAR VAR NUMBER VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER IF VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR NUMBER ASSIGN VAR VAR IF VAR VAR ASSIGN VAR BIN_OP VAR VAR IF VAR VAR VAR VAR NUMBER IF BIN_OP VAR VAR BIN_OP VAR VAR NUMBER VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR
Chef came across a new online judge that has N problems, and decided that he wants to solve them. Chef takes A_{i} consecutive minutes to solve the i-th problem, and will take a break of B_{i} minutes immediately after solving it. That is, Chef will solve a problem, then take a break. Solve another problem, then take another break, and so on. Chef has K minutes of free time. If he chooses the problems and their order optimally, what is the maximum number of problems he can solve in this time? Note that a problem is considered solved if Chef finishes solving it by the K-th minute, even if the break time of the last problem extends past minute K. See the sample tests below for an example. ------ Input Format ------ - The first line of input will contain a single integer T, denoting the number of test cases. - Each test case consists of three lines of input. - The first line of each test case contains two space-separated integers N and K β€” the number of problems in the online judge and Chef's free time, respectively. - The second line of each test case contains N space-separated integers β€” the values A_{1}, A_{2}, \ldots, A_{N}. - The third line of each test case contains N space-separated integers β€” the values B_{1}, B_{2}, \ldots, B_{N}. ------ Output Format ------ For each test case, output on a new line the maximum number of problems that Chef can solve within K minutes. ------ Constraints ------ $1 ≀ T ≀ 2\cdot 10^{4}$ $1 ≀ N ≀ 2\cdot 10^{5}$ $1 ≀ K ≀ 10^{8}$ $1 ≀ A_{i} ≀ 10^{8}$ $0 ≀ B_{i} ≀ 10^{8}$ - The sum of $N$ over all test cases won't exceed $2\cdot 10^{5}$. ----- Sample Input 1 ------ 4 3 10 3 4 5 2 4 2 3 8 3 4 5 2 4 2 5 20 23 54 124 54 83 2 9 5 2 10 5 20 4 7 12 34 13 30 4 3 0 9 ----- Sample Output 1 ------ 2 1 0 2 ----- explanation 1 ------ Test case $1$: Chef can solve the first problem followed by the second problem. - $3$ minutes for the first problem - $2$ minutes of break time after solving it, for a total of $5$ minutes - $5$ minutes for the third problem, for a total of $10$ minutes - There's two minutes of break time left which goes beyond $10$ minutes, but that's ok: Chef finished solving $2$ problems within $10$ minutes. Test case $2$: The same times as the first sample, but with $K = 8$. Now, Chef can solve any one problem, but cannot solve a second one no matter what. Test case $3$: Chef can't solve anything within $20$ minutes. Test case $4$: Chef can solve the third problem followed by the first problem.
def getSolution(n, k, a): mx = 0 sum = 0 i = 0 while i < n: if a[i][0] + a[i][1] + sum >= k: break else: sum += a[i][0] + a[i][1] mx = max(mx, a[i][1]) i += 1 temp = [0] * n kk = n - 1 while kk >= 0: if kk == n - 1: temp[kk] = a[kk][0] else: temp[kk] = temp[kk + 1] if temp[kk + 1] <= a[kk][0] else a[kk][0] kk -= 1 ans = i if i < n: rem = k - sum if temp[i] <= rem: ans += 1 elif rem + mx >= a[i][0] + a[i][1]: ans += 1 return ans def compare(a, b): if a[0] + a[1] == b[0] + b[1]: return a[0] < b[0] return a[0] + a[1] < b[0] + b[1] def c_p_c(): t = int(input()) for _ in range(t): n, k = map(int, input().split()) a = list(map(int, input().split())) b = list(map(int, input().split())) arr = [(a[i], b[i]) for i in range(n)] arr.sort(key=lambda x: (x[0] + x[1], x[0])) print(getSolution(n, k, arr)) c_p_c()
FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR IF BIN_OP BIN_OP VAR VAR NUMBER VAR VAR NUMBER VAR VAR VAR BIN_OP VAR VAR NUMBER VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR BIN_OP VAR NUMBER WHILE VAR NUMBER IF VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR VAR NUMBER ASSIGN VAR VAR VAR BIN_OP VAR NUMBER VAR VAR NUMBER VAR BIN_OP VAR NUMBER VAR VAR NUMBER VAR NUMBER ASSIGN VAR VAR IF VAR VAR ASSIGN VAR BIN_OP VAR VAR IF VAR VAR VAR VAR NUMBER IF BIN_OP VAR VAR BIN_OP VAR VAR NUMBER VAR VAR NUMBER VAR NUMBER RETURN VAR FUNC_DEF IF BIN_OP VAR NUMBER VAR NUMBER BIN_OP VAR NUMBER VAR NUMBER RETURN VAR NUMBER VAR NUMBER RETURN BIN_OP VAR NUMBER VAR NUMBER BIN_OP VAR NUMBER VAR NUMBER FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR VAR VAR VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR
Chef came across a new online judge that has N problems, and decided that he wants to solve them. Chef takes A_{i} consecutive minutes to solve the i-th problem, and will take a break of B_{i} minutes immediately after solving it. That is, Chef will solve a problem, then take a break. Solve another problem, then take another break, and so on. Chef has K minutes of free time. If he chooses the problems and their order optimally, what is the maximum number of problems he can solve in this time? Note that a problem is considered solved if Chef finishes solving it by the K-th minute, even if the break time of the last problem extends past minute K. See the sample tests below for an example. ------ Input Format ------ - The first line of input will contain a single integer T, denoting the number of test cases. - Each test case consists of three lines of input. - The first line of each test case contains two space-separated integers N and K β€” the number of problems in the online judge and Chef's free time, respectively. - The second line of each test case contains N space-separated integers β€” the values A_{1}, A_{2}, \ldots, A_{N}. - The third line of each test case contains N space-separated integers β€” the values B_{1}, B_{2}, \ldots, B_{N}. ------ Output Format ------ For each test case, output on a new line the maximum number of problems that Chef can solve within K minutes. ------ Constraints ------ $1 ≀ T ≀ 2\cdot 10^{4}$ $1 ≀ N ≀ 2\cdot 10^{5}$ $1 ≀ K ≀ 10^{8}$ $1 ≀ A_{i} ≀ 10^{8}$ $0 ≀ B_{i} ≀ 10^{8}$ - The sum of $N$ over all test cases won't exceed $2\cdot 10^{5}$. ----- Sample Input 1 ------ 4 3 10 3 4 5 2 4 2 3 8 3 4 5 2 4 2 5 20 23 54 124 54 83 2 9 5 2 10 5 20 4 7 12 34 13 30 4 3 0 9 ----- Sample Output 1 ------ 2 1 0 2 ----- explanation 1 ------ Test case $1$: Chef can solve the first problem followed by the second problem. - $3$ minutes for the first problem - $2$ minutes of break time after solving it, for a total of $5$ minutes - $5$ minutes for the third problem, for a total of $10$ minutes - There's two minutes of break time left which goes beyond $10$ minutes, but that's ok: Chef finished solving $2$ problems within $10$ minutes. Test case $2$: The same times as the first sample, but with $K = 8$. Now, Chef can solve any one problem, but cannot solve a second one no matter what. Test case $3$: Chef can't solve anything within $20$ minutes. Test case $4$: Chef can solve the third problem followed by the first problem.
for _ in range(int(input())): n, t = map(int, input().split()) a = list(map(int, input().split())) b = list(map(int, input().split())) c = [[a[i], b[i], a[i] + b[i]] for i in range(len(a))] c.sort(key=lambda x: x[2]) i = 0 aux = [] while i < len(c) and t >= c[i][2]: t -= c[i][2] aux.append(c[i]) i += 1 aux.sort(key=lambda x: x[1]) j = len(aux) - 1 ans = i while i < len(c): if j >= 0: if aux[j][0] + c[i][2] <= aux[j][2] + c[i][0]: if t - (c[i][2] - aux[j][1]) >= 0: ans += 1 t -= c[i][2] - aux[j][1] break elif t >= c[i][0]: ans += 1 break elif t >= c[i][0]: ans += 1 break elif t >= c[i][0]: ans += 1 break i += 1 print(ans)
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST VAR VAR VAR VAR BIN_OP VAR VAR VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR LIST WHILE VAR FUNC_CALL VAR VAR VAR VAR VAR NUMBER VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR WHILE VAR FUNC_CALL VAR VAR IF VAR NUMBER IF BIN_OP VAR VAR NUMBER VAR VAR NUMBER BIN_OP VAR VAR NUMBER VAR VAR NUMBER IF BIN_OP VAR BIN_OP VAR VAR NUMBER VAR VAR NUMBER NUMBER VAR NUMBER VAR BIN_OP VAR VAR NUMBER VAR VAR NUMBER IF VAR VAR VAR NUMBER VAR NUMBER IF VAR VAR VAR NUMBER VAR NUMBER IF VAR VAR VAR NUMBER VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR
Chef came across a new online judge that has N problems, and decided that he wants to solve them. Chef takes A_{i} consecutive minutes to solve the i-th problem, and will take a break of B_{i} minutes immediately after solving it. That is, Chef will solve a problem, then take a break. Solve another problem, then take another break, and so on. Chef has K minutes of free time. If he chooses the problems and their order optimally, what is the maximum number of problems he can solve in this time? Note that a problem is considered solved if Chef finishes solving it by the K-th minute, even if the break time of the last problem extends past minute K. See the sample tests below for an example. ------ Input Format ------ - The first line of input will contain a single integer T, denoting the number of test cases. - Each test case consists of three lines of input. - The first line of each test case contains two space-separated integers N and K β€” the number of problems in the online judge and Chef's free time, respectively. - The second line of each test case contains N space-separated integers β€” the values A_{1}, A_{2}, \ldots, A_{N}. - The third line of each test case contains N space-separated integers β€” the values B_{1}, B_{2}, \ldots, B_{N}. ------ Output Format ------ For each test case, output on a new line the maximum number of problems that Chef can solve within K minutes. ------ Constraints ------ $1 ≀ T ≀ 2\cdot 10^{4}$ $1 ≀ N ≀ 2\cdot 10^{5}$ $1 ≀ K ≀ 10^{8}$ $1 ≀ A_{i} ≀ 10^{8}$ $0 ≀ B_{i} ≀ 10^{8}$ - The sum of $N$ over all test cases won't exceed $2\cdot 10^{5}$. ----- Sample Input 1 ------ 4 3 10 3 4 5 2 4 2 3 8 3 4 5 2 4 2 5 20 23 54 124 54 83 2 9 5 2 10 5 20 4 7 12 34 13 30 4 3 0 9 ----- Sample Output 1 ------ 2 1 0 2 ----- explanation 1 ------ Test case $1$: Chef can solve the first problem followed by the second problem. - $3$ minutes for the first problem - $2$ minutes of break time after solving it, for a total of $5$ minutes - $5$ minutes for the third problem, for a total of $10$ minutes - There's two minutes of break time left which goes beyond $10$ minutes, but that's ok: Chef finished solving $2$ problems within $10$ minutes. Test case $2$: The same times as the first sample, but with $K = 8$. Now, Chef can solve any one problem, but cannot solve a second one no matter what. Test case $3$: Chef can't solve anything within $20$ minutes. Test case $4$: Chef can solve the third problem followed by the first problem.
t = int(input()) for i in range(t): n, s = map(int, input().split(" ")) a = input().split(" ") b = input().split(" ") c = [] for j in range(n): c.append((int(a[j]), int(b[j]), int(a[j]) + int(b[j]))) c1 = sorted(c, key=lambda l: l[2]) add = True k = 0 sum = 0 while add and k < n: if sum + c1[k][2] <= s: sum += c1[k][2] k += 1 else: add = False if k == n: print(n) elif not add: asm = sorted(c1[k:], key=lambda l: l[0])[0] if asm[0] + sum <= s: print(k + 1) else: for k1 in range(k): if c1[k1][0] + sum - c1[k1][2] + c1[k][2] <= s: print(k + 1) break else: print(k)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR BIN_OP FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR VAR IF BIN_OP VAR VAR VAR NUMBER VAR VAR VAR VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER IF VAR VAR EXPR FUNC_CALL VAR VAR IF VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR NUMBER NUMBER IF BIN_OP VAR NUMBER VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF BIN_OP BIN_OP BIN_OP VAR VAR NUMBER VAR VAR VAR NUMBER VAR VAR NUMBER VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR
Chef came across a new online judge that has N problems, and decided that he wants to solve them. Chef takes A_{i} consecutive minutes to solve the i-th problem, and will take a break of B_{i} minutes immediately after solving it. That is, Chef will solve a problem, then take a break. Solve another problem, then take another break, and so on. Chef has K minutes of free time. If he chooses the problems and their order optimally, what is the maximum number of problems he can solve in this time? Note that a problem is considered solved if Chef finishes solving it by the K-th minute, even if the break time of the last problem extends past minute K. See the sample tests below for an example. ------ Input Format ------ - The first line of input will contain a single integer T, denoting the number of test cases. - Each test case consists of three lines of input. - The first line of each test case contains two space-separated integers N and K β€” the number of problems in the online judge and Chef's free time, respectively. - The second line of each test case contains N space-separated integers β€” the values A_{1}, A_{2}, \ldots, A_{N}. - The third line of each test case contains N space-separated integers β€” the values B_{1}, B_{2}, \ldots, B_{N}. ------ Output Format ------ For each test case, output on a new line the maximum number of problems that Chef can solve within K minutes. ------ Constraints ------ $1 ≀ T ≀ 2\cdot 10^{4}$ $1 ≀ N ≀ 2\cdot 10^{5}$ $1 ≀ K ≀ 10^{8}$ $1 ≀ A_{i} ≀ 10^{8}$ $0 ≀ B_{i} ≀ 10^{8}$ - The sum of $N$ over all test cases won't exceed $2\cdot 10^{5}$. ----- Sample Input 1 ------ 4 3 10 3 4 5 2 4 2 3 8 3 4 5 2 4 2 5 20 23 54 124 54 83 2 9 5 2 10 5 20 4 7 12 34 13 30 4 3 0 9 ----- Sample Output 1 ------ 2 1 0 2 ----- explanation 1 ------ Test case $1$: Chef can solve the first problem followed by the second problem. - $3$ minutes for the first problem - $2$ minutes of break time after solving it, for a total of $5$ minutes - $5$ minutes for the third problem, for a total of $10$ minutes - There's two minutes of break time left which goes beyond $10$ minutes, but that's ok: Chef finished solving $2$ problems within $10$ minutes. Test case $2$: The same times as the first sample, but with $K = 8$. Now, Chef can solve any one problem, but cannot solve a second one no matter what. Test case $3$: Chef can't solve anything within $20$ minutes. Test case $4$: Chef can solve the third problem followed by the first problem.
def solve(): m = [] for i in range(n): m.append([a[i], b[i]]) m.sort(key=lambda x: x[0] + x[1]) mx = 0 sum = 0 i = 0 while i < n: if m[i][0] + m[i][1] + sum <= k: sum += m[i][0] + m[i][1] mx = max(mx, m[i][1]) i += 1 else: break ans = i if i < n: rem = k - sum mn = float("inf") for j in range(i, n): mn = min(mn, m[j][0]) if mn <= rem: ans += 1 elif rem + mx >= m[i][0] + m[i][1]: ans += 1 return ans for _ in range(int(input())): n, k = map(int, input().split()) a = list(map(int, input().split())) b = list(map(int, input().split())) print(solve())
FUNC_DEF ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR LIST VAR VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR IF BIN_OP BIN_OP VAR VAR NUMBER VAR VAR NUMBER VAR VAR VAR BIN_OP VAR VAR NUMBER VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR NUMBER VAR NUMBER ASSIGN VAR VAR IF VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR STRING FOR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR NUMBER IF VAR VAR VAR NUMBER IF BIN_OP VAR VAR BIN_OP VAR VAR NUMBER VAR VAR NUMBER VAR NUMBER RETURN VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR
Chef came across a new online judge that has N problems, and decided that he wants to solve them. Chef takes A_{i} consecutive minutes to solve the i-th problem, and will take a break of B_{i} minutes immediately after solving it. That is, Chef will solve a problem, then take a break. Solve another problem, then take another break, and so on. Chef has K minutes of free time. If he chooses the problems and their order optimally, what is the maximum number of problems he can solve in this time? Note that a problem is considered solved if Chef finishes solving it by the K-th minute, even if the break time of the last problem extends past minute K. See the sample tests below for an example. ------ Input Format ------ - The first line of input will contain a single integer T, denoting the number of test cases. - Each test case consists of three lines of input. - The first line of each test case contains two space-separated integers N and K β€” the number of problems in the online judge and Chef's free time, respectively. - The second line of each test case contains N space-separated integers β€” the values A_{1}, A_{2}, \ldots, A_{N}. - The third line of each test case contains N space-separated integers β€” the values B_{1}, B_{2}, \ldots, B_{N}. ------ Output Format ------ For each test case, output on a new line the maximum number of problems that Chef can solve within K minutes. ------ Constraints ------ $1 ≀ T ≀ 2\cdot 10^{4}$ $1 ≀ N ≀ 2\cdot 10^{5}$ $1 ≀ K ≀ 10^{8}$ $1 ≀ A_{i} ≀ 10^{8}$ $0 ≀ B_{i} ≀ 10^{8}$ - The sum of $N$ over all test cases won't exceed $2\cdot 10^{5}$. ----- Sample Input 1 ------ 4 3 10 3 4 5 2 4 2 3 8 3 4 5 2 4 2 5 20 23 54 124 54 83 2 9 5 2 10 5 20 4 7 12 34 13 30 4 3 0 9 ----- Sample Output 1 ------ 2 1 0 2 ----- explanation 1 ------ Test case $1$: Chef can solve the first problem followed by the second problem. - $3$ minutes for the first problem - $2$ minutes of break time after solving it, for a total of $5$ minutes - $5$ minutes for the third problem, for a total of $10$ minutes - There's two minutes of break time left which goes beyond $10$ minutes, but that's ok: Chef finished solving $2$ problems within $10$ minutes. Test case $2$: The same times as the first sample, but with $K = 8$. Now, Chef can solve any one problem, but cannot solve a second one no matter what. Test case $3$: Chef can't solve anything within $20$ minutes. Test case $4$: Chef can solve the third problem followed by the first problem.
for tcase in range(int(input())): n, k = map(int, input().split()) a = list(map(int, input().split())) b = list(map(int, input().split())) ab = [(ai + bi, ai, bi) for ai, bi in zip(a, b)] ab.sort() t0, m0, i, ans = 0, 0, 0, 0 for i in range(n): t1, m1 = t0 + ab[i][0], max(m0, ab[i][2]) if t1 <= k + m1: t0, m0 = t1, m1 ans += 1 print(ans)
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP VAR VAR VAR VAR VAR VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR VAR VAR VAR NUMBER NUMBER NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR BIN_OP VAR VAR VAR NUMBER FUNC_CALL VAR VAR VAR VAR NUMBER IF VAR BIN_OP VAR VAR ASSIGN VAR VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR
Chef came across a new online judge that has N problems, and decided that he wants to solve them. Chef takes A_{i} consecutive minutes to solve the i-th problem, and will take a break of B_{i} minutes immediately after solving it. That is, Chef will solve a problem, then take a break. Solve another problem, then take another break, and so on. Chef has K minutes of free time. If he chooses the problems and their order optimally, what is the maximum number of problems he can solve in this time? Note that a problem is considered solved if Chef finishes solving it by the K-th minute, even if the break time of the last problem extends past minute K. See the sample tests below for an example. ------ Input Format ------ - The first line of input will contain a single integer T, denoting the number of test cases. - Each test case consists of three lines of input. - The first line of each test case contains two space-separated integers N and K β€” the number of problems in the online judge and Chef's free time, respectively. - The second line of each test case contains N space-separated integers β€” the values A_{1}, A_{2}, \ldots, A_{N}. - The third line of each test case contains N space-separated integers β€” the values B_{1}, B_{2}, \ldots, B_{N}. ------ Output Format ------ For each test case, output on a new line the maximum number of problems that Chef can solve within K minutes. ------ Constraints ------ $1 ≀ T ≀ 2\cdot 10^{4}$ $1 ≀ N ≀ 2\cdot 10^{5}$ $1 ≀ K ≀ 10^{8}$ $1 ≀ A_{i} ≀ 10^{8}$ $0 ≀ B_{i} ≀ 10^{8}$ - The sum of $N$ over all test cases won't exceed $2\cdot 10^{5}$. ----- Sample Input 1 ------ 4 3 10 3 4 5 2 4 2 3 8 3 4 5 2 4 2 5 20 23 54 124 54 83 2 9 5 2 10 5 20 4 7 12 34 13 30 4 3 0 9 ----- Sample Output 1 ------ 2 1 0 2 ----- explanation 1 ------ Test case $1$: Chef can solve the first problem followed by the second problem. - $3$ minutes for the first problem - $2$ minutes of break time after solving it, for a total of $5$ minutes - $5$ minutes for the third problem, for a total of $10$ minutes - There's two minutes of break time left which goes beyond $10$ minutes, but that's ok: Chef finished solving $2$ problems within $10$ minutes. Test case $2$: The same times as the first sample, but with $K = 8$. Now, Chef can solve any one problem, but cannot solve a second one no matter what. Test case $3$: Chef can't solve anything within $20$ minutes. Test case $4$: Chef can solve the third problem followed by the first problem.
from sys import stdin, stdout input = stdin.readline r_int = lambda: int(input()) m_int = lambda: map(int, input().split()) l_int = lambda: list(map(int, input().split())) def solve(): n, k = m_int() a = l_int() b = l_int() l = [(x + y, x, y) for x, y in zip(a, b)] l.sort() acc = 0 n_ex = 0 while n_ex < n and acc + l[n_ex][0] <= k: acc += l[n_ex][0] n_ex += 1 if n_ex == n: print(n) return if acc + min(l[i][1] for i in range(n_ex, n)) <= k: print(n_ex + 1) return if n_ex == 0: print(0) return if acc + l[n_ex][0] - max(l[i][2] for i in range(n_ex)) <= k: print(n_ex + 1) else: print(n_ex) def main(): n_cases = r_int() for _ in range(n_cases): solve() main()
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 VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR BIN_OP VAR VAR VAR VAR VAR VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR BIN_OP VAR VAR VAR NUMBER VAR VAR VAR VAR NUMBER VAR NUMBER IF VAR VAR EXPR FUNC_CALL VAR VAR RETURN IF BIN_OP VAR FUNC_CALL VAR VAR VAR NUMBER VAR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER RETURN IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER RETURN IF BIN_OP BIN_OP VAR VAR VAR NUMBER FUNC_CALL VAR VAR VAR NUMBER VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR
Chef came across a new online judge that has N problems, and decided that he wants to solve them. Chef takes A_{i} consecutive minutes to solve the i-th problem, and will take a break of B_{i} minutes immediately after solving it. That is, Chef will solve a problem, then take a break. Solve another problem, then take another break, and so on. Chef has K minutes of free time. If he chooses the problems and their order optimally, what is the maximum number of problems he can solve in this time? Note that a problem is considered solved if Chef finishes solving it by the K-th minute, even if the break time of the last problem extends past minute K. See the sample tests below for an example. ------ Input Format ------ - The first line of input will contain a single integer T, denoting the number of test cases. - Each test case consists of three lines of input. - The first line of each test case contains two space-separated integers N and K β€” the number of problems in the online judge and Chef's free time, respectively. - The second line of each test case contains N space-separated integers β€” the values A_{1}, A_{2}, \ldots, A_{N}. - The third line of each test case contains N space-separated integers β€” the values B_{1}, B_{2}, \ldots, B_{N}. ------ Output Format ------ For each test case, output on a new line the maximum number of problems that Chef can solve within K minutes. ------ Constraints ------ $1 ≀ T ≀ 2\cdot 10^{4}$ $1 ≀ N ≀ 2\cdot 10^{5}$ $1 ≀ K ≀ 10^{8}$ $1 ≀ A_{i} ≀ 10^{8}$ $0 ≀ B_{i} ≀ 10^{8}$ - The sum of $N$ over all test cases won't exceed $2\cdot 10^{5}$. ----- Sample Input 1 ------ 4 3 10 3 4 5 2 4 2 3 8 3 4 5 2 4 2 5 20 23 54 124 54 83 2 9 5 2 10 5 20 4 7 12 34 13 30 4 3 0 9 ----- Sample Output 1 ------ 2 1 0 2 ----- explanation 1 ------ Test case $1$: Chef can solve the first problem followed by the second problem. - $3$ minutes for the first problem - $2$ minutes of break time after solving it, for a total of $5$ minutes - $5$ minutes for the third problem, for a total of $10$ minutes - There's two minutes of break time left which goes beyond $10$ minutes, but that's ok: Chef finished solving $2$ problems within $10$ minutes. Test case $2$: The same times as the first sample, but with $K = 8$. Now, Chef can solve any one problem, but cannot solve a second one no matter what. Test case $3$: Chef can't solve anything within $20$ minutes. Test case $4$: Chef can solve the third problem followed by the first problem.
for _ in range(int(input())): n, k = map(int, input().split()) A = [int(x) for x in input().split()] B = [int(x) for x in input().split()] arr = [(A[x] + B[x], A[x]) for x in range(n)] arr.sort() ans = 0 pre = [0] * n for i in range(n): pre[i] = arr[i][0] + (pre[i - 1] if i > 0 else 0) if pre[i] <= k: ans = i + 1 if arr[i][1] <= k: ans = max(ans, 1) for i in range(n): t = k - arr[i][1] l, h = i + 1, n - 1 left = False if i + 1 == n or pre[i + 1] - arr[i][0] > t: l, h = 0, i - 1 left = True while l <= h: mid = l + h >> 1 if left: if pre[mid] <= t: ans = max(ans, mid + 2) l = mid + 1 else: h = mid - 1 elif pre[mid] - arr[i][0] <= t: ans = max(ans, mid + 1) l = mid + 1 else: h = mid - 1 print(ans)
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP VAR VAR VAR VAR VAR VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR BIN_OP VAR VAR NUMBER VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER IF VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER IF VAR VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR NUMBER IF BIN_OP VAR NUMBER VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR NUMBER VAR ASSIGN VAR VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF VAR IF VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER IF BIN_OP VAR VAR VAR VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR
Chef came across a new online judge that has N problems, and decided that he wants to solve them. Chef takes A_{i} consecutive minutes to solve the i-th problem, and will take a break of B_{i} minutes immediately after solving it. That is, Chef will solve a problem, then take a break. Solve another problem, then take another break, and so on. Chef has K minutes of free time. If he chooses the problems and their order optimally, what is the maximum number of problems he can solve in this time? Note that a problem is considered solved if Chef finishes solving it by the K-th minute, even if the break time of the last problem extends past minute K. See the sample tests below for an example. ------ Input Format ------ - The first line of input will contain a single integer T, denoting the number of test cases. - Each test case consists of three lines of input. - The first line of each test case contains two space-separated integers N and K β€” the number of problems in the online judge and Chef's free time, respectively. - The second line of each test case contains N space-separated integers β€” the values A_{1}, A_{2}, \ldots, A_{N}. - The third line of each test case contains N space-separated integers β€” the values B_{1}, B_{2}, \ldots, B_{N}. ------ Output Format ------ For each test case, output on a new line the maximum number of problems that Chef can solve within K minutes. ------ Constraints ------ $1 ≀ T ≀ 2\cdot 10^{4}$ $1 ≀ N ≀ 2\cdot 10^{5}$ $1 ≀ K ≀ 10^{8}$ $1 ≀ A_{i} ≀ 10^{8}$ $0 ≀ B_{i} ≀ 10^{8}$ - The sum of $N$ over all test cases won't exceed $2\cdot 10^{5}$. ----- Sample Input 1 ------ 4 3 10 3 4 5 2 4 2 3 8 3 4 5 2 4 2 5 20 23 54 124 54 83 2 9 5 2 10 5 20 4 7 12 34 13 30 4 3 0 9 ----- Sample Output 1 ------ 2 1 0 2 ----- explanation 1 ------ Test case $1$: Chef can solve the first problem followed by the second problem. - $3$ minutes for the first problem - $2$ minutes of break time after solving it, for a total of $5$ minutes - $5$ minutes for the third problem, for a total of $10$ minutes - There's two minutes of break time left which goes beyond $10$ minutes, but that's ok: Chef finished solving $2$ problems within $10$ minutes. Test case $2$: The same times as the first sample, but with $K = 8$. Now, Chef can solve any one problem, but cannot solve a second one no matter what. Test case $3$: Chef can't solve anything within $20$ minutes. Test case $4$: Chef can solve the third problem followed by the first problem.
for _ in range(int(input())): n, k = list(map(int, input().split())) a = list(map(int, input().split())) b = list(map(int, input().split())) ans = [] for i in range(n): ans.append([a[i] + b[i], a[i]]) ans.sort() res = 0 i = 0 last = -1 while i < n: if ans[i][0] <= k: res += 1 k -= ans[i][0] else: last = i break i += 1 aagya = 0 while i < n: if ans[i][1] <= k: k -= ans[i][1] res += 1 aagya = 1 break i += 1 if aagya == 0 and last != -1: k -= ans[last][0] for j in range(last - 1, -1, -1): if k + ans[j][0] - ans[j][1] >= 0: res += 1 break print(res)
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL 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 FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR LIST BIN_OP VAR VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR IF VAR VAR NUMBER VAR VAR NUMBER VAR VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR IF VAR VAR NUMBER VAR VAR VAR VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER VAR NUMBER IF VAR NUMBER VAR NUMBER VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER IF BIN_OP BIN_OP VAR VAR VAR NUMBER VAR VAR NUMBER NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR
Chef came across a new online judge that has N problems, and decided that he wants to solve them. Chef takes A_{i} consecutive minutes to solve the i-th problem, and will take a break of B_{i} minutes immediately after solving it. That is, Chef will solve a problem, then take a break. Solve another problem, then take another break, and so on. Chef has K minutes of free time. If he chooses the problems and their order optimally, what is the maximum number of problems he can solve in this time? Note that a problem is considered solved if Chef finishes solving it by the K-th minute, even if the break time of the last problem extends past minute K. See the sample tests below for an example. ------ Input Format ------ - The first line of input will contain a single integer T, denoting the number of test cases. - Each test case consists of three lines of input. - The first line of each test case contains two space-separated integers N and K β€” the number of problems in the online judge and Chef's free time, respectively. - The second line of each test case contains N space-separated integers β€” the values A_{1}, A_{2}, \ldots, A_{N}. - The third line of each test case contains N space-separated integers β€” the values B_{1}, B_{2}, \ldots, B_{N}. ------ Output Format ------ For each test case, output on a new line the maximum number of problems that Chef can solve within K minutes. ------ Constraints ------ $1 ≀ T ≀ 2\cdot 10^{4}$ $1 ≀ N ≀ 2\cdot 10^{5}$ $1 ≀ K ≀ 10^{8}$ $1 ≀ A_{i} ≀ 10^{8}$ $0 ≀ B_{i} ≀ 10^{8}$ - The sum of $N$ over all test cases won't exceed $2\cdot 10^{5}$. ----- Sample Input 1 ------ 4 3 10 3 4 5 2 4 2 3 8 3 4 5 2 4 2 5 20 23 54 124 54 83 2 9 5 2 10 5 20 4 7 12 34 13 30 4 3 0 9 ----- Sample Output 1 ------ 2 1 0 2 ----- explanation 1 ------ Test case $1$: Chef can solve the first problem followed by the second problem. - $3$ minutes for the first problem - $2$ minutes of break time after solving it, for a total of $5$ minutes - $5$ minutes for the third problem, for a total of $10$ minutes - There's two minutes of break time left which goes beyond $10$ minutes, but that's ok: Chef finished solving $2$ problems within $10$ minutes. Test case $2$: The same times as the first sample, but with $K = 8$. Now, Chef can solve any one problem, but cannot solve a second one no matter what. Test case $3$: Chef can't solve anything within $20$ minutes. Test case $4$: Chef can solve the third problem followed by the first problem.
t = int(input()) while t: N, K = map(int, input().split()) A = list(map(int, input().split())) B = list(map(int, input().split())) T = list() for i in range(0, N): T.append((A[i] + B[i], i)) T.sort() ans = 0 maxs = 0 for i in range(0, N): if T[i][0] > K: break ans += 1 K -= T[i][0] if ans != N: for j in range(N): if A[j] <= K and (B[j] + A[j] >= T[i][0] or K + B[j] >= T[i][0]): ans += 1 break print(ans) t = t - 1
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR WHILE VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR 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 FOR VAR FUNC_CALL VAR NUMBER VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR VAR NUMBER VAR VAR NUMBER VAR VAR VAR NUMBER IF VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR BIN_OP VAR VAR VAR VAR VAR VAR NUMBER BIN_OP VAR VAR VAR VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR NUMBER
Chef came across a new online judge that has N problems, and decided that he wants to solve them. Chef takes A_{i} consecutive minutes to solve the i-th problem, and will take a break of B_{i} minutes immediately after solving it. That is, Chef will solve a problem, then take a break. Solve another problem, then take another break, and so on. Chef has K minutes of free time. If he chooses the problems and their order optimally, what is the maximum number of problems he can solve in this time? Note that a problem is considered solved if Chef finishes solving it by the K-th minute, even if the break time of the last problem extends past minute K. See the sample tests below for an example. ------ Input Format ------ - The first line of input will contain a single integer T, denoting the number of test cases. - Each test case consists of three lines of input. - The first line of each test case contains two space-separated integers N and K β€” the number of problems in the online judge and Chef's free time, respectively. - The second line of each test case contains N space-separated integers β€” the values A_{1}, A_{2}, \ldots, A_{N}. - The third line of each test case contains N space-separated integers β€” the values B_{1}, B_{2}, \ldots, B_{N}. ------ Output Format ------ For each test case, output on a new line the maximum number of problems that Chef can solve within K minutes. ------ Constraints ------ $1 ≀ T ≀ 2\cdot 10^{4}$ $1 ≀ N ≀ 2\cdot 10^{5}$ $1 ≀ K ≀ 10^{8}$ $1 ≀ A_{i} ≀ 10^{8}$ $0 ≀ B_{i} ≀ 10^{8}$ - The sum of $N$ over all test cases won't exceed $2\cdot 10^{5}$. ----- Sample Input 1 ------ 4 3 10 3 4 5 2 4 2 3 8 3 4 5 2 4 2 5 20 23 54 124 54 83 2 9 5 2 10 5 20 4 7 12 34 13 30 4 3 0 9 ----- Sample Output 1 ------ 2 1 0 2 ----- explanation 1 ------ Test case $1$: Chef can solve the first problem followed by the second problem. - $3$ minutes for the first problem - $2$ minutes of break time after solving it, for a total of $5$ minutes - $5$ minutes for the third problem, for a total of $10$ minutes - There's two minutes of break time left which goes beyond $10$ minutes, but that's ok: Chef finished solving $2$ problems within $10$ minutes. Test case $2$: The same times as the first sample, but with $K = 8$. Now, Chef can solve any one problem, but cannot solve a second one no matter what. Test case $3$: Chef can't solve anything within $20$ minutes. Test case $4$: Chef can solve the third problem followed by the first problem.
T = int(input()) for _ in range(T): N, K = map(int, input().split()) A = list(map(int, input().split())) B = list(map(int, input().split())) C = [(A[i], B[i]) for i in range(N)] C.sort(key=sum) ans = 0 for i in range(N): if sum(C[i]) <= K: ans += 1 K -= sum(C[i]) else: break else: print(ans) continue if min(x[0] for x in C[i:]) <= K: ans += 1 elif i > 0: m = min(sum(x) for x in C[i:]) n = max(x[1] for x in C[:i]) if n >= m - K: ans += 1 print(ans)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR 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 VAR VAR VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR VAR VAR VAR NUMBER VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR NUMBER VAR VAR VAR VAR VAR NUMBER IF VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER VAR VAR VAR IF VAR BIN_OP VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR
Chef came across a new online judge that has N problems, and decided that he wants to solve them. Chef takes A_{i} consecutive minutes to solve the i-th problem, and will take a break of B_{i} minutes immediately after solving it. That is, Chef will solve a problem, then take a break. Solve another problem, then take another break, and so on. Chef has K minutes of free time. If he chooses the problems and their order optimally, what is the maximum number of problems he can solve in this time? Note that a problem is considered solved if Chef finishes solving it by the K-th minute, even if the break time of the last problem extends past minute K. See the sample tests below for an example. ------ Input Format ------ - The first line of input will contain a single integer T, denoting the number of test cases. - Each test case consists of three lines of input. - The first line of each test case contains two space-separated integers N and K β€” the number of problems in the online judge and Chef's free time, respectively. - The second line of each test case contains N space-separated integers β€” the values A_{1}, A_{2}, \ldots, A_{N}. - The third line of each test case contains N space-separated integers β€” the values B_{1}, B_{2}, \ldots, B_{N}. ------ Output Format ------ For each test case, output on a new line the maximum number of problems that Chef can solve within K minutes. ------ Constraints ------ $1 ≀ T ≀ 2\cdot 10^{4}$ $1 ≀ N ≀ 2\cdot 10^{5}$ $1 ≀ K ≀ 10^{8}$ $1 ≀ A_{i} ≀ 10^{8}$ $0 ≀ B_{i} ≀ 10^{8}$ - The sum of $N$ over all test cases won't exceed $2\cdot 10^{5}$. ----- Sample Input 1 ------ 4 3 10 3 4 5 2 4 2 3 8 3 4 5 2 4 2 5 20 23 54 124 54 83 2 9 5 2 10 5 20 4 7 12 34 13 30 4 3 0 9 ----- Sample Output 1 ------ 2 1 0 2 ----- explanation 1 ------ Test case $1$: Chef can solve the first problem followed by the second problem. - $3$ minutes for the first problem - $2$ minutes of break time after solving it, for a total of $5$ minutes - $5$ minutes for the third problem, for a total of $10$ minutes - There's two minutes of break time left which goes beyond $10$ minutes, but that's ok: Chef finished solving $2$ problems within $10$ minutes. Test case $2$: The same times as the first sample, but with $K = 8$. Now, Chef can solve any one problem, but cannot solve a second one no matter what. Test case $3$: Chef can't solve anything within $20$ minutes. Test case $4$: Chef can solve the third problem followed by the first problem.
from sys import stdin input = stdin.readline def solve(N, K, A, B): problem = sorted([(a + b, b, i) for i, (a, b) in enumerate(zip(A, B))]) seen = set() answer = 0 count = 0 maxwait = 0 while answer < N: if count + problem[answer][0] <= K: count += problem[answer][0] maxwait = max(maxwait, problem[answer][1]) seen.add(problem[answer][2]) answer += 1 else: break for i in range(N): if i not in seen and (count + A[i] <= K or count + A[i] + B[i] - maxwait <= K): answer += 1 break return answer T = int(input().strip()) for problem in range(1, T + 1): N, K = [int(x) for x in input().strip().split()] A = [int(x) for x in input().strip().split()] B = [int(x) for x in input().strip().split()] print(solve(N, K, A, B))
ASSIGN VAR VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR IF BIN_OP VAR VAR VAR NUMBER VAR VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR BIN_OP VAR VAR VAR VAR BIN_OP BIN_OP BIN_OP VAR VAR VAR VAR VAR VAR VAR VAR NUMBER RETURN VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL FUNC_CALL VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR VAR
Vasya has a string $s$ of length $n$. He decides to make the following modification to the string: Pick an integer $k$, ($1 \leq k \leq n$). For $i$ from $1$ to $n-k+1$, reverse the substring $s[i:i+k-1]$ of $s$. For example, if string $s$ is qwer and $k = 2$, below is the series of transformations the string goes through: qwer (original string) wqer (after reversing the first substring of length $2$) weqr (after reversing the second substring of length $2$) werq (after reversing the last substring of length $2$) Hence, the resulting string after modifying $s$ with $k = 2$ is werq. Vasya wants to choose a $k$ such that the string obtained after the above-mentioned modification is lexicographically smallest possible among all choices of $k$. Among all such $k$, he wants to choose the smallest one. Since he is busy attending Felicity 2020, he asks for your help. A string $a$ is lexicographically smaller than a string $b$ if and only if one of the following holds: $a$ is a prefix of $b$, but $a \ne b$; in the first position where $a$ and $b$ differ, the string $a$ has a letter that appears earlier in the alphabet than the corresponding letter in $b$. -----Input----- Each test contains multiple test cases. The first line contains the number of test cases $t$ ($1 \le t \le 5000$). The description of the test cases follows. The first line of each test case contains a single integer $n$ ($1 \le n \le 5000$)Β β€” the length of the string $s$. The second line of each test case contains the string $s$ of $n$ lowercase latin letters. It is guaranteed that the sum of $n$ over all test cases does not exceed $5000$. -----Output----- For each testcase output two lines: In the first line output the lexicographically smallest string $s'$ achievable after the above-mentioned modification. In the second line output the appropriate value of $k$ ($1 \leq k \leq n$) that you chose for performing the modification. If there are multiple values of $k$ that give the lexicographically smallest string, output the smallest value of $k$ among them. -----Example----- Input 6 4 abab 6 qwerty 5 aaaaa 6 alaska 9 lfpbavjsm 1 p Output abab 1 ertyqw 3 aaaaa 1 aksala 6 avjsmbpfl 5 p 1 -----Note----- In the first testcase of the first sample, the string modification results for the sample abab are as follows : for $k = 1$ : abab for $k = 2$ : baba for $k = 3$ : abab for $k = 4$ : baba The lexicographically smallest string achievable through modification is abab for $k = 1$ and $3$. Smallest value of $k$ needed to achieve is hence $1$.
import sys INF = 10**10 def main(): n = get_int() st = input() mini = st ans = 1 for k in range(1, n + 1): a = st[: k - 1] if (n - k + 1) % 2 != 0: a = a[::-1] s = st[k - 1 :] + a if s < mini: ans = k mini = s print("".join(mini)) print(ans) input = iter(sys.stdin.buffer.read().decode().splitlines()).__next__ if sys.version_info[0] < 3: input = raw_input out = [] get_int = lambda: int(input()) get_list = lambda: list(map(int, input().split())) [main() for _ in range(int(input()))]
IMPORT ASSIGN VAR BIN_OP NUMBER NUMBER FUNC_DEF ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER IF BIN_OP BIN_OP BIN_OP VAR VAR NUMBER NUMBER NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR BIN_OP VAR BIN_OP VAR NUMBER VAR IF VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR FUNC_CALL STRING VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR IF VAR NUMBER NUMBER ASSIGN VAR VAR ASSIGN VAR LIST ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR
Vasya has a string $s$ of length $n$. He decides to make the following modification to the string: Pick an integer $k$, ($1 \leq k \leq n$). For $i$ from $1$ to $n-k+1$, reverse the substring $s[i:i+k-1]$ of $s$. For example, if string $s$ is qwer and $k = 2$, below is the series of transformations the string goes through: qwer (original string) wqer (after reversing the first substring of length $2$) weqr (after reversing the second substring of length $2$) werq (after reversing the last substring of length $2$) Hence, the resulting string after modifying $s$ with $k = 2$ is werq. Vasya wants to choose a $k$ such that the string obtained after the above-mentioned modification is lexicographically smallest possible among all choices of $k$. Among all such $k$, he wants to choose the smallest one. Since he is busy attending Felicity 2020, he asks for your help. A string $a$ is lexicographically smaller than a string $b$ if and only if one of the following holds: $a$ is a prefix of $b$, but $a \ne b$; in the first position where $a$ and $b$ differ, the string $a$ has a letter that appears earlier in the alphabet than the corresponding letter in $b$. -----Input----- Each test contains multiple test cases. The first line contains the number of test cases $t$ ($1 \le t \le 5000$). The description of the test cases follows. The first line of each test case contains a single integer $n$ ($1 \le n \le 5000$)Β β€” the length of the string $s$. The second line of each test case contains the string $s$ of $n$ lowercase latin letters. It is guaranteed that the sum of $n$ over all test cases does not exceed $5000$. -----Output----- For each testcase output two lines: In the first line output the lexicographically smallest string $s'$ achievable after the above-mentioned modification. In the second line output the appropriate value of $k$ ($1 \leq k \leq n$) that you chose for performing the modification. If there are multiple values of $k$ that give the lexicographically smallest string, output the smallest value of $k$ among them. -----Example----- Input 6 4 abab 6 qwerty 5 aaaaa 6 alaska 9 lfpbavjsm 1 p Output abab 1 ertyqw 3 aaaaa 1 aksala 6 avjsmbpfl 5 p 1 -----Note----- In the first testcase of the first sample, the string modification results for the sample abab are as follows : for $k = 1$ : abab for $k = 2$ : baba for $k = 3$ : abab for $k = 4$ : baba The lexicographically smallest string achievable through modification is abab for $k = 1$ and $3$. Smallest value of $k$ needed to achieve is hence $1$.
T = int(input()) for _ in range(T): n = int(input()) a = input() k = [] s = a[0] for i in range(1, n): if a[i] < s: k = [i] s = a[i] elif a[i] == s: k.append(i) b = a s = b k1 = 1 if n % 2 == 0: fo = 0 else: fo = 1 for i in k: if (i + 1) % 2 == 0 and fo == 0 or (i + 1) % 2 != 0 and fo == 1: s = a[i:] + a[i - 1 :: -1] if s < b: b = s k1 = i + 1 else: s = a[i:] + a[:i] if s < b: b = s k1 = i + 1 print(b) print(k1)
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 ASSIGN VAR LIST ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR VAR VAR ASSIGN VAR LIST VAR ASSIGN VAR VAR VAR IF VAR VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR NUMBER IF BIN_OP VAR NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR IF BIN_OP BIN_OP VAR NUMBER NUMBER NUMBER VAR NUMBER BIN_OP BIN_OP VAR NUMBER NUMBER NUMBER VAR NUMBER ASSIGN VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER NUMBER IF VAR VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR VAR VAR VAR IF VAR VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR
Vasya has a string $s$ of length $n$. He decides to make the following modification to the string: Pick an integer $k$, ($1 \leq k \leq n$). For $i$ from $1$ to $n-k+1$, reverse the substring $s[i:i+k-1]$ of $s$. For example, if string $s$ is qwer and $k = 2$, below is the series of transformations the string goes through: qwer (original string) wqer (after reversing the first substring of length $2$) weqr (after reversing the second substring of length $2$) werq (after reversing the last substring of length $2$) Hence, the resulting string after modifying $s$ with $k = 2$ is werq. Vasya wants to choose a $k$ such that the string obtained after the above-mentioned modification is lexicographically smallest possible among all choices of $k$. Among all such $k$, he wants to choose the smallest one. Since he is busy attending Felicity 2020, he asks for your help. A string $a$ is lexicographically smaller than a string $b$ if and only if one of the following holds: $a$ is a prefix of $b$, but $a \ne b$; in the first position where $a$ and $b$ differ, the string $a$ has a letter that appears earlier in the alphabet than the corresponding letter in $b$. -----Input----- Each test contains multiple test cases. The first line contains the number of test cases $t$ ($1 \le t \le 5000$). The description of the test cases follows. The first line of each test case contains a single integer $n$ ($1 \le n \le 5000$)Β β€” the length of the string $s$. The second line of each test case contains the string $s$ of $n$ lowercase latin letters. It is guaranteed that the sum of $n$ over all test cases does not exceed $5000$. -----Output----- For each testcase output two lines: In the first line output the lexicographically smallest string $s'$ achievable after the above-mentioned modification. In the second line output the appropriate value of $k$ ($1 \leq k \leq n$) that you chose for performing the modification. If there are multiple values of $k$ that give the lexicographically smallest string, output the smallest value of $k$ among them. -----Example----- Input 6 4 abab 6 qwerty 5 aaaaa 6 alaska 9 lfpbavjsm 1 p Output abab 1 ertyqw 3 aaaaa 1 aksala 6 avjsmbpfl 5 p 1 -----Note----- In the first testcase of the first sample, the string modification results for the sample abab are as follows : for $k = 1$ : abab for $k = 2$ : baba for $k = 3$ : abab for $k = 4$ : baba The lexicographically smallest string achievable through modification is abab for $k = 1$ and $3$. Smallest value of $k$ needed to achieve is hence $1$.
for _ in range(int(input())): n = int(input()) s = input() MAX = s ans = 1 for i in range(n): tmp = s[0:i] now = s[i:n] + tmp[:: (-1) ** (n - i)] if now < MAX: MAX = now ans = i + 1 print(MAX) print(ans)
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR NUMBER VAR ASSIGN VAR BIN_OP VAR VAR VAR VAR BIN_OP NUMBER BIN_OP VAR VAR IF VAR VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR
Vasya has a string $s$ of length $n$. He decides to make the following modification to the string: Pick an integer $k$, ($1 \leq k \leq n$). For $i$ from $1$ to $n-k+1$, reverse the substring $s[i:i+k-1]$ of $s$. For example, if string $s$ is qwer and $k = 2$, below is the series of transformations the string goes through: qwer (original string) wqer (after reversing the first substring of length $2$) weqr (after reversing the second substring of length $2$) werq (after reversing the last substring of length $2$) Hence, the resulting string after modifying $s$ with $k = 2$ is werq. Vasya wants to choose a $k$ such that the string obtained after the above-mentioned modification is lexicographically smallest possible among all choices of $k$. Among all such $k$, he wants to choose the smallest one. Since he is busy attending Felicity 2020, he asks for your help. A string $a$ is lexicographically smaller than a string $b$ if and only if one of the following holds: $a$ is a prefix of $b$, but $a \ne b$; in the first position where $a$ and $b$ differ, the string $a$ has a letter that appears earlier in the alphabet than the corresponding letter in $b$. -----Input----- Each test contains multiple test cases. The first line contains the number of test cases $t$ ($1 \le t \le 5000$). The description of the test cases follows. The first line of each test case contains a single integer $n$ ($1 \le n \le 5000$)Β β€” the length of the string $s$. The second line of each test case contains the string $s$ of $n$ lowercase latin letters. It is guaranteed that the sum of $n$ over all test cases does not exceed $5000$. -----Output----- For each testcase output two lines: In the first line output the lexicographically smallest string $s'$ achievable after the above-mentioned modification. In the second line output the appropriate value of $k$ ($1 \leq k \leq n$) that you chose for performing the modification. If there are multiple values of $k$ that give the lexicographically smallest string, output the smallest value of $k$ among them. -----Example----- Input 6 4 abab 6 qwerty 5 aaaaa 6 alaska 9 lfpbavjsm 1 p Output abab 1 ertyqw 3 aaaaa 1 aksala 6 avjsmbpfl 5 p 1 -----Note----- In the first testcase of the first sample, the string modification results for the sample abab are as follows : for $k = 1$ : abab for $k = 2$ : baba for $k = 3$ : abab for $k = 4$ : baba The lexicographically smallest string achievable through modification is abab for $k = 1$ and $3$. Smallest value of $k$ needed to achieve is hence $1$.
for _ in range(int(input())): n = int(input()) a = input() b = sorted(a) indices = [i for i, x in enumerate(a) if x == b[0]] ans = a set1 = {} for j in indices: if (n - j) % 2 == 0: s1 = a[j:n] + a[0:j] else: s1 = a[j:n] + a[0:j][::-1] if set1.get(s1, 0) == 0: set1[s1] = j + 1 ans = min(ans, s1) print(ans) print(set1[ans]) n = None a = None b = None indices = None ans = None
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR FUNC_CALL VAR VAR VAR VAR NUMBER ASSIGN VAR VAR ASSIGN VAR DICT FOR VAR VAR IF BIN_OP BIN_OP VAR VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR VAR VAR VAR NUMBER VAR ASSIGN VAR BIN_OP VAR VAR VAR VAR NUMBER VAR NUMBER IF FUNC_CALL VAR VAR NUMBER NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR NONE ASSIGN VAR NONE ASSIGN VAR NONE ASSIGN VAR NONE ASSIGN VAR NONE
Vasya has a string $s$ of length $n$. He decides to make the following modification to the string: Pick an integer $k$, ($1 \leq k \leq n$). For $i$ from $1$ to $n-k+1$, reverse the substring $s[i:i+k-1]$ of $s$. For example, if string $s$ is qwer and $k = 2$, below is the series of transformations the string goes through: qwer (original string) wqer (after reversing the first substring of length $2$) weqr (after reversing the second substring of length $2$) werq (after reversing the last substring of length $2$) Hence, the resulting string after modifying $s$ with $k = 2$ is werq. Vasya wants to choose a $k$ such that the string obtained after the above-mentioned modification is lexicographically smallest possible among all choices of $k$. Among all such $k$, he wants to choose the smallest one. Since he is busy attending Felicity 2020, he asks for your help. A string $a$ is lexicographically smaller than a string $b$ if and only if one of the following holds: $a$ is a prefix of $b$, but $a \ne b$; in the first position where $a$ and $b$ differ, the string $a$ has a letter that appears earlier in the alphabet than the corresponding letter in $b$. -----Input----- Each test contains multiple test cases. The first line contains the number of test cases $t$ ($1 \le t \le 5000$). The description of the test cases follows. The first line of each test case contains a single integer $n$ ($1 \le n \le 5000$)Β β€” the length of the string $s$. The second line of each test case contains the string $s$ of $n$ lowercase latin letters. It is guaranteed that the sum of $n$ over all test cases does not exceed $5000$. -----Output----- For each testcase output two lines: In the first line output the lexicographically smallest string $s'$ achievable after the above-mentioned modification. In the second line output the appropriate value of $k$ ($1 \leq k \leq n$) that you chose for performing the modification. If there are multiple values of $k$ that give the lexicographically smallest string, output the smallest value of $k$ among them. -----Example----- Input 6 4 abab 6 qwerty 5 aaaaa 6 alaska 9 lfpbavjsm 1 p Output abab 1 ertyqw 3 aaaaa 1 aksala 6 avjsmbpfl 5 p 1 -----Note----- In the first testcase of the first sample, the string modification results for the sample abab are as follows : for $k = 1$ : abab for $k = 2$ : baba for $k = 3$ : abab for $k = 4$ : baba The lexicographically smallest string achievable through modification is abab for $k = 1$ and $3$. Smallest value of $k$ needed to achieve is hence $1$.
for _ in range(int(input())): n = int(input()) a = input() x = a s = sorted(a)[0] p = 1 if n == 1: print(a, "\n", n, sep="") else: for i in range(1, n): if a[i - 1] == s: c = a[i - 1 :] + (a[: i - 1][::-1] if not (n - i) % 2 else a[: i - 1]) if c < x: x = c p = i if a[::-1] < x: x = a[::-1] p = n print(x, "\n", p, sep="")
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR VAR STRING VAR STRING FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR BIN_OP VAR NUMBER VAR ASSIGN VAR BIN_OP VAR BIN_OP VAR NUMBER BIN_OP BIN_OP VAR VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER VAR BIN_OP VAR NUMBER IF VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR IF VAR NUMBER VAR ASSIGN VAR VAR NUMBER ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR STRING VAR STRING
Vasya has a string $s$ of length $n$. He decides to make the following modification to the string: Pick an integer $k$, ($1 \leq k \leq n$). For $i$ from $1$ to $n-k+1$, reverse the substring $s[i:i+k-1]$ of $s$. For example, if string $s$ is qwer and $k = 2$, below is the series of transformations the string goes through: qwer (original string) wqer (after reversing the first substring of length $2$) weqr (after reversing the second substring of length $2$) werq (after reversing the last substring of length $2$) Hence, the resulting string after modifying $s$ with $k = 2$ is werq. Vasya wants to choose a $k$ such that the string obtained after the above-mentioned modification is lexicographically smallest possible among all choices of $k$. Among all such $k$, he wants to choose the smallest one. Since he is busy attending Felicity 2020, he asks for your help. A string $a$ is lexicographically smaller than a string $b$ if and only if one of the following holds: $a$ is a prefix of $b$, but $a \ne b$; in the first position where $a$ and $b$ differ, the string $a$ has a letter that appears earlier in the alphabet than the corresponding letter in $b$. -----Input----- Each test contains multiple test cases. The first line contains the number of test cases $t$ ($1 \le t \le 5000$). The description of the test cases follows. The first line of each test case contains a single integer $n$ ($1 \le n \le 5000$)Β β€” the length of the string $s$. The second line of each test case contains the string $s$ of $n$ lowercase latin letters. It is guaranteed that the sum of $n$ over all test cases does not exceed $5000$. -----Output----- For each testcase output two lines: In the first line output the lexicographically smallest string $s'$ achievable after the above-mentioned modification. In the second line output the appropriate value of $k$ ($1 \leq k \leq n$) that you chose for performing the modification. If there are multiple values of $k$ that give the lexicographically smallest string, output the smallest value of $k$ among them. -----Example----- Input 6 4 abab 6 qwerty 5 aaaaa 6 alaska 9 lfpbavjsm 1 p Output abab 1 ertyqw 3 aaaaa 1 aksala 6 avjsmbpfl 5 p 1 -----Note----- In the first testcase of the first sample, the string modification results for the sample abab are as follows : for $k = 1$ : abab for $k = 2$ : baba for $k = 3$ : abab for $k = 4$ : baba The lexicographically smallest string achievable through modification is abab for $k = 1$ and $3$. Smallest value of $k$ needed to achieve is hence $1$.
for _ in range(int(input())): l = int(input()) s = input() cmp = s res = s count = 1 for i in range(2, l + 1): if l % 2 == i % 2: new = s[i - 1 :] + s[: i - 1][::-1] if cmp > new: count = i res = new cmp = new else: new = s[i - 1 :] + s[: i - 1] if cmp > new: count = i res = new cmp = new print(res) print(count)
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER IF BIN_OP VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER IF VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER IF VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR
Vasya has a string $s$ of length $n$. He decides to make the following modification to the string: Pick an integer $k$, ($1 \leq k \leq n$). For $i$ from $1$ to $n-k+1$, reverse the substring $s[i:i+k-1]$ of $s$. For example, if string $s$ is qwer and $k = 2$, below is the series of transformations the string goes through: qwer (original string) wqer (after reversing the first substring of length $2$) weqr (after reversing the second substring of length $2$) werq (after reversing the last substring of length $2$) Hence, the resulting string after modifying $s$ with $k = 2$ is werq. Vasya wants to choose a $k$ such that the string obtained after the above-mentioned modification is lexicographically smallest possible among all choices of $k$. Among all such $k$, he wants to choose the smallest one. Since he is busy attending Felicity 2020, he asks for your help. A string $a$ is lexicographically smaller than a string $b$ if and only if one of the following holds: $a$ is a prefix of $b$, but $a \ne b$; in the first position where $a$ and $b$ differ, the string $a$ has a letter that appears earlier in the alphabet than the corresponding letter in $b$. -----Input----- Each test contains multiple test cases. The first line contains the number of test cases $t$ ($1 \le t \le 5000$). The description of the test cases follows. The first line of each test case contains a single integer $n$ ($1 \le n \le 5000$)Β β€” the length of the string $s$. The second line of each test case contains the string $s$ of $n$ lowercase latin letters. It is guaranteed that the sum of $n$ over all test cases does not exceed $5000$. -----Output----- For each testcase output two lines: In the first line output the lexicographically smallest string $s'$ achievable after the above-mentioned modification. In the second line output the appropriate value of $k$ ($1 \leq k \leq n$) that you chose for performing the modification. If there are multiple values of $k$ that give the lexicographically smallest string, output the smallest value of $k$ among them. -----Example----- Input 6 4 abab 6 qwerty 5 aaaaa 6 alaska 9 lfpbavjsm 1 p Output abab 1 ertyqw 3 aaaaa 1 aksala 6 avjsmbpfl 5 p 1 -----Note----- In the first testcase of the first sample, the string modification results for the sample abab are as follows : for $k = 1$ : abab for $k = 2$ : baba for $k = 3$ : abab for $k = 4$ : baba The lexicographically smallest string achievable through modification is abab for $k = 1$ and $3$. Smallest value of $k$ needed to achieve is hence $1$.
for _ in range(int(input())): n = int(input()) s = input() ch = s[0] for i in range(1, n): if ch > s[i]: ch = s[i] ks = [] for i in range(n): if s[i] == ch: ks.append(i + 1) low = -1 for k in ks: rev = False if (n - k + 1) % 2 == 1: rev = True temp = s[k - 1 : n] str1 = s[0 : k - 1] if rev: str1 = str1[::-1] temp = temp + str1 if low == -1 or ans > temp: low = k ans = temp print(ans) print(low)
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR ASSIGN VAR NUMBER IF BIN_OP BIN_OP BIN_OP VAR VAR NUMBER NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER VAR ASSIGN VAR VAR NUMBER BIN_OP VAR NUMBER IF VAR ASSIGN VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR IF VAR NUMBER VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR
Vasya has a string $s$ of length $n$. He decides to make the following modification to the string: Pick an integer $k$, ($1 \leq k \leq n$). For $i$ from $1$ to $n-k+1$, reverse the substring $s[i:i+k-1]$ of $s$. For example, if string $s$ is qwer and $k = 2$, below is the series of transformations the string goes through: qwer (original string) wqer (after reversing the first substring of length $2$) weqr (after reversing the second substring of length $2$) werq (after reversing the last substring of length $2$) Hence, the resulting string after modifying $s$ with $k = 2$ is werq. Vasya wants to choose a $k$ such that the string obtained after the above-mentioned modification is lexicographically smallest possible among all choices of $k$. Among all such $k$, he wants to choose the smallest one. Since he is busy attending Felicity 2020, he asks for your help. A string $a$ is lexicographically smaller than a string $b$ if and only if one of the following holds: $a$ is a prefix of $b$, but $a \ne b$; in the first position where $a$ and $b$ differ, the string $a$ has a letter that appears earlier in the alphabet than the corresponding letter in $b$. -----Input----- Each test contains multiple test cases. The first line contains the number of test cases $t$ ($1 \le t \le 5000$). The description of the test cases follows. The first line of each test case contains a single integer $n$ ($1 \le n \le 5000$)Β β€” the length of the string $s$. The second line of each test case contains the string $s$ of $n$ lowercase latin letters. It is guaranteed that the sum of $n$ over all test cases does not exceed $5000$. -----Output----- For each testcase output two lines: In the first line output the lexicographically smallest string $s'$ achievable after the above-mentioned modification. In the second line output the appropriate value of $k$ ($1 \leq k \leq n$) that you chose for performing the modification. If there are multiple values of $k$ that give the lexicographically smallest string, output the smallest value of $k$ among them. -----Example----- Input 6 4 abab 6 qwerty 5 aaaaa 6 alaska 9 lfpbavjsm 1 p Output abab 1 ertyqw 3 aaaaa 1 aksala 6 avjsmbpfl 5 p 1 -----Note----- In the first testcase of the first sample, the string modification results for the sample abab are as follows : for $k = 1$ : abab for $k = 2$ : baba for $k = 3$ : abab for $k = 4$ : baba The lexicographically smallest string achievable through modification is abab for $k = 1$ and $3$. Smallest value of $k$ needed to achieve is hence $1$.
for _ in range(int(input())): n = int(input()) s = input().rstrip() mi = s[:] l = 1 for k in range(2, n + 1): z = s[k - 1 :] + s[: k - 1][::-1] if n % 2 == k % 2 else s[k - 1 :] + s[: k - 1] if mi > z: mi = z[:] l = k print(mi) print(l)
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER BIN_OP VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER BIN_OP VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER IF VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR
Vasya has a string $s$ of length $n$. He decides to make the following modification to the string: Pick an integer $k$, ($1 \leq k \leq n$). For $i$ from $1$ to $n-k+1$, reverse the substring $s[i:i+k-1]$ of $s$. For example, if string $s$ is qwer and $k = 2$, below is the series of transformations the string goes through: qwer (original string) wqer (after reversing the first substring of length $2$) weqr (after reversing the second substring of length $2$) werq (after reversing the last substring of length $2$) Hence, the resulting string after modifying $s$ with $k = 2$ is werq. Vasya wants to choose a $k$ such that the string obtained after the above-mentioned modification is lexicographically smallest possible among all choices of $k$. Among all such $k$, he wants to choose the smallest one. Since he is busy attending Felicity 2020, he asks for your help. A string $a$ is lexicographically smaller than a string $b$ if and only if one of the following holds: $a$ is a prefix of $b$, but $a \ne b$; in the first position where $a$ and $b$ differ, the string $a$ has a letter that appears earlier in the alphabet than the corresponding letter in $b$. -----Input----- Each test contains multiple test cases. The first line contains the number of test cases $t$ ($1 \le t \le 5000$). The description of the test cases follows. The first line of each test case contains a single integer $n$ ($1 \le n \le 5000$)Β β€” the length of the string $s$. The second line of each test case contains the string $s$ of $n$ lowercase latin letters. It is guaranteed that the sum of $n$ over all test cases does not exceed $5000$. -----Output----- For each testcase output two lines: In the first line output the lexicographically smallest string $s'$ achievable after the above-mentioned modification. In the second line output the appropriate value of $k$ ($1 \leq k \leq n$) that you chose for performing the modification. If there are multiple values of $k$ that give the lexicographically smallest string, output the smallest value of $k$ among them. -----Example----- Input 6 4 abab 6 qwerty 5 aaaaa 6 alaska 9 lfpbavjsm 1 p Output abab 1 ertyqw 3 aaaaa 1 aksala 6 avjsmbpfl 5 p 1 -----Note----- In the first testcase of the first sample, the string modification results for the sample abab are as follows : for $k = 1$ : abab for $k = 2$ : baba for $k = 3$ : abab for $k = 4$ : baba The lexicographically smallest string achievable through modification is abab for $k = 1$ and $3$. Smallest value of $k$ needed to achieve is hence $1$.
t = int(input()) for you in range(t): n = int(input()) s = input() pref = [(0) for i in range(n)] suffer = [(0) for i in range(n)] real = [(0) for i in range(n)] sumi = "" suf = "" reo = "" for i in range(n): sumi = s[n - i - 1] + sumi pref[n - 1 - i] = sumi suf = s[i] + suf reo = reo + s[i] real[i] = reo suffer[i] = suf lfi = [] curr = 1 for i in range(n - 1, -1, -1): if i == n - 1 and i != 0: lfi.append((pref[n - 1] + suffer[n - 2], i)) curr = 0 else: if i == 0: lfi.append((s, i)) continue if curr: lfi.append((pref[i] + suffer[i - 1], i)) curr = 0 else: curr = 1 lfi.append((pref[i] + real[i - 1], i)) lfi.sort() print(lfi[0][0]) print(lfi[0][1] + 1)
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 ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR ASSIGN VAR STRING ASSIGN VAR STRING ASSIGN VAR STRING FOR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR BIN_OP BIN_OP VAR VAR NUMBER VAR ASSIGN VAR BIN_OP BIN_OP VAR NUMBER VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR LIST ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER IF VAR BIN_OP VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR ASSIGN VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR VAR VAR IF VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER
Vasya has a string $s$ of length $n$. He decides to make the following modification to the string: Pick an integer $k$, ($1 \leq k \leq n$). For $i$ from $1$ to $n-k+1$, reverse the substring $s[i:i+k-1]$ of $s$. For example, if string $s$ is qwer and $k = 2$, below is the series of transformations the string goes through: qwer (original string) wqer (after reversing the first substring of length $2$) weqr (after reversing the second substring of length $2$) werq (after reversing the last substring of length $2$) Hence, the resulting string after modifying $s$ with $k = 2$ is werq. Vasya wants to choose a $k$ such that the string obtained after the above-mentioned modification is lexicographically smallest possible among all choices of $k$. Among all such $k$, he wants to choose the smallest one. Since he is busy attending Felicity 2020, he asks for your help. A string $a$ is lexicographically smaller than a string $b$ if and only if one of the following holds: $a$ is a prefix of $b$, but $a \ne b$; in the first position where $a$ and $b$ differ, the string $a$ has a letter that appears earlier in the alphabet than the corresponding letter in $b$. -----Input----- Each test contains multiple test cases. The first line contains the number of test cases $t$ ($1 \le t \le 5000$). The description of the test cases follows. The first line of each test case contains a single integer $n$ ($1 \le n \le 5000$)Β β€” the length of the string $s$. The second line of each test case contains the string $s$ of $n$ lowercase latin letters. It is guaranteed that the sum of $n$ over all test cases does not exceed $5000$. -----Output----- For each testcase output two lines: In the first line output the lexicographically smallest string $s'$ achievable after the above-mentioned modification. In the second line output the appropriate value of $k$ ($1 \leq k \leq n$) that you chose for performing the modification. If there are multiple values of $k$ that give the lexicographically smallest string, output the smallest value of $k$ among them. -----Example----- Input 6 4 abab 6 qwerty 5 aaaaa 6 alaska 9 lfpbavjsm 1 p Output abab 1 ertyqw 3 aaaaa 1 aksala 6 avjsmbpfl 5 p 1 -----Note----- In the first testcase of the first sample, the string modification results for the sample abab are as follows : for $k = 1$ : abab for $k = 2$ : baba for $k = 3$ : abab for $k = 4$ : baba The lexicographically smallest string achievable through modification is abab for $k = 1$ and $3$. Smallest value of $k$ needed to achieve is hence $1$.
t = int(input()) while t > 0: t = t - 1 n = int(input()) s = input() li = [] for k in range(n - 1): if (n - k + 2) % 2 == 0: li.append((s[k:] + s[:k], k + 1)) else: li.append((s[k:] + s[:k][::-1], k + 1)) li.append((s[::-1], n)) s, k = min(li) print(s) print(k)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR WHILE VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER IF BIN_OP BIN_OP BIN_OP VAR VAR NUMBER NUMBER NUMBER EXPR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR NUMBER BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER VAR ASSIGN VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR
Vasya has a string $s$ of length $n$. He decides to make the following modification to the string: Pick an integer $k$, ($1 \leq k \leq n$). For $i$ from $1$ to $n-k+1$, reverse the substring $s[i:i+k-1]$ of $s$. For example, if string $s$ is qwer and $k = 2$, below is the series of transformations the string goes through: qwer (original string) wqer (after reversing the first substring of length $2$) weqr (after reversing the second substring of length $2$) werq (after reversing the last substring of length $2$) Hence, the resulting string after modifying $s$ with $k = 2$ is werq. Vasya wants to choose a $k$ such that the string obtained after the above-mentioned modification is lexicographically smallest possible among all choices of $k$. Among all such $k$, he wants to choose the smallest one. Since he is busy attending Felicity 2020, he asks for your help. A string $a$ is lexicographically smaller than a string $b$ if and only if one of the following holds: $a$ is a prefix of $b$, but $a \ne b$; in the first position where $a$ and $b$ differ, the string $a$ has a letter that appears earlier in the alphabet than the corresponding letter in $b$. -----Input----- Each test contains multiple test cases. The first line contains the number of test cases $t$ ($1 \le t \le 5000$). The description of the test cases follows. The first line of each test case contains a single integer $n$ ($1 \le n \le 5000$)Β β€” the length of the string $s$. The second line of each test case contains the string $s$ of $n$ lowercase latin letters. It is guaranteed that the sum of $n$ over all test cases does not exceed $5000$. -----Output----- For each testcase output two lines: In the first line output the lexicographically smallest string $s'$ achievable after the above-mentioned modification. In the second line output the appropriate value of $k$ ($1 \leq k \leq n$) that you chose for performing the modification. If there are multiple values of $k$ that give the lexicographically smallest string, output the smallest value of $k$ among them. -----Example----- Input 6 4 abab 6 qwerty 5 aaaaa 6 alaska 9 lfpbavjsm 1 p Output abab 1 ertyqw 3 aaaaa 1 aksala 6 avjsmbpfl 5 p 1 -----Note----- In the first testcase of the first sample, the string modification results for the sample abab are as follows : for $k = 1$ : abab for $k = 2$ : baba for $k = 3$ : abab for $k = 4$ : baba The lexicographically smallest string achievable through modification is abab for $k = 1$ and $3$. Smallest value of $k$ needed to achieve is hence $1$.
import sys input = sys.stdin.readline t = int(input()) for _ in range(t): n = int(input()) a = input() tmp = a[0:] ans = -1 for i in range(n): cnt = n - i if cnt % 2 == 0: if a[i:n] + a[0:i] < tmp: tmp = a[i:n] + a[0:i] ans = i elif a[i:n] + a[0:i][::-1] < tmp: tmp = a[i:n] + a[0:i][::-1] ans = i print(tmp) print(ans + 1)
IMPORT ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR VAR IF BIN_OP VAR NUMBER NUMBER IF BIN_OP VAR VAR VAR VAR NUMBER VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR VAR NUMBER VAR ASSIGN VAR VAR IF BIN_OP VAR VAR VAR VAR NUMBER VAR NUMBER VAR ASSIGN VAR BIN_OP VAR VAR VAR VAR NUMBER VAR NUMBER ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER
Vasya has a string $s$ of length $n$. He decides to make the following modification to the string: Pick an integer $k$, ($1 \leq k \leq n$). For $i$ from $1$ to $n-k+1$, reverse the substring $s[i:i+k-1]$ of $s$. For example, if string $s$ is qwer and $k = 2$, below is the series of transformations the string goes through: qwer (original string) wqer (after reversing the first substring of length $2$) weqr (after reversing the second substring of length $2$) werq (after reversing the last substring of length $2$) Hence, the resulting string after modifying $s$ with $k = 2$ is werq. Vasya wants to choose a $k$ such that the string obtained after the above-mentioned modification is lexicographically smallest possible among all choices of $k$. Among all such $k$, he wants to choose the smallest one. Since he is busy attending Felicity 2020, he asks for your help. A string $a$ is lexicographically smaller than a string $b$ if and only if one of the following holds: $a$ is a prefix of $b$, but $a \ne b$; in the first position where $a$ and $b$ differ, the string $a$ has a letter that appears earlier in the alphabet than the corresponding letter in $b$. -----Input----- Each test contains multiple test cases. The first line contains the number of test cases $t$ ($1 \le t \le 5000$). The description of the test cases follows. The first line of each test case contains a single integer $n$ ($1 \le n \le 5000$)Β β€” the length of the string $s$. The second line of each test case contains the string $s$ of $n$ lowercase latin letters. It is guaranteed that the sum of $n$ over all test cases does not exceed $5000$. -----Output----- For each testcase output two lines: In the first line output the lexicographically smallest string $s'$ achievable after the above-mentioned modification. In the second line output the appropriate value of $k$ ($1 \leq k \leq n$) that you chose for performing the modification. If there are multiple values of $k$ that give the lexicographically smallest string, output the smallest value of $k$ among them. -----Example----- Input 6 4 abab 6 qwerty 5 aaaaa 6 alaska 9 lfpbavjsm 1 p Output abab 1 ertyqw 3 aaaaa 1 aksala 6 avjsmbpfl 5 p 1 -----Note----- In the first testcase of the first sample, the string modification results for the sample abab are as follows : for $k = 1$ : abab for $k = 2$ : baba for $k = 3$ : abab for $k = 4$ : baba The lexicographically smallest string achievable through modification is abab for $k = 1$ and $3$. Smallest value of $k$ needed to achieve is hence $1$.
def modified(s, n, k): result_prefix = s[k - 1 :] result_suffix = s[: k - 1] if n % 2 == k % 2: result_suffix = result_suffix[::-1] return result_prefix + result_suffix for _ in range(int(input())): n = int(input()) s = input() best_s = modified(s, n, 1) best_k = 1 for i in range(2, n + 1): temp = modified(s, n, i) if temp < best_s: best_s = temp best_k = i print(best_s) print(best_k)
FUNC_DEF ASSIGN VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER IF BIN_OP VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR NUMBER RETURN BIN_OP VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR IF VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR
Vasya has a string $s$ of length $n$. He decides to make the following modification to the string: Pick an integer $k$, ($1 \leq k \leq n$). For $i$ from $1$ to $n-k+1$, reverse the substring $s[i:i+k-1]$ of $s$. For example, if string $s$ is qwer and $k = 2$, below is the series of transformations the string goes through: qwer (original string) wqer (after reversing the first substring of length $2$) weqr (after reversing the second substring of length $2$) werq (after reversing the last substring of length $2$) Hence, the resulting string after modifying $s$ with $k = 2$ is werq. Vasya wants to choose a $k$ such that the string obtained after the above-mentioned modification is lexicographically smallest possible among all choices of $k$. Among all such $k$, he wants to choose the smallest one. Since he is busy attending Felicity 2020, he asks for your help. A string $a$ is lexicographically smaller than a string $b$ if and only if one of the following holds: $a$ is a prefix of $b$, but $a \ne b$; in the first position where $a$ and $b$ differ, the string $a$ has a letter that appears earlier in the alphabet than the corresponding letter in $b$. -----Input----- Each test contains multiple test cases. The first line contains the number of test cases $t$ ($1 \le t \le 5000$). The description of the test cases follows. The first line of each test case contains a single integer $n$ ($1 \le n \le 5000$)Β β€” the length of the string $s$. The second line of each test case contains the string $s$ of $n$ lowercase latin letters. It is guaranteed that the sum of $n$ over all test cases does not exceed $5000$. -----Output----- For each testcase output two lines: In the first line output the lexicographically smallest string $s'$ achievable after the above-mentioned modification. In the second line output the appropriate value of $k$ ($1 \leq k \leq n$) that you chose for performing the modification. If there are multiple values of $k$ that give the lexicographically smallest string, output the smallest value of $k$ among them. -----Example----- Input 6 4 abab 6 qwerty 5 aaaaa 6 alaska 9 lfpbavjsm 1 p Output abab 1 ertyqw 3 aaaaa 1 aksala 6 avjsmbpfl 5 p 1 -----Note----- In the first testcase of the first sample, the string modification results for the sample abab are as follows : for $k = 1$ : abab for $k = 2$ : baba for $k = 3$ : abab for $k = 4$ : baba The lexicographically smallest string achievable through modification is abab for $k = 1$ and $3$. Smallest value of $k$ needed to achieve is hence $1$.
t = int(input()) for _ in range(t): n = int(input()) s = str(input()) X = [] for k in range(1, n + 1): p = s[k - 1 :] q = s[0 : k - 1] if (n - k - 1) % 2 == 1: t = p + q[::-1] else: t = p + q X.append([t, k]) X.sort() print(X[0][0]) print(X[0][1])
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR NUMBER BIN_OP VAR NUMBER IF BIN_OP BIN_OP BIN_OP VAR VAR NUMBER NUMBER NUMBER ASSIGN VAR BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR LIST VAR VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR NUMBER NUMBER
Vasya has a string $s$ of length $n$. He decides to make the following modification to the string: Pick an integer $k$, ($1 \leq k \leq n$). For $i$ from $1$ to $n-k+1$, reverse the substring $s[i:i+k-1]$ of $s$. For example, if string $s$ is qwer and $k = 2$, below is the series of transformations the string goes through: qwer (original string) wqer (after reversing the first substring of length $2$) weqr (after reversing the second substring of length $2$) werq (after reversing the last substring of length $2$) Hence, the resulting string after modifying $s$ with $k = 2$ is werq. Vasya wants to choose a $k$ such that the string obtained after the above-mentioned modification is lexicographically smallest possible among all choices of $k$. Among all such $k$, he wants to choose the smallest one. Since he is busy attending Felicity 2020, he asks for your help. A string $a$ is lexicographically smaller than a string $b$ if and only if one of the following holds: $a$ is a prefix of $b$, but $a \ne b$; in the first position where $a$ and $b$ differ, the string $a$ has a letter that appears earlier in the alphabet than the corresponding letter in $b$. -----Input----- Each test contains multiple test cases. The first line contains the number of test cases $t$ ($1 \le t \le 5000$). The description of the test cases follows. The first line of each test case contains a single integer $n$ ($1 \le n \le 5000$)Β β€” the length of the string $s$. The second line of each test case contains the string $s$ of $n$ lowercase latin letters. It is guaranteed that the sum of $n$ over all test cases does not exceed $5000$. -----Output----- For each testcase output two lines: In the first line output the lexicographically smallest string $s'$ achievable after the above-mentioned modification. In the second line output the appropriate value of $k$ ($1 \leq k \leq n$) that you chose for performing the modification. If there are multiple values of $k$ that give the lexicographically smallest string, output the smallest value of $k$ among them. -----Example----- Input 6 4 abab 6 qwerty 5 aaaaa 6 alaska 9 lfpbavjsm 1 p Output abab 1 ertyqw 3 aaaaa 1 aksala 6 avjsmbpfl 5 p 1 -----Note----- In the first testcase of the first sample, the string modification results for the sample abab are as follows : for $k = 1$ : abab for $k = 2$ : baba for $k = 3$ : abab for $k = 4$ : baba The lexicographically smallest string achievable through modification is abab for $k = 1$ and $3$. Smallest value of $k$ needed to achieve is hence $1$.
def f(): n = int(input()) s = input() rk = 0 rs = s for k in range(1, n): if (n - k) % 2: cur = s[k:] + s[:k][::-1] else: cur = s[k:] + s[:k] if cur < rs: rs = cur rk = k print(rs) print(rk + 1) t = int(input()) for i in range(t): f()
FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR VAR FOR VAR FUNC_CALL VAR NUMBER VAR IF BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR VAR VAR IF VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR
Vasya has a string $s$ of length $n$. He decides to make the following modification to the string: Pick an integer $k$, ($1 \leq k \leq n$). For $i$ from $1$ to $n-k+1$, reverse the substring $s[i:i+k-1]$ of $s$. For example, if string $s$ is qwer and $k = 2$, below is the series of transformations the string goes through: qwer (original string) wqer (after reversing the first substring of length $2$) weqr (after reversing the second substring of length $2$) werq (after reversing the last substring of length $2$) Hence, the resulting string after modifying $s$ with $k = 2$ is werq. Vasya wants to choose a $k$ such that the string obtained after the above-mentioned modification is lexicographically smallest possible among all choices of $k$. Among all such $k$, he wants to choose the smallest one. Since he is busy attending Felicity 2020, he asks for your help. A string $a$ is lexicographically smaller than a string $b$ if and only if one of the following holds: $a$ is a prefix of $b$, but $a \ne b$; in the first position where $a$ and $b$ differ, the string $a$ has a letter that appears earlier in the alphabet than the corresponding letter in $b$. -----Input----- Each test contains multiple test cases. The first line contains the number of test cases $t$ ($1 \le t \le 5000$). The description of the test cases follows. The first line of each test case contains a single integer $n$ ($1 \le n \le 5000$)Β β€” the length of the string $s$. The second line of each test case contains the string $s$ of $n$ lowercase latin letters. It is guaranteed that the sum of $n$ over all test cases does not exceed $5000$. -----Output----- For each testcase output two lines: In the first line output the lexicographically smallest string $s'$ achievable after the above-mentioned modification. In the second line output the appropriate value of $k$ ($1 \leq k \leq n$) that you chose for performing the modification. If there are multiple values of $k$ that give the lexicographically smallest string, output the smallest value of $k$ among them. -----Example----- Input 6 4 abab 6 qwerty 5 aaaaa 6 alaska 9 lfpbavjsm 1 p Output abab 1 ertyqw 3 aaaaa 1 aksala 6 avjsmbpfl 5 p 1 -----Note----- In the first testcase of the first sample, the string modification results for the sample abab are as follows : for $k = 1$ : abab for $k = 2$ : baba for $k = 3$ : abab for $k = 4$ : baba The lexicographically smallest string achievable through modification is abab for $k = 1$ and $3$. Smallest value of $k$ needed to achieve is hence $1$.
def main(): for _ in range(int(input())): n = int(input()) s = list(input()) k = 1 ans = s for i in range(2, n + 1): if (n - i - 1) % 2 == 1 and s[i - 1 : n] + s[i - 2 :: -1] < ans: k = i ans = s[i - 1 : n] + s[i - 2 :: -1] elif (n - i - 1) % 2 == 0 and s[i - 1 : n] + s[0 : i - 1] < ans: k = i ans = s[i - 1 : n] + s[0 : i - 1] print("".join(ans)) print(k) main()
FUNC_DEF FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER IF BIN_OP BIN_OP BIN_OP VAR VAR NUMBER NUMBER NUMBER BIN_OP VAR BIN_OP VAR NUMBER VAR VAR BIN_OP VAR NUMBER NUMBER VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR BIN_OP VAR NUMBER NUMBER IF BIN_OP BIN_OP BIN_OP VAR VAR NUMBER NUMBER NUMBER BIN_OP VAR BIN_OP VAR NUMBER VAR VAR NUMBER BIN_OP VAR NUMBER VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR NUMBER BIN_OP VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL STRING VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR
Vasya has a string $s$ of length $n$. He decides to make the following modification to the string: Pick an integer $k$, ($1 \leq k \leq n$). For $i$ from $1$ to $n-k+1$, reverse the substring $s[i:i+k-1]$ of $s$. For example, if string $s$ is qwer and $k = 2$, below is the series of transformations the string goes through: qwer (original string) wqer (after reversing the first substring of length $2$) weqr (after reversing the second substring of length $2$) werq (after reversing the last substring of length $2$) Hence, the resulting string after modifying $s$ with $k = 2$ is werq. Vasya wants to choose a $k$ such that the string obtained after the above-mentioned modification is lexicographically smallest possible among all choices of $k$. Among all such $k$, he wants to choose the smallest one. Since he is busy attending Felicity 2020, he asks for your help. A string $a$ is lexicographically smaller than a string $b$ if and only if one of the following holds: $a$ is a prefix of $b$, but $a \ne b$; in the first position where $a$ and $b$ differ, the string $a$ has a letter that appears earlier in the alphabet than the corresponding letter in $b$. -----Input----- Each test contains multiple test cases. The first line contains the number of test cases $t$ ($1 \le t \le 5000$). The description of the test cases follows. The first line of each test case contains a single integer $n$ ($1 \le n \le 5000$)Β β€” the length of the string $s$. The second line of each test case contains the string $s$ of $n$ lowercase latin letters. It is guaranteed that the sum of $n$ over all test cases does not exceed $5000$. -----Output----- For each testcase output two lines: In the first line output the lexicographically smallest string $s'$ achievable after the above-mentioned modification. In the second line output the appropriate value of $k$ ($1 \leq k \leq n$) that you chose for performing the modification. If there are multiple values of $k$ that give the lexicographically smallest string, output the smallest value of $k$ among them. -----Example----- Input 6 4 abab 6 qwerty 5 aaaaa 6 alaska 9 lfpbavjsm 1 p Output abab 1 ertyqw 3 aaaaa 1 aksala 6 avjsmbpfl 5 p 1 -----Note----- In the first testcase of the first sample, the string modification results for the sample abab are as follows : for $k = 1$ : abab for $k = 2$ : baba for $k = 3$ : abab for $k = 4$ : baba The lexicographically smallest string achievable through modification is abab for $k = 1$ and $3$. Smallest value of $k$ needed to achieve is hence $1$.
t = 1 t = int(input()) while t: t -= 1 n = int(input()) s = input() minStr, minIdx = s[:], 0 for i in range(n): comp = s[i:] + s[:i] if (n - i) % 2 == 0 else s[i:] + s[:i][::-1] if comp < minStr: minStr = comp minIdx = i print(minStr) print(minIdx + 1)
ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR WHILE VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER NUMBER BIN_OP VAR VAR VAR VAR BIN_OP VAR VAR VAR VAR NUMBER IF VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER
Vasya has a string $s$ of length $n$. He decides to make the following modification to the string: Pick an integer $k$, ($1 \leq k \leq n$). For $i$ from $1$ to $n-k+1$, reverse the substring $s[i:i+k-1]$ of $s$. For example, if string $s$ is qwer and $k = 2$, below is the series of transformations the string goes through: qwer (original string) wqer (after reversing the first substring of length $2$) weqr (after reversing the second substring of length $2$) werq (after reversing the last substring of length $2$) Hence, the resulting string after modifying $s$ with $k = 2$ is werq. Vasya wants to choose a $k$ such that the string obtained after the above-mentioned modification is lexicographically smallest possible among all choices of $k$. Among all such $k$, he wants to choose the smallest one. Since he is busy attending Felicity 2020, he asks for your help. A string $a$ is lexicographically smaller than a string $b$ if and only if one of the following holds: $a$ is a prefix of $b$, but $a \ne b$; in the first position where $a$ and $b$ differ, the string $a$ has a letter that appears earlier in the alphabet than the corresponding letter in $b$. -----Input----- Each test contains multiple test cases. The first line contains the number of test cases $t$ ($1 \le t \le 5000$). The description of the test cases follows. The first line of each test case contains a single integer $n$ ($1 \le n \le 5000$)Β β€” the length of the string $s$. The second line of each test case contains the string $s$ of $n$ lowercase latin letters. It is guaranteed that the sum of $n$ over all test cases does not exceed $5000$. -----Output----- For each testcase output two lines: In the first line output the lexicographically smallest string $s'$ achievable after the above-mentioned modification. In the second line output the appropriate value of $k$ ($1 \leq k \leq n$) that you chose for performing the modification. If there are multiple values of $k$ that give the lexicographically smallest string, output the smallest value of $k$ among them. -----Example----- Input 6 4 abab 6 qwerty 5 aaaaa 6 alaska 9 lfpbavjsm 1 p Output abab 1 ertyqw 3 aaaaa 1 aksala 6 avjsmbpfl 5 p 1 -----Note----- In the first testcase of the first sample, the string modification results for the sample abab are as follows : for $k = 1$ : abab for $k = 2$ : baba for $k = 3$ : abab for $k = 4$ : baba The lexicographically smallest string achievable through modification is abab for $k = 1$ and $3$. Smallest value of $k$ needed to achieve is hence $1$.
t = int(input()) for _ in range(t): n = int(input()) s = input() ls = min(s) i = s.find(ls) candidates = [] while i != -1: if i == n - 1: candidates.append((s[::-1], n)) break if (n - i) % 2 == 0: candidates.append((s[i:] + s[:i], i + 1)) else: candidates.append((s[i:] + s[:i][::-1], i + 1)) i = s.find(ls, i + 1) candidates.sort() print(candidates[0][0] + "\n" + str(candidates[0][1]))
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 ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR LIST WHILE VAR NUMBER IF VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER VAR IF BIN_OP BIN_OP VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR BIN_OP BIN_OP VAR NUMBER NUMBER STRING FUNC_CALL VAR VAR NUMBER NUMBER
Vasya has a string $s$ of length $n$. He decides to make the following modification to the string: Pick an integer $k$, ($1 \leq k \leq n$). For $i$ from $1$ to $n-k+1$, reverse the substring $s[i:i+k-1]$ of $s$. For example, if string $s$ is qwer and $k = 2$, below is the series of transformations the string goes through: qwer (original string) wqer (after reversing the first substring of length $2$) weqr (after reversing the second substring of length $2$) werq (after reversing the last substring of length $2$) Hence, the resulting string after modifying $s$ with $k = 2$ is werq. Vasya wants to choose a $k$ such that the string obtained after the above-mentioned modification is lexicographically smallest possible among all choices of $k$. Among all such $k$, he wants to choose the smallest one. Since he is busy attending Felicity 2020, he asks for your help. A string $a$ is lexicographically smaller than a string $b$ if and only if one of the following holds: $a$ is a prefix of $b$, but $a \ne b$; in the first position where $a$ and $b$ differ, the string $a$ has a letter that appears earlier in the alphabet than the corresponding letter in $b$. -----Input----- Each test contains multiple test cases. The first line contains the number of test cases $t$ ($1 \le t \le 5000$). The description of the test cases follows. The first line of each test case contains a single integer $n$ ($1 \le n \le 5000$)Β β€” the length of the string $s$. The second line of each test case contains the string $s$ of $n$ lowercase latin letters. It is guaranteed that the sum of $n$ over all test cases does not exceed $5000$. -----Output----- For each testcase output two lines: In the first line output the lexicographically smallest string $s'$ achievable after the above-mentioned modification. In the second line output the appropriate value of $k$ ($1 \leq k \leq n$) that you chose for performing the modification. If there are multiple values of $k$ that give the lexicographically smallest string, output the smallest value of $k$ among them. -----Example----- Input 6 4 abab 6 qwerty 5 aaaaa 6 alaska 9 lfpbavjsm 1 p Output abab 1 ertyqw 3 aaaaa 1 aksala 6 avjsmbpfl 5 p 1 -----Note----- In the first testcase of the first sample, the string modification results for the sample abab are as follows : for $k = 1$ : abab for $k = 2$ : baba for $k = 3$ : abab for $k = 4$ : baba The lexicographically smallest string achievable through modification is abab for $k = 1$ and $3$. Smallest value of $k$ needed to achieve is hence $1$.
def check(k, st): n = len(st) if (n - k + 1) % 2 == 0: return st[k - 1 : n : 1] + st[0 : k - 1 : 1] else: return st[k - 1 : n : 1] + st[k - 2 :: -1] def process(n, st): s = st result = 1 for k in range(1, n + 1): kq = check(k, st) if kq < s: s = kq result = k print(s) print(result) t = int(input()) for i in range(0, t): n = int(input()) st = input() process(n, st)
FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR IF BIN_OP BIN_OP BIN_OP VAR VAR NUMBER NUMBER NUMBER RETURN BIN_OP VAR BIN_OP VAR NUMBER VAR NUMBER VAR NUMBER BIN_OP VAR NUMBER NUMBER RETURN BIN_OP VAR BIN_OP VAR NUMBER VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER FUNC_DEF ASSIGN VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR IF VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR VAR
Vasya has a string $s$ of length $n$. He decides to make the following modification to the string: Pick an integer $k$, ($1 \leq k \leq n$). For $i$ from $1$ to $n-k+1$, reverse the substring $s[i:i+k-1]$ of $s$. For example, if string $s$ is qwer and $k = 2$, below is the series of transformations the string goes through: qwer (original string) wqer (after reversing the first substring of length $2$) weqr (after reversing the second substring of length $2$) werq (after reversing the last substring of length $2$) Hence, the resulting string after modifying $s$ with $k = 2$ is werq. Vasya wants to choose a $k$ such that the string obtained after the above-mentioned modification is lexicographically smallest possible among all choices of $k$. Among all such $k$, he wants to choose the smallest one. Since he is busy attending Felicity 2020, he asks for your help. A string $a$ is lexicographically smaller than a string $b$ if and only if one of the following holds: $a$ is a prefix of $b$, but $a \ne b$; in the first position where $a$ and $b$ differ, the string $a$ has a letter that appears earlier in the alphabet than the corresponding letter in $b$. -----Input----- Each test contains multiple test cases. The first line contains the number of test cases $t$ ($1 \le t \le 5000$). The description of the test cases follows. The first line of each test case contains a single integer $n$ ($1 \le n \le 5000$)Β β€” the length of the string $s$. The second line of each test case contains the string $s$ of $n$ lowercase latin letters. It is guaranteed that the sum of $n$ over all test cases does not exceed $5000$. -----Output----- For each testcase output two lines: In the first line output the lexicographically smallest string $s'$ achievable after the above-mentioned modification. In the second line output the appropriate value of $k$ ($1 \leq k \leq n$) that you chose for performing the modification. If there are multiple values of $k$ that give the lexicographically smallest string, output the smallest value of $k$ among them. -----Example----- Input 6 4 abab 6 qwerty 5 aaaaa 6 alaska 9 lfpbavjsm 1 p Output abab 1 ertyqw 3 aaaaa 1 aksala 6 avjsmbpfl 5 p 1 -----Note----- In the first testcase of the first sample, the string modification results for the sample abab are as follows : for $k = 1$ : abab for $k = 2$ : baba for $k = 3$ : abab for $k = 4$ : baba The lexicographically smallest string achievable through modification is abab for $k = 1$ and $3$. Smallest value of $k$ needed to achieve is hence $1$.
t = int(input()) while t: n = int(input()) s = input() m = s mk = 1 for k in range(2, n + 1): tmp = s[k - n - 1 :] + (s[k - 2 :: -1] if (n - k + 1) % 2 == 1 else s[: k - 1]) if tmp < m: m = tmp mk = k print(m) print(mk) t -= 1
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR WHILE VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR BIN_OP BIN_OP VAR VAR NUMBER BIN_OP BIN_OP BIN_OP VAR VAR NUMBER NUMBER NUMBER VAR BIN_OP VAR NUMBER NUMBER VAR BIN_OP VAR NUMBER IF VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR NUMBER
Vasya has a string $s$ of length $n$. He decides to make the following modification to the string: Pick an integer $k$, ($1 \leq k \leq n$). For $i$ from $1$ to $n-k+1$, reverse the substring $s[i:i+k-1]$ of $s$. For example, if string $s$ is qwer and $k = 2$, below is the series of transformations the string goes through: qwer (original string) wqer (after reversing the first substring of length $2$) weqr (after reversing the second substring of length $2$) werq (after reversing the last substring of length $2$) Hence, the resulting string after modifying $s$ with $k = 2$ is werq. Vasya wants to choose a $k$ such that the string obtained after the above-mentioned modification is lexicographically smallest possible among all choices of $k$. Among all such $k$, he wants to choose the smallest one. Since he is busy attending Felicity 2020, he asks for your help. A string $a$ is lexicographically smaller than a string $b$ if and only if one of the following holds: $a$ is a prefix of $b$, but $a \ne b$; in the first position where $a$ and $b$ differ, the string $a$ has a letter that appears earlier in the alphabet than the corresponding letter in $b$. -----Input----- Each test contains multiple test cases. The first line contains the number of test cases $t$ ($1 \le t \le 5000$). The description of the test cases follows. The first line of each test case contains a single integer $n$ ($1 \le n \le 5000$)Β β€” the length of the string $s$. The second line of each test case contains the string $s$ of $n$ lowercase latin letters. It is guaranteed that the sum of $n$ over all test cases does not exceed $5000$. -----Output----- For each testcase output two lines: In the first line output the lexicographically smallest string $s'$ achievable after the above-mentioned modification. In the second line output the appropriate value of $k$ ($1 \leq k \leq n$) that you chose for performing the modification. If there are multiple values of $k$ that give the lexicographically smallest string, output the smallest value of $k$ among them. -----Example----- Input 6 4 abab 6 qwerty 5 aaaaa 6 alaska 9 lfpbavjsm 1 p Output abab 1 ertyqw 3 aaaaa 1 aksala 6 avjsmbpfl 5 p 1 -----Note----- In the first testcase of the first sample, the string modification results for the sample abab are as follows : for $k = 1$ : abab for $k = 2$ : baba for $k = 3$ : abab for $k = 4$ : baba The lexicographically smallest string achievable through modification is abab for $k = 1$ and $3$. Smallest value of $k$ needed to achieve is hence $1$.
def swap(s, n, k): if (n - k + 1) % 2 == 0: return s[k - 1 :] + s[: k - 1] return s[k - 1 :] + s[: k - 1][::-1] for _ in range(int(input())): n = int(input()) s = input() a = [] c = "" m = s k = 0 for i in "abcdefghijklmnopqrstuvwxyz": if i in s: c = i break for i in range(n): if c == s[i]: a.append(i) for i in a: if i == 0: k = 1 else: t = swap(s, n, i + 1) if m > t: k = i + 1 m = t print(m) print(k)
FUNC_DEF IF BIN_OP BIN_OP BIN_OP VAR VAR NUMBER NUMBER NUMBER RETURN BIN_OP VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER RETURN BIN_OP VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR STRING ASSIGN VAR VAR ASSIGN VAR NUMBER FOR VAR STRING IF VAR VAR ASSIGN VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR EXPR FUNC_CALL VAR VAR FOR VAR VAR IF VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER IF VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR
Vasya has a string $s$ of length $n$. He decides to make the following modification to the string: Pick an integer $k$, ($1 \leq k \leq n$). For $i$ from $1$ to $n-k+1$, reverse the substring $s[i:i+k-1]$ of $s$. For example, if string $s$ is qwer and $k = 2$, below is the series of transformations the string goes through: qwer (original string) wqer (after reversing the first substring of length $2$) weqr (after reversing the second substring of length $2$) werq (after reversing the last substring of length $2$) Hence, the resulting string after modifying $s$ with $k = 2$ is werq. Vasya wants to choose a $k$ such that the string obtained after the above-mentioned modification is lexicographically smallest possible among all choices of $k$. Among all such $k$, he wants to choose the smallest one. Since he is busy attending Felicity 2020, he asks for your help. A string $a$ is lexicographically smaller than a string $b$ if and only if one of the following holds: $a$ is a prefix of $b$, but $a \ne b$; in the first position where $a$ and $b$ differ, the string $a$ has a letter that appears earlier in the alphabet than the corresponding letter in $b$. -----Input----- Each test contains multiple test cases. The first line contains the number of test cases $t$ ($1 \le t \le 5000$). The description of the test cases follows. The first line of each test case contains a single integer $n$ ($1 \le n \le 5000$)Β β€” the length of the string $s$. The second line of each test case contains the string $s$ of $n$ lowercase latin letters. It is guaranteed that the sum of $n$ over all test cases does not exceed $5000$. -----Output----- For each testcase output two lines: In the first line output the lexicographically smallest string $s'$ achievable after the above-mentioned modification. In the second line output the appropriate value of $k$ ($1 \leq k \leq n$) that you chose for performing the modification. If there are multiple values of $k$ that give the lexicographically smallest string, output the smallest value of $k$ among them. -----Example----- Input 6 4 abab 6 qwerty 5 aaaaa 6 alaska 9 lfpbavjsm 1 p Output abab 1 ertyqw 3 aaaaa 1 aksala 6 avjsmbpfl 5 p 1 -----Note----- In the first testcase of the first sample, the string modification results for the sample abab are as follows : for $k = 1$ : abab for $k = 2$ : baba for $k = 3$ : abab for $k = 4$ : baba The lexicographically smallest string achievable through modification is abab for $k = 1$ and $3$. Smallest value of $k$ needed to achieve is hence $1$.
import sys input = sys.stdin.readline tc = int(input()) def newallswap(x, k): n = len(x) if k == 1: return x if k == len(x): return x[::-1] b = n % 2 a = x[k - 2 :: -1] if k % 2 == b else x[: k - 1] y = x[k - 1 :] + a return y for _ in range(tc): n = int(input()) a = input().rstrip() x = list(a) t = min(a) y = x index = [] for k in range(2, len(x) + 1): if a[k - 1] == t: index.append(k) m = 1 for k in index: new = newallswap(x, k) if y > new: y = new m = k print("".join(y)) print(m)
IMPORT ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR IF VAR NUMBER RETURN VAR IF VAR FUNC_CALL VAR VAR RETURN VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER VAR VAR BIN_OP VAR NUMBER NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR BIN_OP VAR NUMBER VAR RETURN VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER IF VAR BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR IF VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR FUNC_CALL STRING VAR EXPR FUNC_CALL VAR VAR
Vasya has a string $s$ of length $n$. He decides to make the following modification to the string: Pick an integer $k$, ($1 \leq k \leq n$). For $i$ from $1$ to $n-k+1$, reverse the substring $s[i:i+k-1]$ of $s$. For example, if string $s$ is qwer and $k = 2$, below is the series of transformations the string goes through: qwer (original string) wqer (after reversing the first substring of length $2$) weqr (after reversing the second substring of length $2$) werq (after reversing the last substring of length $2$) Hence, the resulting string after modifying $s$ with $k = 2$ is werq. Vasya wants to choose a $k$ such that the string obtained after the above-mentioned modification is lexicographically smallest possible among all choices of $k$. Among all such $k$, he wants to choose the smallest one. Since he is busy attending Felicity 2020, he asks for your help. A string $a$ is lexicographically smaller than a string $b$ if and only if one of the following holds: $a$ is a prefix of $b$, but $a \ne b$; in the first position where $a$ and $b$ differ, the string $a$ has a letter that appears earlier in the alphabet than the corresponding letter in $b$. -----Input----- Each test contains multiple test cases. The first line contains the number of test cases $t$ ($1 \le t \le 5000$). The description of the test cases follows. The first line of each test case contains a single integer $n$ ($1 \le n \le 5000$)Β β€” the length of the string $s$. The second line of each test case contains the string $s$ of $n$ lowercase latin letters. It is guaranteed that the sum of $n$ over all test cases does not exceed $5000$. -----Output----- For each testcase output two lines: In the first line output the lexicographically smallest string $s'$ achievable after the above-mentioned modification. In the second line output the appropriate value of $k$ ($1 \leq k \leq n$) that you chose for performing the modification. If there are multiple values of $k$ that give the lexicographically smallest string, output the smallest value of $k$ among them. -----Example----- Input 6 4 abab 6 qwerty 5 aaaaa 6 alaska 9 lfpbavjsm 1 p Output abab 1 ertyqw 3 aaaaa 1 aksala 6 avjsmbpfl 5 p 1 -----Note----- In the first testcase of the first sample, the string modification results for the sample abab are as follows : for $k = 1$ : abab for $k = 2$ : baba for $k = 3$ : abab for $k = 4$ : baba The lexicographically smallest string achievable through modification is abab for $k = 1$ and $3$. Smallest value of $k$ needed to achieve is hence $1$.
testCases = int(input()) for i1 in range(testCases): length = int(input()) sequence = input() ans = 1 result = sequence for k in range(length): if k % 2 == length % 2: new = sequence[k:] + sequence[0:k] else: new = sequence[k:] + sequence[0:k][::-1] if new < result: result = new ans = k + 1 print(result) 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 ASSIGN VAR NUMBER ASSIGN VAR VAR FOR VAR FUNC_CALL VAR VAR IF BIN_OP VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR VAR VAR NUMBER VAR ASSIGN VAR BIN_OP VAR VAR VAR NUMBER VAR NUMBER IF VAR VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR
Vasya has a string $s$ of length $n$. He decides to make the following modification to the string: Pick an integer $k$, ($1 \leq k \leq n$). For $i$ from $1$ to $n-k+1$, reverse the substring $s[i:i+k-1]$ of $s$. For example, if string $s$ is qwer and $k = 2$, below is the series of transformations the string goes through: qwer (original string) wqer (after reversing the first substring of length $2$) weqr (after reversing the second substring of length $2$) werq (after reversing the last substring of length $2$) Hence, the resulting string after modifying $s$ with $k = 2$ is werq. Vasya wants to choose a $k$ such that the string obtained after the above-mentioned modification is lexicographically smallest possible among all choices of $k$. Among all such $k$, he wants to choose the smallest one. Since he is busy attending Felicity 2020, he asks for your help. A string $a$ is lexicographically smaller than a string $b$ if and only if one of the following holds: $a$ is a prefix of $b$, but $a \ne b$; in the first position where $a$ and $b$ differ, the string $a$ has a letter that appears earlier in the alphabet than the corresponding letter in $b$. -----Input----- Each test contains multiple test cases. The first line contains the number of test cases $t$ ($1 \le t \le 5000$). The description of the test cases follows. The first line of each test case contains a single integer $n$ ($1 \le n \le 5000$)Β β€” the length of the string $s$. The second line of each test case contains the string $s$ of $n$ lowercase latin letters. It is guaranteed that the sum of $n$ over all test cases does not exceed $5000$. -----Output----- For each testcase output two lines: In the first line output the lexicographically smallest string $s'$ achievable after the above-mentioned modification. In the second line output the appropriate value of $k$ ($1 \leq k \leq n$) that you chose for performing the modification. If there are multiple values of $k$ that give the lexicographically smallest string, output the smallest value of $k$ among them. -----Example----- Input 6 4 abab 6 qwerty 5 aaaaa 6 alaska 9 lfpbavjsm 1 p Output abab 1 ertyqw 3 aaaaa 1 aksala 6 avjsmbpfl 5 p 1 -----Note----- In the first testcase of the first sample, the string modification results for the sample abab are as follows : for $k = 1$ : abab for $k = 2$ : baba for $k = 3$ : abab for $k = 4$ : baba The lexicographically smallest string achievable through modification is abab for $k = 1$ and $3$. Smallest value of $k$ needed to achieve is hence $1$.
t = int(input()) for _ in range(t): n = int(input()) s = input() l = [] for k in range(1, n + 1): if (n - k) % 2 == 1: l.append((s[k - 1 :] + s[: k - 1], k)) else: l.append((s[k - 1 :] + s[: k - 1][::-1], k)) ans = min(l) print(ans[0]) print(ans[1])
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 ASSIGN VAR LIST FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER IF BIN_OP BIN_OP VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR BIN_OP VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR BIN_OP VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER
Vasya has a string $s$ of length $n$. He decides to make the following modification to the string: Pick an integer $k$, ($1 \leq k \leq n$). For $i$ from $1$ to $n-k+1$, reverse the substring $s[i:i+k-1]$ of $s$. For example, if string $s$ is qwer and $k = 2$, below is the series of transformations the string goes through: qwer (original string) wqer (after reversing the first substring of length $2$) weqr (after reversing the second substring of length $2$) werq (after reversing the last substring of length $2$) Hence, the resulting string after modifying $s$ with $k = 2$ is werq. Vasya wants to choose a $k$ such that the string obtained after the above-mentioned modification is lexicographically smallest possible among all choices of $k$. Among all such $k$, he wants to choose the smallest one. Since he is busy attending Felicity 2020, he asks for your help. A string $a$ is lexicographically smaller than a string $b$ if and only if one of the following holds: $a$ is a prefix of $b$, but $a \ne b$; in the first position where $a$ and $b$ differ, the string $a$ has a letter that appears earlier in the alphabet than the corresponding letter in $b$. -----Input----- Each test contains multiple test cases. The first line contains the number of test cases $t$ ($1 \le t \le 5000$). The description of the test cases follows. The first line of each test case contains a single integer $n$ ($1 \le n \le 5000$)Β β€” the length of the string $s$. The second line of each test case contains the string $s$ of $n$ lowercase latin letters. It is guaranteed that the sum of $n$ over all test cases does not exceed $5000$. -----Output----- For each testcase output two lines: In the first line output the lexicographically smallest string $s'$ achievable after the above-mentioned modification. In the second line output the appropriate value of $k$ ($1 \leq k \leq n$) that you chose for performing the modification. If there are multiple values of $k$ that give the lexicographically smallest string, output the smallest value of $k$ among them. -----Example----- Input 6 4 abab 6 qwerty 5 aaaaa 6 alaska 9 lfpbavjsm 1 p Output abab 1 ertyqw 3 aaaaa 1 aksala 6 avjsmbpfl 5 p 1 -----Note----- In the first testcase of the first sample, the string modification results for the sample abab are as follows : for $k = 1$ : abab for $k = 2$ : baba for $k = 3$ : abab for $k = 4$ : baba The lexicographically smallest string achievable through modification is abab for $k = 1$ and $3$. Smallest value of $k$ needed to achieve is hence $1$.
def do(s, k): l = len(s) t = s k = k - 1 if l % 2 == 0: if k % 2 == 0: return t[k:] + t[:k] else: return t[k:] + t[-l + k - 1 : -l - 1 : -1] elif k % 2 == 1: return t[k:] + t[:k] else: return t[k:] + t[-l + k - 1 : -l - 1 : -1] t = int(input()) for _ in range(t): n = int(input()) s = input() l = list(s) res = [] for i in range(1, n + 1): res.append((do(s, i), i)) res.sort() print(res[0][0]) print(res[0][1])
FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR NUMBER IF BIN_OP VAR NUMBER NUMBER IF BIN_OP VAR NUMBER NUMBER RETURN BIN_OP VAR VAR VAR VAR RETURN BIN_OP VAR VAR VAR BIN_OP BIN_OP VAR VAR NUMBER BIN_OP VAR NUMBER NUMBER IF BIN_OP VAR NUMBER NUMBER RETURN BIN_OP VAR VAR VAR VAR RETURN BIN_OP VAR VAR VAR BIN_OP BIN_OP VAR VAR NUMBER BIN_OP VAR NUMBER NUMBER 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 ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR NUMBER NUMBER
Vasya has a string $s$ of length $n$. He decides to make the following modification to the string: Pick an integer $k$, ($1 \leq k \leq n$). For $i$ from $1$ to $n-k+1$, reverse the substring $s[i:i+k-1]$ of $s$. For example, if string $s$ is qwer and $k = 2$, below is the series of transformations the string goes through: qwer (original string) wqer (after reversing the first substring of length $2$) weqr (after reversing the second substring of length $2$) werq (after reversing the last substring of length $2$) Hence, the resulting string after modifying $s$ with $k = 2$ is werq. Vasya wants to choose a $k$ such that the string obtained after the above-mentioned modification is lexicographically smallest possible among all choices of $k$. Among all such $k$, he wants to choose the smallest one. Since he is busy attending Felicity 2020, he asks for your help. A string $a$ is lexicographically smaller than a string $b$ if and only if one of the following holds: $a$ is a prefix of $b$, but $a \ne b$; in the first position where $a$ and $b$ differ, the string $a$ has a letter that appears earlier in the alphabet than the corresponding letter in $b$. -----Input----- Each test contains multiple test cases. The first line contains the number of test cases $t$ ($1 \le t \le 5000$). The description of the test cases follows. The first line of each test case contains a single integer $n$ ($1 \le n \le 5000$)Β β€” the length of the string $s$. The second line of each test case contains the string $s$ of $n$ lowercase latin letters. It is guaranteed that the sum of $n$ over all test cases does not exceed $5000$. -----Output----- For each testcase output two lines: In the first line output the lexicographically smallest string $s'$ achievable after the above-mentioned modification. In the second line output the appropriate value of $k$ ($1 \leq k \leq n$) that you chose for performing the modification. If there are multiple values of $k$ that give the lexicographically smallest string, output the smallest value of $k$ among them. -----Example----- Input 6 4 abab 6 qwerty 5 aaaaa 6 alaska 9 lfpbavjsm 1 p Output abab 1 ertyqw 3 aaaaa 1 aksala 6 avjsmbpfl 5 p 1 -----Note----- In the first testcase of the first sample, the string modification results for the sample abab are as follows : for $k = 1$ : abab for $k = 2$ : baba for $k = 3$ : abab for $k = 4$ : baba The lexicographically smallest string achievable through modification is abab for $k = 1$ and $3$. Smallest value of $k$ needed to achieve is hence $1$.
test = int(input()) for i in range(test): strLen = int(input()) s = input() best = s k = 0 temp = "" for i in range(1, strLen + 1): p = s[i:strLen] ss = "" if (i + strLen) % 2: ss = s[0:i][::-1] else: ss = s[0:i] temp = p + ss if temp < best: best = temp k = i print(best) print(k + 1)
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 ASSIGN VAR VAR ASSIGN VAR NUMBER ASSIGN VAR STRING FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR VAR VAR ASSIGN VAR STRING IF BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR VAR NUMBER VAR NUMBER ASSIGN VAR VAR NUMBER VAR ASSIGN VAR BIN_OP VAR VAR IF VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER
Vasya has a string $s$ of length $n$. He decides to make the following modification to the string: Pick an integer $k$, ($1 \leq k \leq n$). For $i$ from $1$ to $n-k+1$, reverse the substring $s[i:i+k-1]$ of $s$. For example, if string $s$ is qwer and $k = 2$, below is the series of transformations the string goes through: qwer (original string) wqer (after reversing the first substring of length $2$) weqr (after reversing the second substring of length $2$) werq (after reversing the last substring of length $2$) Hence, the resulting string after modifying $s$ with $k = 2$ is werq. Vasya wants to choose a $k$ such that the string obtained after the above-mentioned modification is lexicographically smallest possible among all choices of $k$. Among all such $k$, he wants to choose the smallest one. Since he is busy attending Felicity 2020, he asks for your help. A string $a$ is lexicographically smaller than a string $b$ if and only if one of the following holds: $a$ is a prefix of $b$, but $a \ne b$; in the first position where $a$ and $b$ differ, the string $a$ has a letter that appears earlier in the alphabet than the corresponding letter in $b$. -----Input----- Each test contains multiple test cases. The first line contains the number of test cases $t$ ($1 \le t \le 5000$). The description of the test cases follows. The first line of each test case contains a single integer $n$ ($1 \le n \le 5000$)Β β€” the length of the string $s$. The second line of each test case contains the string $s$ of $n$ lowercase latin letters. It is guaranteed that the sum of $n$ over all test cases does not exceed $5000$. -----Output----- For each testcase output two lines: In the first line output the lexicographically smallest string $s'$ achievable after the above-mentioned modification. In the second line output the appropriate value of $k$ ($1 \leq k \leq n$) that you chose for performing the modification. If there are multiple values of $k$ that give the lexicographically smallest string, output the smallest value of $k$ among them. -----Example----- Input 6 4 abab 6 qwerty 5 aaaaa 6 alaska 9 lfpbavjsm 1 p Output abab 1 ertyqw 3 aaaaa 1 aksala 6 avjsmbpfl 5 p 1 -----Note----- In the first testcase of the first sample, the string modification results for the sample abab are as follows : for $k = 1$ : abab for $k = 2$ : baba for $k = 3$ : abab for $k = 4$ : baba The lexicographically smallest string achievable through modification is abab for $k = 1$ and $3$. Smallest value of $k$ needed to achieve is hence $1$.
import sys input = sys.stdin.readline t = int(input()) while t: t -= 1 n = int(input()) a = input().rstrip() p = 200 k = 1 res = a ma = a[:] for i in range(n): if (n - i - 2) % 2 == 0: s = a[i + 1 :] + a[: i + 1][::-1] else: s = a[i + 1 :] + a[: i + 1] if s < ma: res = s ma = s[:] k = i + 2 print(res) print(k)
IMPORT ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR WHILE VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR ASSIGN VAR VAR FOR VAR FUNC_CALL VAR VAR IF BIN_OP BIN_OP BIN_OP VAR VAR NUMBER NUMBER NUMBER ASSIGN VAR BIN_OP VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER IF VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR
Vasya has a string $s$ of length $n$. He decides to make the following modification to the string: Pick an integer $k$, ($1 \leq k \leq n$). For $i$ from $1$ to $n-k+1$, reverse the substring $s[i:i+k-1]$ of $s$. For example, if string $s$ is qwer and $k = 2$, below is the series of transformations the string goes through: qwer (original string) wqer (after reversing the first substring of length $2$) weqr (after reversing the second substring of length $2$) werq (after reversing the last substring of length $2$) Hence, the resulting string after modifying $s$ with $k = 2$ is werq. Vasya wants to choose a $k$ such that the string obtained after the above-mentioned modification is lexicographically smallest possible among all choices of $k$. Among all such $k$, he wants to choose the smallest one. Since he is busy attending Felicity 2020, he asks for your help. A string $a$ is lexicographically smaller than a string $b$ if and only if one of the following holds: $a$ is a prefix of $b$, but $a \ne b$; in the first position where $a$ and $b$ differ, the string $a$ has a letter that appears earlier in the alphabet than the corresponding letter in $b$. -----Input----- Each test contains multiple test cases. The first line contains the number of test cases $t$ ($1 \le t \le 5000$). The description of the test cases follows. The first line of each test case contains a single integer $n$ ($1 \le n \le 5000$)Β β€” the length of the string $s$. The second line of each test case contains the string $s$ of $n$ lowercase latin letters. It is guaranteed that the sum of $n$ over all test cases does not exceed $5000$. -----Output----- For each testcase output two lines: In the first line output the lexicographically smallest string $s'$ achievable after the above-mentioned modification. In the second line output the appropriate value of $k$ ($1 \leq k \leq n$) that you chose for performing the modification. If there are multiple values of $k$ that give the lexicographically smallest string, output the smallest value of $k$ among them. -----Example----- Input 6 4 abab 6 qwerty 5 aaaaa 6 alaska 9 lfpbavjsm 1 p Output abab 1 ertyqw 3 aaaaa 1 aksala 6 avjsmbpfl 5 p 1 -----Note----- In the first testcase of the first sample, the string modification results for the sample abab are as follows : for $k = 1$ : abab for $k = 2$ : baba for $k = 3$ : abab for $k = 4$ : baba The lexicographically smallest string achievable through modification is abab for $k = 1$ and $3$. Smallest value of $k$ needed to achieve is hence $1$.
import sys reader = (s.rstrip() for s in sys.stdin) input = reader.__next__ def lexi(a, b): lenof = len(a) for i in range(lenof): if a[i] == b[i]: continue elif a[i] < b[i]: return True else: return False return False def gift(): for _ in range(t): n = int(input()) string = input() loweststring = string[0] lowestpos = [0] for i in range(1, n): if ord(string[i]) < ord(loweststring): loweststring = string[i] lowestpos = [i] elif string[i] == loweststring: lowestpos.append(i) correstring = [] nonrepeat = [] for i in lowestpos: if i % 2 == (n - 1) % 2: tempstring = string[i:] + string[:i][::-1] else: tempstring = string[i:] + string[:i] if tempstring not in nonrepeat: nonrepeat.append(tempstring) correstring.append([tempstring, i + 1]) ans, ansinde = correstring[0] lenans = len(correstring) for i in range(lenans - 1): if lexi(correstring[i + 1][0], ans): ans, ansinde = correstring[i + 1] yield ans yield ansinde t = int(input()) ans = gift() print(*ans, sep="\n")
IMPORT ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR IF VAR VAR VAR VAR RETURN NUMBER RETURN NUMBER RETURN NUMBER FUNC_DEF FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR VAR NUMBER ASSIGN VAR LIST NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR IF FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR LIST VAR IF VAR VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR LIST ASSIGN VAR LIST FOR VAR VAR IF BIN_OP VAR NUMBER BIN_OP BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR VAR VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR VAR VAR IF VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR LIST VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER IF FUNC_CALL VAR VAR BIN_OP VAR NUMBER NUMBER VAR ASSIGN VAR VAR VAR BIN_OP VAR NUMBER EXPR VAR EXPR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR STRING
Vasya has a string $s$ of length $n$. He decides to make the following modification to the string: Pick an integer $k$, ($1 \leq k \leq n$). For $i$ from $1$ to $n-k+1$, reverse the substring $s[i:i+k-1]$ of $s$. For example, if string $s$ is qwer and $k = 2$, below is the series of transformations the string goes through: qwer (original string) wqer (after reversing the first substring of length $2$) weqr (after reversing the second substring of length $2$) werq (after reversing the last substring of length $2$) Hence, the resulting string after modifying $s$ with $k = 2$ is werq. Vasya wants to choose a $k$ such that the string obtained after the above-mentioned modification is lexicographically smallest possible among all choices of $k$. Among all such $k$, he wants to choose the smallest one. Since he is busy attending Felicity 2020, he asks for your help. A string $a$ is lexicographically smaller than a string $b$ if and only if one of the following holds: $a$ is a prefix of $b$, but $a \ne b$; in the first position where $a$ and $b$ differ, the string $a$ has a letter that appears earlier in the alphabet than the corresponding letter in $b$. -----Input----- Each test contains multiple test cases. The first line contains the number of test cases $t$ ($1 \le t \le 5000$). The description of the test cases follows. The first line of each test case contains a single integer $n$ ($1 \le n \le 5000$)Β β€” the length of the string $s$. The second line of each test case contains the string $s$ of $n$ lowercase latin letters. It is guaranteed that the sum of $n$ over all test cases does not exceed $5000$. -----Output----- For each testcase output two lines: In the first line output the lexicographically smallest string $s'$ achievable after the above-mentioned modification. In the second line output the appropriate value of $k$ ($1 \leq k \leq n$) that you chose for performing the modification. If there are multiple values of $k$ that give the lexicographically smallest string, output the smallest value of $k$ among them. -----Example----- Input 6 4 abab 6 qwerty 5 aaaaa 6 alaska 9 lfpbavjsm 1 p Output abab 1 ertyqw 3 aaaaa 1 aksala 6 avjsmbpfl 5 p 1 -----Note----- In the first testcase of the first sample, the string modification results for the sample abab are as follows : for $k = 1$ : abab for $k = 2$ : baba for $k = 3$ : abab for $k = 4$ : baba The lexicographically smallest string achievable through modification is abab for $k = 1$ and $3$. Smallest value of $k$ needed to achieve is hence $1$.
t = int(input()) for _ in range(t): n = int(input()) S = list(ord(s) - 97 for s in input()) C = S ans = 1 for i in range(2, n + 1): N = [0] * n for j in range(i - 1, n): N[j - (i - 1)] = S[j] for j in range(i - 1): if (n - i + 1) % 2 == 0: N[n - i + 1 + j] = S[j] else: N[n - 1 - j] = S[j] Chk = False for j in range(n): if C[j] == N[j]: continue elif C[j] > N[j]: Chk = True break else: break if Chk: ans = i C = N Ans = "" for c in C: Ans += chr(97 + c) print(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 BIN_OP FUNC_CALL VAR VAR NUMBER VAR FUNC_CALL VAR ASSIGN VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR ASSIGN VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER IF BIN_OP BIN_OP BIN_OP VAR VAR NUMBER NUMBER NUMBER ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR VAR NUMBER VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR NUMBER VAR VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR IF VAR VAR VAR VAR ASSIGN VAR NUMBER IF VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR STRING FOR VAR VAR VAR FUNC_CALL VAR BIN_OP NUMBER VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR
Vasya has a string $s$ of length $n$. He decides to make the following modification to the string: Pick an integer $k$, ($1 \leq k \leq n$). For $i$ from $1$ to $n-k+1$, reverse the substring $s[i:i+k-1]$ of $s$. For example, if string $s$ is qwer and $k = 2$, below is the series of transformations the string goes through: qwer (original string) wqer (after reversing the first substring of length $2$) weqr (after reversing the second substring of length $2$) werq (after reversing the last substring of length $2$) Hence, the resulting string after modifying $s$ with $k = 2$ is werq. Vasya wants to choose a $k$ such that the string obtained after the above-mentioned modification is lexicographically smallest possible among all choices of $k$. Among all such $k$, he wants to choose the smallest one. Since he is busy attending Felicity 2020, he asks for your help. A string $a$ is lexicographically smaller than a string $b$ if and only if one of the following holds: $a$ is a prefix of $b$, but $a \ne b$; in the first position where $a$ and $b$ differ, the string $a$ has a letter that appears earlier in the alphabet than the corresponding letter in $b$. -----Input----- Each test contains multiple test cases. The first line contains the number of test cases $t$ ($1 \le t \le 5000$). The description of the test cases follows. The first line of each test case contains a single integer $n$ ($1 \le n \le 5000$)Β β€” the length of the string $s$. The second line of each test case contains the string $s$ of $n$ lowercase latin letters. It is guaranteed that the sum of $n$ over all test cases does not exceed $5000$. -----Output----- For each testcase output two lines: In the first line output the lexicographically smallest string $s'$ achievable after the above-mentioned modification. In the second line output the appropriate value of $k$ ($1 \leq k \leq n$) that you chose for performing the modification. If there are multiple values of $k$ that give the lexicographically smallest string, output the smallest value of $k$ among them. -----Example----- Input 6 4 abab 6 qwerty 5 aaaaa 6 alaska 9 lfpbavjsm 1 p Output abab 1 ertyqw 3 aaaaa 1 aksala 6 avjsmbpfl 5 p 1 -----Note----- In the first testcase of the first sample, the string modification results for the sample abab are as follows : for $k = 1$ : abab for $k = 2$ : baba for $k = 3$ : abab for $k = 4$ : baba The lexicographically smallest string achievable through modification is abab for $k = 1$ and $3$. Smallest value of $k$ needed to achieve is hence $1$.
import sys input = sys.stdin.readline def main(): for _ in range(int(input())): n = int(input()) s = input().strip() ret = s ret_idx = 0 for k in range(1, n): if (n - k) % 2 == 0: tmp = s[k:] + s[:k] else: tmp = s[k:] + "".join(reversed(s[:k])) if ret > tmp: ret = tmp ret_idx = k print(ret) print(ret_idx + 1) main()
IMPORT ASSIGN VAR VAR FUNC_DEF FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR IF BIN_OP BIN_OP VAR VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR FUNC_CALL STRING FUNC_CALL VAR VAR VAR IF VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR
Vasya has a string $s$ of length $n$. He decides to make the following modification to the string: Pick an integer $k$, ($1 \leq k \leq n$). For $i$ from $1$ to $n-k+1$, reverse the substring $s[i:i+k-1]$ of $s$. For example, if string $s$ is qwer and $k = 2$, below is the series of transformations the string goes through: qwer (original string) wqer (after reversing the first substring of length $2$) weqr (after reversing the second substring of length $2$) werq (after reversing the last substring of length $2$) Hence, the resulting string after modifying $s$ with $k = 2$ is werq. Vasya wants to choose a $k$ such that the string obtained after the above-mentioned modification is lexicographically smallest possible among all choices of $k$. Among all such $k$, he wants to choose the smallest one. Since he is busy attending Felicity 2020, he asks for your help. A string $a$ is lexicographically smaller than a string $b$ if and only if one of the following holds: $a$ is a prefix of $b$, but $a \ne b$; in the first position where $a$ and $b$ differ, the string $a$ has a letter that appears earlier in the alphabet than the corresponding letter in $b$. -----Input----- Each test contains multiple test cases. The first line contains the number of test cases $t$ ($1 \le t \le 5000$). The description of the test cases follows. The first line of each test case contains a single integer $n$ ($1 \le n \le 5000$)Β β€” the length of the string $s$. The second line of each test case contains the string $s$ of $n$ lowercase latin letters. It is guaranteed that the sum of $n$ over all test cases does not exceed $5000$. -----Output----- For each testcase output two lines: In the first line output the lexicographically smallest string $s'$ achievable after the above-mentioned modification. In the second line output the appropriate value of $k$ ($1 \leq k \leq n$) that you chose for performing the modification. If there are multiple values of $k$ that give the lexicographically smallest string, output the smallest value of $k$ among them. -----Example----- Input 6 4 abab 6 qwerty 5 aaaaa 6 alaska 9 lfpbavjsm 1 p Output abab 1 ertyqw 3 aaaaa 1 aksala 6 avjsmbpfl 5 p 1 -----Note----- In the first testcase of the first sample, the string modification results for the sample abab are as follows : for $k = 1$ : abab for $k = 2$ : baba for $k = 3$ : abab for $k = 4$ : baba The lexicographically smallest string achievable through modification is abab for $k = 1$ and $3$. Smallest value of $k$ needed to achieve is hence $1$.
e = int(input()) p = [] f = [] for ee in range(e): n = int(input()) s = str(input()) sr = s words = [] er = [] er.append(s) words.append(s) for k in range(2, n + 1): j = "" j = s[k - 1 :] if (len(s) - k + 1) % 2 == 0: j = j + s[: k - 1] else: g = s[: k - 1] g = g[::-1] j = j + g words.append(j) er.append(j) words.sort() for i in range(len(er)): if words[0] == er[i]: f.append(i + 1) break p.append(words[0]) for i in range(len(p)): print(p[i]) print(f[i])
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR ASSIGN VAR LIST ASSIGN VAR LIST EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR STRING ASSIGN VAR VAR BIN_OP VAR NUMBER IF BIN_OP BIN_OP BIN_OP FUNC_CALL VAR VAR VAR NUMBER NUMBER NUMBER ASSIGN VAR BIN_OP VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR NUMBER VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR
Vasya has a string $s$ of length $n$. He decides to make the following modification to the string: Pick an integer $k$, ($1 \leq k \leq n$). For $i$ from $1$ to $n-k+1$, reverse the substring $s[i:i+k-1]$ of $s$. For example, if string $s$ is qwer and $k = 2$, below is the series of transformations the string goes through: qwer (original string) wqer (after reversing the first substring of length $2$) weqr (after reversing the second substring of length $2$) werq (after reversing the last substring of length $2$) Hence, the resulting string after modifying $s$ with $k = 2$ is werq. Vasya wants to choose a $k$ such that the string obtained after the above-mentioned modification is lexicographically smallest possible among all choices of $k$. Among all such $k$, he wants to choose the smallest one. Since he is busy attending Felicity 2020, he asks for your help. A string $a$ is lexicographically smaller than a string $b$ if and only if one of the following holds: $a$ is a prefix of $b$, but $a \ne b$; in the first position where $a$ and $b$ differ, the string $a$ has a letter that appears earlier in the alphabet than the corresponding letter in $b$. -----Input----- Each test contains multiple test cases. The first line contains the number of test cases $t$ ($1 \le t \le 5000$). The description of the test cases follows. The first line of each test case contains a single integer $n$ ($1 \le n \le 5000$)Β β€” the length of the string $s$. The second line of each test case contains the string $s$ of $n$ lowercase latin letters. It is guaranteed that the sum of $n$ over all test cases does not exceed $5000$. -----Output----- For each testcase output two lines: In the first line output the lexicographically smallest string $s'$ achievable after the above-mentioned modification. In the second line output the appropriate value of $k$ ($1 \leq k \leq n$) that you chose for performing the modification. If there are multiple values of $k$ that give the lexicographically smallest string, output the smallest value of $k$ among them. -----Example----- Input 6 4 abab 6 qwerty 5 aaaaa 6 alaska 9 lfpbavjsm 1 p Output abab 1 ertyqw 3 aaaaa 1 aksala 6 avjsmbpfl 5 p 1 -----Note----- In the first testcase of the first sample, the string modification results for the sample abab are as follows : for $k = 1$ : abab for $k = 2$ : baba for $k = 3$ : abab for $k = 4$ : baba The lexicographically smallest string achievable through modification is abab for $k = 1$ and $3$. Smallest value of $k$ needed to achieve is hence $1$.
t = int(input()) for i in range(t): n = int(input()) s = input() a = dict() a[s] = 1 for j in range(1, n + 1): if j == 1: continue u = n - j + 1 z = s[0 : j - 1] if u % 2 == 1: z = z[::-1] s1 = s[j - 1 :] + z if s1 in a: continue a[s1] = j l = list(a.keys()) l.sort() print(l[0]) print(a[l[0]])
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER IF VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR VAR NUMBER BIN_OP VAR NUMBER IF BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR BIN_OP VAR BIN_OP VAR NUMBER VAR IF VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR NUMBER
Vasya has a string $s$ of length $n$. He decides to make the following modification to the string: Pick an integer $k$, ($1 \leq k \leq n$). For $i$ from $1$ to $n-k+1$, reverse the substring $s[i:i+k-1]$ of $s$. For example, if string $s$ is qwer and $k = 2$, below is the series of transformations the string goes through: qwer (original string) wqer (after reversing the first substring of length $2$) weqr (after reversing the second substring of length $2$) werq (after reversing the last substring of length $2$) Hence, the resulting string after modifying $s$ with $k = 2$ is werq. Vasya wants to choose a $k$ such that the string obtained after the above-mentioned modification is lexicographically smallest possible among all choices of $k$. Among all such $k$, he wants to choose the smallest one. Since he is busy attending Felicity 2020, he asks for your help. A string $a$ is lexicographically smaller than a string $b$ if and only if one of the following holds: $a$ is a prefix of $b$, but $a \ne b$; in the first position where $a$ and $b$ differ, the string $a$ has a letter that appears earlier in the alphabet than the corresponding letter in $b$. -----Input----- Each test contains multiple test cases. The first line contains the number of test cases $t$ ($1 \le t \le 5000$). The description of the test cases follows. The first line of each test case contains a single integer $n$ ($1 \le n \le 5000$)Β β€” the length of the string $s$. The second line of each test case contains the string $s$ of $n$ lowercase latin letters. It is guaranteed that the sum of $n$ over all test cases does not exceed $5000$. -----Output----- For each testcase output two lines: In the first line output the lexicographically smallest string $s'$ achievable after the above-mentioned modification. In the second line output the appropriate value of $k$ ($1 \leq k \leq n$) that you chose for performing the modification. If there are multiple values of $k$ that give the lexicographically smallest string, output the smallest value of $k$ among them. -----Example----- Input 6 4 abab 6 qwerty 5 aaaaa 6 alaska 9 lfpbavjsm 1 p Output abab 1 ertyqw 3 aaaaa 1 aksala 6 avjsmbpfl 5 p 1 -----Note----- In the first testcase of the first sample, the string modification results for the sample abab are as follows : for $k = 1$ : abab for $k = 2$ : baba for $k = 3$ : abab for $k = 4$ : baba The lexicographically smallest string achievable through modification is abab for $k = 1$ and $3$. Smallest value of $k$ needed to achieve is hence $1$.
def sim(s, t): if len(s) == t: return s[::-1] if (len(s) - t) % 2 == 0: return s[t:] + s[:t] else: return s[t:] + s[:t][::-1] def f(s): n = len(s) t = min([(sim(s, i), i) for i in range(0, n + 1, 1)], key=lambda x: x[0]) return t[0], t[1] + 1 for i in range(int(input())): n = int(input()) s = input() print(*f(s), sep="\n")
FUNC_DEF IF FUNC_CALL VAR VAR VAR RETURN VAR NUMBER IF BIN_OP BIN_OP FUNC_CALL VAR VAR VAR NUMBER NUMBER RETURN BIN_OP VAR VAR VAR VAR RETURN BIN_OP VAR VAR VAR VAR NUMBER FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER NUMBER VAR NUMBER RETURN VAR NUMBER BIN_OP VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR STRING
Vasya has a string $s$ of length $n$. He decides to make the following modification to the string: Pick an integer $k$, ($1 \leq k \leq n$). For $i$ from $1$ to $n-k+1$, reverse the substring $s[i:i+k-1]$ of $s$. For example, if string $s$ is qwer and $k = 2$, below is the series of transformations the string goes through: qwer (original string) wqer (after reversing the first substring of length $2$) weqr (after reversing the second substring of length $2$) werq (after reversing the last substring of length $2$) Hence, the resulting string after modifying $s$ with $k = 2$ is werq. Vasya wants to choose a $k$ such that the string obtained after the above-mentioned modification is lexicographically smallest possible among all choices of $k$. Among all such $k$, he wants to choose the smallest one. Since he is busy attending Felicity 2020, he asks for your help. A string $a$ is lexicographically smaller than a string $b$ if and only if one of the following holds: $a$ is a prefix of $b$, but $a \ne b$; in the first position where $a$ and $b$ differ, the string $a$ has a letter that appears earlier in the alphabet than the corresponding letter in $b$. -----Input----- Each test contains multiple test cases. The first line contains the number of test cases $t$ ($1 \le t \le 5000$). The description of the test cases follows. The first line of each test case contains a single integer $n$ ($1 \le n \le 5000$)Β β€” the length of the string $s$. The second line of each test case contains the string $s$ of $n$ lowercase latin letters. It is guaranteed that the sum of $n$ over all test cases does not exceed $5000$. -----Output----- For each testcase output two lines: In the first line output the lexicographically smallest string $s'$ achievable after the above-mentioned modification. In the second line output the appropriate value of $k$ ($1 \leq k \leq n$) that you chose for performing the modification. If there are multiple values of $k$ that give the lexicographically smallest string, output the smallest value of $k$ among them. -----Example----- Input 6 4 abab 6 qwerty 5 aaaaa 6 alaska 9 lfpbavjsm 1 p Output abab 1 ertyqw 3 aaaaa 1 aksala 6 avjsmbpfl 5 p 1 -----Note----- In the first testcase of the first sample, the string modification results for the sample abab are as follows : for $k = 1$ : abab for $k = 2$ : baba for $k = 3$ : abab for $k = 4$ : baba The lexicographically smallest string achievable through modification is abab for $k = 1$ and $3$. Smallest value of $k$ needed to achieve is hence $1$.
t = int(input()) while t > 0: dic = {} t -= 1 n = int(input()) STR = input() arr = [0] * n for i in range(n): arr[i] = ord(STR[i]) m_val = 10000000 for i in range(n): if arr[i] < m_val: m_val = arr[i] arr2 = [] for i in range(n): if arr[i] == m_val: arr2.append(i) for i in arr2: s1 = "" if i == 0: s1 = STR elif i == n - 1: s1 = STR[::-1] elif (n - i) % 2 == 1: s1 = STR[i:] + STR[:i][::-1] else: s1 = STR[i:] + STR[:i] if s1 in dic: dic[s1] = min(dic[s1], i + 1) else: dic[s1] = i + 1 temp = dic.keys() s3 = min(temp) print(s3) print(dic[s3])
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR WHILE VAR NUMBER ASSIGN VAR DICT VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR EXPR FUNC_CALL VAR VAR FOR VAR VAR ASSIGN VAR STRING IF VAR NUMBER ASSIGN VAR VAR IF VAR BIN_OP VAR NUMBER ASSIGN VAR VAR NUMBER IF BIN_OP BIN_OP VAR VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR VAR VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR VAR VAR IF VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR
Vasya has a string $s$ of length $n$. He decides to make the following modification to the string: Pick an integer $k$, ($1 \leq k \leq n$). For $i$ from $1$ to $n-k+1$, reverse the substring $s[i:i+k-1]$ of $s$. For example, if string $s$ is qwer and $k = 2$, below is the series of transformations the string goes through: qwer (original string) wqer (after reversing the first substring of length $2$) weqr (after reversing the second substring of length $2$) werq (after reversing the last substring of length $2$) Hence, the resulting string after modifying $s$ with $k = 2$ is werq. Vasya wants to choose a $k$ such that the string obtained after the above-mentioned modification is lexicographically smallest possible among all choices of $k$. Among all such $k$, he wants to choose the smallest one. Since he is busy attending Felicity 2020, he asks for your help. A string $a$ is lexicographically smaller than a string $b$ if and only if one of the following holds: $a$ is a prefix of $b$, but $a \ne b$; in the first position where $a$ and $b$ differ, the string $a$ has a letter that appears earlier in the alphabet than the corresponding letter in $b$. -----Input----- Each test contains multiple test cases. The first line contains the number of test cases $t$ ($1 \le t \le 5000$). The description of the test cases follows. The first line of each test case contains a single integer $n$ ($1 \le n \le 5000$)Β β€” the length of the string $s$. The second line of each test case contains the string $s$ of $n$ lowercase latin letters. It is guaranteed that the sum of $n$ over all test cases does not exceed $5000$. -----Output----- For each testcase output two lines: In the first line output the lexicographically smallest string $s'$ achievable after the above-mentioned modification. In the second line output the appropriate value of $k$ ($1 \leq k \leq n$) that you chose for performing the modification. If there are multiple values of $k$ that give the lexicographically smallest string, output the smallest value of $k$ among them. -----Example----- Input 6 4 abab 6 qwerty 5 aaaaa 6 alaska 9 lfpbavjsm 1 p Output abab 1 ertyqw 3 aaaaa 1 aksala 6 avjsmbpfl 5 p 1 -----Note----- In the first testcase of the first sample, the string modification results for the sample abab are as follows : for $k = 1$ : abab for $k = 2$ : baba for $k = 3$ : abab for $k = 4$ : baba The lexicographically smallest string achievable through modification is abab for $k = 1$ and $3$. Smallest value of $k$ needed to achieve is hence $1$.
def krev(s, k): l = list(s) for i in range(len(s) - k + 1): l[i : i + k] = l[i : i + k][::-1] return "".join(l) def rrev(s, k): left = s[k - 1 :] right = s[: k - 1] if len(s) % 2 == k % 2: right = right[::-1] return left + right def solve(n, s): minc = min(s) I = [i for i, c in enumerate(s) if c == minc] return min((rrev(s, i + 1), i + 1) for i in I) t = int(input()) for _ in range(t): n = int(input()) s = input() w, k = solve(n, s) print(w) print(k)
FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP BIN_OP FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR VAR BIN_OP VAR VAR VAR VAR BIN_OP VAR VAR NUMBER RETURN FUNC_CALL STRING VAR FUNC_DEF ASSIGN VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER IF BIN_OP FUNC_CALL VAR VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR NUMBER RETURN BIN_OP VAR VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR FUNC_CALL VAR VAR VAR VAR RETURN FUNC_CALL VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR
Vasya has a string $s$ of length $n$. He decides to make the following modification to the string: Pick an integer $k$, ($1 \leq k \leq n$). For $i$ from $1$ to $n-k+1$, reverse the substring $s[i:i+k-1]$ of $s$. For example, if string $s$ is qwer and $k = 2$, below is the series of transformations the string goes through: qwer (original string) wqer (after reversing the first substring of length $2$) weqr (after reversing the second substring of length $2$) werq (after reversing the last substring of length $2$) Hence, the resulting string after modifying $s$ with $k = 2$ is werq. Vasya wants to choose a $k$ such that the string obtained after the above-mentioned modification is lexicographically smallest possible among all choices of $k$. Among all such $k$, he wants to choose the smallest one. Since he is busy attending Felicity 2020, he asks for your help. A string $a$ is lexicographically smaller than a string $b$ if and only if one of the following holds: $a$ is a prefix of $b$, but $a \ne b$; in the first position where $a$ and $b$ differ, the string $a$ has a letter that appears earlier in the alphabet than the corresponding letter in $b$. -----Input----- Each test contains multiple test cases. The first line contains the number of test cases $t$ ($1 \le t \le 5000$). The description of the test cases follows. The first line of each test case contains a single integer $n$ ($1 \le n \le 5000$)Β β€” the length of the string $s$. The second line of each test case contains the string $s$ of $n$ lowercase latin letters. It is guaranteed that the sum of $n$ over all test cases does not exceed $5000$. -----Output----- For each testcase output two lines: In the first line output the lexicographically smallest string $s'$ achievable after the above-mentioned modification. In the second line output the appropriate value of $k$ ($1 \leq k \leq n$) that you chose for performing the modification. If there are multiple values of $k$ that give the lexicographically smallest string, output the smallest value of $k$ among them. -----Example----- Input 6 4 abab 6 qwerty 5 aaaaa 6 alaska 9 lfpbavjsm 1 p Output abab 1 ertyqw 3 aaaaa 1 aksala 6 avjsmbpfl 5 p 1 -----Note----- In the first testcase of the first sample, the string modification results for the sample abab are as follows : for $k = 1$ : abab for $k = 2$ : baba for $k = 3$ : abab for $k = 4$ : baba The lexicographically smallest string achievable through modification is abab for $k = 1$ and $3$. Smallest value of $k$ needed to achieve is hence $1$.
t = int(input()) for k in range(t): n = int(input()) aString = input() aStringRev = aString[::-1] a = [] for i in range(n): if (n - i) % 2 == 0: a.append(aString[i:n] + aString[0:i]) else: a.append(aString[i:n] + aStringRev[n - i : n]) minStrID = 0 for i in range(n): if a[i] < a[minStrID]: minStrID = i print(a[minStrID] + "\n" + str(minStrID + 1))
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 ASSIGN VAR VAR NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR IF BIN_OP BIN_OP VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR NUMBER VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR BIN_OP VAR VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR STRING FUNC_CALL VAR BIN_OP VAR NUMBER
Vasya has a string $s$ of length $n$. He decides to make the following modification to the string: Pick an integer $k$, ($1 \leq k \leq n$). For $i$ from $1$ to $n-k+1$, reverse the substring $s[i:i+k-1]$ of $s$. For example, if string $s$ is qwer and $k = 2$, below is the series of transformations the string goes through: qwer (original string) wqer (after reversing the first substring of length $2$) weqr (after reversing the second substring of length $2$) werq (after reversing the last substring of length $2$) Hence, the resulting string after modifying $s$ with $k = 2$ is werq. Vasya wants to choose a $k$ such that the string obtained after the above-mentioned modification is lexicographically smallest possible among all choices of $k$. Among all such $k$, he wants to choose the smallest one. Since he is busy attending Felicity 2020, he asks for your help. A string $a$ is lexicographically smaller than a string $b$ if and only if one of the following holds: $a$ is a prefix of $b$, but $a \ne b$; in the first position where $a$ and $b$ differ, the string $a$ has a letter that appears earlier in the alphabet than the corresponding letter in $b$. -----Input----- Each test contains multiple test cases. The first line contains the number of test cases $t$ ($1 \le t \le 5000$). The description of the test cases follows. The first line of each test case contains a single integer $n$ ($1 \le n \le 5000$)Β β€” the length of the string $s$. The second line of each test case contains the string $s$ of $n$ lowercase latin letters. It is guaranteed that the sum of $n$ over all test cases does not exceed $5000$. -----Output----- For each testcase output two lines: In the first line output the lexicographically smallest string $s'$ achievable after the above-mentioned modification. In the second line output the appropriate value of $k$ ($1 \leq k \leq n$) that you chose for performing the modification. If there are multiple values of $k$ that give the lexicographically smallest string, output the smallest value of $k$ among them. -----Example----- Input 6 4 abab 6 qwerty 5 aaaaa 6 alaska 9 lfpbavjsm 1 p Output abab 1 ertyqw 3 aaaaa 1 aksala 6 avjsmbpfl 5 p 1 -----Note----- In the first testcase of the first sample, the string modification results for the sample abab are as follows : for $k = 1$ : abab for $k = 2$ : baba for $k = 3$ : abab for $k = 4$ : baba The lexicographically smallest string achievable through modification is abab for $k = 1$ and $3$. Smallest value of $k$ needed to achieve is hence $1$.
from sys import stdin, stdout rl = lambda: stdin.readline() rll = lambda: stdin.readline().split() def swap(s, i): num_moves = len(s) - i parity = (len(s) - i) % 2 return s[i:] + (s[:i] if parity == 0 else s[:i][::-1]) def main(): T = int(rl()) numchar = lambda c: ord(c) - 97 for _ in range(T): n = int(rl()) s = rll()[0] best, choices = min(numchar(c) for c in s), [] for i, c in enumerate(s): num = numchar(c) if num <= best: best = num choices.append((swap(s, i), i)) choices.sort() ans, shift = choices[0] stdout.write(str(ans)) stdout.write("\n") stdout.write(str(shift + 1)) stdout.write("\n") stdout.close() main()
ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR FUNC_DEF ASSIGN VAR BIN_OP FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP FUNC_CALL VAR VAR VAR NUMBER RETURN BIN_OP VAR VAR VAR NUMBER VAR VAR VAR VAR NUMBER FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR LIST FOR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR IF VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR VAR VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR
Vasya has a string $s$ of length $n$. He decides to make the following modification to the string: Pick an integer $k$, ($1 \leq k \leq n$). For $i$ from $1$ to $n-k+1$, reverse the substring $s[i:i+k-1]$ of $s$. For example, if string $s$ is qwer and $k = 2$, below is the series of transformations the string goes through: qwer (original string) wqer (after reversing the first substring of length $2$) weqr (after reversing the second substring of length $2$) werq (after reversing the last substring of length $2$) Hence, the resulting string after modifying $s$ with $k = 2$ is werq. Vasya wants to choose a $k$ such that the string obtained after the above-mentioned modification is lexicographically smallest possible among all choices of $k$. Among all such $k$, he wants to choose the smallest one. Since he is busy attending Felicity 2020, he asks for your help. A string $a$ is lexicographically smaller than a string $b$ if and only if one of the following holds: $a$ is a prefix of $b$, but $a \ne b$; in the first position where $a$ and $b$ differ, the string $a$ has a letter that appears earlier in the alphabet than the corresponding letter in $b$. -----Input----- Each test contains multiple test cases. The first line contains the number of test cases $t$ ($1 \le t \le 5000$). The description of the test cases follows. The first line of each test case contains a single integer $n$ ($1 \le n \le 5000$)Β β€” the length of the string $s$. The second line of each test case contains the string $s$ of $n$ lowercase latin letters. It is guaranteed that the sum of $n$ over all test cases does not exceed $5000$. -----Output----- For each testcase output two lines: In the first line output the lexicographically smallest string $s'$ achievable after the above-mentioned modification. In the second line output the appropriate value of $k$ ($1 \leq k \leq n$) that you chose for performing the modification. If there are multiple values of $k$ that give the lexicographically smallest string, output the smallest value of $k$ among them. -----Example----- Input 6 4 abab 6 qwerty 5 aaaaa 6 alaska 9 lfpbavjsm 1 p Output abab 1 ertyqw 3 aaaaa 1 aksala 6 avjsmbpfl 5 p 1 -----Note----- In the first testcase of the first sample, the string modification results for the sample abab are as follows : for $k = 1$ : abab for $k = 2$ : baba for $k = 3$ : abab for $k = 4$ : baba The lexicographically smallest string achievable through modification is abab for $k = 1$ and $3$. Smallest value of $k$ needed to achieve is hence $1$.
numTests = int(input()) minLexIx = 0 answers = [] for i in range(numTests): strLen = int(input()) word = input() results = [] for ix in range(0, strLen): res = word[ix:strLen] rhs = word[0:ix] if ix % 2 == 0 and strLen % 2 == 1 or ix % 2 == 1 and strLen % 2 == 0: rhs = rhs[::-1] res += rhs results.append(res) minIx = 0 minAns = results[0] for ix, ans in enumerate(results): if ans < minAns: minAns = ans minIx = ix answers.append((minAns, minIx)) for a in answers: print(a[0]) print(str(a[1] + 1))
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR VAR VAR ASSIGN VAR VAR NUMBER VAR IF BIN_OP VAR NUMBER NUMBER BIN_OP VAR NUMBER NUMBER BIN_OP VAR NUMBER NUMBER BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR NUMBER VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR VAR NUMBER FOR VAR VAR FUNC_CALL VAR VAR IF VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR VAR FOR VAR VAR EXPR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER
Vasya has a string $s$ of length $n$. He decides to make the following modification to the string: Pick an integer $k$, ($1 \leq k \leq n$). For $i$ from $1$ to $n-k+1$, reverse the substring $s[i:i+k-1]$ of $s$. For example, if string $s$ is qwer and $k = 2$, below is the series of transformations the string goes through: qwer (original string) wqer (after reversing the first substring of length $2$) weqr (after reversing the second substring of length $2$) werq (after reversing the last substring of length $2$) Hence, the resulting string after modifying $s$ with $k = 2$ is werq. Vasya wants to choose a $k$ such that the string obtained after the above-mentioned modification is lexicographically smallest possible among all choices of $k$. Among all such $k$, he wants to choose the smallest one. Since he is busy attending Felicity 2020, he asks for your help. A string $a$ is lexicographically smaller than a string $b$ if and only if one of the following holds: $a$ is a prefix of $b$, but $a \ne b$; in the first position where $a$ and $b$ differ, the string $a$ has a letter that appears earlier in the alphabet than the corresponding letter in $b$. -----Input----- Each test contains multiple test cases. The first line contains the number of test cases $t$ ($1 \le t \le 5000$). The description of the test cases follows. The first line of each test case contains a single integer $n$ ($1 \le n \le 5000$)Β β€” the length of the string $s$. The second line of each test case contains the string $s$ of $n$ lowercase latin letters. It is guaranteed that the sum of $n$ over all test cases does not exceed $5000$. -----Output----- For each testcase output two lines: In the first line output the lexicographically smallest string $s'$ achievable after the above-mentioned modification. In the second line output the appropriate value of $k$ ($1 \leq k \leq n$) that you chose for performing the modification. If there are multiple values of $k$ that give the lexicographically smallest string, output the smallest value of $k$ among them. -----Example----- Input 6 4 abab 6 qwerty 5 aaaaa 6 alaska 9 lfpbavjsm 1 p Output abab 1 ertyqw 3 aaaaa 1 aksala 6 avjsmbpfl 5 p 1 -----Note----- In the first testcase of the first sample, the string modification results for the sample abab are as follows : for $k = 1$ : abab for $k = 2$ : baba for $k = 3$ : abab for $k = 4$ : baba The lexicographically smallest string achievable through modification is abab for $k = 1$ and $3$. Smallest value of $k$ needed to achieve is hence $1$.
T = int(input()) for test in range(T): n = int(input()) lst = list(input()) a = [] a.append(("".join(lst), 1)) for i in range(1, n): string = "" string += "".join(lst[i:]) if i % 2 == 0 and n % 2 == 1 or i % 2 == 1 and n % 2 == 0: string += "".join(list(reversed(lst[:i]))) else: string += "".join(lst[:i]) a.append((string, i + 1)) a.sort(key=lambda x: x[0]) print(a[0][0]) print(a[0][1])
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST EXPR FUNC_CALL VAR FUNC_CALL STRING VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR STRING VAR FUNC_CALL STRING VAR VAR IF BIN_OP VAR NUMBER NUMBER BIN_OP VAR NUMBER NUMBER BIN_OP VAR NUMBER NUMBER BIN_OP VAR NUMBER NUMBER VAR FUNC_CALL STRING FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR FUNC_CALL STRING VAR VAR EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR NUMBER NUMBER
Vasya has a string $s$ of length $n$. He decides to make the following modification to the string: Pick an integer $k$, ($1 \leq k \leq n$). For $i$ from $1$ to $n-k+1$, reverse the substring $s[i:i+k-1]$ of $s$. For example, if string $s$ is qwer and $k = 2$, below is the series of transformations the string goes through: qwer (original string) wqer (after reversing the first substring of length $2$) weqr (after reversing the second substring of length $2$) werq (after reversing the last substring of length $2$) Hence, the resulting string after modifying $s$ with $k = 2$ is werq. Vasya wants to choose a $k$ such that the string obtained after the above-mentioned modification is lexicographically smallest possible among all choices of $k$. Among all such $k$, he wants to choose the smallest one. Since he is busy attending Felicity 2020, he asks for your help. A string $a$ is lexicographically smaller than a string $b$ if and only if one of the following holds: $a$ is a prefix of $b$, but $a \ne b$; in the first position where $a$ and $b$ differ, the string $a$ has a letter that appears earlier in the alphabet than the corresponding letter in $b$. -----Input----- Each test contains multiple test cases. The first line contains the number of test cases $t$ ($1 \le t \le 5000$). The description of the test cases follows. The first line of each test case contains a single integer $n$ ($1 \le n \le 5000$)Β β€” the length of the string $s$. The second line of each test case contains the string $s$ of $n$ lowercase latin letters. It is guaranteed that the sum of $n$ over all test cases does not exceed $5000$. -----Output----- For each testcase output two lines: In the first line output the lexicographically smallest string $s'$ achievable after the above-mentioned modification. In the second line output the appropriate value of $k$ ($1 \leq k \leq n$) that you chose for performing the modification. If there are multiple values of $k$ that give the lexicographically smallest string, output the smallest value of $k$ among them. -----Example----- Input 6 4 abab 6 qwerty 5 aaaaa 6 alaska 9 lfpbavjsm 1 p Output abab 1 ertyqw 3 aaaaa 1 aksala 6 avjsmbpfl 5 p 1 -----Note----- In the first testcase of the first sample, the string modification results for the sample abab are as follows : for $k = 1$ : abab for $k = 2$ : baba for $k = 3$ : abab for $k = 4$ : baba The lexicographically smallest string achievable through modification is abab for $k = 1$ and $3$. Smallest value of $k$ needed to achieve is hence $1$.
t = int(input()) for _ in range(t): n = int(input()) s = input() mi, m1 = s, 1 for i in range(2, n + 1): s1 = "" if (n - i) % 2 == 0: s1 = s[i - 1 :] + s[: i - 1][::-1] else: s1 = s[i - 1 :] + s[: i - 1] if s1 < mi: mi, m1 = s1, i print(mi) print(m1)
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 ASSIGN VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR STRING IF BIN_OP BIN_OP VAR VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER IF VAR VAR ASSIGN VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR
Vasya has a string $s$ of length $n$. He decides to make the following modification to the string: Pick an integer $k$, ($1 \leq k \leq n$). For $i$ from $1$ to $n-k+1$, reverse the substring $s[i:i+k-1]$ of $s$. For example, if string $s$ is qwer and $k = 2$, below is the series of transformations the string goes through: qwer (original string) wqer (after reversing the first substring of length $2$) weqr (after reversing the second substring of length $2$) werq (after reversing the last substring of length $2$) Hence, the resulting string after modifying $s$ with $k = 2$ is werq. Vasya wants to choose a $k$ such that the string obtained after the above-mentioned modification is lexicographically smallest possible among all choices of $k$. Among all such $k$, he wants to choose the smallest one. Since he is busy attending Felicity 2020, he asks for your help. A string $a$ is lexicographically smaller than a string $b$ if and only if one of the following holds: $a$ is a prefix of $b$, but $a \ne b$; in the first position where $a$ and $b$ differ, the string $a$ has a letter that appears earlier in the alphabet than the corresponding letter in $b$. -----Input----- Each test contains multiple test cases. The first line contains the number of test cases $t$ ($1 \le t \le 5000$). The description of the test cases follows. The first line of each test case contains a single integer $n$ ($1 \le n \le 5000$)Β β€” the length of the string $s$. The second line of each test case contains the string $s$ of $n$ lowercase latin letters. It is guaranteed that the sum of $n$ over all test cases does not exceed $5000$. -----Output----- For each testcase output two lines: In the first line output the lexicographically smallest string $s'$ achievable after the above-mentioned modification. In the second line output the appropriate value of $k$ ($1 \leq k \leq n$) that you chose for performing the modification. If there are multiple values of $k$ that give the lexicographically smallest string, output the smallest value of $k$ among them. -----Example----- Input 6 4 abab 6 qwerty 5 aaaaa 6 alaska 9 lfpbavjsm 1 p Output abab 1 ertyqw 3 aaaaa 1 aksala 6 avjsmbpfl 5 p 1 -----Note----- In the first testcase of the first sample, the string modification results for the sample abab are as follows : for $k = 1$ : abab for $k = 2$ : baba for $k = 3$ : abab for $k = 4$ : baba The lexicographically smallest string achievable through modification is abab for $k = 1$ and $3$. Smallest value of $k$ needed to achieve is hence $1$.
t = int(input()) for _ in range(t): n = int(input()) s = input() assert len(s) == n def vasya(s, k): if (n - k) % 2 == 0: suffix = s[k - 2 :: -1] else: suffix = s[: k - 1] return s[k - 1 :] + suffix best = s, 1 for k in range(2, n + 1): v = vasya(s, k), k if v < best: best = v print(*best, sep="\n")
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR FUNC_DEF IF BIN_OP BIN_OP VAR VAR NUMBER NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER RETURN BIN_OP VAR BIN_OP VAR NUMBER VAR ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR IF VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR STRING
Vasya has a string $s$ of length $n$. He decides to make the following modification to the string: Pick an integer $k$, ($1 \leq k \leq n$). For $i$ from $1$ to $n-k+1$, reverse the substring $s[i:i+k-1]$ of $s$. For example, if string $s$ is qwer and $k = 2$, below is the series of transformations the string goes through: qwer (original string) wqer (after reversing the first substring of length $2$) weqr (after reversing the second substring of length $2$) werq (after reversing the last substring of length $2$) Hence, the resulting string after modifying $s$ with $k = 2$ is werq. Vasya wants to choose a $k$ such that the string obtained after the above-mentioned modification is lexicographically smallest possible among all choices of $k$. Among all such $k$, he wants to choose the smallest one. Since he is busy attending Felicity 2020, he asks for your help. A string $a$ is lexicographically smaller than a string $b$ if and only if one of the following holds: $a$ is a prefix of $b$, but $a \ne b$; in the first position where $a$ and $b$ differ, the string $a$ has a letter that appears earlier in the alphabet than the corresponding letter in $b$. -----Input----- Each test contains multiple test cases. The first line contains the number of test cases $t$ ($1 \le t \le 5000$). The description of the test cases follows. The first line of each test case contains a single integer $n$ ($1 \le n \le 5000$)Β β€” the length of the string $s$. The second line of each test case contains the string $s$ of $n$ lowercase latin letters. It is guaranteed that the sum of $n$ over all test cases does not exceed $5000$. -----Output----- For each testcase output two lines: In the first line output the lexicographically smallest string $s'$ achievable after the above-mentioned modification. In the second line output the appropriate value of $k$ ($1 \leq k \leq n$) that you chose for performing the modification. If there are multiple values of $k$ that give the lexicographically smallest string, output the smallest value of $k$ among them. -----Example----- Input 6 4 abab 6 qwerty 5 aaaaa 6 alaska 9 lfpbavjsm 1 p Output abab 1 ertyqw 3 aaaaa 1 aksala 6 avjsmbpfl 5 p 1 -----Note----- In the first testcase of the first sample, the string modification results for the sample abab are as follows : for $k = 1$ : abab for $k = 2$ : baba for $k = 3$ : abab for $k = 4$ : baba The lexicographically smallest string achievable through modification is abab for $k = 1$ and $3$. Smallest value of $k$ needed to achieve is hence $1$.
for _ in range(int(input())): s = [input() for i in "aa"][1] idx = [i for i in range(len(s)) if s[i] == min(s)] ss = [ (s[i:] + s[:i][::-1] if (len(s) + i) % 2 != 0 else s[i:] + s[:i]) for i in idx ] print(min(ss), idx[ss.index(min(ss))] + 1, sep="\n")
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR STRING NUMBER ASSIGN VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP BIN_OP FUNC_CALL VAR VAR VAR NUMBER NUMBER BIN_OP VAR VAR VAR VAR NUMBER BIN_OP VAR VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR BIN_OP VAR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER STRING
Vasya has a string $s$ of length $n$. He decides to make the following modification to the string: Pick an integer $k$, ($1 \leq k \leq n$). For $i$ from $1$ to $n-k+1$, reverse the substring $s[i:i+k-1]$ of $s$. For example, if string $s$ is qwer and $k = 2$, below is the series of transformations the string goes through: qwer (original string) wqer (after reversing the first substring of length $2$) weqr (after reversing the second substring of length $2$) werq (after reversing the last substring of length $2$) Hence, the resulting string after modifying $s$ with $k = 2$ is werq. Vasya wants to choose a $k$ such that the string obtained after the above-mentioned modification is lexicographically smallest possible among all choices of $k$. Among all such $k$, he wants to choose the smallest one. Since he is busy attending Felicity 2020, he asks for your help. A string $a$ is lexicographically smaller than a string $b$ if and only if one of the following holds: $a$ is a prefix of $b$, but $a \ne b$; in the first position where $a$ and $b$ differ, the string $a$ has a letter that appears earlier in the alphabet than the corresponding letter in $b$. -----Input----- Each test contains multiple test cases. The first line contains the number of test cases $t$ ($1 \le t \le 5000$). The description of the test cases follows. The first line of each test case contains a single integer $n$ ($1 \le n \le 5000$)Β β€” the length of the string $s$. The second line of each test case contains the string $s$ of $n$ lowercase latin letters. It is guaranteed that the sum of $n$ over all test cases does not exceed $5000$. -----Output----- For each testcase output two lines: In the first line output the lexicographically smallest string $s'$ achievable after the above-mentioned modification. In the second line output the appropriate value of $k$ ($1 \leq k \leq n$) that you chose for performing the modification. If there are multiple values of $k$ that give the lexicographically smallest string, output the smallest value of $k$ among them. -----Example----- Input 6 4 abab 6 qwerty 5 aaaaa 6 alaska 9 lfpbavjsm 1 p Output abab 1 ertyqw 3 aaaaa 1 aksala 6 avjsmbpfl 5 p 1 -----Note----- In the first testcase of the first sample, the string modification results for the sample abab are as follows : for $k = 1$ : abab for $k = 2$ : baba for $k = 3$ : abab for $k = 4$ : baba The lexicographically smallest string achievable through modification is abab for $k = 1$ and $3$. Smallest value of $k$ needed to achieve is hence $1$.
T = int(input()) while T: T -= 1 n = int(input()) s = input() ret = s k = 1 for i in range(1, n): if (n - i) % 2: t = s[i:] + s[0:i][::-1] else: t = s[i:] + s[0:i] if ret > t: ret = t k = i + 1 t = s[::-1] if ret > t: ret = t k = n print(ret) print(k)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR WHILE VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR IF BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP VAR VAR VAR NUMBER VAR IF VAR VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR VAR NUMBER IF VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR
Vasya has a string $s$ of length $n$. He decides to make the following modification to the string: Pick an integer $k$, ($1 \leq k \leq n$). For $i$ from $1$ to $n-k+1$, reverse the substring $s[i:i+k-1]$ of $s$. For example, if string $s$ is qwer and $k = 2$, below is the series of transformations the string goes through: qwer (original string) wqer (after reversing the first substring of length $2$) weqr (after reversing the second substring of length $2$) werq (after reversing the last substring of length $2$) Hence, the resulting string after modifying $s$ with $k = 2$ is werq. Vasya wants to choose a $k$ such that the string obtained after the above-mentioned modification is lexicographically smallest possible among all choices of $k$. Among all such $k$, he wants to choose the smallest one. Since he is busy attending Felicity 2020, he asks for your help. A string $a$ is lexicographically smaller than a string $b$ if and only if one of the following holds: $a$ is a prefix of $b$, but $a \ne b$; in the first position where $a$ and $b$ differ, the string $a$ has a letter that appears earlier in the alphabet than the corresponding letter in $b$. -----Input----- Each test contains multiple test cases. The first line contains the number of test cases $t$ ($1 \le t \le 5000$). The description of the test cases follows. The first line of each test case contains a single integer $n$ ($1 \le n \le 5000$)Β β€” the length of the string $s$. The second line of each test case contains the string $s$ of $n$ lowercase latin letters. It is guaranteed that the sum of $n$ over all test cases does not exceed $5000$. -----Output----- For each testcase output two lines: In the first line output the lexicographically smallest string $s'$ achievable after the above-mentioned modification. In the second line output the appropriate value of $k$ ($1 \leq k \leq n$) that you chose for performing the modification. If there are multiple values of $k$ that give the lexicographically smallest string, output the smallest value of $k$ among them. -----Example----- Input 6 4 abab 6 qwerty 5 aaaaa 6 alaska 9 lfpbavjsm 1 p Output abab 1 ertyqw 3 aaaaa 1 aksala 6 avjsmbpfl 5 p 1 -----Note----- In the first testcase of the first sample, the string modification results for the sample abab are as follows : for $k = 1$ : abab for $k = 2$ : baba for $k = 3$ : abab for $k = 4$ : baba The lexicographically smallest string achievable through modification is abab for $k = 1$ and $3$. Smallest value of $k$ needed to achieve is hence $1$.
T = int(input()) def compare(n, l, k, best): l1 = [] l2 = [] if (best + n) % 2 == 1: for i in range(n): l1.append(l[(best - 1 + i) % n]) else: for i in range(best - 1, n): l1.append(l[i]) for i in range(best - 2, -1, -1): l1.append(l[i]) if (k + n) % 2 == 1: for i in range(n): l2.append(l[(k - 1 + i) % n]) else: for i in range(k - 1, n): l2.append(l[i]) for i in range(k - 2, -1, -1): l2.append(l[i]) for i in range(n): if l1[i] < l2[i]: return l1, best elif l1[i] > l2[i]: return l2, k return l1, best for _ in range(T): n = int(input()) s = input() l = list(s) best = l.copy() bestk = 1 for k in range(2, n + 1): best, bestk = compare(n, l, k, bestk) for i in range(n): print(best[i], end="") print() print(bestk)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_DEF ASSIGN VAR LIST ASSIGN VAR LIST IF BIN_OP BIN_OP VAR VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR BIN_OP BIN_OP BIN_OP VAR NUMBER VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER EXPR FUNC_CALL VAR VAR VAR IF BIN_OP BIN_OP VAR VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR BIN_OP BIN_OP BIN_OP VAR NUMBER VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER EXPR FUNC_CALL VAR VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR RETURN VAR VAR IF VAR VAR VAR VAR RETURN VAR VAR RETURN VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR VAR VAR VAR FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR STRING EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR
Vasya has a string $s$ of length $n$. He decides to make the following modification to the string: Pick an integer $k$, ($1 \leq k \leq n$). For $i$ from $1$ to $n-k+1$, reverse the substring $s[i:i+k-1]$ of $s$. For example, if string $s$ is qwer and $k = 2$, below is the series of transformations the string goes through: qwer (original string) wqer (after reversing the first substring of length $2$) weqr (after reversing the second substring of length $2$) werq (after reversing the last substring of length $2$) Hence, the resulting string after modifying $s$ with $k = 2$ is werq. Vasya wants to choose a $k$ such that the string obtained after the above-mentioned modification is lexicographically smallest possible among all choices of $k$. Among all such $k$, he wants to choose the smallest one. Since he is busy attending Felicity 2020, he asks for your help. A string $a$ is lexicographically smaller than a string $b$ if and only if one of the following holds: $a$ is a prefix of $b$, but $a \ne b$; in the first position where $a$ and $b$ differ, the string $a$ has a letter that appears earlier in the alphabet than the corresponding letter in $b$. -----Input----- Each test contains multiple test cases. The first line contains the number of test cases $t$ ($1 \le t \le 5000$). The description of the test cases follows. The first line of each test case contains a single integer $n$ ($1 \le n \le 5000$)Β β€” the length of the string $s$. The second line of each test case contains the string $s$ of $n$ lowercase latin letters. It is guaranteed that the sum of $n$ over all test cases does not exceed $5000$. -----Output----- For each testcase output two lines: In the first line output the lexicographically smallest string $s'$ achievable after the above-mentioned modification. In the second line output the appropriate value of $k$ ($1 \leq k \leq n$) that you chose for performing the modification. If there are multiple values of $k$ that give the lexicographically smallest string, output the smallest value of $k$ among them. -----Example----- Input 6 4 abab 6 qwerty 5 aaaaa 6 alaska 9 lfpbavjsm 1 p Output abab 1 ertyqw 3 aaaaa 1 aksala 6 avjsmbpfl 5 p 1 -----Note----- In the first testcase of the first sample, the string modification results for the sample abab are as follows : for $k = 1$ : abab for $k = 2$ : baba for $k = 3$ : abab for $k = 4$ : baba The lexicographically smallest string achievable through modification is abab for $k = 1$ and $3$. Smallest value of $k$ needed to achieve is hence $1$.
for t in range(int(input())): n = int(input()) s = input() dic, se = {s: 1}, {s} for k in range(2, n): p = s[k - 1 :] + (s[: k - 1], s[: k - 1][::-1])[n % 2 == k % 2] if p not in dic: dic[p] = k se.add(p) if s[::-1] not in dic: dic[s[::-1]] = n se.add(s[::-1]) ans = min(se) print(ans) print(dic[ans])
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR VAR DICT VAR NUMBER VAR FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR BIN_OP VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER BIN_OP VAR NUMBER BIN_OP VAR NUMBER IF VAR VAR ASSIGN VAR VAR VAR EXPR FUNC_CALL VAR VAR IF VAR NUMBER VAR ASSIGN VAR VAR NUMBER VAR EXPR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR
Vasya has a string $s$ of length $n$. He decides to make the following modification to the string: Pick an integer $k$, ($1 \leq k \leq n$). For $i$ from $1$ to $n-k+1$, reverse the substring $s[i:i+k-1]$ of $s$. For example, if string $s$ is qwer and $k = 2$, below is the series of transformations the string goes through: qwer (original string) wqer (after reversing the first substring of length $2$) weqr (after reversing the second substring of length $2$) werq (after reversing the last substring of length $2$) Hence, the resulting string after modifying $s$ with $k = 2$ is werq. Vasya wants to choose a $k$ such that the string obtained after the above-mentioned modification is lexicographically smallest possible among all choices of $k$. Among all such $k$, he wants to choose the smallest one. Since he is busy attending Felicity 2020, he asks for your help. A string $a$ is lexicographically smaller than a string $b$ if and only if one of the following holds: $a$ is a prefix of $b$, but $a \ne b$; in the first position where $a$ and $b$ differ, the string $a$ has a letter that appears earlier in the alphabet than the corresponding letter in $b$. -----Input----- Each test contains multiple test cases. The first line contains the number of test cases $t$ ($1 \le t \le 5000$). The description of the test cases follows. The first line of each test case contains a single integer $n$ ($1 \le n \le 5000$)Β β€” the length of the string $s$. The second line of each test case contains the string $s$ of $n$ lowercase latin letters. It is guaranteed that the sum of $n$ over all test cases does not exceed $5000$. -----Output----- For each testcase output two lines: In the first line output the lexicographically smallest string $s'$ achievable after the above-mentioned modification. In the second line output the appropriate value of $k$ ($1 \leq k \leq n$) that you chose for performing the modification. If there are multiple values of $k$ that give the lexicographically smallest string, output the smallest value of $k$ among them. -----Example----- Input 6 4 abab 6 qwerty 5 aaaaa 6 alaska 9 lfpbavjsm 1 p Output abab 1 ertyqw 3 aaaaa 1 aksala 6 avjsmbpfl 5 p 1 -----Note----- In the first testcase of the first sample, the string modification results for the sample abab are as follows : for $k = 1$ : abab for $k = 2$ : baba for $k = 3$ : abab for $k = 4$ : baba The lexicographically smallest string achievable through modification is abab for $k = 1$ and $3$. Smallest value of $k$ needed to achieve is hence $1$.
for _ in range(int(input())): n = int(input()) s = input() c = "" k = 0 for i in range(n + 1): t = s[i:] if i % 2 != n % 2: t += s[:i][::-1] else: t += s[:i] if not c or t < c: c = t k = i print(c) print(k + 1)
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR STRING ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR IF BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR VAR VAR NUMBER VAR VAR VAR IF VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER
Vasya has a string $s$ of length $n$. He decides to make the following modification to the string: Pick an integer $k$, ($1 \leq k \leq n$). For $i$ from $1$ to $n-k+1$, reverse the substring $s[i:i+k-1]$ of $s$. For example, if string $s$ is qwer and $k = 2$, below is the series of transformations the string goes through: qwer (original string) wqer (after reversing the first substring of length $2$) weqr (after reversing the second substring of length $2$) werq (after reversing the last substring of length $2$) Hence, the resulting string after modifying $s$ with $k = 2$ is werq. Vasya wants to choose a $k$ such that the string obtained after the above-mentioned modification is lexicographically smallest possible among all choices of $k$. Among all such $k$, he wants to choose the smallest one. Since he is busy attending Felicity 2020, he asks for your help. A string $a$ is lexicographically smaller than a string $b$ if and only if one of the following holds: $a$ is a prefix of $b$, but $a \ne b$; in the first position where $a$ and $b$ differ, the string $a$ has a letter that appears earlier in the alphabet than the corresponding letter in $b$. -----Input----- Each test contains multiple test cases. The first line contains the number of test cases $t$ ($1 \le t \le 5000$). The description of the test cases follows. The first line of each test case contains a single integer $n$ ($1 \le n \le 5000$)Β β€” the length of the string $s$. The second line of each test case contains the string $s$ of $n$ lowercase latin letters. It is guaranteed that the sum of $n$ over all test cases does not exceed $5000$. -----Output----- For each testcase output two lines: In the first line output the lexicographically smallest string $s'$ achievable after the above-mentioned modification. In the second line output the appropriate value of $k$ ($1 \leq k \leq n$) that you chose for performing the modification. If there are multiple values of $k$ that give the lexicographically smallest string, output the smallest value of $k$ among them. -----Example----- Input 6 4 abab 6 qwerty 5 aaaaa 6 alaska 9 lfpbavjsm 1 p Output abab 1 ertyqw 3 aaaaa 1 aksala 6 avjsmbpfl 5 p 1 -----Note----- In the first testcase of the first sample, the string modification results for the sample abab are as follows : for $k = 1$ : abab for $k = 2$ : baba for $k = 3$ : abab for $k = 4$ : baba The lexicographically smallest string achievable through modification is abab for $k = 1$ and $3$. Smallest value of $k$ needed to achieve is hence $1$.
t = int(input()) for _ in range(t): n = int(input()) string = input() ans = 0 result = string for i in range(0, n): choti = string[0:i] if n % 2 == (i + 1) % 2: choti = choti[::-1] new = string[i:] + choti if new < result: result = new ans = i print(result) print(ans + 1)
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 ASSIGN VAR NUMBER ASSIGN VAR VAR FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR NUMBER VAR IF BIN_OP VAR NUMBER BIN_OP BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR VAR IF VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER
Vasya has a string $s$ of length $n$. He decides to make the following modification to the string: Pick an integer $k$, ($1 \leq k \leq n$). For $i$ from $1$ to $n-k+1$, reverse the substring $s[i:i+k-1]$ of $s$. For example, if string $s$ is qwer and $k = 2$, below is the series of transformations the string goes through: qwer (original string) wqer (after reversing the first substring of length $2$) weqr (after reversing the second substring of length $2$) werq (after reversing the last substring of length $2$) Hence, the resulting string after modifying $s$ with $k = 2$ is werq. Vasya wants to choose a $k$ such that the string obtained after the above-mentioned modification is lexicographically smallest possible among all choices of $k$. Among all such $k$, he wants to choose the smallest one. Since he is busy attending Felicity 2020, he asks for your help. A string $a$ is lexicographically smaller than a string $b$ if and only if one of the following holds: $a$ is a prefix of $b$, but $a \ne b$; in the first position where $a$ and $b$ differ, the string $a$ has a letter that appears earlier in the alphabet than the corresponding letter in $b$. -----Input----- Each test contains multiple test cases. The first line contains the number of test cases $t$ ($1 \le t \le 5000$). The description of the test cases follows. The first line of each test case contains a single integer $n$ ($1 \le n \le 5000$)Β β€” the length of the string $s$. The second line of each test case contains the string $s$ of $n$ lowercase latin letters. It is guaranteed that the sum of $n$ over all test cases does not exceed $5000$. -----Output----- For each testcase output two lines: In the first line output the lexicographically smallest string $s'$ achievable after the above-mentioned modification. In the second line output the appropriate value of $k$ ($1 \leq k \leq n$) that you chose for performing the modification. If there are multiple values of $k$ that give the lexicographically smallest string, output the smallest value of $k$ among them. -----Example----- Input 6 4 abab 6 qwerty 5 aaaaa 6 alaska 9 lfpbavjsm 1 p Output abab 1 ertyqw 3 aaaaa 1 aksala 6 avjsmbpfl 5 p 1 -----Note----- In the first testcase of the first sample, the string modification results for the sample abab are as follows : for $k = 1$ : abab for $k = 2$ : baba for $k = 3$ : abab for $k = 4$ : baba The lexicographically smallest string achievable through modification is abab for $k = 1$ and $3$. Smallest value of $k$ needed to achieve is hence $1$.
t = int(input()) for mo in range(1, t + 1): l = [] n = int(input()) s = input() for i in range(1, n + 1): if (n - i + 1) % 2 and i >= 2: l.append((s[i - 1 :] + s[i - 2 :: -1], i)) elif (n - i + 1) % 2: l.append((s, i)) else: l.append((s[i - 1 :] + s[: i - 1], i)) print(min(l)[0]) print(min(l)[1])
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR LIST ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER IF BIN_OP BIN_OP BIN_OP VAR VAR NUMBER NUMBER VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER VAR IF BIN_OP BIN_OP BIN_OP VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER
Vasya has a string $s$ of length $n$. He decides to make the following modification to the string: Pick an integer $k$, ($1 \leq k \leq n$). For $i$ from $1$ to $n-k+1$, reverse the substring $s[i:i+k-1]$ of $s$. For example, if string $s$ is qwer and $k = 2$, below is the series of transformations the string goes through: qwer (original string) wqer (after reversing the first substring of length $2$) weqr (after reversing the second substring of length $2$) werq (after reversing the last substring of length $2$) Hence, the resulting string after modifying $s$ with $k = 2$ is werq. Vasya wants to choose a $k$ such that the string obtained after the above-mentioned modification is lexicographically smallest possible among all choices of $k$. Among all such $k$, he wants to choose the smallest one. Since he is busy attending Felicity 2020, he asks for your help. A string $a$ is lexicographically smaller than a string $b$ if and only if one of the following holds: $a$ is a prefix of $b$, but $a \ne b$; in the first position where $a$ and $b$ differ, the string $a$ has a letter that appears earlier in the alphabet than the corresponding letter in $b$. -----Input----- Each test contains multiple test cases. The first line contains the number of test cases $t$ ($1 \le t \le 5000$). The description of the test cases follows. The first line of each test case contains a single integer $n$ ($1 \le n \le 5000$)Β β€” the length of the string $s$. The second line of each test case contains the string $s$ of $n$ lowercase latin letters. It is guaranteed that the sum of $n$ over all test cases does not exceed $5000$. -----Output----- For each testcase output two lines: In the first line output the lexicographically smallest string $s'$ achievable after the above-mentioned modification. In the second line output the appropriate value of $k$ ($1 \leq k \leq n$) that you chose for performing the modification. If there are multiple values of $k$ that give the lexicographically smallest string, output the smallest value of $k$ among them. -----Example----- Input 6 4 abab 6 qwerty 5 aaaaa 6 alaska 9 lfpbavjsm 1 p Output abab 1 ertyqw 3 aaaaa 1 aksala 6 avjsmbpfl 5 p 1 -----Note----- In the first testcase of the first sample, the string modification results for the sample abab are as follows : for $k = 1$ : abab for $k = 2$ : baba for $k = 3$ : abab for $k = 4$ : baba The lexicographically smallest string achievable through modification is abab for $k = 1$ and $3$. Smallest value of $k$ needed to achieve is hence $1$.
for _ in range(int(input())): n = int(input()) s = input() r = s d = 0 for i in range(1, n): if (n - i) % 2 == 1: a = s[i:] + s[:i][::-1] else: a = s[i:] + s[:i] if a < r: r = a d = i print(r) print(d + 1)
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR IF BIN_OP BIN_OP VAR VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR VAR VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR VAR VAR IF VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER
Vasya has a string $s$ of length $n$. He decides to make the following modification to the string: Pick an integer $k$, ($1 \leq k \leq n$). For $i$ from $1$ to $n-k+1$, reverse the substring $s[i:i+k-1]$ of $s$. For example, if string $s$ is qwer and $k = 2$, below is the series of transformations the string goes through: qwer (original string) wqer (after reversing the first substring of length $2$) weqr (after reversing the second substring of length $2$) werq (after reversing the last substring of length $2$) Hence, the resulting string after modifying $s$ with $k = 2$ is werq. Vasya wants to choose a $k$ such that the string obtained after the above-mentioned modification is lexicographically smallest possible among all choices of $k$. Among all such $k$, he wants to choose the smallest one. Since he is busy attending Felicity 2020, he asks for your help. A string $a$ is lexicographically smaller than a string $b$ if and only if one of the following holds: $a$ is a prefix of $b$, but $a \ne b$; in the first position where $a$ and $b$ differ, the string $a$ has a letter that appears earlier in the alphabet than the corresponding letter in $b$. -----Input----- Each test contains multiple test cases. The first line contains the number of test cases $t$ ($1 \le t \le 5000$). The description of the test cases follows. The first line of each test case contains a single integer $n$ ($1 \le n \le 5000$)Β β€” the length of the string $s$. The second line of each test case contains the string $s$ of $n$ lowercase latin letters. It is guaranteed that the sum of $n$ over all test cases does not exceed $5000$. -----Output----- For each testcase output two lines: In the first line output the lexicographically smallest string $s'$ achievable after the above-mentioned modification. In the second line output the appropriate value of $k$ ($1 \leq k \leq n$) that you chose for performing the modification. If there are multiple values of $k$ that give the lexicographically smallest string, output the smallest value of $k$ among them. -----Example----- Input 6 4 abab 6 qwerty 5 aaaaa 6 alaska 9 lfpbavjsm 1 p Output abab 1 ertyqw 3 aaaaa 1 aksala 6 avjsmbpfl 5 p 1 -----Note----- In the first testcase of the first sample, the string modification results for the sample abab are as follows : for $k = 1$ : abab for $k = 2$ : baba for $k = 3$ : abab for $k = 4$ : baba The lexicographically smallest string achievable through modification is abab for $k = 1$ and $3$. Smallest value of $k$ needed to achieve is hence $1$.
t = int(input()) while t > 0: n = int(input()) s = input() mem = [] lst = list(s) for i in range(0, n): if i == 0: mem.append([s[i:], i + 1]) elif i < n - 1: if (n - i) % 2 == 0: mem.append([s[i:] + s[:i], i + 1]) else: mem.append([s[i:] + s[i - 1 :: -1], i + 1]) else: mem.append([s[-1::-1], n]) mem.sort() print(mem[0][0]) print(mem[0][1]) t -= 1
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR WHILE VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR NUMBER EXPR FUNC_CALL VAR LIST VAR VAR BIN_OP VAR NUMBER IF VAR BIN_OP VAR NUMBER IF BIN_OP BIN_OP VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR LIST BIN_OP VAR VAR VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR LIST BIN_OP VAR VAR VAR BIN_OP VAR NUMBER NUMBER BIN_OP VAR NUMBER EXPR FUNC_CALL VAR LIST VAR NUMBER NUMBER VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR NUMBER NUMBER VAR NUMBER
Vasya has a string $s$ of length $n$. He decides to make the following modification to the string: Pick an integer $k$, ($1 \leq k \leq n$). For $i$ from $1$ to $n-k+1$, reverse the substring $s[i:i+k-1]$ of $s$. For example, if string $s$ is qwer and $k = 2$, below is the series of transformations the string goes through: qwer (original string) wqer (after reversing the first substring of length $2$) weqr (after reversing the second substring of length $2$) werq (after reversing the last substring of length $2$) Hence, the resulting string after modifying $s$ with $k = 2$ is werq. Vasya wants to choose a $k$ such that the string obtained after the above-mentioned modification is lexicographically smallest possible among all choices of $k$. Among all such $k$, he wants to choose the smallest one. Since he is busy attending Felicity 2020, he asks for your help. A string $a$ is lexicographically smaller than a string $b$ if and only if one of the following holds: $a$ is a prefix of $b$, but $a \ne b$; in the first position where $a$ and $b$ differ, the string $a$ has a letter that appears earlier in the alphabet than the corresponding letter in $b$. -----Input----- Each test contains multiple test cases. The first line contains the number of test cases $t$ ($1 \le t \le 5000$). The description of the test cases follows. The first line of each test case contains a single integer $n$ ($1 \le n \le 5000$)Β β€” the length of the string $s$. The second line of each test case contains the string $s$ of $n$ lowercase latin letters. It is guaranteed that the sum of $n$ over all test cases does not exceed $5000$. -----Output----- For each testcase output two lines: In the first line output the lexicographically smallest string $s'$ achievable after the above-mentioned modification. In the second line output the appropriate value of $k$ ($1 \leq k \leq n$) that you chose for performing the modification. If there are multiple values of $k$ that give the lexicographically smallest string, output the smallest value of $k$ among them. -----Example----- Input 6 4 abab 6 qwerty 5 aaaaa 6 alaska 9 lfpbavjsm 1 p Output abab 1 ertyqw 3 aaaaa 1 aksala 6 avjsmbpfl 5 p 1 -----Note----- In the first testcase of the first sample, the string modification results for the sample abab are as follows : for $k = 1$ : abab for $k = 2$ : baba for $k = 3$ : abab for $k = 4$ : baba The lexicographically smallest string achievable through modification is abab for $k = 1$ and $3$. Smallest value of $k$ needed to achieve is hence $1$.
t = int(input()) R = lambda: map(int, input().split()) for i in range(t): n = int(input()) s = input() ans = [] for i in range(1, n + 1): if i == n: temp = s[::-1] ans.append((temp, i)) continue if (n - i - 1) % 2 == 0: temp = s[i - 1 :] + s[: i - 1] else: temp = s[i - 1 :] + s[: i - 1][::-1] ans.append((temp, i)) z = min(ans) print(z[0]) print(z[1])
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER IF VAR VAR ASSIGN VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR IF BIN_OP BIN_OP BIN_OP VAR VAR NUMBER NUMBER NUMBER ASSIGN VAR BIN_OP VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER
Vasya has a string $s$ of length $n$. He decides to make the following modification to the string: Pick an integer $k$, ($1 \leq k \leq n$). For $i$ from $1$ to $n-k+1$, reverse the substring $s[i:i+k-1]$ of $s$. For example, if string $s$ is qwer and $k = 2$, below is the series of transformations the string goes through: qwer (original string) wqer (after reversing the first substring of length $2$) weqr (after reversing the second substring of length $2$) werq (after reversing the last substring of length $2$) Hence, the resulting string after modifying $s$ with $k = 2$ is werq. Vasya wants to choose a $k$ such that the string obtained after the above-mentioned modification is lexicographically smallest possible among all choices of $k$. Among all such $k$, he wants to choose the smallest one. Since he is busy attending Felicity 2020, he asks for your help. A string $a$ is lexicographically smaller than a string $b$ if and only if one of the following holds: $a$ is a prefix of $b$, but $a \ne b$; in the first position where $a$ and $b$ differ, the string $a$ has a letter that appears earlier in the alphabet than the corresponding letter in $b$. -----Input----- Each test contains multiple test cases. The first line contains the number of test cases $t$ ($1 \le t \le 5000$). The description of the test cases follows. The first line of each test case contains a single integer $n$ ($1 \le n \le 5000$)Β β€” the length of the string $s$. The second line of each test case contains the string $s$ of $n$ lowercase latin letters. It is guaranteed that the sum of $n$ over all test cases does not exceed $5000$. -----Output----- For each testcase output two lines: In the first line output the lexicographically smallest string $s'$ achievable after the above-mentioned modification. In the second line output the appropriate value of $k$ ($1 \leq k \leq n$) that you chose for performing the modification. If there are multiple values of $k$ that give the lexicographically smallest string, output the smallest value of $k$ among them. -----Example----- Input 6 4 abab 6 qwerty 5 aaaaa 6 alaska 9 lfpbavjsm 1 p Output abab 1 ertyqw 3 aaaaa 1 aksala 6 avjsmbpfl 5 p 1 -----Note----- In the first testcase of the first sample, the string modification results for the sample abab are as follows : for $k = 1$ : abab for $k = 2$ : baba for $k = 3$ : abab for $k = 4$ : baba The lexicographically smallest string achievable through modification is abab for $k = 1$ and $3$. Smallest value of $k$ needed to achieve is hence $1$.
for _ in range(int(input())): n = int(input()) s = input() x = min(s) k = [] for i in range(n): if s[i] == x: k.append(i + 1) pos = [] for kvalue in k: r = (n - kvalue + 1) % 2 s2 = s[: kvalue - 1] if r: s2 = "".join(reversed(s2)) s1 = s[kvalue - 1 :] + s2 pos.append(s1) ans = min(pos) i = pos.index(ans) kans = k[i] print(ans) print(kans)
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR LIST FOR VAR VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR VAR NUMBER NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER IF VAR ASSIGN VAR FUNC_CALL STRING FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR
Vasya has a string $s$ of length $n$. He decides to make the following modification to the string: Pick an integer $k$, ($1 \leq k \leq n$). For $i$ from $1$ to $n-k+1$, reverse the substring $s[i:i+k-1]$ of $s$. For example, if string $s$ is qwer and $k = 2$, below is the series of transformations the string goes through: qwer (original string) wqer (after reversing the first substring of length $2$) weqr (after reversing the second substring of length $2$) werq (after reversing the last substring of length $2$) Hence, the resulting string after modifying $s$ with $k = 2$ is werq. Vasya wants to choose a $k$ such that the string obtained after the above-mentioned modification is lexicographically smallest possible among all choices of $k$. Among all such $k$, he wants to choose the smallest one. Since he is busy attending Felicity 2020, he asks for your help. A string $a$ is lexicographically smaller than a string $b$ if and only if one of the following holds: $a$ is a prefix of $b$, but $a \ne b$; in the first position where $a$ and $b$ differ, the string $a$ has a letter that appears earlier in the alphabet than the corresponding letter in $b$. -----Input----- Each test contains multiple test cases. The first line contains the number of test cases $t$ ($1 \le t \le 5000$). The description of the test cases follows. The first line of each test case contains a single integer $n$ ($1 \le n \le 5000$)Β β€” the length of the string $s$. The second line of each test case contains the string $s$ of $n$ lowercase latin letters. It is guaranteed that the sum of $n$ over all test cases does not exceed $5000$. -----Output----- For each testcase output two lines: In the first line output the lexicographically smallest string $s'$ achievable after the above-mentioned modification. In the second line output the appropriate value of $k$ ($1 \leq k \leq n$) that you chose for performing the modification. If there are multiple values of $k$ that give the lexicographically smallest string, output the smallest value of $k$ among them. -----Example----- Input 6 4 abab 6 qwerty 5 aaaaa 6 alaska 9 lfpbavjsm 1 p Output abab 1 ertyqw 3 aaaaa 1 aksala 6 avjsmbpfl 5 p 1 -----Note----- In the first testcase of the first sample, the string modification results for the sample abab are as follows : for $k = 1$ : abab for $k = 2$ : baba for $k = 3$ : abab for $k = 4$ : baba The lexicographically smallest string achievable through modification is abab for $k = 1$ and $3$. Smallest value of $k$ needed to achieve is hence $1$.
import sys t = int(input()) for x in range(t): n = int(input()) s = input() ind = 0 mini = min(s) d = [] for i in range(n): if s[i] == mini: d.append(i) maxi = s n1 = len(d) for i in range(n1): s1 = "" cnt = 0 s1 += s[d[i] :] if (n - d[i]) % 2 == 0: s1 += s[: d[i]] elif d[i] - 1 >= 0: s1 += s[d[i] - 1 :: -1] if maxi > s1: maxi = s1 ind = d[i] print("".join(maxi)) print(ind + 1)
IMPORT 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 ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR STRING ASSIGN VAR NUMBER VAR VAR VAR VAR IF BIN_OP BIN_OP VAR VAR VAR NUMBER NUMBER VAR VAR VAR VAR IF BIN_OP VAR VAR NUMBER NUMBER VAR VAR BIN_OP VAR VAR NUMBER NUMBER IF VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL STRING VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER
Vasya has a string $s$ of length $n$. He decides to make the following modification to the string: Pick an integer $k$, ($1 \leq k \leq n$). For $i$ from $1$ to $n-k+1$, reverse the substring $s[i:i+k-1]$ of $s$. For example, if string $s$ is qwer and $k = 2$, below is the series of transformations the string goes through: qwer (original string) wqer (after reversing the first substring of length $2$) weqr (after reversing the second substring of length $2$) werq (after reversing the last substring of length $2$) Hence, the resulting string after modifying $s$ with $k = 2$ is werq. Vasya wants to choose a $k$ such that the string obtained after the above-mentioned modification is lexicographically smallest possible among all choices of $k$. Among all such $k$, he wants to choose the smallest one. Since he is busy attending Felicity 2020, he asks for your help. A string $a$ is lexicographically smaller than a string $b$ if and only if one of the following holds: $a$ is a prefix of $b$, but $a \ne b$; in the first position where $a$ and $b$ differ, the string $a$ has a letter that appears earlier in the alphabet than the corresponding letter in $b$. -----Input----- Each test contains multiple test cases. The first line contains the number of test cases $t$ ($1 \le t \le 5000$). The description of the test cases follows. The first line of each test case contains a single integer $n$ ($1 \le n \le 5000$)Β β€” the length of the string $s$. The second line of each test case contains the string $s$ of $n$ lowercase latin letters. It is guaranteed that the sum of $n$ over all test cases does not exceed $5000$. -----Output----- For each testcase output two lines: In the first line output the lexicographically smallest string $s'$ achievable after the above-mentioned modification. In the second line output the appropriate value of $k$ ($1 \leq k \leq n$) that you chose for performing the modification. If there are multiple values of $k$ that give the lexicographically smallest string, output the smallest value of $k$ among them. -----Example----- Input 6 4 abab 6 qwerty 5 aaaaa 6 alaska 9 lfpbavjsm 1 p Output abab 1 ertyqw 3 aaaaa 1 aksala 6 avjsmbpfl 5 p 1 -----Note----- In the first testcase of the first sample, the string modification results for the sample abab are as follows : for $k = 1$ : abab for $k = 2$ : baba for $k = 3$ : abab for $k = 4$ : baba The lexicographically smallest string achievable through modification is abab for $k = 1$ and $3$. Smallest value of $k$ needed to achieve is hence $1$.
for _ in range(int(input())): n = int(input()) s = input() l1 = [] l2 = [] min_ = min(s) for i in range(n): if s[i] == min_: l1.append(i) for i in l1: s1 = s[i:] if (n - i) % 2 == 0: s1 += s[:i] else: s1 += s[:i][::-1] l2.append(s1) x = min(l2) print(x) print(l1[l2.index(x)] + 1)
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR LIST ASSIGN VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR EXPR FUNC_CALL VAR VAR FOR VAR VAR ASSIGN VAR VAR VAR IF BIN_OP BIN_OP VAR VAR NUMBER NUMBER VAR VAR VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR FUNC_CALL VAR VAR NUMBER
Vasya has a string $s$ of length $n$. He decides to make the following modification to the string: Pick an integer $k$, ($1 \leq k \leq n$). For $i$ from $1$ to $n-k+1$, reverse the substring $s[i:i+k-1]$ of $s$. For example, if string $s$ is qwer and $k = 2$, below is the series of transformations the string goes through: qwer (original string) wqer (after reversing the first substring of length $2$) weqr (after reversing the second substring of length $2$) werq (after reversing the last substring of length $2$) Hence, the resulting string after modifying $s$ with $k = 2$ is werq. Vasya wants to choose a $k$ such that the string obtained after the above-mentioned modification is lexicographically smallest possible among all choices of $k$. Among all such $k$, he wants to choose the smallest one. Since he is busy attending Felicity 2020, he asks for your help. A string $a$ is lexicographically smaller than a string $b$ if and only if one of the following holds: $a$ is a prefix of $b$, but $a \ne b$; in the first position where $a$ and $b$ differ, the string $a$ has a letter that appears earlier in the alphabet than the corresponding letter in $b$. -----Input----- Each test contains multiple test cases. The first line contains the number of test cases $t$ ($1 \le t \le 5000$). The description of the test cases follows. The first line of each test case contains a single integer $n$ ($1 \le n \le 5000$)Β β€” the length of the string $s$. The second line of each test case contains the string $s$ of $n$ lowercase latin letters. It is guaranteed that the sum of $n$ over all test cases does not exceed $5000$. -----Output----- For each testcase output two lines: In the first line output the lexicographically smallest string $s'$ achievable after the above-mentioned modification. In the second line output the appropriate value of $k$ ($1 \leq k \leq n$) that you chose for performing the modification. If there are multiple values of $k$ that give the lexicographically smallest string, output the smallest value of $k$ among them. -----Example----- Input 6 4 abab 6 qwerty 5 aaaaa 6 alaska 9 lfpbavjsm 1 p Output abab 1 ertyqw 3 aaaaa 1 aksala 6 avjsmbpfl 5 p 1 -----Note----- In the first testcase of the first sample, the string modification results for the sample abab are as follows : for $k = 1$ : abab for $k = 2$ : baba for $k = 3$ : abab for $k = 4$ : baba The lexicographically smallest string achievable through modification is abab for $k = 1$ and $3$. Smallest value of $k$ needed to achieve is hence $1$.
for _ in range(int(input())): n = int(input()) s = input() ans = [] for i in range(1, n + 1): if i == n: temp = s[-1::-1] elif (n - i + 1) % 2 == 0: temp = s[i - 1 :] + s[: i - 1] else: temp = s[i - 1 :] + s[: i - 1][::-1] ans.append([temp, i]) m = ans[0][0] el = 1 for i in range(1, len(ans)): if ans[i][0] < m: m, el = ans[i] print("{}\n{}".format(m, el))
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER IF VAR VAR ASSIGN VAR VAR NUMBER NUMBER IF BIN_OP BIN_OP BIN_OP VAR VAR NUMBER NUMBER NUMBER ASSIGN VAR BIN_OP VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR LIST VAR VAR ASSIGN VAR VAR NUMBER NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR IF VAR VAR NUMBER VAR ASSIGN VAR VAR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL STRING VAR VAR
Vasya has a string $s$ of length $n$. He decides to make the following modification to the string: Pick an integer $k$, ($1 \leq k \leq n$). For $i$ from $1$ to $n-k+1$, reverse the substring $s[i:i+k-1]$ of $s$. For example, if string $s$ is qwer and $k = 2$, below is the series of transformations the string goes through: qwer (original string) wqer (after reversing the first substring of length $2$) weqr (after reversing the second substring of length $2$) werq (after reversing the last substring of length $2$) Hence, the resulting string after modifying $s$ with $k = 2$ is werq. Vasya wants to choose a $k$ such that the string obtained after the above-mentioned modification is lexicographically smallest possible among all choices of $k$. Among all such $k$, he wants to choose the smallest one. Since he is busy attending Felicity 2020, he asks for your help. A string $a$ is lexicographically smaller than a string $b$ if and only if one of the following holds: $a$ is a prefix of $b$, but $a \ne b$; in the first position where $a$ and $b$ differ, the string $a$ has a letter that appears earlier in the alphabet than the corresponding letter in $b$. -----Input----- Each test contains multiple test cases. The first line contains the number of test cases $t$ ($1 \le t \le 5000$). The description of the test cases follows. The first line of each test case contains a single integer $n$ ($1 \le n \le 5000$)Β β€” the length of the string $s$. The second line of each test case contains the string $s$ of $n$ lowercase latin letters. It is guaranteed that the sum of $n$ over all test cases does not exceed $5000$. -----Output----- For each testcase output two lines: In the first line output the lexicographically smallest string $s'$ achievable after the above-mentioned modification. In the second line output the appropriate value of $k$ ($1 \leq k \leq n$) that you chose for performing the modification. If there are multiple values of $k$ that give the lexicographically smallest string, output the smallest value of $k$ among them. -----Example----- Input 6 4 abab 6 qwerty 5 aaaaa 6 alaska 9 lfpbavjsm 1 p Output abab 1 ertyqw 3 aaaaa 1 aksala 6 avjsmbpfl 5 p 1 -----Note----- In the first testcase of the first sample, the string modification results for the sample abab are as follows : for $k = 1$ : abab for $k = 2$ : baba for $k = 3$ : abab for $k = 4$ : baba The lexicographically smallest string achievable through modification is abab for $k = 1$ and $3$. Smallest value of $k$ needed to achieve is hence $1$.
t = int(input()) for _ in range(t): l = int(input()) s = input() ans = s ansl = 1 for i in range(l): ns = s[i:] + s[:i][:: -1 if l - i & 1 else 1] if ns < ans: ans = ns ansl = i + 1 print(ans) print(ansl)
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 ASSIGN VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR VAR BIN_OP BIN_OP VAR VAR NUMBER NUMBER NUMBER IF VAR VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR
Vasya has a string $s$ of length $n$. He decides to make the following modification to the string: Pick an integer $k$, ($1 \leq k \leq n$). For $i$ from $1$ to $n-k+1$, reverse the substring $s[i:i+k-1]$ of $s$. For example, if string $s$ is qwer and $k = 2$, below is the series of transformations the string goes through: qwer (original string) wqer (after reversing the first substring of length $2$) weqr (after reversing the second substring of length $2$) werq (after reversing the last substring of length $2$) Hence, the resulting string after modifying $s$ with $k = 2$ is werq. Vasya wants to choose a $k$ such that the string obtained after the above-mentioned modification is lexicographically smallest possible among all choices of $k$. Among all such $k$, he wants to choose the smallest one. Since he is busy attending Felicity 2020, he asks for your help. A string $a$ is lexicographically smaller than a string $b$ if and only if one of the following holds: $a$ is a prefix of $b$, but $a \ne b$; in the first position where $a$ and $b$ differ, the string $a$ has a letter that appears earlier in the alphabet than the corresponding letter in $b$. -----Input----- Each test contains multiple test cases. The first line contains the number of test cases $t$ ($1 \le t \le 5000$). The description of the test cases follows. The first line of each test case contains a single integer $n$ ($1 \le n \le 5000$)Β β€” the length of the string $s$. The second line of each test case contains the string $s$ of $n$ lowercase latin letters. It is guaranteed that the sum of $n$ over all test cases does not exceed $5000$. -----Output----- For each testcase output two lines: In the first line output the lexicographically smallest string $s'$ achievable after the above-mentioned modification. In the second line output the appropriate value of $k$ ($1 \leq k \leq n$) that you chose for performing the modification. If there are multiple values of $k$ that give the lexicographically smallest string, output the smallest value of $k$ among them. -----Example----- Input 6 4 abab 6 qwerty 5 aaaaa 6 alaska 9 lfpbavjsm 1 p Output abab 1 ertyqw 3 aaaaa 1 aksala 6 avjsmbpfl 5 p 1 -----Note----- In the first testcase of the first sample, the string modification results for the sample abab are as follows : for $k = 1$ : abab for $k = 2$ : baba for $k = 3$ : abab for $k = 4$ : baba The lexicographically smallest string achievable through modification is abab for $k = 1$ and $3$. Smallest value of $k$ needed to achieve is hence $1$.
t = int(input()) for i in range(t): n = int(input()) s = input() ans = s val = 1 for k in range(1, n + 1): if k == n: tmp = s[::-1] else: ss = s[: k - 1] if n % 2 != k % 2: tmp = s[k - 1 :] + ss else: tmp = s[k - 1 :] + ss[::-1] if ans > tmp: ans = tmp val = k print(ans) print(val)
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 ASSIGN VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER IF VAR VAR ASSIGN VAR VAR NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER IF BIN_OP VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR BIN_OP VAR NUMBER VAR ASSIGN VAR BIN_OP VAR BIN_OP VAR NUMBER VAR NUMBER IF VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR
Vasya has a string $s$ of length $n$. He decides to make the following modification to the string: Pick an integer $k$, ($1 \leq k \leq n$). For $i$ from $1$ to $n-k+1$, reverse the substring $s[i:i+k-1]$ of $s$. For example, if string $s$ is qwer and $k = 2$, below is the series of transformations the string goes through: qwer (original string) wqer (after reversing the first substring of length $2$) weqr (after reversing the second substring of length $2$) werq (after reversing the last substring of length $2$) Hence, the resulting string after modifying $s$ with $k = 2$ is werq. Vasya wants to choose a $k$ such that the string obtained after the above-mentioned modification is lexicographically smallest possible among all choices of $k$. Among all such $k$, he wants to choose the smallest one. Since he is busy attending Felicity 2020, he asks for your help. A string $a$ is lexicographically smaller than a string $b$ if and only if one of the following holds: $a$ is a prefix of $b$, but $a \ne b$; in the first position where $a$ and $b$ differ, the string $a$ has a letter that appears earlier in the alphabet than the corresponding letter in $b$. -----Input----- Each test contains multiple test cases. The first line contains the number of test cases $t$ ($1 \le t \le 5000$). The description of the test cases follows. The first line of each test case contains a single integer $n$ ($1 \le n \le 5000$)Β β€” the length of the string $s$. The second line of each test case contains the string $s$ of $n$ lowercase latin letters. It is guaranteed that the sum of $n$ over all test cases does not exceed $5000$. -----Output----- For each testcase output two lines: In the first line output the lexicographically smallest string $s'$ achievable after the above-mentioned modification. In the second line output the appropriate value of $k$ ($1 \leq k \leq n$) that you chose for performing the modification. If there are multiple values of $k$ that give the lexicographically smallest string, output the smallest value of $k$ among them. -----Example----- Input 6 4 abab 6 qwerty 5 aaaaa 6 alaska 9 lfpbavjsm 1 p Output abab 1 ertyqw 3 aaaaa 1 aksala 6 avjsmbpfl 5 p 1 -----Note----- In the first testcase of the first sample, the string modification results for the sample abab are as follows : for $k = 1$ : abab for $k = 2$ : baba for $k = 3$ : abab for $k = 4$ : baba The lexicographically smallest string achievable through modification is abab for $k = 1$ and $3$. Smallest value of $k$ needed to achieve is hence $1$.
for _ in range(int(input())): n = int(input()) s = input() z = [] for i in range(n): if (n - i) % 2: z.append([s[i:] + s[:i][::-1], i]) else: z.append([s[i:] + s[:i], i]) z.sort() print(z[0][0]) print(z[0][1] + 1)
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR IF BIN_OP BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR LIST BIN_OP VAR VAR VAR VAR NUMBER VAR EXPR FUNC_CALL VAR LIST BIN_OP VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER
Vasya has a string $s$ of length $n$. He decides to make the following modification to the string: Pick an integer $k$, ($1 \leq k \leq n$). For $i$ from $1$ to $n-k+1$, reverse the substring $s[i:i+k-1]$ of $s$. For example, if string $s$ is qwer and $k = 2$, below is the series of transformations the string goes through: qwer (original string) wqer (after reversing the first substring of length $2$) weqr (after reversing the second substring of length $2$) werq (after reversing the last substring of length $2$) Hence, the resulting string after modifying $s$ with $k = 2$ is werq. Vasya wants to choose a $k$ such that the string obtained after the above-mentioned modification is lexicographically smallest possible among all choices of $k$. Among all such $k$, he wants to choose the smallest one. Since he is busy attending Felicity 2020, he asks for your help. A string $a$ is lexicographically smaller than a string $b$ if and only if one of the following holds: $a$ is a prefix of $b$, but $a \ne b$; in the first position where $a$ and $b$ differ, the string $a$ has a letter that appears earlier in the alphabet than the corresponding letter in $b$. -----Input----- Each test contains multiple test cases. The first line contains the number of test cases $t$ ($1 \le t \le 5000$). The description of the test cases follows. The first line of each test case contains a single integer $n$ ($1 \le n \le 5000$)Β β€” the length of the string $s$. The second line of each test case contains the string $s$ of $n$ lowercase latin letters. It is guaranteed that the sum of $n$ over all test cases does not exceed $5000$. -----Output----- For each testcase output two lines: In the first line output the lexicographically smallest string $s'$ achievable after the above-mentioned modification. In the second line output the appropriate value of $k$ ($1 \leq k \leq n$) that you chose for performing the modification. If there are multiple values of $k$ that give the lexicographically smallest string, output the smallest value of $k$ among them. -----Example----- Input 6 4 abab 6 qwerty 5 aaaaa 6 alaska 9 lfpbavjsm 1 p Output abab 1 ertyqw 3 aaaaa 1 aksala 6 avjsmbpfl 5 p 1 -----Note----- In the first testcase of the first sample, the string modification results for the sample abab are as follows : for $k = 1$ : abab for $k = 2$ : baba for $k = 3$ : abab for $k = 4$ : baba The lexicographically smallest string achievable through modification is abab for $k = 1$ and $3$. Smallest value of $k$ needed to achieve is hence $1$.
for _ in range(int(input())): Map = {} n = int(input()) s = list(input()) Map["".join(s)] = 1 for k in range(1, n): st = "".join(s[k:]) if (n - k) % 2 == 0: st += "".join(s[:k]) else: rev = s[:k] rev.reverse() st += "".join(rev) if st in Map: Map[st] = min(k + 1, Map[st]) else: Map[st] = k + 1 ans = "".join(s) k = 1 for i in Map: if i < ans: ans = i k = Map[i] elif i == ans: if Map[i] < k: k = Map[i] print(ans) print(k)
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR DICT ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL STRING VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR FUNC_CALL STRING VAR VAR IF BIN_OP BIN_OP VAR VAR NUMBER NUMBER VAR FUNC_CALL STRING VAR VAR ASSIGN VAR VAR VAR EXPR FUNC_CALL VAR VAR FUNC_CALL STRING VAR IF VAR VAR ASSIGN VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL STRING VAR ASSIGN VAR NUMBER FOR VAR VAR IF VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR VAR IF VAR VAR IF VAR VAR VAR ASSIGN VAR VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR
Vasya has a string $s$ of length $n$. He decides to make the following modification to the string: Pick an integer $k$, ($1 \leq k \leq n$). For $i$ from $1$ to $n-k+1$, reverse the substring $s[i:i+k-1]$ of $s$. For example, if string $s$ is qwer and $k = 2$, below is the series of transformations the string goes through: qwer (original string) wqer (after reversing the first substring of length $2$) weqr (after reversing the second substring of length $2$) werq (after reversing the last substring of length $2$) Hence, the resulting string after modifying $s$ with $k = 2$ is werq. Vasya wants to choose a $k$ such that the string obtained after the above-mentioned modification is lexicographically smallest possible among all choices of $k$. Among all such $k$, he wants to choose the smallest one. Since he is busy attending Felicity 2020, he asks for your help. A string $a$ is lexicographically smaller than a string $b$ if and only if one of the following holds: $a$ is a prefix of $b$, but $a \ne b$; in the first position where $a$ and $b$ differ, the string $a$ has a letter that appears earlier in the alphabet than the corresponding letter in $b$. -----Input----- Each test contains multiple test cases. The first line contains the number of test cases $t$ ($1 \le t \le 5000$). The description of the test cases follows. The first line of each test case contains a single integer $n$ ($1 \le n \le 5000$)Β β€” the length of the string $s$. The second line of each test case contains the string $s$ of $n$ lowercase latin letters. It is guaranteed that the sum of $n$ over all test cases does not exceed $5000$. -----Output----- For each testcase output two lines: In the first line output the lexicographically smallest string $s'$ achievable after the above-mentioned modification. In the second line output the appropriate value of $k$ ($1 \leq k \leq n$) that you chose for performing the modification. If there are multiple values of $k$ that give the lexicographically smallest string, output the smallest value of $k$ among them. -----Example----- Input 6 4 abab 6 qwerty 5 aaaaa 6 alaska 9 lfpbavjsm 1 p Output abab 1 ertyqw 3 aaaaa 1 aksala 6 avjsmbpfl 5 p 1 -----Note----- In the first testcase of the first sample, the string modification results for the sample abab are as follows : for $k = 1$ : abab for $k = 2$ : baba for $k = 3$ : abab for $k = 4$ : baba The lexicographically smallest string achievable through modification is abab for $k = 1$ and $3$. Smallest value of $k$ needed to achieve is hence $1$.
for _ in range(int(input())): n = int(input()) s = input() k = 1 z = "" a = [s] b = [] while k < n: z = s[k:n] if n % 2 == k % 2: z += s[0:k] else: z += s[0:k][::-1] a.append(z) b.append(k) k += 1 print(min(a)) try: print(b[a.index(min(a))]) except IndexError: print(n)
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR STRING ASSIGN VAR LIST VAR ASSIGN VAR LIST WHILE VAR VAR ASSIGN VAR VAR VAR VAR IF BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR VAR NUMBER VAR VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR
Vasya has a string $s$ of length $n$. He decides to make the following modification to the string: Pick an integer $k$, ($1 \leq k \leq n$). For $i$ from $1$ to $n-k+1$, reverse the substring $s[i:i+k-1]$ of $s$. For example, if string $s$ is qwer and $k = 2$, below is the series of transformations the string goes through: qwer (original string) wqer (after reversing the first substring of length $2$) weqr (after reversing the second substring of length $2$) werq (after reversing the last substring of length $2$) Hence, the resulting string after modifying $s$ with $k = 2$ is werq. Vasya wants to choose a $k$ such that the string obtained after the above-mentioned modification is lexicographically smallest possible among all choices of $k$. Among all such $k$, he wants to choose the smallest one. Since he is busy attending Felicity 2020, he asks for your help. A string $a$ is lexicographically smaller than a string $b$ if and only if one of the following holds: $a$ is a prefix of $b$, but $a \ne b$; in the first position where $a$ and $b$ differ, the string $a$ has a letter that appears earlier in the alphabet than the corresponding letter in $b$. -----Input----- Each test contains multiple test cases. The first line contains the number of test cases $t$ ($1 \le t \le 5000$). The description of the test cases follows. The first line of each test case contains a single integer $n$ ($1 \le n \le 5000$)Β β€” the length of the string $s$. The second line of each test case contains the string $s$ of $n$ lowercase latin letters. It is guaranteed that the sum of $n$ over all test cases does not exceed $5000$. -----Output----- For each testcase output two lines: In the first line output the lexicographically smallest string $s'$ achievable after the above-mentioned modification. In the second line output the appropriate value of $k$ ($1 \leq k \leq n$) that you chose for performing the modification. If there are multiple values of $k$ that give the lexicographically smallest string, output the smallest value of $k$ among them. -----Example----- Input 6 4 abab 6 qwerty 5 aaaaa 6 alaska 9 lfpbavjsm 1 p Output abab 1 ertyqw 3 aaaaa 1 aksala 6 avjsmbpfl 5 p 1 -----Note----- In the first testcase of the first sample, the string modification results for the sample abab are as follows : for $k = 1$ : abab for $k = 2$ : baba for $k = 3$ : abab for $k = 4$ : baba The lexicographically smallest string achievable through modification is abab for $k = 1$ and $3$. Smallest value of $k$ needed to achieve is hence $1$.
for _ in range(int(input())): a = input() s = input() lex = ["z"] lexmax = 1 lent = len(s) for i in range(1, len(s) + 1): if (lent - i + 1) % 2 == 0: u = s[: i - 1] else: u = s[: i - 1][::-1] temp = s[i - 1 :] + u lex.append(temp) if i == 1: lexmax = i elif lex[lexmax] > lex[i]: lexmax = i print(lex[lexmax]) print(lexmax)
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR LIST STRING ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER IF BIN_OP BIN_OP BIN_OP VAR VAR NUMBER NUMBER NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR VAR IF VAR NUMBER ASSIGN VAR VAR IF VAR VAR VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR
Vasya has a string $s$ of length $n$. He decides to make the following modification to the string: Pick an integer $k$, ($1 \leq k \leq n$). For $i$ from $1$ to $n-k+1$, reverse the substring $s[i:i+k-1]$ of $s$. For example, if string $s$ is qwer and $k = 2$, below is the series of transformations the string goes through: qwer (original string) wqer (after reversing the first substring of length $2$) weqr (after reversing the second substring of length $2$) werq (after reversing the last substring of length $2$) Hence, the resulting string after modifying $s$ with $k = 2$ is werq. Vasya wants to choose a $k$ such that the string obtained after the above-mentioned modification is lexicographically smallest possible among all choices of $k$. Among all such $k$, he wants to choose the smallest one. Since he is busy attending Felicity 2020, he asks for your help. A string $a$ is lexicographically smaller than a string $b$ if and only if one of the following holds: $a$ is a prefix of $b$, but $a \ne b$; in the first position where $a$ and $b$ differ, the string $a$ has a letter that appears earlier in the alphabet than the corresponding letter in $b$. -----Input----- Each test contains multiple test cases. The first line contains the number of test cases $t$ ($1 \le t \le 5000$). The description of the test cases follows. The first line of each test case contains a single integer $n$ ($1 \le n \le 5000$)Β β€” the length of the string $s$. The second line of each test case contains the string $s$ of $n$ lowercase latin letters. It is guaranteed that the sum of $n$ over all test cases does not exceed $5000$. -----Output----- For each testcase output two lines: In the first line output the lexicographically smallest string $s'$ achievable after the above-mentioned modification. In the second line output the appropriate value of $k$ ($1 \leq k \leq n$) that you chose for performing the modification. If there are multiple values of $k$ that give the lexicographically smallest string, output the smallest value of $k$ among them. -----Example----- Input 6 4 abab 6 qwerty 5 aaaaa 6 alaska 9 lfpbavjsm 1 p Output abab 1 ertyqw 3 aaaaa 1 aksala 6 avjsmbpfl 5 p 1 -----Note----- In the first testcase of the first sample, the string modification results for the sample abab are as follows : for $k = 1$ : abab for $k = 2$ : baba for $k = 3$ : abab for $k = 4$ : baba The lexicographically smallest string achievable through modification is abab for $k = 1$ and $3$. Smallest value of $k$ needed to achieve is hence $1$.
def maximizescore(x, y): return min(sum(x), y) def modifystring(x): y = min(x) z = "{" l = 1 for i in range(len(x)): if x[i] == y: if (len(x) - i) % 2 == 0: if z > x[i:] + x[:i]: z = x[i:] + x[:i] l = i + 1 elif z > x[i:] + x[:i][::-1]: z = x[i:] + x[:i][::-1] l = i + 1 return z, l cases = int(input()) for _ in range(cases): l = int(input()) x = input() print(*modifystring(x), sep="\n")
FUNC_DEF RETURN FUNC_CALL VAR FUNC_CALL VAR VAR VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR STRING ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR VAR IF BIN_OP BIN_OP FUNC_CALL VAR VAR VAR NUMBER NUMBER IF VAR BIN_OP VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER IF VAR BIN_OP VAR VAR VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER RETURN VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR STRING
Vasya has a string $s$ of length $n$. He decides to make the following modification to the string: Pick an integer $k$, ($1 \leq k \leq n$). For $i$ from $1$ to $n-k+1$, reverse the substring $s[i:i+k-1]$ of $s$. For example, if string $s$ is qwer and $k = 2$, below is the series of transformations the string goes through: qwer (original string) wqer (after reversing the first substring of length $2$) weqr (after reversing the second substring of length $2$) werq (after reversing the last substring of length $2$) Hence, the resulting string after modifying $s$ with $k = 2$ is werq. Vasya wants to choose a $k$ such that the string obtained after the above-mentioned modification is lexicographically smallest possible among all choices of $k$. Among all such $k$, he wants to choose the smallest one. Since he is busy attending Felicity 2020, he asks for your help. A string $a$ is lexicographically smaller than a string $b$ if and only if one of the following holds: $a$ is a prefix of $b$, but $a \ne b$; in the first position where $a$ and $b$ differ, the string $a$ has a letter that appears earlier in the alphabet than the corresponding letter in $b$. -----Input----- Each test contains multiple test cases. The first line contains the number of test cases $t$ ($1 \le t \le 5000$). The description of the test cases follows. The first line of each test case contains a single integer $n$ ($1 \le n \le 5000$)Β β€” the length of the string $s$. The second line of each test case contains the string $s$ of $n$ lowercase latin letters. It is guaranteed that the sum of $n$ over all test cases does not exceed $5000$. -----Output----- For each testcase output two lines: In the first line output the lexicographically smallest string $s'$ achievable after the above-mentioned modification. In the second line output the appropriate value of $k$ ($1 \leq k \leq n$) that you chose for performing the modification. If there are multiple values of $k$ that give the lexicographically smallest string, output the smallest value of $k$ among them. -----Example----- Input 6 4 abab 6 qwerty 5 aaaaa 6 alaska 9 lfpbavjsm 1 p Output abab 1 ertyqw 3 aaaaa 1 aksala 6 avjsmbpfl 5 p 1 -----Note----- In the first testcase of the first sample, the string modification results for the sample abab are as follows : for $k = 1$ : abab for $k = 2$ : baba for $k = 3$ : abab for $k = 4$ : baba The lexicographically smallest string achievable through modification is abab for $k = 1$ and $3$. Smallest value of $k$ needed to achieve is hence $1$.
import sys input = sys.stdin.readline t = int(input()) for _ in range(t): n = int(input()) s = input().rstrip() words = [] for i in range(n): if (n - i) % 2 == 0: words.append((s[i:] + s[:i], i + 1)) else: words.append((s[i:] + s[:i][::-1], i + 1)) words.sort() print(words[0][0]) print(words[0][1])
IMPORT ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR IF BIN_OP BIN_OP VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR NUMBER BIN_OP VAR NUMBER EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR NUMBER NUMBER
Vasya has a string $s$ of length $n$. He decides to make the following modification to the string: Pick an integer $k$, ($1 \leq k \leq n$). For $i$ from $1$ to $n-k+1$, reverse the substring $s[i:i+k-1]$ of $s$. For example, if string $s$ is qwer and $k = 2$, below is the series of transformations the string goes through: qwer (original string) wqer (after reversing the first substring of length $2$) weqr (after reversing the second substring of length $2$) werq (after reversing the last substring of length $2$) Hence, the resulting string after modifying $s$ with $k = 2$ is werq. Vasya wants to choose a $k$ such that the string obtained after the above-mentioned modification is lexicographically smallest possible among all choices of $k$. Among all such $k$, he wants to choose the smallest one. Since he is busy attending Felicity 2020, he asks for your help. A string $a$ is lexicographically smaller than a string $b$ if and only if one of the following holds: $a$ is a prefix of $b$, but $a \ne b$; in the first position where $a$ and $b$ differ, the string $a$ has a letter that appears earlier in the alphabet than the corresponding letter in $b$. -----Input----- Each test contains multiple test cases. The first line contains the number of test cases $t$ ($1 \le t \le 5000$). The description of the test cases follows. The first line of each test case contains a single integer $n$ ($1 \le n \le 5000$)Β β€” the length of the string $s$. The second line of each test case contains the string $s$ of $n$ lowercase latin letters. It is guaranteed that the sum of $n$ over all test cases does not exceed $5000$. -----Output----- For each testcase output two lines: In the first line output the lexicographically smallest string $s'$ achievable after the above-mentioned modification. In the second line output the appropriate value of $k$ ($1 \leq k \leq n$) that you chose for performing the modification. If there are multiple values of $k$ that give the lexicographically smallest string, output the smallest value of $k$ among them. -----Example----- Input 6 4 abab 6 qwerty 5 aaaaa 6 alaska 9 lfpbavjsm 1 p Output abab 1 ertyqw 3 aaaaa 1 aksala 6 avjsmbpfl 5 p 1 -----Note----- In the first testcase of the first sample, the string modification results for the sample abab are as follows : for $k = 1$ : abab for $k = 2$ : baba for $k = 3$ : abab for $k = 4$ : baba The lexicographically smallest string achievable through modification is abab for $k = 1$ and $3$. Smallest value of $k$ needed to achieve is hence $1$.
t = int(input()) for _ in range(t): n = int(input()) s = input() ans = [(s, 1), (s[::-1], n)] for i in range(2, n): if (n - i + 1) % 2: x = "".join((s[i - 1 :], s[i - 2 :: -1])) ans.append((x, i)) else: x = "".join((s[i - 1 :], s[: i - 1])) ans.append((x, i)) ans.sort() print(*ans[0], sep="\n")
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 ASSIGN VAR LIST VAR NUMBER VAR NUMBER VAR FOR VAR FUNC_CALL VAR NUMBER VAR IF BIN_OP BIN_OP BIN_OP VAR VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL STRING VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL STRING VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR NUMBER STRING
Vasya has a string $s$ of length $n$. He decides to make the following modification to the string: Pick an integer $k$, ($1 \leq k \leq n$). For $i$ from $1$ to $n-k+1$, reverse the substring $s[i:i+k-1]$ of $s$. For example, if string $s$ is qwer and $k = 2$, below is the series of transformations the string goes through: qwer (original string) wqer (after reversing the first substring of length $2$) weqr (after reversing the second substring of length $2$) werq (after reversing the last substring of length $2$) Hence, the resulting string after modifying $s$ with $k = 2$ is werq. Vasya wants to choose a $k$ such that the string obtained after the above-mentioned modification is lexicographically smallest possible among all choices of $k$. Among all such $k$, he wants to choose the smallest one. Since he is busy attending Felicity 2020, he asks for your help. A string $a$ is lexicographically smaller than a string $b$ if and only if one of the following holds: $a$ is a prefix of $b$, but $a \ne b$; in the first position where $a$ and $b$ differ, the string $a$ has a letter that appears earlier in the alphabet than the corresponding letter in $b$. -----Input----- Each test contains multiple test cases. The first line contains the number of test cases $t$ ($1 \le t \le 5000$). The description of the test cases follows. The first line of each test case contains a single integer $n$ ($1 \le n \le 5000$)Β β€” the length of the string $s$. The second line of each test case contains the string $s$ of $n$ lowercase latin letters. It is guaranteed that the sum of $n$ over all test cases does not exceed $5000$. -----Output----- For each testcase output two lines: In the first line output the lexicographically smallest string $s'$ achievable after the above-mentioned modification. In the second line output the appropriate value of $k$ ($1 \leq k \leq n$) that you chose for performing the modification. If there are multiple values of $k$ that give the lexicographically smallest string, output the smallest value of $k$ among them. -----Example----- Input 6 4 abab 6 qwerty 5 aaaaa 6 alaska 9 lfpbavjsm 1 p Output abab 1 ertyqw 3 aaaaa 1 aksala 6 avjsmbpfl 5 p 1 -----Note----- In the first testcase of the first sample, the string modification results for the sample abab are as follows : for $k = 1$ : abab for $k = 2$ : baba for $k = 3$ : abab for $k = 4$ : baba The lexicographically smallest string achievable through modification is abab for $k = 1$ and $3$. Smallest value of $k$ needed to achieve is hence $1$.
t = int(input()) for j in range(t): n = int(input()) a = input() g = a b = 0 for i in range(n): d = a[:i] if (n - i) % 2 == 0: c = a[i:] + d else: c = a[i:] + d[::-1] if c < g: g = c b = i + 1 print(g) if b == 0: print(1) else: print(b)
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 ASSIGN VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR IF BIN_OP BIN_OP VAR VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR NUMBER IF VAR VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR VAR
Vasya has a string $s$ of length $n$. He decides to make the following modification to the string: Pick an integer $k$, ($1 \leq k \leq n$). For $i$ from $1$ to $n-k+1$, reverse the substring $s[i:i+k-1]$ of $s$. For example, if string $s$ is qwer and $k = 2$, below is the series of transformations the string goes through: qwer (original string) wqer (after reversing the first substring of length $2$) weqr (after reversing the second substring of length $2$) werq (after reversing the last substring of length $2$) Hence, the resulting string after modifying $s$ with $k = 2$ is werq. Vasya wants to choose a $k$ such that the string obtained after the above-mentioned modification is lexicographically smallest possible among all choices of $k$. Among all such $k$, he wants to choose the smallest one. Since he is busy attending Felicity 2020, he asks for your help. A string $a$ is lexicographically smaller than a string $b$ if and only if one of the following holds: $a$ is a prefix of $b$, but $a \ne b$; in the first position where $a$ and $b$ differ, the string $a$ has a letter that appears earlier in the alphabet than the corresponding letter in $b$. -----Input----- Each test contains multiple test cases. The first line contains the number of test cases $t$ ($1 \le t \le 5000$). The description of the test cases follows. The first line of each test case contains a single integer $n$ ($1 \le n \le 5000$)Β β€” the length of the string $s$. The second line of each test case contains the string $s$ of $n$ lowercase latin letters. It is guaranteed that the sum of $n$ over all test cases does not exceed $5000$. -----Output----- For each testcase output two lines: In the first line output the lexicographically smallest string $s'$ achievable after the above-mentioned modification. In the second line output the appropriate value of $k$ ($1 \leq k \leq n$) that you chose for performing the modification. If there are multiple values of $k$ that give the lexicographically smallest string, output the smallest value of $k$ among them. -----Example----- Input 6 4 abab 6 qwerty 5 aaaaa 6 alaska 9 lfpbavjsm 1 p Output abab 1 ertyqw 3 aaaaa 1 aksala 6 avjsmbpfl 5 p 1 -----Note----- In the first testcase of the first sample, the string modification results for the sample abab are as follows : for $k = 1$ : abab for $k = 2$ : baba for $k = 3$ : abab for $k = 4$ : baba The lexicographically smallest string achievable through modification is abab for $k = 1$ and $3$. Smallest value of $k$ needed to achieve is hence $1$.
t = int(input()) for i in range(t): le = int(input()) s = list(input()) mg = s[:] i1 = 1 co = 0 for k in range(le): if le % 2 != 0: if k % 2 == 0: l = s[0:k] r = s[k:le] l.reverse() ne = r + l if ne < mg: mg = ne i1 = k + 1 else: l = s[0:k] r = s[k:le] ne = r + l if ne < mg: mg = ne i1 = k + 1 elif k % 2 != 0: l = s[0:k] r = s[k:le] l.reverse() ne = r + l if ne < mg: mg = ne i1 = k + 1 else: l = s[0:k] r = s[k:le] ne = r + l if ne < mg: mg = ne i1 = k + 1 print("".join(mg)) print(i1)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF BIN_OP VAR NUMBER NUMBER IF BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR NUMBER VAR ASSIGN VAR VAR VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR BIN_OP VAR VAR IF VAR VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR VAR NUMBER VAR ASSIGN VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR IF VAR VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR NUMBER IF BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR NUMBER VAR ASSIGN VAR VAR VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR BIN_OP VAR VAR IF VAR VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR VAR NUMBER VAR ASSIGN VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR IF VAR VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL STRING VAR EXPR FUNC_CALL VAR VAR
Vasya has a string $s$ of length $n$. He decides to make the following modification to the string: Pick an integer $k$, ($1 \leq k \leq n$). For $i$ from $1$ to $n-k+1$, reverse the substring $s[i:i+k-1]$ of $s$. For example, if string $s$ is qwer and $k = 2$, below is the series of transformations the string goes through: qwer (original string) wqer (after reversing the first substring of length $2$) weqr (after reversing the second substring of length $2$) werq (after reversing the last substring of length $2$) Hence, the resulting string after modifying $s$ with $k = 2$ is werq. Vasya wants to choose a $k$ such that the string obtained after the above-mentioned modification is lexicographically smallest possible among all choices of $k$. Among all such $k$, he wants to choose the smallest one. Since he is busy attending Felicity 2020, he asks for your help. A string $a$ is lexicographically smaller than a string $b$ if and only if one of the following holds: $a$ is a prefix of $b$, but $a \ne b$; in the first position where $a$ and $b$ differ, the string $a$ has a letter that appears earlier in the alphabet than the corresponding letter in $b$. -----Input----- Each test contains multiple test cases. The first line contains the number of test cases $t$ ($1 \le t \le 5000$). The description of the test cases follows. The first line of each test case contains a single integer $n$ ($1 \le n \le 5000$)Β β€” the length of the string $s$. The second line of each test case contains the string $s$ of $n$ lowercase latin letters. It is guaranteed that the sum of $n$ over all test cases does not exceed $5000$. -----Output----- For each testcase output two lines: In the first line output the lexicographically smallest string $s'$ achievable after the above-mentioned modification. In the second line output the appropriate value of $k$ ($1 \leq k \leq n$) that you chose for performing the modification. If there are multiple values of $k$ that give the lexicographically smallest string, output the smallest value of $k$ among them. -----Example----- Input 6 4 abab 6 qwerty 5 aaaaa 6 alaska 9 lfpbavjsm 1 p Output abab 1 ertyqw 3 aaaaa 1 aksala 6 avjsmbpfl 5 p 1 -----Note----- In the first testcase of the first sample, the string modification results for the sample abab are as follows : for $k = 1$ : abab for $k = 2$ : baba for $k = 3$ : abab for $k = 4$ : baba The lexicographically smallest string achievable through modification is abab for $k = 1$ and $3$. Smallest value of $k$ needed to achieve is hence $1$.
from sys import stdin def inp(): return next(ss.rstrip() for ss in stdin) def itg(): return int(inp()) def mpint(): return map(int, inp().split()) for case in range(itg()): n = itg() s = inp() min_char = min(s) kk = [(i + 1) for i in range(n) if s[i] == min_char] results = [] for k in kk: reverse = n - k + 1 & 1 if not reverse: results.append((s[k - 1 :] + s[: k - 1], k)) else: results.append((s[k - 1 :] + s[: k - 1][::-1], k)) ans = min(results) print(ans[0]) print(ans[1])
FUNC_DEF RETURN FUNC_CALL VAR FUNC_CALL VAR VAR VAR FUNC_DEF RETURN FUNC_CALL VAR FUNC_CALL VAR FUNC_DEF RETURN FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR VAR VAR VAR VAR ASSIGN VAR LIST FOR VAR VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR VAR NUMBER NUMBER IF VAR EXPR FUNC_CALL VAR BIN_OP VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR BIN_OP VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER
Vasya has a string $s$ of length $n$. He decides to make the following modification to the string: Pick an integer $k$, ($1 \leq k \leq n$). For $i$ from $1$ to $n-k+1$, reverse the substring $s[i:i+k-1]$ of $s$. For example, if string $s$ is qwer and $k = 2$, below is the series of transformations the string goes through: qwer (original string) wqer (after reversing the first substring of length $2$) weqr (after reversing the second substring of length $2$) werq (after reversing the last substring of length $2$) Hence, the resulting string after modifying $s$ with $k = 2$ is werq. Vasya wants to choose a $k$ such that the string obtained after the above-mentioned modification is lexicographically smallest possible among all choices of $k$. Among all such $k$, he wants to choose the smallest one. Since he is busy attending Felicity 2020, he asks for your help. A string $a$ is lexicographically smaller than a string $b$ if and only if one of the following holds: $a$ is a prefix of $b$, but $a \ne b$; in the first position where $a$ and $b$ differ, the string $a$ has a letter that appears earlier in the alphabet than the corresponding letter in $b$. -----Input----- Each test contains multiple test cases. The first line contains the number of test cases $t$ ($1 \le t \le 5000$). The description of the test cases follows. The first line of each test case contains a single integer $n$ ($1 \le n \le 5000$)Β β€” the length of the string $s$. The second line of each test case contains the string $s$ of $n$ lowercase latin letters. It is guaranteed that the sum of $n$ over all test cases does not exceed $5000$. -----Output----- For each testcase output two lines: In the first line output the lexicographically smallest string $s'$ achievable after the above-mentioned modification. In the second line output the appropriate value of $k$ ($1 \leq k \leq n$) that you chose for performing the modification. If there are multiple values of $k$ that give the lexicographically smallest string, output the smallest value of $k$ among them. -----Example----- Input 6 4 abab 6 qwerty 5 aaaaa 6 alaska 9 lfpbavjsm 1 p Output abab 1 ertyqw 3 aaaaa 1 aksala 6 avjsmbpfl 5 p 1 -----Note----- In the first testcase of the first sample, the string modification results for the sample abab are as follows : for $k = 1$ : abab for $k = 2$ : baba for $k = 3$ : abab for $k = 4$ : baba The lexicographically smallest string achievable through modification is abab for $k = 1$ and $3$. Smallest value of $k$ needed to achieve is hence $1$.
import sys inp = sys.stdin.readline def input(): return inp().strip() def iin(): return int(input()) def lin(): return list(map(int, input().split())) def main(): T = iin() while T: T -= 1 n = iin() s = list(input()) ans = [] for i in range(1, n + 1): x = i - 1 y = n - x ans.append(["".join(s[x:] + (s[:x] if y % 2 == 0 else s[:x][::-1])), i]) ans.sort() print(*ans[0], sep="\n") main()
IMPORT ASSIGN VAR VAR FUNC_DEF RETURN FUNC_CALL FUNC_CALL 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 WHILE VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR LIST FUNC_CALL STRING BIN_OP VAR VAR BIN_OP VAR NUMBER NUMBER VAR VAR VAR VAR NUMBER VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR NUMBER STRING EXPR FUNC_CALL VAR
Vasya has a string $s$ of length $n$. He decides to make the following modification to the string: Pick an integer $k$, ($1 \leq k \leq n$). For $i$ from $1$ to $n-k+1$, reverse the substring $s[i:i+k-1]$ of $s$. For example, if string $s$ is qwer and $k = 2$, below is the series of transformations the string goes through: qwer (original string) wqer (after reversing the first substring of length $2$) weqr (after reversing the second substring of length $2$) werq (after reversing the last substring of length $2$) Hence, the resulting string after modifying $s$ with $k = 2$ is werq. Vasya wants to choose a $k$ such that the string obtained after the above-mentioned modification is lexicographically smallest possible among all choices of $k$. Among all such $k$, he wants to choose the smallest one. Since he is busy attending Felicity 2020, he asks for your help. A string $a$ is lexicographically smaller than a string $b$ if and only if one of the following holds: $a$ is a prefix of $b$, but $a \ne b$; in the first position where $a$ and $b$ differ, the string $a$ has a letter that appears earlier in the alphabet than the corresponding letter in $b$. -----Input----- Each test contains multiple test cases. The first line contains the number of test cases $t$ ($1 \le t \le 5000$). The description of the test cases follows. The first line of each test case contains a single integer $n$ ($1 \le n \le 5000$)Β β€” the length of the string $s$. The second line of each test case contains the string $s$ of $n$ lowercase latin letters. It is guaranteed that the sum of $n$ over all test cases does not exceed $5000$. -----Output----- For each testcase output two lines: In the first line output the lexicographically smallest string $s'$ achievable after the above-mentioned modification. In the second line output the appropriate value of $k$ ($1 \leq k \leq n$) that you chose for performing the modification. If there are multiple values of $k$ that give the lexicographically smallest string, output the smallest value of $k$ among them. -----Example----- Input 6 4 abab 6 qwerty 5 aaaaa 6 alaska 9 lfpbavjsm 1 p Output abab 1 ertyqw 3 aaaaa 1 aksala 6 avjsmbpfl 5 p 1 -----Note----- In the first testcase of the first sample, the string modification results for the sample abab are as follows : for $k = 1$ : abab for $k = 2$ : baba for $k = 3$ : abab for $k = 4$ : baba The lexicographically smallest string achievable through modification is abab for $k = 1$ and $3$. Smallest value of $k$ needed to achieve is hence $1$.
from sys import stdin, stdout Pi = lambda x: stdout.write(str(x) + "\n") S = lambda x: x * (x + 1) // 2 R = lambda: stdin.readline() Ri = lambda x: map(int, x.split()) def main(): t = int(R()) for i in range(t): n = int(R()) s = R().strip("\n") v = [0] * 26 for c in s: v[ord(c) - 97] += 1 for i in range(26): if v[i]: if v[i] == n: Pi(s) Pi(1) else: ans = -1 c = chr(i + 97) tmp1, tmp2 = s, "" if c == s[0]: ans = 1 for i in range(n): if c == s[i] and i + 1 != n: dif = n - i if dif % 2 == 0: tmp2 = s[i:] + s[:i] else: cad = s[:i] tmp2 = s[i:] + cad[::-1] if tmp2 < tmp1: tmp1 = tmp2 ans = i + 1 elif c == s[i]: tmp2 = s[::-1] if tmp2 < tmp1: tmp1 = tmp2 ans = n Pi(tmp1) Pi(ans) break main()
ASSIGN VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR STRING ASSIGN VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR BIN_OP LIST NUMBER NUMBER FOR VAR VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER IF VAR VAR IF VAR VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR STRING IF VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR BIN_OP VAR NUMBER VAR ASSIGN VAR BIN_OP VAR VAR IF BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR NUMBER IF VAR VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR NUMBER IF VAR VAR VAR ASSIGN VAR VAR NUMBER IF VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR
Vasya has a string $s$ of length $n$. He decides to make the following modification to the string: Pick an integer $k$, ($1 \leq k \leq n$). For $i$ from $1$ to $n-k+1$, reverse the substring $s[i:i+k-1]$ of $s$. For example, if string $s$ is qwer and $k = 2$, below is the series of transformations the string goes through: qwer (original string) wqer (after reversing the first substring of length $2$) weqr (after reversing the second substring of length $2$) werq (after reversing the last substring of length $2$) Hence, the resulting string after modifying $s$ with $k = 2$ is werq. Vasya wants to choose a $k$ such that the string obtained after the above-mentioned modification is lexicographically smallest possible among all choices of $k$. Among all such $k$, he wants to choose the smallest one. Since he is busy attending Felicity 2020, he asks for your help. A string $a$ is lexicographically smaller than a string $b$ if and only if one of the following holds: $a$ is a prefix of $b$, but $a \ne b$; in the first position where $a$ and $b$ differ, the string $a$ has a letter that appears earlier in the alphabet than the corresponding letter in $b$. -----Input----- Each test contains multiple test cases. The first line contains the number of test cases $t$ ($1 \le t \le 5000$). The description of the test cases follows. The first line of each test case contains a single integer $n$ ($1 \le n \le 5000$)Β β€” the length of the string $s$. The second line of each test case contains the string $s$ of $n$ lowercase latin letters. It is guaranteed that the sum of $n$ over all test cases does not exceed $5000$. -----Output----- For each testcase output two lines: In the first line output the lexicographically smallest string $s'$ achievable after the above-mentioned modification. In the second line output the appropriate value of $k$ ($1 \leq k \leq n$) that you chose for performing the modification. If there are multiple values of $k$ that give the lexicographically smallest string, output the smallest value of $k$ among them. -----Example----- Input 6 4 abab 6 qwerty 5 aaaaa 6 alaska 9 lfpbavjsm 1 p Output abab 1 ertyqw 3 aaaaa 1 aksala 6 avjsmbpfl 5 p 1 -----Note----- In the first testcase of the first sample, the string modification results for the sample abab are as follows : for $k = 1$ : abab for $k = 2$ : baba for $k = 3$ : abab for $k = 4$ : baba The lexicographically smallest string achievable through modification is abab for $k = 1$ and $3$. Smallest value of $k$ needed to achieve is hence $1$.
t = int(input()) for i in range(t): n = int(input()) s = input() ans = "" ansk = -1 for k in range(1, n + 1): hilu = s[: k - 1] if n % 2 == 0 and k % 2 == 0: hilu = hilu[::-1] if n % 2 != 0 and k % 2 != 0: hilu = hilu[::-1] yo = s[k - 1 :] + hilu if ans == "": ans = yo ansk = k elif yo < ans: ans = yo ansk = k print(ans) print(ansk)
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 ASSIGN VAR STRING ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER IF BIN_OP VAR NUMBER NUMBER BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR NUMBER IF BIN_OP VAR NUMBER NUMBER BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR BIN_OP VAR BIN_OP VAR NUMBER VAR IF VAR STRING ASSIGN VAR VAR ASSIGN VAR VAR IF VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR
Vasya has a string $s$ of length $n$. He decides to make the following modification to the string: Pick an integer $k$, ($1 \leq k \leq n$). For $i$ from $1$ to $n-k+1$, reverse the substring $s[i:i+k-1]$ of $s$. For example, if string $s$ is qwer and $k = 2$, below is the series of transformations the string goes through: qwer (original string) wqer (after reversing the first substring of length $2$) weqr (after reversing the second substring of length $2$) werq (after reversing the last substring of length $2$) Hence, the resulting string after modifying $s$ with $k = 2$ is werq. Vasya wants to choose a $k$ such that the string obtained after the above-mentioned modification is lexicographically smallest possible among all choices of $k$. Among all such $k$, he wants to choose the smallest one. Since he is busy attending Felicity 2020, he asks for your help. A string $a$ is lexicographically smaller than a string $b$ if and only if one of the following holds: $a$ is a prefix of $b$, but $a \ne b$; in the first position where $a$ and $b$ differ, the string $a$ has a letter that appears earlier in the alphabet than the corresponding letter in $b$. -----Input----- Each test contains multiple test cases. The first line contains the number of test cases $t$ ($1 \le t \le 5000$). The description of the test cases follows. The first line of each test case contains a single integer $n$ ($1 \le n \le 5000$)Β β€” the length of the string $s$. The second line of each test case contains the string $s$ of $n$ lowercase latin letters. It is guaranteed that the sum of $n$ over all test cases does not exceed $5000$. -----Output----- For each testcase output two lines: In the first line output the lexicographically smallest string $s'$ achievable after the above-mentioned modification. In the second line output the appropriate value of $k$ ($1 \leq k \leq n$) that you chose for performing the modification. If there are multiple values of $k$ that give the lexicographically smallest string, output the smallest value of $k$ among them. -----Example----- Input 6 4 abab 6 qwerty 5 aaaaa 6 alaska 9 lfpbavjsm 1 p Output abab 1 ertyqw 3 aaaaa 1 aksala 6 avjsmbpfl 5 p 1 -----Note----- In the first testcase of the first sample, the string modification results for the sample abab are as follows : for $k = 1$ : abab for $k = 2$ : baba for $k = 3$ : abab for $k = 4$ : baba The lexicographically smallest string achievable through modification is abab for $k = 1$ and $3$. Smallest value of $k$ needed to achieve is hence $1$.
def f(L, K): L = list(L) a = L[: K - 1] ans = L[K - 1 :] if K % 2 == len(L) % 2: a = a[::-1] ans += a return "".join(ans) for _ in range(int(input())): N = int(input()) L = input() ans = "z" * 6000 ak = -1 for k in range(1, 1 + N): x = f(L, k) if x < ans: ans = x ak = k print(ans) print(ak)
FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER IF BIN_OP VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR NUMBER VAR VAR RETURN FUNC_CALL STRING VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR BIN_OP STRING NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR VAR IF VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR
Vasya has a string $s$ of length $n$. He decides to make the following modification to the string: Pick an integer $k$, ($1 \leq k \leq n$). For $i$ from $1$ to $n-k+1$, reverse the substring $s[i:i+k-1]$ of $s$. For example, if string $s$ is qwer and $k = 2$, below is the series of transformations the string goes through: qwer (original string) wqer (after reversing the first substring of length $2$) weqr (after reversing the second substring of length $2$) werq (after reversing the last substring of length $2$) Hence, the resulting string after modifying $s$ with $k = 2$ is werq. Vasya wants to choose a $k$ such that the string obtained after the above-mentioned modification is lexicographically smallest possible among all choices of $k$. Among all such $k$, he wants to choose the smallest one. Since he is busy attending Felicity 2020, he asks for your help. A string $a$ is lexicographically smaller than a string $b$ if and only if one of the following holds: $a$ is a prefix of $b$, but $a \ne b$; in the first position where $a$ and $b$ differ, the string $a$ has a letter that appears earlier in the alphabet than the corresponding letter in $b$. -----Input----- Each test contains multiple test cases. The first line contains the number of test cases $t$ ($1 \le t \le 5000$). The description of the test cases follows. The first line of each test case contains a single integer $n$ ($1 \le n \le 5000$)Β β€” the length of the string $s$. The second line of each test case contains the string $s$ of $n$ lowercase latin letters. It is guaranteed that the sum of $n$ over all test cases does not exceed $5000$. -----Output----- For each testcase output two lines: In the first line output the lexicographically smallest string $s'$ achievable after the above-mentioned modification. In the second line output the appropriate value of $k$ ($1 \leq k \leq n$) that you chose for performing the modification. If there are multiple values of $k$ that give the lexicographically smallest string, output the smallest value of $k$ among them. -----Example----- Input 6 4 abab 6 qwerty 5 aaaaa 6 alaska 9 lfpbavjsm 1 p Output abab 1 ertyqw 3 aaaaa 1 aksala 6 avjsmbpfl 5 p 1 -----Note----- In the first testcase of the first sample, the string modification results for the sample abab are as follows : for $k = 1$ : abab for $k = 2$ : baba for $k = 3$ : abab for $k = 4$ : baba The lexicographically smallest string achievable through modification is abab for $k = 1$ and $3$. Smallest value of $k$ needed to achieve is hence $1$.
def ii(): return int(input()) def mii(): return map(int, input().split()) def lmii(): return list(mii()) def main(): for _ in range(ii()): n = ii() s = input() r = [] x = [] y = [] pt = [] for i in range(n): r.append(s[i]) rx = list(set(r)) rx.sort() for i in range(n): p = [] q = [] xs = "" ys = "" vr = "" if s[i] == rx[0]: xs = s[i:] ys = s[:i] if (n - i) % 2 == 1: ys = ys[::-1] vr = xs + ys q.append(vr) q.append(i + 1) if len(q) != 0: y.append(q) if vr != "": x.append(vr) res = min(sub for sub in x if isinstance(sub, str)) for i in range(len(y)): if y[i][0] == res: pt.append(y[i][1]) pt.sort() print(res) print(pt[0]) main()
FUNC_DEF RETURN FUNC_CALL VAR FUNC_CALL VAR FUNC_DEF RETURN FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FUNC_DEF RETURN FUNC_CALL VAR FUNC_CALL VAR FUNC_DEF FOR VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR LIST ASSIGN VAR LIST ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR LIST ASSIGN VAR LIST ASSIGN VAR STRING ASSIGN VAR STRING ASSIGN VAR STRING IF VAR VAR VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR IF BIN_OP BIN_OP VAR VAR NUMBER NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER IF FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR VAR IF VAR STRING EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR FUNC_CALL VAR VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER VAR EXPR FUNC_CALL VAR VAR VAR NUMBER EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR
Vasya has a string $s$ of length $n$. He decides to make the following modification to the string: Pick an integer $k$, ($1 \leq k \leq n$). For $i$ from $1$ to $n-k+1$, reverse the substring $s[i:i+k-1]$ of $s$. For example, if string $s$ is qwer and $k = 2$, below is the series of transformations the string goes through: qwer (original string) wqer (after reversing the first substring of length $2$) weqr (after reversing the second substring of length $2$) werq (after reversing the last substring of length $2$) Hence, the resulting string after modifying $s$ with $k = 2$ is werq. Vasya wants to choose a $k$ such that the string obtained after the above-mentioned modification is lexicographically smallest possible among all choices of $k$. Among all such $k$, he wants to choose the smallest one. Since he is busy attending Felicity 2020, he asks for your help. A string $a$ is lexicographically smaller than a string $b$ if and only if one of the following holds: $a$ is a prefix of $b$, but $a \ne b$; in the first position where $a$ and $b$ differ, the string $a$ has a letter that appears earlier in the alphabet than the corresponding letter in $b$. -----Input----- Each test contains multiple test cases. The first line contains the number of test cases $t$ ($1 \le t \le 5000$). The description of the test cases follows. The first line of each test case contains a single integer $n$ ($1 \le n \le 5000$)Β β€” the length of the string $s$. The second line of each test case contains the string $s$ of $n$ lowercase latin letters. It is guaranteed that the sum of $n$ over all test cases does not exceed $5000$. -----Output----- For each testcase output two lines: In the first line output the lexicographically smallest string $s'$ achievable after the above-mentioned modification. In the second line output the appropriate value of $k$ ($1 \leq k \leq n$) that you chose for performing the modification. If there are multiple values of $k$ that give the lexicographically smallest string, output the smallest value of $k$ among them. -----Example----- Input 6 4 abab 6 qwerty 5 aaaaa 6 alaska 9 lfpbavjsm 1 p Output abab 1 ertyqw 3 aaaaa 1 aksala 6 avjsmbpfl 5 p 1 -----Note----- In the first testcase of the first sample, the string modification results for the sample abab are as follows : for $k = 1$ : abab for $k = 2$ : baba for $k = 3$ : abab for $k = 4$ : baba The lexicographically smallest string achievable through modification is abab for $k = 1$ and $3$. Smallest value of $k$ needed to achieve is hence $1$.
import sys input = lambda: sys.stdin.readline().rstrip() t = int(input()) while t: t -= 1 n = int(input()) a = input() poss = [] for i in range(n): if n - i & 1 and i != 0: poss.append([a[i:] + a[i - 1 :: -1], i]) else: poss.append([a[i:] + a[:i], i]) ans, k = min(poss) print(ans) print(k + 1)
IMPORT ASSIGN VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR WHILE VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR IF BIN_OP BIN_OP VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR LIST BIN_OP VAR VAR VAR BIN_OP VAR NUMBER NUMBER VAR EXPR FUNC_CALL VAR LIST BIN_OP VAR VAR VAR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER
Vasya has a string $s$ of length $n$. He decides to make the following modification to the string: Pick an integer $k$, ($1 \leq k \leq n$). For $i$ from $1$ to $n-k+1$, reverse the substring $s[i:i+k-1]$ of $s$. For example, if string $s$ is qwer and $k = 2$, below is the series of transformations the string goes through: qwer (original string) wqer (after reversing the first substring of length $2$) weqr (after reversing the second substring of length $2$) werq (after reversing the last substring of length $2$) Hence, the resulting string after modifying $s$ with $k = 2$ is werq. Vasya wants to choose a $k$ such that the string obtained after the above-mentioned modification is lexicographically smallest possible among all choices of $k$. Among all such $k$, he wants to choose the smallest one. Since he is busy attending Felicity 2020, he asks for your help. A string $a$ is lexicographically smaller than a string $b$ if and only if one of the following holds: $a$ is a prefix of $b$, but $a \ne b$; in the first position where $a$ and $b$ differ, the string $a$ has a letter that appears earlier in the alphabet than the corresponding letter in $b$. -----Input----- Each test contains multiple test cases. The first line contains the number of test cases $t$ ($1 \le t \le 5000$). The description of the test cases follows. The first line of each test case contains a single integer $n$ ($1 \le n \le 5000$)Β β€” the length of the string $s$. The second line of each test case contains the string $s$ of $n$ lowercase latin letters. It is guaranteed that the sum of $n$ over all test cases does not exceed $5000$. -----Output----- For each testcase output two lines: In the first line output the lexicographically smallest string $s'$ achievable after the above-mentioned modification. In the second line output the appropriate value of $k$ ($1 \leq k \leq n$) that you chose for performing the modification. If there are multiple values of $k$ that give the lexicographically smallest string, output the smallest value of $k$ among them. -----Example----- Input 6 4 abab 6 qwerty 5 aaaaa 6 alaska 9 lfpbavjsm 1 p Output abab 1 ertyqw 3 aaaaa 1 aksala 6 avjsmbpfl 5 p 1 -----Note----- In the first testcase of the first sample, the string modification results for the sample abab are as follows : for $k = 1$ : abab for $k = 2$ : baba for $k = 3$ : abab for $k = 4$ : baba The lexicographically smallest string achievable through modification is abab for $k = 1$ and $3$. Smallest value of $k$ needed to achieve is hence $1$.
t = int(input()) for j in range(t): n = int(input()) s = input() S = s m = min(s) result = -1 x = True l = [] lst = [] while x == True: result = s.find(m, result + 1) if result not in l and result != -1: l.append(result) else: break for k in l: first = "" second = "" if k > 0: if k % 2 == n % 2: first = s[0:k] second = s[k:] lst.append([second + first, k + 1]) else: first = s[k - 1 : 0 : -1] + s[0] second = s[k:] lst.append([second + first, k + 1]) else: lst.append([s, k + 1]) lst.sort(key=lambda x: x[0]) print(lst[0][0]) print(lst[0][1])
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 ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR LIST ASSIGN VAR LIST WHILE VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER IF VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR FOR VAR VAR ASSIGN VAR STRING ASSIGN VAR STRING IF VAR NUMBER IF BIN_OP VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR NUMBER VAR ASSIGN VAR VAR VAR EXPR FUNC_CALL VAR LIST BIN_OP VAR VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR BIN_OP VAR NUMBER NUMBER NUMBER VAR NUMBER ASSIGN VAR VAR VAR EXPR FUNC_CALL VAR LIST BIN_OP VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR LIST VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR NUMBER NUMBER
Vasya has a string $s$ of length $n$. He decides to make the following modification to the string: Pick an integer $k$, ($1 \leq k \leq n$). For $i$ from $1$ to $n-k+1$, reverse the substring $s[i:i+k-1]$ of $s$. For example, if string $s$ is qwer and $k = 2$, below is the series of transformations the string goes through: qwer (original string) wqer (after reversing the first substring of length $2$) weqr (after reversing the second substring of length $2$) werq (after reversing the last substring of length $2$) Hence, the resulting string after modifying $s$ with $k = 2$ is werq. Vasya wants to choose a $k$ such that the string obtained after the above-mentioned modification is lexicographically smallest possible among all choices of $k$. Among all such $k$, he wants to choose the smallest one. Since he is busy attending Felicity 2020, he asks for your help. A string $a$ is lexicographically smaller than a string $b$ if and only if one of the following holds: $a$ is a prefix of $b$, but $a \ne b$; in the first position where $a$ and $b$ differ, the string $a$ has a letter that appears earlier in the alphabet than the corresponding letter in $b$. -----Input----- Each test contains multiple test cases. The first line contains the number of test cases $t$ ($1 \le t \le 5000$). The description of the test cases follows. The first line of each test case contains a single integer $n$ ($1 \le n \le 5000$)Β β€” the length of the string $s$. The second line of each test case contains the string $s$ of $n$ lowercase latin letters. It is guaranteed that the sum of $n$ over all test cases does not exceed $5000$. -----Output----- For each testcase output two lines: In the first line output the lexicographically smallest string $s'$ achievable after the above-mentioned modification. In the second line output the appropriate value of $k$ ($1 \leq k \leq n$) that you chose for performing the modification. If there are multiple values of $k$ that give the lexicographically smallest string, output the smallest value of $k$ among them. -----Example----- Input 6 4 abab 6 qwerty 5 aaaaa 6 alaska 9 lfpbavjsm 1 p Output abab 1 ertyqw 3 aaaaa 1 aksala 6 avjsmbpfl 5 p 1 -----Note----- In the first testcase of the first sample, the string modification results for the sample abab are as follows : for $k = 1$ : abab for $k = 2$ : baba for $k = 3$ : abab for $k = 4$ : baba The lexicographically smallest string achievable through modification is abab for $k = 1$ and $3$. Smallest value of $k$ needed to achieve is hence $1$.
t = int(input()) for _ in range(t): n = int(input()) s = input() t = s[::-1] can = [] for i in range(n): tmp = s[i:] if i == 0: can.append([s, 1]) continue if i % 2 != n % 2: tmp += t[-i:] else: tmp += s[:i] can.append([tmp, i + 1]) print(min(can)[0]) print(min(can)[1])
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 ASSIGN VAR VAR NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR LIST VAR NUMBER IF BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR LIST VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER
Vasya has a string $s$ of length $n$. He decides to make the following modification to the string: Pick an integer $k$, ($1 \leq k \leq n$). For $i$ from $1$ to $n-k+1$, reverse the substring $s[i:i+k-1]$ of $s$. For example, if string $s$ is qwer and $k = 2$, below is the series of transformations the string goes through: qwer (original string) wqer (after reversing the first substring of length $2$) weqr (after reversing the second substring of length $2$) werq (after reversing the last substring of length $2$) Hence, the resulting string after modifying $s$ with $k = 2$ is werq. Vasya wants to choose a $k$ such that the string obtained after the above-mentioned modification is lexicographically smallest possible among all choices of $k$. Among all such $k$, he wants to choose the smallest one. Since he is busy attending Felicity 2020, he asks for your help. A string $a$ is lexicographically smaller than a string $b$ if and only if one of the following holds: $a$ is a prefix of $b$, but $a \ne b$; in the first position where $a$ and $b$ differ, the string $a$ has a letter that appears earlier in the alphabet than the corresponding letter in $b$. -----Input----- Each test contains multiple test cases. The first line contains the number of test cases $t$ ($1 \le t \le 5000$). The description of the test cases follows. The first line of each test case contains a single integer $n$ ($1 \le n \le 5000$)Β β€” the length of the string $s$. The second line of each test case contains the string $s$ of $n$ lowercase latin letters. It is guaranteed that the sum of $n$ over all test cases does not exceed $5000$. -----Output----- For each testcase output two lines: In the first line output the lexicographically smallest string $s'$ achievable after the above-mentioned modification. In the second line output the appropriate value of $k$ ($1 \leq k \leq n$) that you chose for performing the modification. If there are multiple values of $k$ that give the lexicographically smallest string, output the smallest value of $k$ among them. -----Example----- Input 6 4 abab 6 qwerty 5 aaaaa 6 alaska 9 lfpbavjsm 1 p Output abab 1 ertyqw 3 aaaaa 1 aksala 6 avjsmbpfl 5 p 1 -----Note----- In the first testcase of the first sample, the string modification results for the sample abab are as follows : for $k = 1$ : abab for $k = 2$ : baba for $k = 3$ : abab for $k = 4$ : baba The lexicographically smallest string achievable through modification is abab for $k = 1$ and $3$. Smallest value of $k$ needed to achieve is hence $1$.
t = int(input()) st = "" for _ in range(t): n = int(input()) s = input() l = [s + "0000"] for i in range(1, n): if n - i & 1: st = s[i:] + s[:i][::-1] + "{:04d}".format(i) else: st = s[i:] + s[:i] + "{:04d}".format(i) l.append(st) l.sort() print(l[0][:-4]) print(int(l[0][-4:]) + 1)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR STRING FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR LIST BIN_OP VAR STRING FOR VAR FUNC_CALL VAR NUMBER VAR IF BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR VAR NUMBER FUNC_CALL STRING VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR VAR FUNC_CALL STRING VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER NUMBER
Vasya has a string $s$ of length $n$. He decides to make the following modification to the string: Pick an integer $k$, ($1 \leq k \leq n$). For $i$ from $1$ to $n-k+1$, reverse the substring $s[i:i+k-1]$ of $s$. For example, if string $s$ is qwer and $k = 2$, below is the series of transformations the string goes through: qwer (original string) wqer (after reversing the first substring of length $2$) weqr (after reversing the second substring of length $2$) werq (after reversing the last substring of length $2$) Hence, the resulting string after modifying $s$ with $k = 2$ is werq. Vasya wants to choose a $k$ such that the string obtained after the above-mentioned modification is lexicographically smallest possible among all choices of $k$. Among all such $k$, he wants to choose the smallest one. Since he is busy attending Felicity 2020, he asks for your help. A string $a$ is lexicographically smaller than a string $b$ if and only if one of the following holds: $a$ is a prefix of $b$, but $a \ne b$; in the first position where $a$ and $b$ differ, the string $a$ has a letter that appears earlier in the alphabet than the corresponding letter in $b$. -----Input----- Each test contains multiple test cases. The first line contains the number of test cases $t$ ($1 \le t \le 5000$). The description of the test cases follows. The first line of each test case contains a single integer $n$ ($1 \le n \le 5000$)Β β€” the length of the string $s$. The second line of each test case contains the string $s$ of $n$ lowercase latin letters. It is guaranteed that the sum of $n$ over all test cases does not exceed $5000$. -----Output----- For each testcase output two lines: In the first line output the lexicographically smallest string $s'$ achievable after the above-mentioned modification. In the second line output the appropriate value of $k$ ($1 \leq k \leq n$) that you chose for performing the modification. If there are multiple values of $k$ that give the lexicographically smallest string, output the smallest value of $k$ among them. -----Example----- Input 6 4 abab 6 qwerty 5 aaaaa 6 alaska 9 lfpbavjsm 1 p Output abab 1 ertyqw 3 aaaaa 1 aksala 6 avjsmbpfl 5 p 1 -----Note----- In the first testcase of the first sample, the string modification results for the sample abab are as follows : for $k = 1$ : abab for $k = 2$ : baba for $k = 3$ : abab for $k = 4$ : baba The lexicographically smallest string achievable through modification is abab for $k = 1$ and $3$. Smallest value of $k$ needed to achieve is hence $1$.
q = int(input()) for i in range(q): minn = "z" * 5000 n = int(input()) s = input() for j in range(n): step = 1 if (n - j) % 2 == 1: step = -1 st = s[j:] + s[:j][::step] if st < minn: minn = st k = j + 1 print(minn) print(k)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP STRING NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER IF BIN_OP BIN_OP VAR VAR NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR VAR VAR VAR VAR IF VAR VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR
Vasya has a string $s$ of length $n$. He decides to make the following modification to the string: Pick an integer $k$, ($1 \leq k \leq n$). For $i$ from $1$ to $n-k+1$, reverse the substring $s[i:i+k-1]$ of $s$. For example, if string $s$ is qwer and $k = 2$, below is the series of transformations the string goes through: qwer (original string) wqer (after reversing the first substring of length $2$) weqr (after reversing the second substring of length $2$) werq (after reversing the last substring of length $2$) Hence, the resulting string after modifying $s$ with $k = 2$ is werq. Vasya wants to choose a $k$ such that the string obtained after the above-mentioned modification is lexicographically smallest possible among all choices of $k$. Among all such $k$, he wants to choose the smallest one. Since he is busy attending Felicity 2020, he asks for your help. A string $a$ is lexicographically smaller than a string $b$ if and only if one of the following holds: $a$ is a prefix of $b$, but $a \ne b$; in the first position where $a$ and $b$ differ, the string $a$ has a letter that appears earlier in the alphabet than the corresponding letter in $b$. -----Input----- Each test contains multiple test cases. The first line contains the number of test cases $t$ ($1 \le t \le 5000$). The description of the test cases follows. The first line of each test case contains a single integer $n$ ($1 \le n \le 5000$)Β β€” the length of the string $s$. The second line of each test case contains the string $s$ of $n$ lowercase latin letters. It is guaranteed that the sum of $n$ over all test cases does not exceed $5000$. -----Output----- For each testcase output two lines: In the first line output the lexicographically smallest string $s'$ achievable after the above-mentioned modification. In the second line output the appropriate value of $k$ ($1 \leq k \leq n$) that you chose for performing the modification. If there are multiple values of $k$ that give the lexicographically smallest string, output the smallest value of $k$ among them. -----Example----- Input 6 4 abab 6 qwerty 5 aaaaa 6 alaska 9 lfpbavjsm 1 p Output abab 1 ertyqw 3 aaaaa 1 aksala 6 avjsmbpfl 5 p 1 -----Note----- In the first testcase of the first sample, the string modification results for the sample abab are as follows : for $k = 1$ : abab for $k = 2$ : baba for $k = 3$ : abab for $k = 4$ : baba The lexicographically smallest string achievable through modification is abab for $k = 1$ and $3$. Smallest value of $k$ needed to achieve is hence $1$.
def reverse(s): return s[::-1] t = int(input()) for i in range(t): n = int(input()) s = input() p = sorted(s)[0] arr = [] for i in range(len(s)): if s[i] == p: arr.append(i) lis = [] for i in arr: if (n - i) % 2 == 0: lis.append((s[i:] + s[0:i], i + 1)) else: lis.append((s[i:] + reverse(s[0:i]), i + 1)) lis.sort(key=lambda x: x[0]) print(lis[0][0]) print(lis[0][1])
FUNC_DEF RETURN VAR NUMBER 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 ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR LIST FOR VAR VAR IF BIN_OP BIN_OP VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR BIN_OP VAR VAR VAR NUMBER VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR VAR FUNC_CALL VAR VAR NUMBER VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR NUMBER NUMBER
Vasya has a string $s$ of length $n$. He decides to make the following modification to the string: Pick an integer $k$, ($1 \leq k \leq n$). For $i$ from $1$ to $n-k+1$, reverse the substring $s[i:i+k-1]$ of $s$. For example, if string $s$ is qwer and $k = 2$, below is the series of transformations the string goes through: qwer (original string) wqer (after reversing the first substring of length $2$) weqr (after reversing the second substring of length $2$) werq (after reversing the last substring of length $2$) Hence, the resulting string after modifying $s$ with $k = 2$ is werq. Vasya wants to choose a $k$ such that the string obtained after the above-mentioned modification is lexicographically smallest possible among all choices of $k$. Among all such $k$, he wants to choose the smallest one. Since he is busy attending Felicity 2020, he asks for your help. A string $a$ is lexicographically smaller than a string $b$ if and only if one of the following holds: $a$ is a prefix of $b$, but $a \ne b$; in the first position where $a$ and $b$ differ, the string $a$ has a letter that appears earlier in the alphabet than the corresponding letter in $b$. -----Input----- Each test contains multiple test cases. The first line contains the number of test cases $t$ ($1 \le t \le 5000$). The description of the test cases follows. The first line of each test case contains a single integer $n$ ($1 \le n \le 5000$)Β β€” the length of the string $s$. The second line of each test case contains the string $s$ of $n$ lowercase latin letters. It is guaranteed that the sum of $n$ over all test cases does not exceed $5000$. -----Output----- For each testcase output two lines: In the first line output the lexicographically smallest string $s'$ achievable after the above-mentioned modification. In the second line output the appropriate value of $k$ ($1 \leq k \leq n$) that you chose for performing the modification. If there are multiple values of $k$ that give the lexicographically smallest string, output the smallest value of $k$ among them. -----Example----- Input 6 4 abab 6 qwerty 5 aaaaa 6 alaska 9 lfpbavjsm 1 p Output abab 1 ertyqw 3 aaaaa 1 aksala 6 avjsmbpfl 5 p 1 -----Note----- In the first testcase of the first sample, the string modification results for the sample abab are as follows : for $k = 1$ : abab for $k = 2$ : baba for $k = 3$ : abab for $k = 4$ : baba The lexicographically smallest string achievable through modification is abab for $k = 1$ and $3$. Smallest value of $k$ needed to achieve is hence $1$.
t = 0 t = int(input()) for q in range(t): n = int(input()) str = "" str = input() ans_str = "" ans_ind = 0 for i in range(n): if (n - i + 1) % 2 == 0: new_str = str[i:n] + str[0:i][::-1] else: new_str = str[i:n] + str[0:i] if i == 0: ans_str = new_str ans_ind = 0 elif new_str < ans_str: ans_str = new_str ans_ind = i print(ans_str) print(ans_ind + 1)
ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR STRING ASSIGN VAR FUNC_CALL VAR ASSIGN VAR STRING ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF BIN_OP BIN_OP BIN_OP VAR VAR NUMBER NUMBER NUMBER ASSIGN VAR BIN_OP VAR VAR VAR VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP VAR VAR VAR VAR NUMBER VAR IF VAR NUMBER ASSIGN VAR VAR ASSIGN VAR NUMBER IF VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER
Vasya has a string $s$ of length $n$. He decides to make the following modification to the string: Pick an integer $k$, ($1 \leq k \leq n$). For $i$ from $1$ to $n-k+1$, reverse the substring $s[i:i+k-1]$ of $s$. For example, if string $s$ is qwer and $k = 2$, below is the series of transformations the string goes through: qwer (original string) wqer (after reversing the first substring of length $2$) weqr (after reversing the second substring of length $2$) werq (after reversing the last substring of length $2$) Hence, the resulting string after modifying $s$ with $k = 2$ is werq. Vasya wants to choose a $k$ such that the string obtained after the above-mentioned modification is lexicographically smallest possible among all choices of $k$. Among all such $k$, he wants to choose the smallest one. Since he is busy attending Felicity 2020, he asks for your help. A string $a$ is lexicographically smaller than a string $b$ if and only if one of the following holds: $a$ is a prefix of $b$, but $a \ne b$; in the first position where $a$ and $b$ differ, the string $a$ has a letter that appears earlier in the alphabet than the corresponding letter in $b$. -----Input----- Each test contains multiple test cases. The first line contains the number of test cases $t$ ($1 \le t \le 5000$). The description of the test cases follows. The first line of each test case contains a single integer $n$ ($1 \le n \le 5000$)Β β€” the length of the string $s$. The second line of each test case contains the string $s$ of $n$ lowercase latin letters. It is guaranteed that the sum of $n$ over all test cases does not exceed $5000$. -----Output----- For each testcase output two lines: In the first line output the lexicographically smallest string $s'$ achievable after the above-mentioned modification. In the second line output the appropriate value of $k$ ($1 \leq k \leq n$) that you chose for performing the modification. If there are multiple values of $k$ that give the lexicographically smallest string, output the smallest value of $k$ among them. -----Example----- Input 6 4 abab 6 qwerty 5 aaaaa 6 alaska 9 lfpbavjsm 1 p Output abab 1 ertyqw 3 aaaaa 1 aksala 6 avjsmbpfl 5 p 1 -----Note----- In the first testcase of the first sample, the string modification results for the sample abab are as follows : for $k = 1$ : abab for $k = 2$ : baba for $k = 3$ : abab for $k = 4$ : baba The lexicographically smallest string achievable through modification is abab for $k = 1$ and $3$. Smallest value of $k$ needed to achieve is hence $1$.
for _ in range(int(input())): n = int(input()) s = list(input()) if len(set(s)) == 1: print("".join(s)) print(1) else: m = min(s) ans = s[:] ind = 1 for i, c in enumerate(s): fs = s[i:] + s[:i][:: -1 if (i + n) % 2 == 1 else 1] if fs < ans: ans = fs ind = i + 1 print("".join(ans)) print(ind)
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR IF FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL STRING VAR EXPR FUNC_CALL VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR VAR ASSIGN VAR NUMBER FOR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR VAR BIN_OP BIN_OP VAR VAR NUMBER NUMBER NUMBER NUMBER IF VAR VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL STRING VAR EXPR FUNC_CALL VAR VAR
Vasya has a string $s$ of length $n$. He decides to make the following modification to the string: Pick an integer $k$, ($1 \leq k \leq n$). For $i$ from $1$ to $n-k+1$, reverse the substring $s[i:i+k-1]$ of $s$. For example, if string $s$ is qwer and $k = 2$, below is the series of transformations the string goes through: qwer (original string) wqer (after reversing the first substring of length $2$) weqr (after reversing the second substring of length $2$) werq (after reversing the last substring of length $2$) Hence, the resulting string after modifying $s$ with $k = 2$ is werq. Vasya wants to choose a $k$ such that the string obtained after the above-mentioned modification is lexicographically smallest possible among all choices of $k$. Among all such $k$, he wants to choose the smallest one. Since he is busy attending Felicity 2020, he asks for your help. A string $a$ is lexicographically smaller than a string $b$ if and only if one of the following holds: $a$ is a prefix of $b$, but $a \ne b$; in the first position where $a$ and $b$ differ, the string $a$ has a letter that appears earlier in the alphabet than the corresponding letter in $b$. -----Input----- Each test contains multiple test cases. The first line contains the number of test cases $t$ ($1 \le t \le 5000$). The description of the test cases follows. The first line of each test case contains a single integer $n$ ($1 \le n \le 5000$)Β β€” the length of the string $s$. The second line of each test case contains the string $s$ of $n$ lowercase latin letters. It is guaranteed that the sum of $n$ over all test cases does not exceed $5000$. -----Output----- For each testcase output two lines: In the first line output the lexicographically smallest string $s'$ achievable after the above-mentioned modification. In the second line output the appropriate value of $k$ ($1 \leq k \leq n$) that you chose for performing the modification. If there are multiple values of $k$ that give the lexicographically smallest string, output the smallest value of $k$ among them. -----Example----- Input 6 4 abab 6 qwerty 5 aaaaa 6 alaska 9 lfpbavjsm 1 p Output abab 1 ertyqw 3 aaaaa 1 aksala 6 avjsmbpfl 5 p 1 -----Note----- In the first testcase of the first sample, the string modification results for the sample abab are as follows : for $k = 1$ : abab for $k = 2$ : baba for $k = 3$ : abab for $k = 4$ : baba The lexicographically smallest string achievable through modification is abab for $k = 1$ and $3$. Smallest value of $k$ needed to achieve is hence $1$.
inf = int(10000000000.0) t = int(input()) for _ in range(t): n = int(input()) lis = input() if n == 1: print(lis) print(1) else: m = min(lis) index = [] for i in range(n): if lis[i] == m: index.append(i) z = "z" * n maxx = inf for i in range(n + 1): if (n + i) % 2 == 0: s1 = lis[i:] + lis[:i] else: s1 = lis[i:] + lis[:i][::-1] if s1 < z: maxx = i z = s1 print(z) print(maxx + 1)
ASSIGN VAR FUNC_CALL VAR NUMBER 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 IF VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP STRING VAR ASSIGN VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER IF BIN_OP BIN_OP VAR VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR VAR NUMBER IF VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER
Vasya has a string $s$ of length $n$. He decides to make the following modification to the string: Pick an integer $k$, ($1 \leq k \leq n$). For $i$ from $1$ to $n-k+1$, reverse the substring $s[i:i+k-1]$ of $s$. For example, if string $s$ is qwer and $k = 2$, below is the series of transformations the string goes through: qwer (original string) wqer (after reversing the first substring of length $2$) weqr (after reversing the second substring of length $2$) werq (after reversing the last substring of length $2$) Hence, the resulting string after modifying $s$ with $k = 2$ is werq. Vasya wants to choose a $k$ such that the string obtained after the above-mentioned modification is lexicographically smallest possible among all choices of $k$. Among all such $k$, he wants to choose the smallest one. Since he is busy attending Felicity 2020, he asks for your help. A string $a$ is lexicographically smaller than a string $b$ if and only if one of the following holds: $a$ is a prefix of $b$, but $a \ne b$; in the first position where $a$ and $b$ differ, the string $a$ has a letter that appears earlier in the alphabet than the corresponding letter in $b$. -----Input----- Each test contains multiple test cases. The first line contains the number of test cases $t$ ($1 \le t \le 5000$). The description of the test cases follows. The first line of each test case contains a single integer $n$ ($1 \le n \le 5000$)Β β€” the length of the string $s$. The second line of each test case contains the string $s$ of $n$ lowercase latin letters. It is guaranteed that the sum of $n$ over all test cases does not exceed $5000$. -----Output----- For each testcase output two lines: In the first line output the lexicographically smallest string $s'$ achievable after the above-mentioned modification. In the second line output the appropriate value of $k$ ($1 \leq k \leq n$) that you chose for performing the modification. If there are multiple values of $k$ that give the lexicographically smallest string, output the smallest value of $k$ among them. -----Example----- Input 6 4 abab 6 qwerty 5 aaaaa 6 alaska 9 lfpbavjsm 1 p Output abab 1 ertyqw 3 aaaaa 1 aksala 6 avjsmbpfl 5 p 1 -----Note----- In the first testcase of the first sample, the string modification results for the sample abab are as follows : for $k = 1$ : abab for $k = 2$ : baba for $k = 3$ : abab for $k = 4$ : baba The lexicographically smallest string achievable through modification is abab for $k = 1$ and $3$. Smallest value of $k$ needed to achieve is hence $1$.
import sys input = sys.stdin.readline t = int(input()) for _ in range(t): n = int(input()) s = input().strip() tmp = [] for i in range(n): b, c = s[:i], s[i:] if (n - i) % 2: b = b[::-1] tmp.append((c + b, i + 1)) tmp.sort() x, y = tmp[0] print(x) print(y)
IMPORT ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR IF BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR ASSIGN VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR
Vasya has a string $s$ of length $n$. He decides to make the following modification to the string: Pick an integer $k$, ($1 \leq k \leq n$). For $i$ from $1$ to $n-k+1$, reverse the substring $s[i:i+k-1]$ of $s$. For example, if string $s$ is qwer and $k = 2$, below is the series of transformations the string goes through: qwer (original string) wqer (after reversing the first substring of length $2$) weqr (after reversing the second substring of length $2$) werq (after reversing the last substring of length $2$) Hence, the resulting string after modifying $s$ with $k = 2$ is werq. Vasya wants to choose a $k$ such that the string obtained after the above-mentioned modification is lexicographically smallest possible among all choices of $k$. Among all such $k$, he wants to choose the smallest one. Since he is busy attending Felicity 2020, he asks for your help. A string $a$ is lexicographically smaller than a string $b$ if and only if one of the following holds: $a$ is a prefix of $b$, but $a \ne b$; in the first position where $a$ and $b$ differ, the string $a$ has a letter that appears earlier in the alphabet than the corresponding letter in $b$. -----Input----- Each test contains multiple test cases. The first line contains the number of test cases $t$ ($1 \le t \le 5000$). The description of the test cases follows. The first line of each test case contains a single integer $n$ ($1 \le n \le 5000$)Β β€” the length of the string $s$. The second line of each test case contains the string $s$ of $n$ lowercase latin letters. It is guaranteed that the sum of $n$ over all test cases does not exceed $5000$. -----Output----- For each testcase output two lines: In the first line output the lexicographically smallest string $s'$ achievable after the above-mentioned modification. In the second line output the appropriate value of $k$ ($1 \leq k \leq n$) that you chose for performing the modification. If there are multiple values of $k$ that give the lexicographically smallest string, output the smallest value of $k$ among them. -----Example----- Input 6 4 abab 6 qwerty 5 aaaaa 6 alaska 9 lfpbavjsm 1 p Output abab 1 ertyqw 3 aaaaa 1 aksala 6 avjsmbpfl 5 p 1 -----Note----- In the first testcase of the first sample, the string modification results for the sample abab are as follows : for $k = 1$ : abab for $k = 2$ : baba for $k = 3$ : abab for $k = 4$ : baba The lexicographically smallest string achievable through modification is abab for $k = 1$ and $3$. Smallest value of $k$ needed to achieve is hence $1$.
for _ in range(int(input())): n = int(input()) f = input() k = 1 an = f for i in range(1, n + 1): if i == n: s = f[::-1] elif (n - i + 1) % 2 == 0: s = f[i - 1 :] + f[: i - 1] else: s = f[i - 1 :] + f[: i - 1][::-1] if s < an: an = s k = i print(an) print(k)
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER IF VAR VAR ASSIGN VAR VAR NUMBER IF BIN_OP BIN_OP BIN_OP VAR VAR NUMBER NUMBER NUMBER ASSIGN VAR BIN_OP VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER IF VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR
Vasya has a string $s$ of length $n$. He decides to make the following modification to the string: Pick an integer $k$, ($1 \leq k \leq n$). For $i$ from $1$ to $n-k+1$, reverse the substring $s[i:i+k-1]$ of $s$. For example, if string $s$ is qwer and $k = 2$, below is the series of transformations the string goes through: qwer (original string) wqer (after reversing the first substring of length $2$) weqr (after reversing the second substring of length $2$) werq (after reversing the last substring of length $2$) Hence, the resulting string after modifying $s$ with $k = 2$ is werq. Vasya wants to choose a $k$ such that the string obtained after the above-mentioned modification is lexicographically smallest possible among all choices of $k$. Among all such $k$, he wants to choose the smallest one. Since he is busy attending Felicity 2020, he asks for your help. A string $a$ is lexicographically smaller than a string $b$ if and only if one of the following holds: $a$ is a prefix of $b$, but $a \ne b$; in the first position where $a$ and $b$ differ, the string $a$ has a letter that appears earlier in the alphabet than the corresponding letter in $b$. -----Input----- Each test contains multiple test cases. The first line contains the number of test cases $t$ ($1 \le t \le 5000$). The description of the test cases follows. The first line of each test case contains a single integer $n$ ($1 \le n \le 5000$)Β β€” the length of the string $s$. The second line of each test case contains the string $s$ of $n$ lowercase latin letters. It is guaranteed that the sum of $n$ over all test cases does not exceed $5000$. -----Output----- For each testcase output two lines: In the first line output the lexicographically smallest string $s'$ achievable after the above-mentioned modification. In the second line output the appropriate value of $k$ ($1 \leq k \leq n$) that you chose for performing the modification. If there are multiple values of $k$ that give the lexicographically smallest string, output the smallest value of $k$ among them. -----Example----- Input 6 4 abab 6 qwerty 5 aaaaa 6 alaska 9 lfpbavjsm 1 p Output abab 1 ertyqw 3 aaaaa 1 aksala 6 avjsmbpfl 5 p 1 -----Note----- In the first testcase of the first sample, the string modification results for the sample abab are as follows : for $k = 1$ : abab for $k = 2$ : baba for $k = 3$ : abab for $k = 4$ : baba The lexicographically smallest string achievable through modification is abab for $k = 1$ and $3$. Smallest value of $k$ needed to achieve is hence $1$.
for _ in range(int(input())): input() s = input() n = len(s) ans = s final = 1 for k in range(n): if n % 2: if k % 2: temp = s[k:] + s[0:k] else: temp = s[k:] + s[0:k][::-1] elif k % 2 == 0: temp = s[k:] + s[0:k] else: temp = s[k:] + s[0:k][::-1] if temp < ans: ans = temp final = k + 1 print(ans) print(final)
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR EXPR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF BIN_OP VAR NUMBER IF BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR VAR VAR NUMBER VAR ASSIGN VAR BIN_OP VAR VAR VAR NUMBER VAR NUMBER IF BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR VAR VAR NUMBER VAR ASSIGN VAR BIN_OP VAR VAR VAR NUMBER VAR NUMBER IF VAR VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR
Vasya has a string $s$ of length $n$. He decides to make the following modification to the string: Pick an integer $k$, ($1 \leq k \leq n$). For $i$ from $1$ to $n-k+1$, reverse the substring $s[i:i+k-1]$ of $s$. For example, if string $s$ is qwer and $k = 2$, below is the series of transformations the string goes through: qwer (original string) wqer (after reversing the first substring of length $2$) weqr (after reversing the second substring of length $2$) werq (after reversing the last substring of length $2$) Hence, the resulting string after modifying $s$ with $k = 2$ is werq. Vasya wants to choose a $k$ such that the string obtained after the above-mentioned modification is lexicographically smallest possible among all choices of $k$. Among all such $k$, he wants to choose the smallest one. Since he is busy attending Felicity 2020, he asks for your help. A string $a$ is lexicographically smaller than a string $b$ if and only if one of the following holds: $a$ is a prefix of $b$, but $a \ne b$; in the first position where $a$ and $b$ differ, the string $a$ has a letter that appears earlier in the alphabet than the corresponding letter in $b$. -----Input----- Each test contains multiple test cases. The first line contains the number of test cases $t$ ($1 \le t \le 5000$). The description of the test cases follows. The first line of each test case contains a single integer $n$ ($1 \le n \le 5000$)Β β€” the length of the string $s$. The second line of each test case contains the string $s$ of $n$ lowercase latin letters. It is guaranteed that the sum of $n$ over all test cases does not exceed $5000$. -----Output----- For each testcase output two lines: In the first line output the lexicographically smallest string $s'$ achievable after the above-mentioned modification. In the second line output the appropriate value of $k$ ($1 \leq k \leq n$) that you chose for performing the modification. If there are multiple values of $k$ that give the lexicographically smallest string, output the smallest value of $k$ among them. -----Example----- Input 6 4 abab 6 qwerty 5 aaaaa 6 alaska 9 lfpbavjsm 1 p Output abab 1 ertyqw 3 aaaaa 1 aksala 6 avjsmbpfl 5 p 1 -----Note----- In the first testcase of the first sample, the string modification results for the sample abab are as follows : for $k = 1$ : abab for $k = 2$ : baba for $k = 3$ : abab for $k = 4$ : baba The lexicographically smallest string achievable through modification is abab for $k = 1$ and $3$. Smallest value of $k$ needed to achieve is hence $1$.
t = input() for _ in range(int(t)): n = int(input()) s = input() mn = min(s) ind = [] for i in range(n): if s[i] == mn: ind.append(i) ans = "z" * (n + 1) k = -1 parity_len = n % 2 for i in ind: parity_curr = i % 2 front = s[i : i + n] rest = s[0:i] if parity_len != parity_curr: rest = rest[::-1] front = front + rest if front < ans: ans = front k = i print(ans) print(k + 1)
ASSIGN VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP STRING BIN_OP VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER FOR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR BIN_OP VAR VAR ASSIGN VAR VAR NUMBER VAR IF VAR VAR ASSIGN VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR IF VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER
Vasya has a string $s$ of length $n$. He decides to make the following modification to the string: Pick an integer $k$, ($1 \leq k \leq n$). For $i$ from $1$ to $n-k+1$, reverse the substring $s[i:i+k-1]$ of $s$. For example, if string $s$ is qwer and $k = 2$, below is the series of transformations the string goes through: qwer (original string) wqer (after reversing the first substring of length $2$) weqr (after reversing the second substring of length $2$) werq (after reversing the last substring of length $2$) Hence, the resulting string after modifying $s$ with $k = 2$ is werq. Vasya wants to choose a $k$ such that the string obtained after the above-mentioned modification is lexicographically smallest possible among all choices of $k$. Among all such $k$, he wants to choose the smallest one. Since he is busy attending Felicity 2020, he asks for your help. A string $a$ is lexicographically smaller than a string $b$ if and only if one of the following holds: $a$ is a prefix of $b$, but $a \ne b$; in the first position where $a$ and $b$ differ, the string $a$ has a letter that appears earlier in the alphabet than the corresponding letter in $b$. -----Input----- Each test contains multiple test cases. The first line contains the number of test cases $t$ ($1 \le t \le 5000$). The description of the test cases follows. The first line of each test case contains a single integer $n$ ($1 \le n \le 5000$)Β β€” the length of the string $s$. The second line of each test case contains the string $s$ of $n$ lowercase latin letters. It is guaranteed that the sum of $n$ over all test cases does not exceed $5000$. -----Output----- For each testcase output two lines: In the first line output the lexicographically smallest string $s'$ achievable after the above-mentioned modification. In the second line output the appropriate value of $k$ ($1 \leq k \leq n$) that you chose for performing the modification. If there are multiple values of $k$ that give the lexicographically smallest string, output the smallest value of $k$ among them. -----Example----- Input 6 4 abab 6 qwerty 5 aaaaa 6 alaska 9 lfpbavjsm 1 p Output abab 1 ertyqw 3 aaaaa 1 aksala 6 avjsmbpfl 5 p 1 -----Note----- In the first testcase of the first sample, the string modification results for the sample abab are as follows : for $k = 1$ : abab for $k = 2$ : baba for $k = 3$ : abab for $k = 4$ : baba The lexicographically smallest string achievable through modification is abab for $k = 1$ and $3$. Smallest value of $k$ needed to achieve is hence $1$.
t = int(input()) for _ in range(t): n = int(input()) s = list(input()) curr = "".join(s) ind = 1 m = min(s) for i in range(len(s)): if s[i] == m: prev = curr if (n - i) % 2 == 1: curr = min(curr, "".join(s[i:] + list(reversed(s[:i])))) else: curr = min(curr, "".join(s[i:] + s[:i])) if prev != curr: ind = i + 1 print(curr) print(ind)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL STRING VAR ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR VAR ASSIGN VAR VAR IF BIN_OP BIN_OP VAR VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL STRING BIN_OP VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL STRING BIN_OP VAR VAR VAR VAR IF VAR VAR ASSIGN VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR
Vasya has a string $s$ of length $n$. He decides to make the following modification to the string: Pick an integer $k$, ($1 \leq k \leq n$). For $i$ from $1$ to $n-k+1$, reverse the substring $s[i:i+k-1]$ of $s$. For example, if string $s$ is qwer and $k = 2$, below is the series of transformations the string goes through: qwer (original string) wqer (after reversing the first substring of length $2$) weqr (after reversing the second substring of length $2$) werq (after reversing the last substring of length $2$) Hence, the resulting string after modifying $s$ with $k = 2$ is werq. Vasya wants to choose a $k$ such that the string obtained after the above-mentioned modification is lexicographically smallest possible among all choices of $k$. Among all such $k$, he wants to choose the smallest one. Since he is busy attending Felicity 2020, he asks for your help. A string $a$ is lexicographically smaller than a string $b$ if and only if one of the following holds: $a$ is a prefix of $b$, but $a \ne b$; in the first position where $a$ and $b$ differ, the string $a$ has a letter that appears earlier in the alphabet than the corresponding letter in $b$. -----Input----- Each test contains multiple test cases. The first line contains the number of test cases $t$ ($1 \le t \le 5000$). The description of the test cases follows. The first line of each test case contains a single integer $n$ ($1 \le n \le 5000$)Β β€” the length of the string $s$. The second line of each test case contains the string $s$ of $n$ lowercase latin letters. It is guaranteed that the sum of $n$ over all test cases does not exceed $5000$. -----Output----- For each testcase output two lines: In the first line output the lexicographically smallest string $s'$ achievable after the above-mentioned modification. In the second line output the appropriate value of $k$ ($1 \leq k \leq n$) that you chose for performing the modification. If there are multiple values of $k$ that give the lexicographically smallest string, output the smallest value of $k$ among them. -----Example----- Input 6 4 abab 6 qwerty 5 aaaaa 6 alaska 9 lfpbavjsm 1 p Output abab 1 ertyqw 3 aaaaa 1 aksala 6 avjsmbpfl 5 p 1 -----Note----- In the first testcase of the first sample, the string modification results for the sample abab are as follows : for $k = 1$ : abab for $k = 2$ : baba for $k = 3$ : abab for $k = 4$ : baba The lexicographically smallest string achievable through modification is abab for $k = 1$ and $3$. Smallest value of $k$ needed to achieve is hence $1$.
def main(): n = int(input()) string = list(str(input())) index = 0 res = "".join(string) for i in range(1, n): string2 = "" if (i + 1) % 2 != n % 2: string2 = "".join(string[i:n] + string[:i]) else: string2 = "".join(string[i:n] + string[i - 1 :: -1]) if string2 < res: index = i res = string2 print(res) print(index + 1) def test(): t = int(input()) while t: main() t -= 1 test()
FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL STRING VAR FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR STRING IF BIN_OP BIN_OP VAR NUMBER NUMBER BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL STRING BIN_OP VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL STRING BIN_OP VAR VAR VAR VAR BIN_OP VAR NUMBER NUMBER IF VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR WHILE VAR EXPR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR
Vasya has a string $s$ of length $n$. He decides to make the following modification to the string: Pick an integer $k$, ($1 \leq k \leq n$). For $i$ from $1$ to $n-k+1$, reverse the substring $s[i:i+k-1]$ of $s$. For example, if string $s$ is qwer and $k = 2$, below is the series of transformations the string goes through: qwer (original string) wqer (after reversing the first substring of length $2$) weqr (after reversing the second substring of length $2$) werq (after reversing the last substring of length $2$) Hence, the resulting string after modifying $s$ with $k = 2$ is werq. Vasya wants to choose a $k$ such that the string obtained after the above-mentioned modification is lexicographically smallest possible among all choices of $k$. Among all such $k$, he wants to choose the smallest one. Since he is busy attending Felicity 2020, he asks for your help. A string $a$ is lexicographically smaller than a string $b$ if and only if one of the following holds: $a$ is a prefix of $b$, but $a \ne b$; in the first position where $a$ and $b$ differ, the string $a$ has a letter that appears earlier in the alphabet than the corresponding letter in $b$. -----Input----- Each test contains multiple test cases. The first line contains the number of test cases $t$ ($1 \le t \le 5000$). The description of the test cases follows. The first line of each test case contains a single integer $n$ ($1 \le n \le 5000$)Β β€” the length of the string $s$. The second line of each test case contains the string $s$ of $n$ lowercase latin letters. It is guaranteed that the sum of $n$ over all test cases does not exceed $5000$. -----Output----- For each testcase output two lines: In the first line output the lexicographically smallest string $s'$ achievable after the above-mentioned modification. In the second line output the appropriate value of $k$ ($1 \leq k \leq n$) that you chose for performing the modification. If there are multiple values of $k$ that give the lexicographically smallest string, output the smallest value of $k$ among them. -----Example----- Input 6 4 abab 6 qwerty 5 aaaaa 6 alaska 9 lfpbavjsm 1 p Output abab 1 ertyqw 3 aaaaa 1 aksala 6 avjsmbpfl 5 p 1 -----Note----- In the first testcase of the first sample, the string modification results for the sample abab are as follows : for $k = 1$ : abab for $k = 2$ : baba for $k = 3$ : abab for $k = 4$ : baba The lexicographically smallest string achievable through modification is abab for $k = 1$ and $3$. Smallest value of $k$ needed to achieve is hence $1$.
for _ in range(int(input())): n = int(input()) s = input() k = 1 res = s[:] for i in range(1, n): n_res = s[i:] if n % 2 == i % 2: n_res += s[:i] else: n_res += s[i - 1 :: -1] if n_res < res: res = n_res[:] k = i + 1 print(res) print(k)
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR VAR FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR VAR IF BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR VAR VAR VAR VAR BIN_OP VAR NUMBER NUMBER IF VAR VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR