#include<iostream>
#include<cstdio>
#include<string>
using namespace std;
string s;
int main()
{
int N;
cin>>N;
cin>>s;
int blood;
if(N%2==0)blood=0;
else blood=1;
int L=0,R=N-1;
int res=0;
while(L<R)
{
if(s[L]==s[R])//不用交换
{
L++;
R--;
continue;
}
for(int i=R-1;;i--)
{
if(i==L)
{
if(blood==0)
{
cout<<"Impossible"<<endl;
return 0;
}
blood--;
res=res+N/2-L;
L++;
break;
}
if(s[L]==s[i])
{
for(int q=i;q<R;q++)
{
swap(s[q],s[q+1]);
}
res=res+R-i;
L++;
R--;
break;
}
}
}
cout<<res<<endl;
return 0;
}
本文介绍了一种通过最少步骤使字符串变为对称形式的算法。该算法首先判断字符串长度是否为奇数,根据奇偶性设定初始血量值。接着通过两指针从两端向中间逼近,遇到不匹配字符时寻找最近的匹配字符进行交换,并更新操作次数。
342

被折叠的 条评论
为什么被折叠?



