# | 제출 시각 | 아이디 | 문제 | 언어 | 결과 | 실행 시간 | 메모리 |
---|---|---|---|---|---|---|---|
22821 | 2017-04-30T07:42:15 Z | ↓우리보다잘하는팀(#947, ainta, gs12117, gs13068) | New Ocurrences (KRIII5_NO) | C++ | 1000 ms | 5552 KB |
#include<cstdio> #include<algorithm> using namespace std; int n, C[101000], ord1[101000], ord2[101000], SA[101000], Rank[101000], LCP[101000]; char p[101000]; struct AA{ int a, b; }w[101000]; void Suffix_Array(){ int i, M = max(n, 200), LL = 1; for(i=0;i<n;i++){ w[i].a = p[i],w[i].b = p[i+1]; } while(1){ for(i=0;i<=M;i++)C[i]=0; for(i=0;i<n;i++)C[w[i].b]++; for(i=1;i<=M;i++)C[i]+=C[i-1]; for(i=n-1;i>=0;i--)ord1[--C[w[i].b]] = i; for(i=0;i<=M;i++)C[i]=0; for(i=0;i<n;i++)C[w[i].a]++; for(i=1;i<=M;i++)C[i]+=C[i-1]; for(i=n-1;i>=0;i--)ord2[--C[w[ord1[i]].a]] = ord1[i]; int cnt = 0; for(i=0;i<n;i++){ if(i==0||w[ord2[i]].a!=w[ord2[i-1]].a||w[ord2[i]].b!=w[ord2[i-1]].b)cnt++; Rank[ord2[i]] = cnt; } if(cnt == n)break; LL<<=1; for(i=0;i<n;i++){ w[i].a = Rank[i]; if(i+LL >= n)w[i].b = 0; else w[i].b = Rank[i+LL]; } } for(i=0;i<n;i++)SA[Rank[i]] = i; int t = 0, x, y; for(i=0;i<n;i++){ x = Rank[i]; if(t)t--; if(x==n)continue; y = SA[x+1]; while(p[i+t]==p[y+t])t++; LCP[x] = t; } } int top, R[101000]; struct TP{ int l, b; }st[101000]; int main(){ int i, j; scanf("%s",p); for(i=0;p[i];i++); n = i; for(i=0;i<n/2;i++){ swap(p[i],p[n-i-1]); } Suffix_Array(); for(i=1;i<=n;i++){ int b = -1; while(top && st[top].l >= LCP[i-1]){ b = st[top].b; top--; } if(b!=-1){ st[++top] = {LCP[i-1],b}; } int pv = i-1; int r = 0, c = 0; for(j=top;st[j].l;j--){ while(pv>=1 && st[j].b <= pv){ if(SA[pv] > SA[i])c++; pv--; } r += (st[j].l-st[j-1].l) * c; } R[n-SA[i]] += r*2; st[++top] = {n - SA[i], i}; } top = 0; for(i=n;i>=1;i--){ int b = -1; while(top && st[top].l >= LCP[i]){ b = st[top].b; top--; } if(b!=-1){ st[++top] = {LCP[i],b}; } int pv = i+1; int r = 0, c = 0; for(j=top;st[j].l;j--){ while(pv<=n && st[j].b >= pv){ if(SA[pv] > SA[i])c++; pv++; } r += (st[j].l-st[j-1].l) * c; } R[n-SA[i]] += r*2; st[++top] = {n - SA[i], i}; } long long sum = 0; for(i=1;i<=n;i++){ sum = sum + i + R[i]; printf("%lld\n",sum); } }
Compilation message
# | 결과 | 실행 시간 | 메모리 | Grader output |
---|---|---|---|---|
1 | Correct | 0 ms | 5552 KB | Output is correct |
2 | Correct | 0 ms | 5552 KB | Output is correct |
3 | Correct | 0 ms | 5552 KB | Output is correct |
4 | Correct | 0 ms | 5552 KB | Output is correct |
5 | Correct | 13 ms | 5552 KB | Output is correct |
6 | Correct | 9 ms | 5552 KB | Output is correct |
7 | Correct | 23 ms | 5552 KB | Output is correct |
8 | Correct | 0 ms | 5552 KB | Output is correct |
9 | Correct | 0 ms | 5552 KB | Output is correct |
10 | Correct | 9 ms | 5552 KB | Output is correct |
11 | Correct | 9 ms | 5552 KB | Output is correct |
12 | Correct | 49 ms | 5552 KB | Output is correct |
13 | Correct | 49 ms | 5552 KB | Output is correct |
14 | Correct | 43 ms | 5552 KB | Output is correct |
15 | Correct | 43 ms | 5552 KB | Output is correct |
16 | Correct | 39 ms | 5552 KB | Output is correct |
17 | Correct | 33 ms | 5552 KB | Output is correct |
18 | Correct | 43 ms | 5552 KB | Output is correct |
19 | Correct | 23 ms | 5552 KB | Output is correct |
20 | Correct | 9 ms | 5552 KB | Output is correct |
21 | Correct | 16 ms | 5552 KB | Output is correct |
22 | Correct | 23 ms | 5552 KB | Output is correct |
23 | Correct | 9 ms | 5552 KB | Output is correct |
24 | Correct | 9 ms | 5552 KB | Output is correct |
25 | Correct | 9 ms | 5552 KB | Output is correct |
26 | Correct | 6 ms | 5552 KB | Output is correct |
27 | Correct | 6 ms | 5552 KB | Output is correct |
28 | Correct | 6 ms | 5552 KB | Output is correct |
29 | Correct | 9 ms | 5552 KB | Output is correct |
30 | Correct | 9 ms | 5552 KB | Output is correct |
# | 결과 | 실행 시간 | 메모리 | Grader output |
---|---|---|---|---|
1 | Correct | 6 ms | 5552 KB | Output is correct |
2 | Correct | 476 ms | 5552 KB | Output is correct |
3 | Correct | 673 ms | 5552 KB | Output is correct |
4 | Execution timed out | 1000 ms | 5552 KB | Execution timed out |
5 | Halted | 0 ms | 0 KB | - |