Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include "bits/stdc++.h"
- using namespace std;
- #define nl "\n"
- #define ll long long
- #define mod 1'000'000'007
- #define all(v) v.begin(), v.end()
- #define rall(v) v.rbegin(), v.rend()
- #define sz(v) (int) v.size()
- template<typename T = int>
- istream &operator>>(istream &in, vector<T> &v) {
- for (auto &x: v) in >> x;
- return in;
- }
- template<typename T = int>
- ostream &operator<<(ostream &out, const vector<T> &v) {
- for (const T &x: v) out << x << " ";
- return out;
- }
- void Sira() {
- ios_base::sync_with_stdio(false), cin.tie(nullptr), cout.tie(nullptr);
- #ifndef ONLINE_JUDGE
- freopen("input.txt", "r", stdin), freopen("output.txt", "w", stdout);
- #endif
- }
- int n;
- vector < string > words, ans;
- vector < int > word_mask;
- int used_char = 0;
- bool rec(int idx , int cnt) {
- if (cnt == 5) return true;
- if (n - idx < 5 - cnt) return false;
- for (int i = idx ; i < n ; i++) {
- if (!(used_char & word_mask[i])) {
- used_char |= word_mask[i];
- ans.push_back(words[i]);
- if (rec(i + 1, cnt + 1)) return true;
- ans.pop_back();
- used_char ^= word_mask[i];
- }
- }
- return false;
- }
- void solve() {
- int words_size;
- cin >> words_size;
- vector < string > v(words_size);
- cin >> v;
- set < string > st;
- for(auto & word: v) {
- st.insert(word);
- if (sz(word) == 5 && sz(st) == 5) {
- words.push_back(word);
- }
- st.clear();
- }
- n = sz(words);
- word_mask.resize(n);
- for(int i = 0; i < n; i++) {
- int mask = 0;
- for (char c : words[i]) mask |= (1 << (c - 'a'));
- word_mask[i] = mask;
- }
- if (!rec(0, 0)) {
- cout << -1 << nl;
- return;
- }
- for (string &s : ans) cout << s << " ";
- cout << nl;
- }
- int main() {
- Sira();
- int t = 1;
- // cin >> t;
- while (t--) {
- solve();
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement