Advertisement
Guest User

Rafid - Dictionary - DP long

a guest
Aug 20th, 2019
102
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.95 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2.  
  3. using namespace std;
  4.  
  5. typedef long long ll;
  6. typedef long double ld;
  7. typedef pair<int, int> pii;
  8. typedef pair<long long, long long> pll;
  9. #define ff first
  10. #define ss second
  11. #define mp make_pair
  12. #define pb push_back
  13. #define ub upper_bound
  14. #define lb lower_bound
  15. #define all(x) (x).begin(), (x).end()
  16. #define fap(x) cout << __LINE__ << " says: " << #x << " = " << (x) << "\n"
  17. #define FastIO ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0);
  18. const long long INF = 2000000000000000000LL;    // 2e18
  19. const int inf = 0x3f3f3f3f;
  20. const long double EPS = 1e-9;
  21.  
  22. const int N = 53;
  23. const int L = 23;
  24. const int A = 26;
  25. const ll MOD = 1000000007LL;
  26. const ll MOD2 = MOD*MOD;
  27.  
  28. ll dp[L][N][N]; // idx, fr, to
  29. ll aux[N][A+3]; // idx, alphabet
  30.  
  31. string g[N];    // grid
  32. int front[A+3]; // starting pos of char
  33.  
  34. int main() {
  35. //    freopen("out.txt", "w", stdout);
  36.  
  37.     ios::sync_with_stdio(false);
  38.     cin.tie(0); cout.tie(0);
  39.  
  40.     int n;
  41.     cin >> n;
  42.     for(int i=0; i<n; ++i) cin >> g[i];
  43.  
  44.     int len = 0;
  45.     for(int i=0; i<n; ++i) len = max(len, (int) g[i].size());
  46.     for(int i=0; i<n; ++i) g[i] += string(len - (int) g[i].size(), 'a'-1);
  47.  
  48.     for(int fr=0; fr<n; ++fr) {
  49.         dp[len][fr][fr] = 1;
  50.     }
  51.  
  52.     for(int idx=len-1; idx>=0; --idx) {
  53.         for(int gap=1; gap<=n; ++gap) {
  54.             for(int fr=0, to=fr+gap-1; to<n; ++fr,++to) {
  55.                 ll &res = dp[idx][fr][to];
  56.                 res = 0;
  57.  
  58.                 if(fr == to) {
  59.                     if(g[fr][idx] == '?') res += dp[idx+1][fr][to] * 26;
  60.                     else res += dp[idx+1][fr][to];
  61.                 }
  62.                 else {
  63.                     string s = "";
  64.                     for(int i=fr; i<=to; ++i) s += g[i][idx];
  65.                     int sz = (int) s.size();
  66.  
  67.                     memset(front, inf, sizeof front);
  68.                     for(int i=0; i<sz; ++i) if(islower(s[i])) front[s[i]-'a'] = min(front[s[i]-'a'], i);
  69.  
  70. //                    cout << "\n";
  71. //                    fap(idx), fap(fr), fap(to), fap(s);
  72. //                    fap("------------------------");
  73.  
  74.                     for(int i=0; i<=A; ++i) aux[0][i] = 1;
  75.                     // aux[0][0] = 1;
  76.                     for(int i=1; i<=sz; ++i) {
  77.                         bool offset = true;
  78.                         for(int j=0; j<i; ++j) offset &= (s[j] == 'a'-1);
  79.                         aux[i][0] = offset;
  80.  
  81.                         for(int c=1; c<=A; ++c) {
  82.                             aux[i][c] = aux[i][c-1];
  83.  
  84.                             bool ok = true;
  85.                             for(int j=front[c-1]; j<i; ++j) {
  86.                                 if(s[j] != '?' and s[j]-'a' != c-1) {
  87.                                     ok = false;
  88.                                     break;
  89.                                 }
  90.                             }
  91.                             if(!ok) continue;
  92.  
  93.                             for(int j=min(front[c-1]+1, i); j>0; --j) {
  94.                                 if(s[j-1] != '?' and s[j-1]-'a' != c-1) break;
  95.                                 aux[i][c] += (dp[idx+1][fr+j-1][fr+i-1] * aux[j-1][c-1]) % MOD;
  96.                                 if(aux[i][c] >= MOD2) aux[i][c] -= MOD2;
  97. //                                fap(j), fap(fr+j-1), fap(fr+i-1), fap((char) ('a'+c-1)), fap((dp[idx+1][fr+j-1][fr+i-1] * aux[j-1][c-1]));
  98. //                                fap(j), fap(aux[i][c]), fap(fr+j-1), fap(fr+i-1), fap("meantime-----------------------");
  99.                             }
  100.                             aux[i][c] %= MOD;
  101.  
  102. //                            fap(i), fap(c), fap(aux[i][c]);
  103.                         }
  104.                     }
  105.  
  106.                     res = aux[sz][A];
  107. //                    fap(res);
  108.                 }
  109.  
  110.                 res %= MOD;
  111.             }
  112.         }
  113.     }
  114.  
  115.     ll ans = dp[0][0][n-1];
  116.     cout << ans << "\n";
  117.  
  118.     return 0;
  119. }
  120.  
  121. /**
  122.  
  123. 3
  124. snuje
  125. ????e
  126. snule
  127.  
  128. 2
  129. ?sum??mer
  130. c??a??mp
  131.  
  132. **/
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement