Codeforces Round #501 (Div. 3) B. Obtaining the String
输入两个长为n的字符串s和t,问s经过多少次变换后(只能相邻交换,即i与i+1交换,且最后输出i的下标)可以变成t
当时唯一没想到的就是a。。。。c变成c。。。。a的话怎么移动,其实只需要从后往前就可以了233333333
菜到WA哭
#include <bits/stdc++.h> using namespace std; const int MAXN = 55; int n; char s[MAXN], t[MAXN]; vector<int> v; int main() { ios::sync_with_stdio(false); cin >> n; for (int i = 1; i <= n; i++) cin >> s[i]; for (int i = 1; i <= n; i++) cin >> t[i]; for (int i = 1; i <= n; i++) { if (s[i] == t[i]) continue; bool ch = 0; for (int j = i + 1; j <= n; j++) { if (s[j] == t[i]) { for (int k = j - 1; k >= i; k--) { v.push_back(k); swap(s[k], s[k + 1]); } ch = 1; } if (ch == 1) break; } if (!ch) { cout << -1 << endl; exit(0); } } cout << v.size() << endl; int sz = v.size(); for (int i = 0; i < sz; i++) cout << v[i] << " "; cout << endl; return 0; }