0
char patern[]="AGAAGAG";
int n = strlen(patern);
int pat[n];
for (int i = 0; i < n; i++)
{
    int j = 0;
    while(j <= i )
    {
        if (patern.substr(0, j) == patern.substr(i-j+1, j))
        {
            pat[i] = j;
        }
        j++;
    }
    cout<<pat[i]<<endl;
}

I can only use <iostream>, <cstring>, <cstdio>, <cstdlib>. The output is 0011232.

Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770
zead seam
  • 31
  • 5

1 Answers1

1

As character arrays do not have a method like substr then an alternative is using the C standard function strncmp declared in the header <cstring>. For example

if ( strncmp( patern, patern + i-j+1, j ) == 0 )
Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335