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;
typedef long long ll;
const ll MAXN = 1e5 + 5;
const ll INF = 1e9;
#define fi first
#define se second
ll n;
string s;
map <string, bool> vis;
queue <pair <ll, string> > q;
bool cek(string x){
for(ll i = 0; i < n-1; i++){
if(x[i] == x[i+1]){
return false;
}
}
return true;
}
void bfs(string x){
vis[x] = true;
q.push({0, x});
while(!q.empty()){
ll discur = q.front().fi;
string cur = q.front().se;
// cout << cur << " " << discur << endl;
q.pop();
if(cek(cur)){
cout << discur << endl;
exit(0);
}
for(ll i = 0; i < n-1; i++){
if(((i-1 >= 0 && cur[i] == cur[i-1]) || (i+2 <n && cur[i+1] == cur[i+2])) && cur[i] != cur[i+1]){
swap(cur[i], cur[i+1]);
if(!vis[cur]){
vis[cur] = true;
q.push({discur+1, cur});
}
swap(cur[i], cur[i+1]);
}
}
}
cout << -1 << endl;
return;
}
int main(){
ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
cin >> n >> s;
bfs(s);
}
# | 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... |