题解 | String Matching

#include <bits/stdc++.h>
#include <vector>
using namespace std;

vector<int> match(string s,string t){
    vector<int>ans;
    int i,j,n=s.size(),m=t.size();
    for(i=0;i<n-m+1;i++){
        for(j=0;j<m;j++){
            if(s[i+j]!=t[j])break;
        }
        if(j==m)ans.push_back(i);
    }
    return ans;
}

int main(){
    string s,t;
    while(cin>>s>>t){
        cout<<match(s, t).size()<<endl;
    }
}

数据量不大,直接暴力匹配了

全部评论

相关推荐

练习JAVA时长两年半:qps 30000
点赞 评论 收藏
分享
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客企业服务