LightOJ1314---Names for babies

Source: Internet
Author: User

1314-names for Babies
PDF (中文版) Statistics Forum
Time Limit:4 second (s) Memory limit:32 MB

Long ago, there was a strange kingdom. Peoples of different religions, different cultures used to live there. But as they were different, their names were also different. So, in schools, offices, it is quite tough to call someone using his/her name, because some names were too nounced by persons from different culture.

So, the king made a plan. He took a string s and integers p and q and made a rule that names of the babies should is a substring of s, and the L Ength should be between P and Q (inclusive).

Now is given S, p and Q you have to find the number of distinct names that can be made.
Input

Input starts with an integer T (≤100), denoting the number of test cases.

Each case is starts with a line containing a string S. The length of S'll be between 2 and 10000 (inclusive) and S contains lowercase 中文版 letters only. The next line contains the integer p and q (1≤p≤q≤length (S)).
Output

For each case, print the case number and the number of distinct names that can is made.
Sample Input

Output for Sample Input

1

ABCdef

2 5

Case 1:14
Note

This problem is used in contest 12. But the length of S is between 2 and 100.

Obvious suffix array problem, statistics of different substrings, but only when the statistics, only those lengths in the range of requirements of the string

/************************************************************************* > File Name:LightOJ1314.cpp > Author:alex > Mail: [email protected] > Created time:2015 April 09 Thursday 19:52 15 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;Charstr[12000];classsuffixarray{ Public:Static Const intN =12000;intInit[n];intX[n];intY[n];intRank[n];intSa[n];intHeight[n];intBuc[n];intLog[n];intdp[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)//m is generally the maximum value of +1{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; }        }//preprocessing logarithm of each number for RMQ, constant optimization        voidInitlog () {log[0] = -1; for(inti =1; i < N; ++i) {Log[i] = (I & (i-1)) ? Log[i-1]: Log[i-1] +1; }        }voidINITRMQ () {initlog ();intn = size;intLimit for(inti =0; I < n; ++i) {dp[i][0] = Height[i]; } for(intj =1; J <= Log[n]; ++J) {limit = (N-(1<< j)); for(inti =0; I <= limit; ++i) {Dp[i][j] = min (Dp[i][j-1], Dp[i + (1<< (J-1))][j-1]); }            }        }intLCP (intAintb) {intT            A = Rank[a]; b = Rank[b];if(A > B)            {Swap (A, b);            }--b; t = log[b-a +1];returnMin (dp[a][t], dp[b-(1<< T) +1][t]); }voidSolveintPintQ) {intAns =0; for(inti =1; I <= size; ++i) {//Actual length range                 intr = Size-sa[i];intL = height[i-1] +1;if(q <= r && q >= L) {ans + = Q-max (l, p) +1; }Else if(q > R && P <= r) {ans + = R-max (l, p) +1; }            }printf("%d\n", ans); }}sa;intMain () {intTintP, q;scanf("%d", &t);intIcase =1; while(t--) {sa.clear ();scanf('%s ', str);scanf("%d%d", &p, &q);intLen =strlen(str); for(inti =0; i < Len; ++i) {Sa.insert (Str[i]-' A '+1); } Sa.getsa ( -); Sa.getheight ();printf("Case%d:", icase++);    Sa.solve (P, q); }return 0;}

LightOJ1314---Names for babies

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.