相等字符
2025-01-23 18:50:12
发布于:上海
#include<bits/stdc++.h>
using namespace std;
map<string,int> mp;//mp 存 key value
string a,b;
struct node{
string s;
int d;
};
int main(){
cin>>a>>b;
queue<node> q;
q.push({a,0});
mp[a]=0;//变成a这个数用了几次
while(q.size()){
node t = q.front();
q.pop();
string x = t.s;
int op = t.d;
if(x==b){
cout<<mp[b];
return 0;
}
int n = x.size();
for(int i=0;i<n;i++){
for(int j=i+1;j<n;j++){
if((j-i)%2==0){
swap(x[i],x[j]);
if(mp.find(x) == mp.end() || mp[x]>op+1){
mp[x] = op+1;
q.push({x,mp[x]});
}
swap(x[i],x[j]);
}
}
}
}
cout<<-1;
return 0;
}
这里空空如也
有帮助,赞一个