题目来源
题干
解释
通过一些观察,发现在一步之内总能将数组元素交换成目标数组的顺序
因此只需要将原数组与排好序的数组进行一次对比,然后将不相同的元素做好标记再交换就可以啦
代码段
void solve()
{
int n; cin >> n;
string s; cin >> s;
if (is_sorted(s.begin(), s.begin() + n))
{
cout << 0 << endl;
return;
}
string t = s;
sort(t.begin(), t.end());
cout << 1 << endl;
vector<int>ans;
for (int i = 0; i < n; i++)
{
if (s[i] != t[i])
ans.push_back(i + 1);
}
cout << ans.size() << ' ';
for (int i = 0; i < ans.size(); i++)cout << ans[i] << ' ';
cout << endl;
}