#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++){
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 |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
0 ms |
212 KB |
Output is correct |
5 |
Correct |
54 ms |
3196 KB |
Output is correct |
6 |
Correct |
1 ms |
340 KB |
Output is correct |
7 |
Correct |
6 ms |
852 KB |
Output is correct |
8 |
Correct |
6 ms |
736 KB |
Output is correct |
9 |
Correct |
38 ms |
1956 KB |
Output is correct |
10 |
Correct |
5 ms |
468 KB |
Output is correct |
11 |
Correct |
69 ms |
3900 KB |
Output is correct |
12 |
Correct |
22 ms |
1644 KB |
Output is correct |
13 |
Correct |
208 ms |
8036 KB |
Output is correct |
14 |
Correct |
21 ms |
1492 KB |
Output is correct |
15 |
Correct |
3 ms |
468 KB |
Output is correct |
16 |
Correct |
11 ms |
724 KB |
Output is correct |
17 |
Correct |
0 ms |
212 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
0 ms |
212 KB |
Output is correct |
5 |
Correct |
54 ms |
3196 KB |
Output is correct |
6 |
Correct |
1 ms |
340 KB |
Output is correct |
7 |
Correct |
6 ms |
852 KB |
Output is correct |
8 |
Correct |
6 ms |
736 KB |
Output is correct |
9 |
Correct |
38 ms |
1956 KB |
Output is correct |
10 |
Correct |
5 ms |
468 KB |
Output is correct |
11 |
Correct |
69 ms |
3900 KB |
Output is correct |
12 |
Correct |
22 ms |
1644 KB |
Output is correct |
13 |
Correct |
208 ms |
8036 KB |
Output is correct |
14 |
Correct |
21 ms |
1492 KB |
Output is correct |
15 |
Correct |
3 ms |
468 KB |
Output is correct |
16 |
Correct |
11 ms |
724 KB |
Output is correct |
17 |
Correct |
0 ms |
212 KB |
Output is correct |
18 |
Execution timed out |
1087 ms |
204236 KB |
Time limit exceeded |
19 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Execution timed out |
1080 ms |
16844 KB |
Time limit exceeded |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
0 ms |
212 KB |
Output is correct |
5 |
Correct |
54 ms |
3196 KB |
Output is correct |
6 |
Correct |
1 ms |
340 KB |
Output is correct |
7 |
Correct |
6 ms |
852 KB |
Output is correct |
8 |
Correct |
6 ms |
736 KB |
Output is correct |
9 |
Correct |
38 ms |
1956 KB |
Output is correct |
10 |
Correct |
5 ms |
468 KB |
Output is correct |
11 |
Correct |
69 ms |
3900 KB |
Output is correct |
12 |
Correct |
22 ms |
1644 KB |
Output is correct |
13 |
Correct |
208 ms |
8036 KB |
Output is correct |
14 |
Correct |
21 ms |
1492 KB |
Output is correct |
15 |
Correct |
3 ms |
468 KB |
Output is correct |
16 |
Correct |
11 ms |
724 KB |
Output is correct |
17 |
Correct |
0 ms |
212 KB |
Output is correct |
18 |
Execution timed out |
1087 ms |
204236 KB |
Time limit exceeded |
19 |
Halted |
0 ms |
0 KB |
- |