1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
// tested by Hightail: https://github.com/dj3500/hightail
#include <iostream>
#include <cstdio>
#include <string>
#include <vector>
#include <set>
#include <map>
#include <queue>
#include <cmath>
#include <algorithm>
#include <sstream>
#include <stack>
#include <cstring>
#include <iomanip>
#include <ctime>
#include <cassert>
using namespace std;
#define pb push_back
#define INF 1001001001
#define FOR(i,n) for(int (i)=0;(i)<(n);++(i))
#define FORI(i,n) for(int (i)=1;(i)<=(n);++(i))
#define mp make_pair
#define pii pair<int,int>
#define ll long long
#define vi vector<int>
#define SZ(x) ((int)((x).size()))
#define fi first
#define se second
#define wez(n) int (n); scanf("%d",&(n));
#define wez2(n,m) int (n),(m); scanf("%d %d",&(n),&(m));
#define wez3(n,m,k) int (n),(m),(k); scanf("%d %d %d",&(n),&(m),&(k));
inline void pisz(int n) { printf("%d\n",n); }
template<typename T,typename TT> ostream& operator<<(ostream &s,pair<T,TT> t) {return s<<"("<<t.first<<","<<t.second<<")";}
template<typename T> ostream& operator<<(ostream &s,vector<T> t){FOR(i,SZ(t))s<<t[i]<<" ";return s; }
#define DBG(vari) cout<<"["<<__LINE__<<"] "<<#vari<<" = "<<(vari)<<endl;
#define ALL(t) t.begin(),t.end()
#define FOREACH(i,t) for (__typeof(t.begin()) i=t.begin(); i!=t.end(); i++)
#define TESTS wez(testow)while(testow--)
#define REP(i,a,b) for(int (i)=(a);(i)<=(b);++i)
#define REPD(i,a,b) for(int (i)=(a); (i)>=(b);--i)
#define REMAX(a,b) (a)=max((a),(b));
#define REMIN(a,b) (a)=min((a),(b));
#define IOS ios_base::sync_with_stdio(0);

vi getBorders (const vi &v1, const vi &v2) {
   // znajdz wszystkie niezerowe dlugosci x tze
   // x-prefiks v1 = x-sufiks v2
   vi p = v1;
   p.pb(INF);
   p.insert(p.end(), ALL(v2));

   vi kn(SZ(p)+1,-1); // funkcja prefiksowa Knutha
   FORI(i,SZ(p)) {
      int j = kn[i-1];
      while (j != -1 && p[j] != p[i-1]) j=kn[j];
      kn[i] = j+1;
   }

   vi res;
   int i = SZ(p);
   while (true) {
      i = kn[i];
      if (i == -1) break;
      res.pb(i);
   }
   return res;
}

vi getOffsets (const vi &p, const vi &t) {
   // znajdz wszystkie offsety z [0, |t| - |p| + 1] tze
   // t[offset .. offset + |p| - 1] = p (0-based)
   vi kn(SZ(p)+1,-1); // funkcja prefiksowa Knutha
   FORI(i,SZ(p)) {
      int j = kn[i-1];
      while (j != -1 && p[j] != p[i-1]) j=kn[j];
      kn[i] = j+1;
   }

   vi ans;
   int ppos=0,tpos=0;
   while (tpos<SZ(t)) {
      while (ppos!=-1 && (ppos == SZ(p) || p[ppos]!=t[tpos])) ppos=kn[ppos];
      ppos++;
      tpos++;
      if (ppos==SZ(p)) ans.pb(tpos-SZ(p));
   }
   return ans;
}

const int N = 1024, maxK = 11;
vi adj[N*N + N];
int dist[N*N + N];
void bfs1 (int n, int source) {
   FORI(i,n) dist[i] = -1;
   deque<int> q;
   q.pb(source);
   dist[source] = 1; // taka glupia konwencja w tym zadaniu
   while (!q.empty()) {
      int v = q.front();
      q.pop_front();
      FOREACH(it,adj[v]) if (dist[*it] == -1) {
         dist[*it] = dist[v] + 1;
         q.pb(*it);
      }
   }
}


vi replication[N];
bool hasSuf[maxK+2][N][N], B[maxK+2][N][N]; // 2/3 * 156 MB!
vi hasPrefV[maxK+2][N]; // ram!
int len[maxK+2][N];


int main () {
   /*DBG(getBorders({0,1,0,2,0,1,0,3}, {4,0,1,0,2,0,1,0}));
   DBG(getBorders({0,1,0,2,0,1,0,3}, {0,1,0,2,0,1,0}));
   DBG(getBorders({0,1,0,2,0,1,0}, {4,0,1,0,2,0,1,0}));
   DBG(getBorders({0,1,0,2,0,1,0}, {0,1,0,2,0,1,0}));
   DBG(getOffsets({1,1},{1,1,1,1,1}))
   DBG(getOffsets({1,1,1,1,1},{1,1,1,1,1}))*/
   wez2(n,m);
   FORI(i,n) {
      TESTS {
         wez(x);
         replication[i].pb(x);
      }
   }
   vi s;
   FORI(j,m) {
      wez(sj);
      s.pb(sj);
   }

   // wypelnij hasPrefV, hasSuf, B, len
   FORI(p,n) {
      vi pref = {p}, sufRev = {p};
      bool longerThanM = 0;
      REP(k,0,maxK) {
         if (k > 0) {
            // replikuj
            vi newPref, newSuf;
            for (int x : pref) {
               newPref.insert(newPref.end(), ALL(replication[x]));
               if (SZ(newPref) > m) {
                  longerThanM = 1;
                  newPref.erase(newPref.begin() + m, newPref.end());
                  break;
               }
            }
            pref = move(newPref);
            for (int x : sufRev) {
               newSuf.insert(newSuf.end(), replication[x].rbegin(), replication[x].rend());
               if (SZ(newSuf) > m) {
                  longerThanM = 1; // nie trzeba
                  newSuf.erase(newSuf.begin() + m, newSuf.end());
                  break;
               }
            }
            sufRev = move(newSuf);
         }
         vi suf(sufRev.rbegin(), sufRev.rend());

         // mam pref i suf
         hasPrefV[k][p] = getBorders(s, suf);
         vi borders = getBorders(pref, s);
         for (int x : borders) hasSuf[k][p][x] = 1;
         if (!longerThanM) {
            // |pref| <= |s|
            vi offsets = getOffsets(pref, s);
            for (int x : offsets) B[k][p][x] = 1;
            len[k][p] = SZ(pref);
         } else {
            len[k][p] = m+1;
         }
      }
   }

   // zrob graf, wylicz dist
   FORI(i,n) {
      for (int j : replication[i]) {
         adj[i].pb(j);
      }
      FOR(u,SZ(replication[i])-1) {
         adj[i].pb(replication[i][u] * n + replication[i][u+1]);
      }
   }
   FORI(p1,n) FORI(p2,n) {
      int q1 = replication[p1].back(), q2 = replication[p2][0];
      adj[p1 * n + p2].pb(q1 * n + q2);
   }
   bfs1(n*n+n, 1);

   if (m == 1) {
      pisz(dist[s[0]]);
      return 0;
   }

   int res = INF;

   FORI(p,n) if (dist[p] != -1) {
      if (dist[p] > res) continue; // opt.
      bool found = 0;
      for (int k = 1; k <= maxK && !found; ++k) {
         vi Y;
         for (const int q : replication[p]) {
            if (found) break;
            const int l = len[k-1][q];
            vi newY;
            for (const int y : Y) {
               if (y + l < m) {
                  if (B[k-1][q][y]) {
                     newY.pb(y + l);
                  }
               } else {
                  if (hasSuf[k-1][q][m - y]) {
                     // jest
                     REMIN(res, dist[p] + k);
                     found = 1;
                     break;
                  }
               }
            }
            newY.insert(newY.end(), ALL(hasPrefV[k-1][q]));
            Y = move(newY);
         }
      }
   }

   FORI(p1,n) FORI(p2,n) if (dist[p1 * n + p2] != -1) {
      if (dist[p1 * n + p2] > res) continue; // opt.
      bool found = 0;
      for (int k = 0; k <= maxK && !found; ++k) {
         for (const int x : hasPrefV[k][p1]) {
            if (hasSuf[k][p2][m - x]) {
               REMIN(res, dist[p1 * n + p2] + k);
               found = 1;
               break;
            }
         }
      }
   }

   pisz((res == INF) ? -1 : res);
}