hdu3518---Boring counting (suffix array, group suffix)

Source: Internet
Author: User

Problem Description
035 now faced a tough problem,his 中文版 teacher gives him a String,which consists with n lower case Letter,he must figur E out how many substrings appear at least twice,moreover,such apearances can not overlap each other.
Take the AAAA as an example. " A "apears four times," AA "apears-without overlaping.however,aaa can ' t apear more than one time without overlapin G.since we can get "AAA" from [0-2] (the position of string begins with 0) and [1-3]. But the interval [0-2] and [1-3] overlaps each other. So "AAA" can does not take into account. Therefore,the answer is 2 ("a", and "AA").

Input
The input data consist with several test cases. The input ends with a line "#". Each test case contain a string consists with lower letter,the length n won ' t exceed (n <= 1000).

Output
For all test case output a integer Ans,which represent the answer for the test case.you ' d better use int64 to avoid Unne Cessary trouble.

Sample Input

Aaaa
Ababcabb
Aaaaaa
#

Sample Output

2
3
3

Source
ACM-ICPC multi-university Training Contest (9)--host by Hnu

Recommend

Find the suffix array of the string, enumerate the lengths, and then group the suffixes, because each set of suffixes they rank consecutively, so they have the same prefix, so that there is no repetition of the counting problem, note that each set of suffixes, the most forward and trailing suffix position appears, almost 1000* 500 complexity

/************************************************************************* > File Name:HDU3518.cpp > Auth Or:alex > Mail: [email protected] > Created time:2015 April 09 Thursday 13:35 42 seconds ******************************** ****************************************/#include <functional>#include <algorithm>#include <iostream>#include <fstream>#include <cstring>#include <cstdio>#include <cmath>#include <cstdlib>#include <queue>#include <stack>#include <map>#include <bitset>#include <set>#include <vector>using namespace STD;Const DoublePI =ACOs(-1.0);Const intINF =0x3f3f3f3f;Const DoubleEPS =1e-15;typedef Long LongLL;typedefPair <int,int> PLL;classsuffixarray{ Public:Static Const intN = -;intInit[n];intX[n];intY[n];intRank[n];intSa[n];intHeight[n];intBuc[n];intSizevoidClear () {size =0; }voidInsertintN) {init[size++] = n; }BOOLcmpint*r,intAintBintL) {return(R[a] = = R[b] && r[a + l] = = R[b + L]); }voidGetsa (intm = the) {Init[size] =0;intL, p, *x = x, *y = y, n = size +1; for(inti =0; I < m; ++i) {Buc[i] =0; } for(inti =0; I < n;            ++i) {++buc[x[i] = init[i]]; } for(inti =1; I < m; ++i) {Buc[i] + = buc[i-1]; } for(inti = n-1; I >=0;            -i) {sa[--buc[x[i]] = i; } for(L =1, p =1; L <= N && p < n; m = p, l *=2) {p =0; for(inti = n-l; I < n;                ++i) {y[p++] = i; } for(inti =0; i < n; ++i) {if(Sa[i] >= L)                    {y[p++] = sa[i]-l; }                } for(inti =0; I < m; ++i) {Buc[i] =0; } for(inti =0; I < n;                ++i) {++buc[x[y[i]]; } for(inti =1; I < m; ++i) {Buc[i] + = buc[i-1]; } for(inti = n-1; I >=0;                -i) {sa[--buc[x[y[i]]] = y[i]; }intI for(Swap (x, y), x[sa[0]] =0, p =1, i =1; I < n; ++i) {X[sa[i]] = cmp (y, Sa[i-1], Sa[i], L)? P-1: p++; }            }        }voidGetHeight () {inth =0, n = size; for(inti =0; I <= N;            ++i) {Rank[sa[i]] = i; } height[0] =0; for(inti =0; I < n; ++i) {if(H >0) {--h; }intJ =sa[rank[i]-1]; for(; i + H < n && j + H < n && init[i + h] = = Init[j + h]; ++h); Height[rank[i]-1] = h; }        }voidSolve () {intAns =0; for(inti =1; I <= size/2; ++i) {intIl = sa[1], IR = sa[1]; for(intj =1; J < size; ++J) {if(Height[j] >= i) {il = min (il, sa[j +1]); IR = max (IR, sa[j +1]); }Else{if(Ir-il >= i)                        {++ans; } ir = il = sa[j +1]; }                }if(Ir-il >= i)                {++ans; }            }printf("%d\n", ans); }}sa;Charstr[ -];intMain () { while(~scanf('%s ', str)) {if(str[0] ==' # ')        { Break; } sa.clear ();intLen =strlen(str); for(inti =0; i < Len; ++i) {Sa.insert (Str[i]-' A '+1); } Sa.getsa ( -);        Sa.getheight ();    Sa.solve (); }return 0;}

hdu3518---Boring counting (suffix array, group suffix)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.