#include <bits/stdc++.h>
using namespace std;
#pragma GCC target ("avx2")
#pragma GCC optimization ("O3")
#pragma GCC optimization ("unroll-loops")
#define flash ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0)
#define debug(x) cerr << " - " << #x << ": " << x << endl;
#define debugs(x, y) cerr << " - " << #x << ": " << x << " " << #y << ": " << y << endl;
#define all(x) (x).begin(),(x).end()
#define sz(x) (ll)x.size()
#define ll long long
#define INF 1000000000
#define MOD 1000000007
#define pb push_back
#define ve vector<ll>
#define dos pair<ll,ll>
#define vedos vector<dos>
#define rand mt19937 rng(chrono::steady_clock::now().time_since_epoch().count())
struct greateri
{
template<class T>
bool operator()(T const &a, T const &b) const { return a > b; }
};
map<string,ll>vis;
bool checki(string k)
{
for (ll i = 0; i < k.size()-1; ++i)
{
if(k[i]==k[i+1])return false;
}
return true;
}
ll ans;
ll glob=INF;
int main()
{
//flash;
ll x;
cin>>x;
string sol;
cin>>sol;
queue<pair<string,ll>>nowi;
nowi.push({sol,0});
while(!nowi.empty())
{
auto k = nowi.front();
nowi.pop();
if(checki(k.first)){cout<<k.second;exit(0);}
for (ll i = 0; i < k.first.size()-1; ++i)
{
string next = k.first;
swap(next[i],next[i+1]);
if(vis.find(next)==vis.end())
{
vis[next]=1;
nowi.push({next,k.second+1});
}
}
}
cout<<-1;
return 0;
}
//code the AC sol !
// BS/queue/map
Compilation message
joi2019_ho_t3.cpp:4:0: warning: ignoring #pragma GCC optimization [-Wunknown-pragmas]
#pragma GCC optimization ("O3")
joi2019_ho_t3.cpp:5:0: warning: ignoring #pragma GCC optimization [-Wunknown-pragmas]
#pragma GCC optimization ("unroll-loops")
joi2019_ho_t3.cpp: In function 'bool checki(std::__cxx11::string)':
joi2019_ho_t3.cpp:27:19: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for (ll i = 0; i < k.size()-1; ++i)
~~^~~~~~~~~~~~
joi2019_ho_t3.cpp: In function 'int main()':
joi2019_ho_t3.cpp:49:20: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for (ll i = 0; i < k.first.size()-1; ++i)
~~^~~~~~~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
256 KB |
Output is correct |
2 |
Correct |
2 ms |
256 KB |
Output is correct |
3 |
Correct |
2 ms |
256 KB |
Output is correct |
4 |
Correct |
2 ms |
376 KB |
Output is correct |
5 |
Correct |
98 ms |
3416 KB |
Output is correct |
6 |
Correct |
4 ms |
504 KB |
Output is correct |
7 |
Correct |
12 ms |
888 KB |
Output is correct |
8 |
Correct |
11 ms |
888 KB |
Output is correct |
9 |
Correct |
68 ms |
2044 KB |
Output is correct |
10 |
Correct |
11 ms |
636 KB |
Output is correct |
11 |
Correct |
132 ms |
3960 KB |
Output is correct |
12 |
Correct |
42 ms |
1788 KB |
Output is correct |
13 |
Correct |
399 ms |
8312 KB |
Output is correct |
14 |
Correct |
39 ms |
1528 KB |
Output is correct |
15 |
Correct |
7 ms |
504 KB |
Output is correct |
16 |
Correct |
18 ms |
760 KB |
Output is correct |
17 |
Correct |
2 ms |
376 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
256 KB |
Output is correct |
2 |
Correct |
2 ms |
256 KB |
Output is correct |
3 |
Correct |
2 ms |
256 KB |
Output is correct |
4 |
Correct |
2 ms |
376 KB |
Output is correct |
5 |
Correct |
98 ms |
3416 KB |
Output is correct |
6 |
Correct |
4 ms |
504 KB |
Output is correct |
7 |
Correct |
12 ms |
888 KB |
Output is correct |
8 |
Correct |
11 ms |
888 KB |
Output is correct |
9 |
Correct |
68 ms |
2044 KB |
Output is correct |
10 |
Correct |
11 ms |
636 KB |
Output is correct |
11 |
Correct |
132 ms |
3960 KB |
Output is correct |
12 |
Correct |
42 ms |
1788 KB |
Output is correct |
13 |
Correct |
399 ms |
8312 KB |
Output is correct |
14 |
Correct |
39 ms |
1528 KB |
Output is correct |
15 |
Correct |
7 ms |
504 KB |
Output is correct |
16 |
Correct |
18 ms |
760 KB |
Output is correct |
17 |
Correct |
2 ms |
376 KB |
Output is correct |
18 |
Execution timed out |
1089 ms |
96940 KB |
Time limit exceeded |
19 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
376 KB |
Output is correct |
2 |
Execution timed out |
1084 ms |
6572 KB |
Time limit exceeded |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
256 KB |
Output is correct |
2 |
Correct |
2 ms |
256 KB |
Output is correct |
3 |
Correct |
2 ms |
256 KB |
Output is correct |
4 |
Correct |
2 ms |
376 KB |
Output is correct |
5 |
Correct |
98 ms |
3416 KB |
Output is correct |
6 |
Correct |
4 ms |
504 KB |
Output is correct |
7 |
Correct |
12 ms |
888 KB |
Output is correct |
8 |
Correct |
11 ms |
888 KB |
Output is correct |
9 |
Correct |
68 ms |
2044 KB |
Output is correct |
10 |
Correct |
11 ms |
636 KB |
Output is correct |
11 |
Correct |
132 ms |
3960 KB |
Output is correct |
12 |
Correct |
42 ms |
1788 KB |
Output is correct |
13 |
Correct |
399 ms |
8312 KB |
Output is correct |
14 |
Correct |
39 ms |
1528 KB |
Output is correct |
15 |
Correct |
7 ms |
504 KB |
Output is correct |
16 |
Correct |
18 ms |
760 KB |
Output is correct |
17 |
Correct |
2 ms |
376 KB |
Output is correct |
18 |
Execution timed out |
1089 ms |
96940 KB |
Time limit exceeded |
19 |
Halted |
0 ms |
0 KB |
- |