G Donald and substring (---string, suffix array)

Source: Internet
Author: User

Time limit per test:1.0 seconds

Memory limit:256 Megabytes

A substring is defined as a sequence of characters that appear in a string. Here, we uses[l... R] to indicatesString fromLToR(closed interval) substring. In the subject, the string subscript from0Begin. Obviously, for a length ofthe string of N is a total of n(n+1)2 substrings.

For a given strings, Donald givesQInquiry, sectionIThe query includes three parametersli,Ri,zi , asked in s[l... RI] How many of the substrings are exactly zi.

Input

The input has the following form:

SQL1R1Z1L2 r2  z2 ?lq rq  zq

The first line is a string s.

The second line is an integer q.

Next per line: first two integersli,rI (0≤li≤rI<| S| ), followed by a non-empty string zi. integers and integers, separated by a single space between integers and strings.

Only a few lowercase English letters will appear in the string.

Data size Conventions:

  • For easy files:1≤|S| ≤,q≤∑| Zi| ≤ 。
  • For hard files:1≤|S|≤5,q≤∑| Zi| ≤5 。
Output

For each query, output an integer that represents the answer.

Examplesinput
THISISAGARBAGECOMPETITIONHAHAHA50 A1 5 Is25 Hah6 Ag7
Output
62221
Test instructions

Given a template string x, the number of occurrences of the match string at each given point in the template string x [L,r] interval.

Own ideas:

Small data, KMP water over.

Official:

Suffix array + tree-like array.

Emmmm, think of will suffix automata didn't want to come out, GG.

Small Data
#include <cstdio>#include<cstdlib>#include<iostream>#include<cstring>#include<string>#include<algorithm>#include<memory.h>using namespacestd;Chara[1000010],b[1000010];intnext[1000010],l1,l2,ans,x,y;void_next () {inti,k; next[1]=0; for(i=2, k=0; i<=l2;i++){ while(k&&b[k+1]!=b[i]) k=Next[k]; if(b[k+1]==b[i]) k++; Next[i]=K; }}void_kmp () {_next (); inti,k; Ans=0; for(i=x,k=0; i<=y;i++){ while(k&&b[k+1]!=a[i]) k=Next[k]; if(b[k+1]==a[i]) k++; if(k==l2&&i-l2+1>=x) {ans++; k=Next[k]; } }}intMain () {intQ; scanf ("%s", A +1); scanf ("%d",&q); L1=strlen (A +1); while(q--) {ans=0; scanf ("%d%d",&x,&y); X++;y++; scanf ("%s", B +1); L2=strlen (b +1); _next (); _KMP (); printf ("%d\n", ans); } return 0;}

G Donald and substring (---string, suffix array)

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.