1729C. Jumping on Tiles
题意
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 ss.
In other words, you are given a string ss 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 ii-th tile to jj-th tile has a cost equal to |index(si)−index(sj)||index(si)−index(sj)|, where index©index© is the index of the letter cc in the alphabet (for example, index(index(‘a’)=1)=1, index(index(‘b’)=2)=2, …, index(index(‘z’)=26)=26) .
Polycarp wants to get to the nn-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 ss on which he should jump.
Input
The first line of the input contains an integer tt (1≤t≤1041≤t≤104) — the number of test cases in the test.
Each test case is given by the string ss (2≤|s|≤2⋅1052≤|s|≤2⋅105), where |s||s| — is the length of string ss. The string ss consists of lowercase Latin letters.
It is guaranteed that the sum of string lengths ss over all test cases does not exceed 2⋅1052⋅105.
Output
The answer to each test case consists of two lines.
In the first line print two integers costcost, mm, where costcost is the minimum total cost of the path, and mm is the maximum number of visited tiles Polycarp can make to get to nn-th tiles for the minimum total cost costcost (i.e. the number of jumps is m−1m−1).
In the next line print mm different numbers j1,j2,…,jmj1,j2,…,jm (1≤ji≤|s|1≤ji≤|s|) — the sequence of indices of the tiles Polycarp will jump on. The first number in the sequence must be 11 (that is, j1=1j1=1) and the last number must be the value of |s||s| (that is, jm=|s|jm=|s|).
If there are multiple answers, print any of them.
Example
input
Copy
6
logic
codeforces
bca
aaaaaaaaaaa
adbaadabad
to
output
Copy
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
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(index(‘l’)=12)=12, index(index(‘o’)=15)=15, index(index(‘g’)=7)=7, index(index(‘i’)=9)=9, index(index(‘c’)=3)=3, then the total cost of the path is |12−9|+|9−7|+|7−3|=3+2+4=9|12−9|+|9−7|+|7−3|=3+2+4=9.
题解
#include<bits/stdc++.h>
using namespace std;
const int N = 1e6+10;
void solve()
{
string a;
cin>>a;
vector<int> id[27],ans;
for(int i = 0 ; i < a.size() ;i ++)
{
id[ a[i] - 'a' ].push_back(i+1);
}
int b = a[0] - 'a' , e = a[ a.size()-1 ] - 'a';
for(int i = b ; i != e; i < e ? i++:i-- )
{
if(!id[i].empty())
{
for(int j : id[i])ans.push_back(j);
}
}
for(int j : id[e])ans.push_back(j);
cout<<fabs(b - e) <<" "<<ans.size()<<endl;
for(int j : ans)cout<<j<<" ";
cout<<endl;
}
int main()
{
int t;
cin>>t;
while(t--)
{
solve();
}
}