1729C - Jumping on Tiles - CodeForces Solution


constructive algorithms strings

Please click on ads to support us..

Python Code:

import math
from sys import stdin
input = stdin.readline

t = int(input())
alpha = "abcdefghijklmnopqrstuvwxyz"
out = []
for _ in range(t):

  
  s = input().strip()
  p1 = s[0]
  p2 = s[-1]
  new = []
  cost = 0
  cost = (alpha.index(s[0]) - alpha.index(s[-1]))
  for i in range(len(s)):

    new.append([s[i],i+1])
  if cost <= 0:
    new.sort(key = lambda x: (x[0],x[1]))
  else:
    new.sort(key = lambda x: (x[0],-x[1]))

 
  end = len(s)

  seq = []

  start = False
  
  for i in range(len(new)):
    
    if (new[i][1] == 1 or new[i][1] == end) and start == False:

      start = True

    elif (new[i][1] == 1 or new[i][1] == end) and start == True:

      break

    elif start == True:

      seq.append(new[i][1])

  if cost > 0:
    seq.reverse()
  cost = abs(cost)
  seq.insert(0,1)
  seq.append(end)
  
  out.append(f'{cost} {len(seq)}')
  out.append(' '.join(map(str, seq)))
print('\n'.join(out))
      

C++ Code:

#include <bits/stdc++.h>

using namespace std;


void solve() {
	string s;
	cin >> s;
	int n = s.size();
	vector<vector<int>> pos(300);
	for (int i = 0; i < n; i++) pos[s[i]].push_back(i);
	vector<int> ans;
	if (s[0] <= s[n - 1]) {
		for (int i = s[0]; i <= s[n - 1]; i++) {
			for (int j : pos[i]) ans.push_back(j);
		}
	} 
	else {
		for (int i = s[0]; i >= s[n - 1]; i--) {
			for (int j : pos[i]) ans.push_back(j);
		}
	}
	cout << abs(s[0] - s[n - 1]) << " " << ans.size() << '\n';
	for (int i : ans) cout << i + 1 << " ";
	cout << '\n';
}
int main() {
	ios_base::sync_with_stdio(0);
	cin.tie(0);

	int t;
	cin >> t;
	while (t--) {
		solve();
	}
	return 0;
}


Comments

Submit
0 Comments
More Questions

987B - High School Become Human
1223A - CME
1658B - Marin and Anti-coprime Permutation
14B - Young Photographer
143A - Help Vasilisa the Wise 2
320A - Magic Numbers
1658A - Marin and Photoshoot
514A - Chewbaсca and Number
382A - Ksenia and Pan Scales
734B - Anton and Digits
1080A - Petya and Origami
1642D - Repetitions Decoding
1440A - Buy the String
1658F - Juju and Binary String
478A - Initial Bet
981A - Antipalindrome
365A - Good Number
1204B - Mislove Has Lost an Array
1409D - Decrease the Sum of Digits
1476E - Pattern Matching
1107A - Digits Sequence Dividing
1348A - Phoenix and Balance
1343B - Balanced Array
1186A - Vus the Cossack and a Contest
1494A - ABC String
1606A - AB Balance
1658C - Shinju and the Lost Permutation
1547C - Pair Programming
550A - Two Substrings
797B - Odd sum