题意:给出一个字符串能否通过交换两个相邻的字母形成一个回文序列,如果可以输出最少交换次数,否则输出impossible
题解:交换次数最少思路参照别人,从左右两边开始将两个相同字母距离各自的两边距离总和最短的将他们移到两边,然后两边距离各自减一,直到两边重合。
#include <stdio.h>
#include <string.h>
#include <algorithm>
using namespace std;
const int N = 200;
int main() {
int t, vis[N], ans;
char str[N], mid;
scanf("%d", &t);
while (t--) {
ans = 0;
memset(vis, 0, sizeof(vis));
scanf("%s", str);
int len = strlen(str);
for (int i = 0; i < len; i++)
if (!vis[str[i] - 'a'])
vis[str[i] - 'a'] = 1;
else
vis[str[i] - 'a'] = 0;
int temp = 0;
for (int i = 0; i < 26; i++)
if (vis[i]) {
temp++;
mid = i + 'a';
}
if (len % 2 && temp > 1) {
printf("Impossible\n");
continue;
}
if (len % 2 == 0 && temp) {
printf("Impossible\n");
continue;
}
int mindist, left = 0, right = len - 1, l1, r1;
while (left < right) {
mindist = N;
memset(vis, 0, sizeof(vis));
for (int i = left; i < right; i++) {
if (!vis[i]) {
vis[i] = 1;
for (int j = right; j >= i; j--) {
if (str[j] == str[i]) {
vis[j] = 1;
temp = j;
break;
}
}
if (i - left + right - temp < mindist) {
mindist = i - left + right - temp;
l1 = i;
r1 = temp;
}
}
}
for (int i = l1; i > left; i--) {
swap(str[i], str[i - 1]);
ans++;
}
for (int i = r1; i < right; i++) {
swap(str[i], str[i + 1]);
ans++;
}
left++;
right--;
}
printf("%d\n", ans);
}
return 0;
}