#include #include #include #include #include using namespace std; class Rolling_Hash{ public: int N; vector p_pow; const int P; const int MOD; vector> hash; Rolling_Hash(const int n) : P(vector{10009, 10007}[0/*rand()%2*/]), MOD(vector{1000000007, 1000000009}[0/*rand()%2*/]), N(n), p_pow(n+1) { p_pow[0] = 1; for(int i=1; i<=N; i++){ p_pow[i] = (1LL * p_pow[i-1] * P) % MOD; } } // [l,r) long long get_hash(int at, int l, int r){ if(r>=hash[at].size()) return -at; int len = r-l; return (hash[at][r] - (1LL * hash[at][l]* p_pow[len])%MOD + MOD) % MOD; } void insert(const string& s){ //hash[i] = [0,i) hash.push_back({}); hash.back().resize(s.size()+1); int at = hash.size() - 1; for(int i=0; i> n; vector s(n); int m = 0; for(int i=0; i> s[i]; s[i] += '{'; m = max(m, (int)s[i].size()); } vector x(n); for(int i=0; i> x[i]; x[i]--; } Rolling_Hash lolita(m); for(int i=0; i ans(n); for(int i=0; i1){ int mid = (lb+ub)/2; int my_hash = lolita.get_hash(x[i], 0,mid); bool ok = true; for(int j=n-1; j>i; j--){ int perv_hash = lolita.get_hash(x[j], 0,mid); if(perv_hash == my_hash){ ok = false; break; } } if(ok){ ub = mid; }else{ lb = mid; } } ans[i] = ub; } for(int i=0; i