// #includes {{{ #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include using namespace std; // }}} // pre-written code {{{ #define REP(i,n) for(int i=0;i<(int)(n);++i) #define RREP(i,a,b) for(int i=(int)(a);i<(int)(b);++i) #define FOR(i,c) for(__typeof((c).begin()) i=(c).begin();i!=(c).end();++i) #define LET(x,a) __typeof(a) x(a) //#define IFOR(i,it,c) for(__typeof((c).begin())it=(c).begin();it!=(c).end();++it,++i) #define ALL(c) (c).begin(), (c).end() #define MP make_pair #define EXIST(e,s) ((s).find(e)!=(s).end()) #define RESET(a) memset((a),0,sizeof(a)) #define SET(a) memset((a),-1,sizeof(a)) #define PB push_back #define DEC(it,command) __typeof(command) it=command //debug #define dump(x) cerr << #x << " = " << (x) << endl; #define debug(x) cerr << #x << " = " << (x) << " (L" << __LINE__ << ")" << " " << __FILE__ << endl; #define debug2(x) cerr << #x << " = [";REP(__ind,(x).size()){cerr << (x)[__ind] << ", ";}cerr << "] (L" << __LINE__ << ")" << endl; const int INF=0x3f3f3f3f; typedef long long Int; typedef unsigned long long uInt; #ifdef __MINGW32__ typedef double rn; #else typedef long double rn; #endif typedef pair pii; /* #ifdef MYDEBUG #include"debug.h" #include"print.h" #endif */ // }}} //{{{ Suffix Array //{{{ for DEBUG void writeSuffix(char *t,int i){ cout<<(t+i)<<"$"< 0) --h; } } //}}} //{{{ call RMQ = buildRMQ(lcp, n+1) void buildRMQ() { int logn = 1; for (int k = 1; k < N; k *= 2) ++logn; rmq=new int[N*logn]; int *b = rmq; copy(lcp, lcp+N, b); for (int k = 1; k < N; k *= 2) { copy(b, b+N, b+N); b += N; REP(i, N-k) b[i] = min(b[i], b[i+k]); } } //}}} //{{{ inner LCP computation with RMQ: O(1) int minimum(int x, int y) { x++; int z = y - x, k = 0, e = 1, s; // y - x >= e = 2^k 縺ェ繧区怙螟ァ k s = ( (z & 0xffff0000) != 0 ) << 4; z >>= s; e <<= s; k |= s; s = ( (z & 0x0000ff00) != 0 ) << 3; z >>= s; e <<= s; k |= s; s = ( (z & 0x000000f0) != 0 ) << 2; z >>= s; e <<= s; k |= s; s = ( (z & 0x0000000c) != 0 ) << 1; z >>= s; e <<= s; k |= s; s = ( (z & 0x00000002) != 0 ) << 0; z >>= s; e <<= s; k |= s; return min( rmq[x+N*k], rmq[y+N*k-e+1] ); } //}}} //{{{ outer LCP computation: O(m - o) int computeLCP(char *t, int n, char *p, int m, int o, int k) { int i = o; for (; i < m && k+i < n && p[i] == t[k+i]; ++i); return i; } //}}} //{{{ Mamber-Myers's O(m + log n) string matching with LCP/RMQ #define COMP(h, k) (h == m || (k+h= B) { if (lh < A) l = k; else if (lh > A) r = k, rh = A; else { int i = computeLCP(t, N, p, m, A, sa[k]); if (COMP(i, sa[k])) r = k, rh = i; else l = k, lh = i; } } else { if (rh < B) r = k; else if (rh > B) l = k, lh = B; else { int i = computeLCP(t, N, p, m, B, sa[k]); if (COMP(i, sa[k])) r = k, rh = i; else l = k, lh = i; } } } return rh == m ? sa[r] : -1; } //}}} }; //}}} uInt p = 137; char S[500010]; int main(){ cin>>S; SuffixArray sa(S); sa.builds(); return 0; }