Jumping on Tiles (Codeforces 1729C)
Tags: Others, strings

Polycarp was given a row of tiles. Each tile contains one lowercase letter of the Latin alphabet. The entire sequence of tiles forms the string $$$s$$$.In other words, you are given a string $$$s$$$ consisting of lowercase Latin letters.Initially, Polycarp is on the first tile of the row and wants to get to the last tile by jumping on the tiles. Jumping from $$$i$$$-th tile to $$$j$$$-th tile has a cost equal to $$$|index(s_i) - index(s_j)|$$$, where $$$index(c)$$$ is the index of the letter $$$c$$$ in the alphabet (for example, $$$index($$$'a'$$$)=1$$$, $$$index($$$'b'$$$)=2$$$, ..., $$$index($$$'z'$$$)=26$$$) .Polycarp wants to get to the $$$n$$$-th tile for the minimum total cost, but at the same time make maximum number of jumps.In other words, among all possible ways to get to the last tile for the minimum total cost, he will choose the one with the maximum number of jumps.Polycarp can visit each tile at most once.Polycarp asks you to help — print the sequence of indices of string $$$s$$$ on which he should jump.

Input: The first line of the input contains an integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — the number of test cases in the test.Each test case is given by the string $$$s$$$ ($$$2 \le |s| \le 2 \cdot 10^5$$$), where $$$|s|$$$ — is the length of string $$$s$$$. The string $$$s$$$ consists of lowercase Latin letters.It is guaranteed that the sum of string lengths $$$s$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$.

Output: The answer to each test case consists of two lines.In the first line print two integers $$$cost$$$, $$$m$$$, where $$$cost$$$ is the minimum total cost of the path, and $$$m$$$ is the maximum number of visited tiles Polycarp can make to get to $$$n$$$-th tiles for the minimum total cost $$$cost$$$ (i.e. the number of jumps is $$$m-1$$$).In the next line print $$$m$$$ different numbers $$$j_1, j_2, \dots, j_m$$$ ($$$1 \le j_i \le |s|$$$) — the sequence of indices of the tiles Polycarp will jump on. The first number in the sequence must be $$$1$$$ (that is, $$$j_1=1$$$) and the last number must be the value of $$$|s|$$$ (that is, $$$j_m=|s|$$$).If there are multiple answers, print any of them.

Note: In the first test case, the required path corresponds to the picture:  In this case, the minimum possible total cost of the path is achieved. Since $$$index($$$'l'$$$)=12$$$, $$$index($$$'o'$$$)=15$$$, $$$index($$$'g'$$$)=7$$$, $$$index($$$'i'$$$)=9$$$, $$$index($$$'c'$$$)=3$$$, then the total cost of the path is $$$|12-9|+|9-7|+|7-3|=3+2+4=9$$$.

Sample Inputs:
6
logic
codeforces
bca
aaaaaaaaaaa
adbaadabad
to

Sample Outputs:
9 4
1 4 3 5
16 10
1 8 3 4 9 5 2 6 7 10
1 2
1 3
0 11
1 8 10 4 3 5 7 2 9 6 11
3 10
1 9 5 4 7 3 8 6 2 10
5 2
1 2

Solution (Python):
import sys, os, io
input = io.BytesIO(os.read(0, os.fstat(0).st_size)).readline
t = int(input())
ans = []
for _ in range(t):
    s = list(input().rstrip())
    n = len(s)
    c = abs(s[0] - s[-1])
    x = [[] for _ in range(26)]
    for i in range(n):
        x[s[i] - 97].append(i + 1)
    (u, v) = (s[0] - 97, s[-1] - 97)
    ans0 = []
    if u <= v:
        for i in range(u, v + 1):
            for j in x[i]:
                ans0.append(j)
    else:
        for i in range(u, v - 1, -1):
            for j in x[i]:
                ans0.append(j)
    m = len(ans0)
    ans.append(' '.join(map(str, (c, m))))
    ans.append(' '.join(map(str, ans0)))
sys.stdout.write('\n'.join(ans))

Solution (Python):
def solve():
    s = input()
    a = []
    for ele in s:
        a.append(ord(ele))
    (l, r) = (a[0], a[-1])
    flag = False
    if l > r:
        (l, r) = (r, l)
        flag = True
    if len(s) == 2:
        print(f'{r - l} {2}')
        print('1 2')
        return
    ans = []
    for i in range(1, len(s) - 1):
        if l <= a[i] <= r:
            ans.append((a[i], i + 1))
    res = []
    ans = sorted(ans)
    for (_, i) in ans:
        res.append(i)
    if flag:
        res = res[::-1]
    res = [1] + res
    print(f'{r - l} {len(res) + 1}')
    print(' '.join(map(str, res + [len(s)])))
for _ in range(int(input())):
    solve()

Solution (Python):
t = int(input())
results = []
for i in range(t):
    s = input()
    result = []
    indices = {}
    for (idx, c) in enumerate(s):
        if s[0] <= c and c <= s[-1] or (s[-1] <= c and c <= s[0]):
            c = ord(c)
            if c not in indices.keys():
                indices[c] = []
            indices[c].append(idx + 1)
    dir = -1 if s[0] > s[-1] else 1
    for i in range(ord(s[0]), ord(s[-1]) + dir, dir):
        if i in indices.keys():
            result.extend(indices[i])
    results.append(((abs(ord(s[-1]) - ord(s[0])), len(result)), [str(item) for item in result]))
for item in results:
    print(item[0][0], item[0][1])
    print(' '.join(item[1]))