求以下问题的完整代码

求以下问题的完整代码,要求使用c++面向对象的程序设计方法和构造函数

img

img

最后一个数字后面无空格,在输出的时候需要根据找到的位置个数进行控制。
运行结果:

img

代码:

#include <iostream>
using namespace std;

class StrFind
{
public:
    StrFind()  {}
    
    void find(const char* p1,const char* p2)
    {
        int cnt = 0;
        for (int i = 0; i <= strlen(p2) - strlen(p1); i++)
        {
            int j = 0;
            for (; j < strlen(p1); j++)
            {
                if (p1[j] != p2[i + j])
                    break;
            }
            if (j == strlen(p1))
            {
                cnt++;
                if (cnt == 1)
                    cout << i;
                else
                    cout << " " << i;
            }
        }
        if (cnt == 0)
            cout << "-1";
    }
};

int main()
{
    char p1[10001] = { 0 };
    char p2[10001] = { 0 };
    cin.getline(p1,10000);
    cin.getline(p2,10000);
    StrFind ph;
    ph.find(p1,p2);
    return 0;
}

题目有歧义
比如aaaaaaa中寻找aa,是输出0 2 4,还是0 1 2 3 4 5

def find_occurrences(s1, s2):
    index = s2.find(s1)
    if index == -1:
        return -1
    else:
        occurrences = [index]
        while True:
            index = s2.find(s1, index + 1)
            if index == -1:
                break
            occurrences.append(index)
        return ' '.join(map(str, occurrences))

s1 = input()
s2 = input()
print(find_occurrences(s1, s2))

您好,我是有问必答小助手,您的问题已经有小伙伴帮您解答,感谢您对有问必答的支持与关注!
PS:问答VIP年卡 【限时加赠:IT技术图书免费领】,了解详情>>> https://vip.csdn.net/askvip?utm_source=1146287632