Dang_Quan_10_Tin

eticket

Apr 19th, 2021
447
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <iostream>
  2. #include <cstdio>
  3. #include <algorithm>
  4. #include <vector>
  5. #include <bitset>
  6. #include <set>
  7. #define task "eticket"
  8.  
  9. using namespace std;
  10. using ld = long double;
  11. using ll = long long;
  12.  
  13. const int N = 2e3 + 5;
  14. const ll base = 101;
  15. const ll mod = 1e9 + 3;
  16.  
  17. struct Trie {
  18.     set<ll> s[N];
  19.     void Update(const string &p) {
  20.         ll v = 0;
  21.         for(auto i : p)
  22.             v = (v * base + (i == '.' ? 28 : (i - 'a' + 1))) % mod;
  23.         s[p.size()].insert(v);
  24.     }
  25.  
  26.     int Check(int x, ll y){
  27.         auto j = s[x].find(y);
  28.         return (j != s[x].end());
  29.     }
  30.  
  31. } pref;
  32.  
  33. int n, m, nxt[N * 2];
  34. bitset<N> f[N];
  35. ll Pow[N], a[N * 2];
  36.  
  37. void Prepare(){
  38.     Pow[0] = 1;
  39.     for(int i = 1; i < N; ++i) Pow[i] = Pow[i - 1] * base % mod;
  40. }
  41.  
  42. ll Get(int x, int y){
  43.     return ((a[y] - a[x - 1] * Pow[y - x + 1]) % mod + mod) % mod;
  44. }
  45.  
  46. void Get(const string &s, bitset<N> &f) {
  47.     for(int i = 1; i <= m * 2; ++i)
  48.         a[i] = (a[i - 1] * base + (s[i] == '.' ? 28 : (s[i] - 'a' + 1))) % mod;
  49.  
  50.     for(int i = m * 2; i; --i)
  51.         nxt[i] = ( (s[i] >= 'a' && s[i] <= 'z') && (i == m * 2 || s[i + 1] > 'z' || s[i + 1] < 'a') )
  52.                 ? i : nxt[i + 1];
  53.  
  54.     int cnt(0), res(0), last(1), i(1);
  55.  
  56.     while(i < m * 2) {
  57.         /// Update for last
  58.  
  59.         if((s[i - 1] > 'z' || s[i - 1] < 'a') && (s[i] >= 'a' && s[i] <= 'z')){
  60.             ++cnt;
  61.             last = i;
  62.         }
  63.  
  64.         if(s[i] > 'z' || s[i] < 'a') last = i;
  65.         last = max(last, i - m + 1);
  66.  
  67.         if(s[i] >= 'a' && s[i] <= 'z')
  68.             res += pref.Check(i - last + 1, Get(last, i)) - pref.Check(i - last, Get(last, i - 1));
  69.  
  70.         ///Update for first
  71.  
  72.         if(i >= m && (s[i - m + 1] < 'a' || s[i - m + 1] > 'z'))
  73.             cnt -= (s[i - m] >= 'a' && s[i - m] <= 'z');
  74.  
  75.         if(i >= m && s[i - m] >= 'a' && s[i - m] <= 'z') {
  76.             int nex = min(nxt[i - m], i);
  77.             res += pref.Check(nex - (i - m + 1) + 1, Get(i - m + 1, nex)) - pref.Check(nex - (i - m) + 1, Get(i - m, nex));
  78.         }
  79.  
  80.         if(i >= m) f[i - m] = (res == cnt);
  81.         //cout << i << " --- " << res << " " << cnt << "\n";
  82.  
  83.         ++i;
  84.     }
  85. }
  86.  
  87. void Read() {
  88.     cin >> m;
  89.  
  90.     for(int i = 1; i <= m; ++i){
  91.         string s;
  92.         cin >> s;
  93.  
  94.         pref.Update(s);
  95.     }
  96.  
  97.     cin >> n;
  98.  
  99.     for(int i = 1; i <= n; ++i) {
  100.         string s;
  101.         cin >> s;
  102.  
  103.         m = s.size();
  104.         s = " " + s + s;
  105.  
  106.         //cout << i << ": " << s << "\n";
  107.         Get(s, f[i]);
  108.  
  109.         //cout << i << ": " << f[i] << "\n";
  110.     }
  111.  
  112. }
  113.  
  114. void Solve() {
  115.     for(int i = 0; i < m; ++i) f[0][i] = 1;
  116.  
  117.     for(int i = 1; i <= n; ++i)
  118.         f[0] &= f[i];
  119.  
  120.     cout << f[0].count() << '\n';
  121.  
  122.     for(int i = 0; i < m; ++i)
  123.         if(f[0][i]) cout << i << " ";
  124. }
  125.  
  126. int32_t main(){
  127.     ios::sync_with_stdio(0);
  128.     cin.tie(0);
  129.     cout.tie(0);
  130.  
  131.     if(fopen(task".INP", "r")){
  132.         freopen(task".INP", "r", stdin);
  133.         freopen(task".OUT", "w", stdout);
  134.     }
  135.  
  136.     Prepare();
  137.     Read();
  138.     Solve();
  139. }
  140.  
RAW Paste Data

Adblocker detected! Please consider disabling it...

We've detected AdBlock Plus or some other adblocking software preventing Pastebin.com from fully loading.

We don't have any obnoxious sound, or popup ads, we actively block these annoying types of ads!

Please add Pastebin.com to your ad blocker whitelist or disable your adblocking software.

×