-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path1327.cpp
42 lines (42 loc) · 984 Bytes
/
1327.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
#include <bits/stdc++.h>
#define ll long long
#define X first
#define Y second
#define pii pair<int,int>
using namespace std;
int n, k, ans = 0;
string str;
map<string, int> vis;
int main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
cin >> n >> k;
for(int i = 0; i < n; i++) {
char x; cin >> x; str += x;
}
queue<string> q;
q.push(str);
vis[str] = 1;
while(!q.empty()) {
auto cur = q.front(); q.pop();
int s = vis[cur];
bool flag = 1;
for(int i = 0; i < cur.size() - 1; i++) {
if(cur[i] > cur[i + 1]) {
flag = 0; break;
}
}
if(flag) {
ans = s;
break;
}
for(int i = 0; i < n - k + 1; i++) {
string nxt = cur;
reverse(nxt.begin() + i, nxt.begin() + i + k);
if(vis[nxt]) continue;
vis[nxt] = s + 1;
q.push(nxt);
}
}
cout << ans - 1;
}