Advertisement
Josif_tepe

Untitled

Dec 14th, 2023
749
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.60 KB | None | 0 0
  1. #include <iostream>
  2. #include <vector>
  3. #include <string>
  4. #include <map>
  5. #include <set>
  6. using namespace std;
  7. typedef long long ll;
  8. const int maxn = 205;
  9. int n, o;
  10. const int alphabet_size = 26;
  11. int trie[maxn][alphabet_size];
  12. ll dp[alphabet_size][1 << 9][maxn];
  13. map<int, int> at_trie_end_of_word;
  14. ll rek(int at, int visited, int at_trie) {
  15.     if(at == o and __builtin_popcount(visited) == n) {
  16.         return 1;
  17.     }
  18.     if(at >= o) {
  19.         return 0;
  20.     }
  21.     if(dp[at][visited][at_trie] != -1) {
  22.         return dp[at][visited][at_trie];
  23.     }
  24.     ll res = 0;
  25.     for(int i = 0; i < 26; i++) {
  26.         int c = trie[at_trie][i];
  27.         res += rek(at + 1, visited | at_trie_end_of_word[c], c);
  28.     }
  29.     return dp[at][visited][at_trie] = res;
  30. }
  31. set<string> words;
  32. void gen_strings(int at, int visited, int at_trie, string s) {
  33.     if(at == o and __builtin_popcount(visited) == n) {
  34.         words.insert(s);
  35.         return;
  36.     }
  37.     if(at >= o) {
  38.         return;
  39.     }
  40.     for(int i = 0; i < 26; i++) {
  41.         int c = trie[at_trie][i];
  42.         if(rek(at + 1, visited | at_trie_end_of_word[c], c) > 0) {
  43.             gen_strings(at + 1, visited | at_trie_end_of_word[c], c, s + (char) (i + 'a'));
  44.         }
  45.     }
  46. }
  47. int main() {
  48.     ios_base::sync_with_stdio(false);
  49.     memset(dp, -1, sizeof dp);
  50.     cin >> o >> n;
  51.     vector<string> v(n);
  52.    
  53.     for(int i = 0; i < n; i++) {
  54.         cin >> v[i];
  55.     }
  56.     map<string, int> m1;
  57.     map<int, string> m2;
  58.     int cnt = 1;
  59.     for(int i = 0; i < n; i++) {
  60.         string tmp = "";
  61.         for(int j = 0; j < (int) v[i].size(); j++) {
  62.             tmp += v[i][j];
  63.             m1[tmp] = cnt;
  64.             m2[cnt] = tmp;
  65.             cnt++;
  66.         }
  67.     }
  68.     m1[""] = 0;
  69.     m2[0] = "";
  70.     for(int i = 0; i <= 100; i++) {
  71.         if(m2.find(i) != m2.end()) {
  72.             for(int j = 0; j < n; j++) {
  73.                 if((int) m2[i].size() >= (int) v[j].size() and m2[i].find(v[j]) != string::npos) {
  74.                     at_trie_end_of_word[i] |= (1 << j);
  75.                 }
  76.             }
  77.         }
  78.         string tmp = "";
  79.         for(int j = 0; j < 26; j++) {
  80.             tmp = m2[i] + (char) (j + 'a');
  81.             while(m1.find(tmp) == m1.end()) {
  82.                 tmp.erase(tmp.begin());
  83.             }
  84.             trie[i][j] = m1[tmp];
  85.         }
  86.        
  87.     }
  88.     ll res = rek(0, 0, 0);
  89.     if(res <= 30) {
  90.         cout << res << endl;
  91.         gen_strings(0, 0, 0, "");
  92.         for(string s : words) {
  93.             cout << s << "\n";
  94.         }
  95.     }
  96.     else {
  97.         cout << res << endl;
  98.     }
  99.     return 0;
  100. }
  101.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement