This documentation is automatically generated by online-judge-tools/verification-helper
View the Project on GitHub beet-aizu/library
// verification-helper: PROBLEM https://judge.yosupo.jp/problem/zalgorithm #include <bits/stdc++.h> using namespace std; #define call_from_test #include "../../io/space.cpp" #include "../../string/zalgorithm.cpp" #undef call_from_test signed main(){ cin.tie(0); ios::sync_with_stdio(0); string s; cin>>s; auto zs=zalgorithm(s); zs.pop_back(); space(zs); return 0; }
#line 1 "test/yosupo/zalgorithm.test.cpp" // verification-helper: PROBLEM https://judge.yosupo.jp/problem/zalgorithm #include <bits/stdc++.h> using namespace std; #define call_from_test #line 1 "io/space.cpp" #line 3 "io/space.cpp" using namespace std; #endif //BEGIN CUT HERE template<typename T> void space(const vector<T> &vs){ for(size_t i=0;i<vs.size();i++){ if(i) cout<<' '; cout<<vs[i]; } cout<<'\n'; } //END CUT HERE #ifndef call_from_test signed main(){ return 0; } #endif #line 1 "string/zalgorithm.cpp" #line 3 "string/zalgorithm.cpp" using namespace std; #endif //BEGIN CUT HERE // longest common prefix of s and s[i:n] // return n + 1 elements for run enumerate template<typename T> vector<int> zalgorithm(vector<T> vs){ int n=vs.size(); vector<int> as(n+1,0); as[0]=n; int i=1,j=0; while(i<n){ while(i+j<n and vs[j]==vs[i+j]) j++; as[i]=j; if(j==0){ i++; continue; } int k=1; while(i+k<n and k+as[k]<j) as[i+k]=as[k],k++; i+=k; j-=k; } return as; } vector<int> zalgorithm(string s){ return zalgorithm(vector<char>(s.begin(),s.end())); } //END CUT HERE #ifndef call_from_test signed main(){ return 0; } #endif #line 9 "test/yosupo/zalgorithm.test.cpp" #undef call_from_test signed main(){ cin.tie(0); ios::sync_with_stdio(0); string s; cin>>s; auto zs=zalgorithm(s); zs.pop_back(); space(zs); return 0; }