// SmallHuiWen.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include <iostream>
#include <string>
using namespace std;
void solve(string str) {
int n = str.size();
int count = 0;
int countAll = 0;
bool tag = true;
for (int i = 0, j = n - 1; i < j; ++i) {
// 从后往前寻找
for (int k = j; k >= i; --k) {
if (str[k] == str[i]) {
if (k == i) {
++count;
if (count == 2 || n % 2 == 0) {
tag = false; break;
}
countAll += n / 2 - i;
}
else {
countAll += j - k;
for (int k1 = k; k1 < j; ++k1)
swap(str[k1], str[k1 + 1]);
--j;
break;
}
}
}
if (!tag) break;
}
if (!tag) cout << "impossible" << endl;
else cout << countAll << endl;
}
int main()
{
string str;
while (cin >> str) {
solve(str);
}
return 0;
}
完美的代价
最新推荐文章于 2019-10-22 23:49:16 发布