Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <cstdio>
- #include <cstdlib>
- #include <cstring>
- #include <cassert>
- #include <ctime>
- #include <cmath>
- #include <algorithm>
- #include <string>
- #include <vector>
- #include <deque>
- #include <queue>
- #include <list>
- #include <set>
- #include <map>
- #define pb push_back
- #define mp make_pair
- #define TASKNAME ""
- #ifdef DEBUG
- #define eprintf(...) fprintf(stderr,__VA_ARGS__)
- #else
- #define eprintf(...)
- #endif
- #define TIMESTAMP(x) eprintf("[" #x "] Time = %.3lfs\n",clock()*1.0/CLOCKS_PER_SEC)
- #ifdef _WIN32
- #define LLD "%I64d"
- #else
- #define LLD "%lld"
- #endif
- #define sz(x) ((int)(x).size())
- #define forn(i, n) for (int i = 0; i < (n); i++)
- using namespace std;
- typedef long double ld;
- typedef long long ll;
- typedef vector<ll> vll;
- typedef vector<int> vi;
- typedef vector<vi> vvi;
- typedef vector<bool> vb;
- typedef vector<vb> vvb;
- typedef pair<int, int> pii;
- typedef pair<ll, int> pli;
- typedef pair<int, ll> pil;
- typedef pair<ll, ll> pll;
- typedef vector<pii> vpii;
- const int inf = 1e9;
- const double eps = 1e-9;
- const int INF = inf;
- const double EPS = eps;
- #ifdef DEBUG
- struct __timestamper {
- ~__timestamper(){
- TIMESTAMP(end);
- }
- } __Timestamper;
- #else
- struct __timestamper {};
- #endif
- /*Template end*/
- int get_fixed(int msk, const string &w) {
- int res = 0;
- for (int i = 0; i < sz(w); i++) {
- if (msk & (1 << (w[i] - 'A'))) res |= 1 << i;
- }
- return res;
- }
- int len;
- vector<string> ws;
- vector<vb> cache;
- vector<vb> cwas;
- int maxc;
- bool calc(int msk, int id) {
- if (cwas[id][msk]) return cache[id][msk];
- cwas[id][msk] = true;
- int fixed = get_fixed(msk, ws[id]);
- if (fixed == (1 << len) - 1) return cache[id][msk] = false;
- {
- int bad = msk;
- forn (i, len)
- bad &= ~(1 << (ws[id][i] - 'A'));
- int bcnt = __builtin_popcount(bad);
- if (bcnt > len) return cache[id][msk] = true;
- }
- bool res = true;
- for (int ne = 0; ne < maxc; ne++) if (!(msk & (1 << ne))) {
- bool cres = false;
- for (int nid = 0; nid < sz(ws); nid++) {
- bool cok = true;
- forn (i, len) if (fixed & (1 << i)) {
- cok = cok && ws[nid][i] == ws[id][i];
- } else {
- cok = cok && !(msk & (1 << (ws[nid][i] - 'A')));
- }
- if (cok) {
- cres = cres || calc(msk | (1 << ne), nid);
- }
- }
- res = res && cres;
- }
- return cache[id][msk] = res;
- }
- bool solve() {
- assert(!ws.empty());
- len = sz(ws[0]);
- maxc = 0;
- forn (i, sz(ws))
- forn (i2, len)
- maxc = max(maxc, ws[i][i2] - 'A');
- maxc++;
- cache = vvb(sz(ws), vb(1 << maxc, false));
- cwas = vvb(sz(ws), vb(1 << maxc, false));
- return calc(0, 0);
- }
- int main() {
- #ifdef DEBUG
- freopen(TASKNAME".in","r",stdin);
- #endif
- int TN;
- assert(scanf("%d", &TN) == 1);
- while (TN --> 0) {
- int n;
- scanf("%d", &n);
- map<int, vector<string> > aws;
- while (n --> 0) {
- char str[10];
- scanf("%s", str);
- aws[strlen(str)].pb(str);
- }
- bool ok = false;
- for (map<int, vector<string> >::const_iterator it = aws.begin(); it != aws.end(); it++) {
- ws = it->second;
- ok = ok || solve();
- }
- printf("%s\n", ok ? "Yes" : "No");
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement