vendredi 28 juillet 2017

Substring query, count no. of length L occuring P times

You are given a string S and N queries. Each query is defined by two integers - Li and Pi. Count the number of strings of the length Li that occur exactly Pi times (as the consecutive substrings) in the string S.

Example 1

Input

abacaba

1

3 2

Output

aba

Explanation We need substrings of length 3 occurring twice in S. Clearly, aba is the only such substring

Example 2

Input

mynameispujanshahandmybrothersnameischintanshah

1

4 2

Output

name

amei

meis

ansh

nsha

shah

Explanation We need substrings of length 4 occurring twice.

Here is the code to count the number of the substrings, but what I want is to print out the substrings itself.

How do I print the substrings itself?

     int main(){

     string s;

     int q,l,p;

     cin>>s;

     cin>>q;

     while(q--){

     cin>>l>>p;

     int cnt =0;

     string ss,ss1;

     for(int i=0;i<=s.size()-l;i++){

     ss=s.substr(i,l);

     int tot = 0;

    for(int j=0;j<=s.size()-l;j++){

    ss1 = s.substr(j,l);

    if(ss==ss1){

    if(j<i)

    break;

    else

     tot++;
  }

}

if(tot==p)

    cnt++;

     }

       printf("%d\n",cnt);

         }

         return 0;

    }

Aucun commentaire:

Enregistrer un commentaire