Problem - C - Codeforces
codeforces.com
정렬
+
같은 문자가 존재할 때, 주의
- 제목
Jumping on Tiles
- 조건
time limit per test : 1 second
memory limit per test : 256 megabytes
input : standard input
output : standard output
- 문제
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(si)−index(sj)|, 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.
- 입력
The first line of the input contains an integer t (1≤t≤10^4) — the number of test cases in the test.
Each test case is given by the string s (2≤|s|≤2⋅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⋅10^5.
- 출력
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 j1,j2,…,jm (1≤ji≤|s|) — the sequence of indices of the tiles Polycarp will jump on. The first number in the sequence must be 1 (that is, j1=1) and the last number must be the value of |s| (that is, jm=|s|).
If there are multiple answers, print any of them.
예제 입력1 | 예제 출력1 |
6 logic codeforces bca aaaaaaaaaaa adbaadabad to |
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 |
#include <iostream>
#include <algorithm>
#include <string>
#include <vector>
#include <utility>
#include <functional>
using namespace std;
#define endl '\n'
#define fastio ios::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL);
bool endBigCompare(pair<char, int> a, pair<char, int> b){
if(a.first == b.first) return a.second < b.second;
return a.first < b.first;
}
bool beginBigCompare(pair<char, int> a, pair<char, int> b){
if(a.first == b.first) return a.second < b.second;
return a.first > b.first;
}
int main() {
fastio;
int tc;
string str;
cin >> tc;
while(tc--){
cin >> str;
char sc = str[0], ec = str[str.size() - 1];
int si, ei, sz = str.size();
cout << abs(int(sc - ec)) << ' ';
vector<pair<char, int>> vc;
for(int n = 0 ; n < sz ; n++) vc.push_back({str[n], n + 1});
if(sc <= ec) sort(vc.begin(), vc.end(), endBigCompare);
else sort(vc.begin(), vc.end(), beginBigCompare);
bool check = false;
for(int n = 0 ; n < sz ; n++){
if(vc[n].second == 1){
check = true;
for(int m = sz - 1 ; m >= 0 ; m--){
if(vc[m].second == sz){
cout << m - n + 1 << endl;
break;
}
}
}
if(check) cout << vc[n].second << ' ';
if(vc[n].second == sz) break;
}
cout << endl;
}
return 0;
}