This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include<bits/stdc++.h>
using namespace std;
#define int long long
#define ss second
#define ff first
#define pb push_back
const int N=3e5+5;
int b[N];
int a[N];
map<string,int>mp;
signed main(){
int n;
cin>>n;
string s;
cin>>s;
mp[s]=1;
queue<string>q;
q.push(s);
while(!q.empty()){
string a=q.front();
q.pop();
for(int i=0;i<n-1;i++){
if(a[i]==a[i+1]){
cout<<mp[a]-1;
return 0;
}
}
for(int i=0;i<n-1;i++){
string b=a;
swap(b[i],b[i+1]);
if(mp[b]==0 ){
mp[b]=mp[a]+1;
q.push(b);
}
}
}
cout<<-1;
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |