Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <vector>
- #include <string>
- #include <map>
- using namespace std;
- typedef long long ll;
- const int maxn = 205;
- int n, o;
- const int alphabet_size = 26;
- int trie[maxn][alphabet_size];
- ll dp[alphabet_size][1 << 9][maxn];
- map<int, int> at_trie_end_of_word;
- ll rek(int at, int visited, int at_trie) {
- if(at == o and __builtin_popcount(visited) == n) {
- return 1;
- }
- if(at >= o) {
- return 0;
- }
- if(dp[at][visited][at_trie] != -1) {
- return dp[at][visited][at_trie];
- }
- ll res = 0;
- for(int i = 0; i < 26; i++) {
- int c = trie[at_trie][i];
- res += rek(at + 1, visited | at_trie_end_of_word[c], c);
- }
- return dp[at][visited][at_trie] = res;
- }
- int main() {
- ios_base::sync_with_stdio(false);
- memset(dp, -1, sizeof dp);
- cin >> o >> n;
- vector<string> v(n);
- for(int i = 0; i < n; i++) {
- cin >> v[i];
- }
- map<string, int> m1;
- map<int, string> m2;
- int cnt = 1;
- for(int i = 0; i < n; i++) {
- string tmp = "";
- for(int j = 0; j < (int) v[i].size(); j++) {
- tmp += v[i][j];
- m1[tmp] = cnt;
- m2[cnt] = tmp;
- cnt++;
- }
- }
- m1[""] = 0;
- m2[0] = "";
- for(int i = 0; i <= 100; i++) {
- if(m2.find(i) != m2.end()) {
- for(int j = 0; j < n; j++) {
- if((int) m2[i].size() >= (int) v[j].size() and m2[i].find(v[j]) != string::npos) {
- at_trie_end_of_word[i] |= (1 << j);
- }
- }
- }
- string tmp = "";
- for(int j = 0; j < 26; j++) {
- tmp = m2[i] + (char) (j + 'a');
- while(m1.find(tmp) == m1.end()) {
- tmp.erase(tmp.begin());
- }
- trie[i][j] = m1[tmp];
- }
- }
- cout << rek(0, 0, 0) << endl;
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement