查找等于s1的s2所有子序列的索引

时间:2019-07-16 09:42:51

标签: c++ algorithm

编写递归算法以查找字符串s2中所有出现的字符串s1。

我尝试将s1的最后一个字符与s2的最后一个字符匹配。 如果匹配 然后比较s1的倒数第二个字符和s2的倒数第二个字符,依此类推。 其他 比较s1的最后一个字符和s2的倒数第二个字符。

int countOcc(string a, string b, int m, int n)
{
if((m==0 && n==0)|| n==0)
        return 1;
if(m==0)
        return 0;
if(a[m-1] == a[n-1]){
            cout << m<<" "<< a[m-1] <<" ";
        return countOcc(a,b,m-1,n-1);
}
else
        return countOcc(a,b,m-1,n);
}
int main()
{
    string s2 = "this is test string";
    string s1 = "this";
    countOcc(s2,s1,s2.size(),s1.size());
    return 0;
}

,但是此算法未打印预期结果。我的预期结果是 (0,1,2,3),(0,1,2,6),(0,1,2,12),(0,1,2,15),(0,1,5,6), (0,1,5,12),依此类推。

0 个答案:

没有答案