字符串统计子串的个数和第一次出现的位置

#include<bits/stdc++.h>
//字符串统计子串的个数和第一次出现的位置
using namespace std;

vector<int> fun(string a, string b){
    int first = -1, cnt = 0;
    for(int i = 0;i <a.size(); ++i){
        int j = i, k = 0;
        while(a[j] == b[k] && j < a.size() && k < b.size()){
            j++;
            k++;
        }
        if(k == b.size()){
            if(first == -1){
                first = i;
            }
            cnt++;
        }
    }
    return {first, cnt};
}

int main()
{
    string a = "123,45621234,612.78312357";
    string b = "123";
    vector<int> ans = fun(a, b);
    cout<<ans[0]<<" "<<ans[1]<<endl;
    return 0;
}

猜你喜欢

转载自blog.csdn.net/qq_24624539/article/details/108802140