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;
}
我只能使用<iostream>
、<cstring>
、<cstdio>
、<cstdlib>
。输出为0011232
。
由于字符数组没有类似substr
的方法,因此另一种选择是使用标头<cstring>
中声明的C标准函数strncmp
。例如
if ( strncmp( patern, patern + i-j+1, j ) == 0 )