Advertisement
Guest User

Untitled

a guest
Jul 23rd, 2014
308
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.19 KB | None | 0 0
  1. #include <cstdio>
  2. #include <cstdlib>
  3. #include <cstring>
  4. #include <cassert>
  5. #include <ctime>
  6. #include <cmath>
  7. #include <algorithm>
  8. #include <string>
  9. #include <vector>
  10. #include <deque>
  11. #include <queue>
  12. #include <list>
  13. #include <set>
  14. #include <map>
  15.  
  16. #define pb push_back
  17. #define mp make_pair
  18. #define TASKNAME ""
  19.  
  20. #ifdef DEBUG
  21. #define eprintf(...) fprintf(stderr,__VA_ARGS__)
  22. #else
  23. #define eprintf(...)
  24. #endif
  25.  
  26. #define TIMESTAMP(x) eprintf("[" #x "] Time = %.3lfs\n",clock()*1.0/CLOCKS_PER_SEC)
  27.  
  28. #ifdef _WIN32
  29. #define LLD "%I64d"
  30. #else
  31. #define LLD "%lld"
  32. #endif
  33.  
  34. #define sz(x) ((int)(x).size())
  35. #define forn(i, n) for (int i = 0; i < (n); i++)
  36.  
  37. using namespace std;
  38.  
  39. typedef long double ld;
  40. typedef long long ll;
  41. typedef vector<ll> vll;
  42. typedef vector<int> vi;
  43. typedef vector<vi> vvi;
  44. typedef vector<bool> vb;
  45. typedef vector<vb> vvb;
  46. typedef pair<int, int> pii;
  47. typedef pair<ll, int> pli;
  48. typedef pair<int, ll> pil;
  49. typedef pair<ll, ll> pll;
  50. typedef vector<pii> vpii;
  51.  
  52. const int inf = 1e9;
  53. const double eps = 1e-9;
  54. const int INF = inf;
  55. const double EPS = eps;
  56.  
  57. #ifdef DEBUG
  58. struct __timestamper {
  59.   ~__timestamper(){
  60.     TIMESTAMP(end);
  61.   }
  62. } __Timestamper;
  63. #else
  64. struct __timestamper {};
  65. #endif
  66.  
  67. /*Template end*/
  68.  
  69. int get_fixed(int msk, const string &w) {
  70.   int res = 0;
  71.   for (int i = 0; i < sz(w); i++) {
  72.     if (msk & (1 << (w[i] - 'A'))) res |= 1 << i;
  73.   }
  74.   return res;
  75. }
  76.  
  77. int len;
  78. vector<string> ws;
  79. vector<vb> cache;
  80. vector<vb> cwas;
  81. int maxc;
  82.  
  83. bool calc(int msk, int id) {
  84.   if (cwas[id][msk]) return cache[id][msk];
  85.   cwas[id][msk] = true;
  86.  
  87.   int fixed = get_fixed(msk, ws[id]);
  88.   if (fixed == (1 << len) - 1) return cache[id][msk] = false;
  89.   {
  90.     int bad = msk;
  91.     forn (i, len)
  92.       bad &= ~(1 << (ws[id][i] - 'A'));
  93.     int bcnt = __builtin_popcount(bad);
  94.     if (bcnt > len) return cache[id][msk] = true;
  95.   }
  96.  
  97.   bool res = true;
  98.   for (int ne = 0; ne < maxc; ne++) if (!(msk & (1 << ne))) {
  99.     bool cres = false;
  100.     for (int nid = 0; nid < sz(ws); nid++) {
  101.       bool cok = true;
  102.       forn (i, len) if (fixed & (1 << i)) {
  103.         cok = cok && ws[nid][i] == ws[id][i];
  104.       } else {
  105.         cok = cok && !(msk & (1 << (ws[nid][i] - 'A')));
  106.       }
  107.       if (cok) {
  108.         cres = cres || calc(msk | (1 << ne), nid);
  109.       }
  110.     }
  111.     res = res && cres;
  112.   }
  113.   return cache[id][msk] = res;
  114. }
  115.  
  116. bool solve() {
  117.   assert(!ws.empty());
  118.   len = sz(ws[0]);
  119.   maxc = 0;
  120.   forn (i, sz(ws))
  121.   forn (i2, len)
  122.     maxc = max(maxc, ws[i][i2] - 'A');
  123.   maxc++;
  124.  
  125.   cache = vvb(sz(ws), vb(1 << maxc, false));
  126.   cwas  = vvb(sz(ws), vb(1 << maxc, false));
  127.   return calc(0, 0);
  128. }
  129.  
  130. int main() {
  131.   #ifdef DEBUG
  132.   freopen(TASKNAME".in","r",stdin);
  133.   #endif
  134.  
  135.   int TN;
  136.   assert(scanf("%d", &TN) == 1);
  137.   while (TN --> 0) {
  138.     int n;
  139.     scanf("%d", &n);
  140.     map<int, vector<string> > aws;
  141.     while (n --> 0) {
  142.       char str[10];
  143.       scanf("%s", str);
  144.       aws[strlen(str)].pb(str);
  145.     }
  146.     bool ok = false;
  147.     for (map<int, vector<string> >::const_iterator it = aws.begin(); it != aws.end(); it++) {
  148.       ws = it->second;
  149.       ok = ok || solve();
  150.     }
  151.     printf("%s\n", ok ? "Yes" : "No");
  152.   }
  153.   return 0;
  154. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement