forked from mpfeifer1/Kattis
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlongswaps.cpp
60 lines (46 loc) · 1012 Bytes
/
longswaps.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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
#include <bits/stdc++.h>
using namespace std;
int find(vector<int>& d, int a) {
if(d[a] == -1) return a;
return d[a] = find(d,d[a]);
}
void join(vector<int>& d, int a, int b) {
a = find(d,a);
b = find(d,b);
if(a == b) return;
d[a] = b;
}
int main() {
string s;
cin >> s;
int n = s.size();
int k;
cin >> k;
vector<int> d(n,-1);
for(int i = 0; i < n; i++) {
for(int j = i+1; j < n; j++) {
if(j-i >= k) {
join(d,j,i);
}
}
}
vector<deque<char>> chars(n);
for(int i = 0; i < n; i++) {
chars[find(d,i)].push_back(s[i]);
}
for(auto& i : chars) {
sort(i.begin(),i.end());
}
string res;
for(int i = 0; i < n; i++) {
res.push_back(chars[find(d,i)].front());
chars[find(d,i)].pop_front();
}
sort(s.begin(),s.end());
if(s == res) {
cout << "Yes" << endl;
}
else {
cout << "No" << endl;
}
}