Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <vector>
- #include <algorithm>
- #include <cstring>
- #include <map>
- using namespace std;
- typedef long long ll;
- int n, k;
- int letter[16][6], cnt[16], D;
- int pos_bitmask[6][26];
- int row_use[16];
- ll res = 0;
- void rec(int x, const int * col_mask) {
- if(x == n) {
- vector<int> used(16);
- for(int i = 0; i < D; i++) {
- used[i] = row_use[i];
- }
- for(int j = 0; j < n; j++) {
- int idx = __builtin_ctz(col_mask[j]);
- used[idx]++;
- if(used[idx] > cnt[idx]) {
- return;
- }
- }
- res++;
- return;
- }
- for(int r = 0; r < D; r++) {
- if(row_use[r] + 1 > cnt[r]) continue;
- int new_col_mask[6];
- bool ok = true;
- for(int j = 0; j < n; j++) {
- int new_mask = col_mask[j] & pos_bitmask[x][letter[r][j]];
- if(new_mask == 0) {
- ok = false;
- break;
- }
- new_col_mask[j] = new_mask;
- }
- if(!ok) continue;
- row_use[r]++;
- rec(x + 1, new_col_mask);
- row_use[r]--;
- }
- }
- int main() {
- ios_base::sync_with_stdio(false);
- cin >> n >> k;
- D = 0;
- map<string, int> m;
- for(int i = 0; i < k; i++) {
- string s;
- cin >> s;
- m[s]++;
- }
- for(pair<string, int> p: m) {
- cnt[D] = p.second;
- for(int i = 0; i < n; i++) {
- letter[D][i] = p.first[i] - 'A';
- }
- D++;
- }
- memset(pos_bitmask, 0, sizeof pos_bitmask);
- for(int i = 0; i < D; i++){
- for(int j = 0; j < n; j++) {
- pos_bitmask[j][letter[i][j]] |= (1 << i);
- }
- }
- int mask[6];
- int full_bitmask = (1 << D) - 1;
- for(int j = 0; j < n; j++) {
- mask[j] = full_bitmask;
- }
- for(int i = 0; i < D; i++) {
- row_use[i] = 0;
- }
- rec(0, mask);
- cout << res << endl;
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment