腾讯-计算机视觉笔试题 ac
第一题:字符串系数
#include <bits/stdc++.h> using namespace std; vector<string> get(string &str,int k){ vector<string> v; if(str.size()<k||k<=0) return v; for(int i=0;i<str.size()-k;++i) { string s=""; for(int j=i;j<i+k;j++) { s+=str[j]; } v.push_back(s); } sort(v.begin(),v.end()); v.erase(unique(v.begin(),v.end()), v.end()); return v; } int fun(const std::string &str, const std::string& sub) { int num = 0; for (size_t i=0; (i=str.find(sub,i)) != std::string::npos; num++, i++); return num; } int main() { int k; cin>>k; string stra; cin>>stra; string strb; cin>>strb; vector<string> s; s=get(stra,k); int result=0; for(int i=0;i<s.size();++i) { int cnt=fun(strb,s[i]);//错的:int cnt=std::count(strb.begin(),strb.end(),s[i]); result+=cnt; } cout<<result<<endl; return 0; }
#腾讯##笔试题目##题解#