Contest Duration: ~ (local time) (100 minutes) Back to Home

Submission #7556684

Source Code Expand

Copy
```#include<bits/stdc++.h>
using namespace std;
using ll = long long;
using VI = vector<ll>;
using VV = vector<VI>;
using VS = vector<string>;

template<class T>bool chmax(T &a, const T &b) { if (a<b) { a=b; return 1; } return 0; }
template<class T>bool chmin(T &a, const T &b) { if (b<a) { a=b; return 1; } return 0; }

#define FOR(i,a,b) for(ll i=(a);i<(b);++i)
#define rep(i,b) FOR(i, 0, b)
#define ALL(v) (v).begin(), (v).end()
#define p(s) cout<<(s)<<endl
#define p2(s, t) cout << (s) << " " << (t) << endl
#define br() p("")
#define pn(s) cout << (#s) << " " << (s) << endl
#define p_yes() p("YES")
#define p_no() p("NO")
#define pb(s) push_back(s)
#define SZ(x) ((int)(x).size())

void no(){p_no(); exit(0);}
void yes(){p_yes(); exit(0);}

const ll mod = 1e9 + 7;
const ll inf = 1e18;

// Sparse Table
template<class MeetSemiLattice> struct SparseTable {
vector<vector<MeetSemiLattice> > dat;
vector<int> height;

SparseTable() { }
SparseTable(const vector<MeetSemiLattice> &vec) { init(vec); }
void init(const vector<MeetSemiLattice> &vec) {
int n = (int)vec.size(), h = 0;
while ((1<<h) < n) ++h;
dat.assign(h, vector<MeetSemiLattice>(1<<h));
height.assign(n+1, 0);
for (int i = 2; i <= n; i++) height[i] = height[i>>1]+1;
for (int i = 0; i < n; ++i) dat[0][i] = vec[i];
for (int i = 1; i < h; ++i)
for (int j = 0; j < n; ++j)
dat[i][j] = min(dat[i-1][j], dat[i-1][min(j+(1<<(i-1)),n-1)]);
}

MeetSemiLattice get(int a, int b) {
return min(dat[height[b-a]][a], dat[height[b-a]][b-(1<<height[b-a])]);
}
};

// Suffix Array ( Manber&Myers: O(n (logn)^2) )
struct SuffixArray {
string str;
vector<int> sa;         // sa[i] : the starting index of the i-th smallest suffix (i = 0, 1, ..., n)
vector<int> lcp;        // lcp[i]: the lcp of sa[i] and sa[i+1] (i = 0, 1, ..., n-1)
inline int& operator [] (int i) {return sa[i];}

SuffixArray(const string& str_) : str(str_) { buildSA(); calcLCP(); }
void init(const string& str_) { str = str_; buildSA(); calcLCP(); }

// build SA
vector<int> rank_sa, tmp_rank_sa;
struct CompareSA {
int n, k;
const vector<int> &rank;
CompareSA(int n, int k, const vector<int> &rank_sa) : n(n), k(k), rank(rank_sa) {}
bool operator()(int i, int j) {
if (rank[i] != rank[j]) return (rank[i] < rank[j]);
else {
int rank_ik = (i + k <= n ? rank[i + k] : -1);
int rank_jk = (j + k <= n ? rank[j + k] : -1);
return (rank_ik < rank_jk);
}
}
};
void buildSA() {
int n = (int)str.size();
sa.resize(n+1), lcp.resize(n+1), rank_sa.resize(n+1), tmp_rank_sa.resize(n+1);
for (int i = 0; i < n; ++i) sa[i] = i, rank_sa[i] = (int)str[i];
sa[n] = n, rank_sa[n] = -1;
for (int k = 1; k <= n; k *= 2) {
CompareSA csa(n, k, rank_sa);
sort(sa.begin(), sa.end(), csa);
tmp_rank_sa[sa[0]] = 0;
for (int i = 1; i <= n; ++i) {
tmp_rank_sa[sa[i]] = tmp_rank_sa[sa[i - 1]];
if (csa(sa[i - 1], sa[i])) ++tmp_rank_sa[sa[i]];
}
for (int i = 0; i <= n; ++i) rank_sa[i] = tmp_rank_sa[i];
}
}
vector<int> rsa;
SparseTable<int> st;
void calcLCP() {
int n = (int)str.size();
rsa.resize(n+1);
for (int i = 0; i <= n; ++i) rsa[sa[i]] = i;
lcp.resize(n+1);
lcp[0] = 0;
int cur = 0;
for (int i = 0; i < n; ++i) {
int pi = sa[rsa[i] - 1];
if (cur > 0) --cur;
for (; pi + cur < n && i + cur < n; ++cur) {
if (str[pi + cur] != str[i + cur]) break;
}
lcp[rsa[i] - 1] = cur;
}
st.init(lcp);
}

// calc lcp
int getLCP(int a, int b) {          // lcp of str.sutstr(a) and str.substr(b)
return st.get(min(rsa[a], rsa[b]), max(rsa[a], rsa[b]));
}
};

int main(){
cin.tie(0);
ios::sync_with_stdio(false);

// input
ll N; cin >> N;
string s; cin >> s;

SuffixArray SA(s);
ll ma = 0;
rep(i, N){
FOR(j, i+1, N){
// i以降の文字列と
// j以降の文字列
ll lcp = SA.getLCP(i, j);
ll a = min(j-i, lcp);
chmax(ma, a);
}
}

p(ma);

return 0;
}

```

#### Submission Info

Submission Time 2019-09-16 14:49:29+0900 E - Who Says a Pun? peroon C++14 (GCC 5.4.1) 500 4625 Byte AC 80 ms 896 KB

#### Judge Result

Set Name Sample All
Score / Max Score 0 / 0 500 / 500
Status
 AC × 3
 AC × 70
Set Name Test Cases
Sample 00-sample-00, 00-sample-01, 00-sample-02
All 00-sample-00, 00-sample-01, 00-sample-02, 01-handmade-03, 01-handmade-04, 01-handmade-05, 01-handmade-06, 01-handmade-07, 01-handmade-08, 01-handmade-09, 01-handmade-10, 01-handmade-11, 01-handmade-12, 02-binary-13, 02-binary-14, 02-binary-15, 02-binary-16, 02-binary-17, 02-binary-18, 02-binary-19, 02-binary-20, 02-binary-21, 02-binary-22, 02-binary-23, 03-BigRandom-24, 03-BigRandom-25, 03-BigRandom-26, 03-BigRandom-27, 03-BigRandom-28, 03-BigRandom-29, 03-BigRandom-30, 03-BigRandom-31, 03-BigRandom-32, 03-BigRandom-33, 03-BigRandom-34, 03-BigRandom-35, 03-BigRandom-36, 03-BigRandom-37, 03-BigRandom-38, 03-BigRandom-39, 03-BigRandom-40, 03-BigRandom-41, 03-BigRandom-42, 03-BigRandom-43, 03-BigRandom-44, 03-BigRandom-45, 03-BigRandom-46, 03-BigRandom-47, 03-BigRandom-48, 03-BigRandom-49, 03-BigRandom-50, 03-BigRandom-51, 03-BigRandom-52, 03-BigRandom-53, 03-BigRandom-54, 04-zero-55, 04-zero-56, 05-AllRandom-57, 05-AllRandom-58, 05-AllRandom-59, 05-AllRandom-60, 05-AllRandom-61, 05-AllRandom-62, 05-AllRandom-63, 05-AllRandom-64, 05-AllRandom-65, 05-AllRandom-66, 05-AllRandom-67, 05-AllRandom-68, 05-AllRandom-69
Case Name Status Exec Time Memory
00-sample-00 1 ms 256 KB
00-sample-01 1 ms 256 KB
00-sample-02 1 ms 256 KB
02-binary-13 48 ms 512 KB
02-binary-14 58 ms 768 KB
02-binary-15 44 ms 512 KB
02-binary-16 66 ms 768 KB
02-binary-17 67 ms 768 KB
02-binary-18 58 ms 768 KB
02-binary-19 35 ms 512 KB
02-binary-20 42 ms 512 KB
02-binary-21 39 ms 512 KB
02-binary-22 37 ms 512 KB
02-binary-23 32 ms 512 KB
03-BigRandom-24 66 ms 768 KB
03-BigRandom-25 65 ms 768 KB
03-BigRandom-26 64 ms 768 KB
03-BigRandom-27 78 ms 768 KB
03-BigRandom-28 69 ms 768 KB
03-BigRandom-29 75 ms 768 KB
03-BigRandom-30 69 ms 768 KB
03-BigRandom-31 71 ms 768 KB
03-BigRandom-32 77 ms 768 KB
03-BigRandom-33 65 ms 768 KB
03-BigRandom-34 73 ms 768 KB
03-BigRandom-35 63 ms 768 KB
03-BigRandom-36 77 ms 768 KB
03-BigRandom-37 71 ms 768 KB
03-BigRandom-38 65 ms 768 KB
03-BigRandom-39 74 ms 768 KB
03-BigRandom-40 68 ms 768 KB
03-BigRandom-41 74 ms 768 KB
03-BigRandom-42 71 ms 768 KB
03-BigRandom-43 66 ms 768 KB
03-BigRandom-44 68 ms 768 KB
03-BigRandom-45 76 ms 768 KB
03-BigRandom-46 63 ms 768 KB
03-BigRandom-47 78 ms 768 KB
03-BigRandom-48 68 ms 768 KB
03-BigRandom-49 68 ms 768 KB
03-BigRandom-50 78 ms 768 KB
03-BigRandom-51 73 ms 768 KB
03-BigRandom-52 76 ms 768 KB
03-BigRandom-53 77 ms 768 KB
03-BigRandom-54 75 ms 768 KB
04-zero-55 1 ms 256 KB
04-zero-56 1 ms 256 KB
05-AllRandom-57 77 ms 768 KB
05-AllRandom-58 69 ms 768 KB
05-AllRandom-59 78 ms 768 KB
05-AllRandom-60 74 ms 768 KB
05-AllRandom-61 73 ms 768 KB
05-AllRandom-62 72 ms 768 KB
05-AllRandom-63 69 ms 768 KB
05-AllRandom-64 69 ms 768 KB
05-AllRandom-65 71 ms 768 KB
05-AllRandom-66 73 ms 768 KB
05-AllRandom-67 72 ms 768 KB
05-AllRandom-68 80 ms 768 KB
05-AllRandom-69 79 ms 768 KB