-
Notifications
You must be signed in to change notification settings - Fork 1
/
filter.cpp
48 lines (43 loc) · 1.04 KB
/
filter.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
#include <bits/stdc++.h>
using namespace std;
bool multiple_occurrences(const string & s){
vector<bool> letters(26);
for(char c : s){
if (letters[c]){
return true;
}
letters[c] = true;
}
return false;
}
unordered_set<string> seen;
bool permutation_of_seen(string s){
sort(s.begin(), s.end() );
bool answer = seen.count(s);
seen.insert(s);
return answer;
}
template <typename F>
void filter(F f){
while(true){
string word;
cin >> word;
if(!cin.eof()){
if(f(word)){
cout << word << endl;
}
}else{
break;
}
}
}
int main(int argc, char ** argv){
if(argc <= 1){
cerr << "Run this program like " << argv[0] << " <words_lengths>" << endl;
return 1;
}
size_t length = atoi(argv[1]);
ios_base::sync_with_stdio(0); cin.tie(0);
filter([&length](const string & s){return s.size() == length && !multiple_occurrences(s) && !permutation_of_seen(s); });
return 0;
}