A string is given. Each time two adjacent characters are exchanged, the minimum number of times the string is exchanged. Find the two outermost letters each time. If the two letters are the same, indent them inward. If the two letters are different, find the same closest characters on both sides, exchange them, and indent them inward. After a long call, you must clarify the algorithm and try again later... Code:
/* * Author: illuz <iilluzen[at]gmail.com> * Blog: http://blog.csdn.net/hcbbt * File: uva10716.cpp * Lauguage: C/C++ * Create Date: 2013-09-03 23:03:20 * Descripton: UVA 10716 Evil Straw Warts Live, greed */ #include <cstdio> #include <cstring> #define rep(i, n) for (int i = 0; i < (n); i++) #define swap(a, b) {int t = a; a = b; b = t;} #define mc(a) memset(a, 0, sizeof(a)) const int MAXN = 110; int n, len, app[26], cnt; char s[MAXN]; void solve(char* a, int l) { if (a[0] == a[l - 1]) return; for (int i = 1; i < l - 1; i++) if (a[i] == a[l - 1]) { for (int j = i; j > 0; j--) swap(a[j], a[j - 1]); cnt += i; return; } else if (a[l - i - 1] == a[0]) { for (int j = l - i - 1; j < l - 1; j++) swap(a[j], a[j + 1]); cnt += i; return; } } int main() { scanf("%d", &n); while (n--) { scanf("%s", s); len = strlen(s); mc(app); rep(i, len) app[s[i] - 'a']++; cnt = 0; rep(i, 26) if (app[i] % 2) cnt++; if (cnt > 1) printf("Impossible\n"); else { cnt = 0; rep(i, len / 2) solve(s + i, len - 2 * i); printf("%d\n", cnt); } } return 0; }