#include <bits/stdc++.h>
#define eb emplace_back
using namespace std;
set<string> PQ;
queue<pair<string, int>> V;
string A;
int N;
bool f(string &str) {
for(int i = 1; i < N; i++)
if(str[i-1] == str[i]) return false;
return true;
}
int main() {
cin >> N >> A;
V.emplace(A, 0);
PQ.insert(A);
for(string str; !V.empty();) {
int day; tie(str, day) = V.front(); V.pop();
if(f(str)) {
cout << day << endl;
return 0;
}
day++;
for(int i = 1; i < N; i++) {
swap(str[i-1], str[i]);
auto it = PQ.find(str);
if(PQ.end() == it) {
PQ.insert(str);
V.emplace(str, day);
}
swap(str[i-1], str[i]);
}
}
cout << -1 << endl;
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
384 KB |
Output is correct |
2 |
Correct |
2 ms |
384 KB |
Output is correct |
3 |
Correct |
2 ms |
256 KB |
Output is correct |
4 |
Correct |
2 ms |
384 KB |
Output is correct |
5 |
Correct |
82 ms |
3448 KB |
Output is correct |
6 |
Correct |
4 ms |
512 KB |
Output is correct |
7 |
Correct |
10 ms |
896 KB |
Output is correct |
8 |
Correct |
9 ms |
896 KB |
Output is correct |
9 |
Correct |
58 ms |
2168 KB |
Output is correct |
10 |
Correct |
9 ms |
512 KB |
Output is correct |
11 |
Correct |
114 ms |
4052 KB |
Output is correct |
12 |
Correct |
35 ms |
1784 KB |
Output is correct |
13 |
Correct |
339 ms |
8312 KB |
Output is correct |
14 |
Correct |
34 ms |
1528 KB |
Output is correct |
15 |
Correct |
6 ms |
512 KB |
Output is correct |
16 |
Correct |
15 ms |
768 KB |
Output is correct |
17 |
Correct |
2 ms |
256 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
384 KB |
Output is correct |
2 |
Correct |
2 ms |
384 KB |
Output is correct |
3 |
Correct |
2 ms |
256 KB |
Output is correct |
4 |
Correct |
2 ms |
384 KB |
Output is correct |
5 |
Correct |
82 ms |
3448 KB |
Output is correct |
6 |
Correct |
4 ms |
512 KB |
Output is correct |
7 |
Correct |
10 ms |
896 KB |
Output is correct |
8 |
Correct |
9 ms |
896 KB |
Output is correct |
9 |
Correct |
58 ms |
2168 KB |
Output is correct |
10 |
Correct |
9 ms |
512 KB |
Output is correct |
11 |
Correct |
114 ms |
4052 KB |
Output is correct |
12 |
Correct |
35 ms |
1784 KB |
Output is correct |
13 |
Correct |
339 ms |
8312 KB |
Output is correct |
14 |
Correct |
34 ms |
1528 KB |
Output is correct |
15 |
Correct |
6 ms |
512 KB |
Output is correct |
16 |
Correct |
15 ms |
768 KB |
Output is correct |
17 |
Correct |
2 ms |
256 KB |
Output is correct |
18 |
Execution timed out |
1072 ms |
112460 KB |
Time limit exceeded |
19 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
384 KB |
Output is correct |
2 |
Execution timed out |
1069 ms |
9356 KB |
Time limit exceeded |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
384 KB |
Output is correct |
2 |
Correct |
2 ms |
384 KB |
Output is correct |
3 |
Correct |
2 ms |
256 KB |
Output is correct |
4 |
Correct |
2 ms |
384 KB |
Output is correct |
5 |
Correct |
82 ms |
3448 KB |
Output is correct |
6 |
Correct |
4 ms |
512 KB |
Output is correct |
7 |
Correct |
10 ms |
896 KB |
Output is correct |
8 |
Correct |
9 ms |
896 KB |
Output is correct |
9 |
Correct |
58 ms |
2168 KB |
Output is correct |
10 |
Correct |
9 ms |
512 KB |
Output is correct |
11 |
Correct |
114 ms |
4052 KB |
Output is correct |
12 |
Correct |
35 ms |
1784 KB |
Output is correct |
13 |
Correct |
339 ms |
8312 KB |
Output is correct |
14 |
Correct |
34 ms |
1528 KB |
Output is correct |
15 |
Correct |
6 ms |
512 KB |
Output is correct |
16 |
Correct |
15 ms |
768 KB |
Output is correct |
17 |
Correct |
2 ms |
256 KB |
Output is correct |
18 |
Execution timed out |
1072 ms |
112460 KB |
Time limit exceeded |
19 |
Halted |
0 ms |
0 KB |
- |