大概回忆一下, 想起哪个好写就写哪个, 求gank
思路:
1. 先求前缀和cnt1[i] 1<=i<=len
:表示前i个里面c1出现多少次
2. 枚举c2即可
#include<iostream>
#include<cstring>
using namespace std;
const int N = 5e5+10;
int cnt1[N];
int main()
{
int k;
cin >> k;
char c1, c2;
string s;
cin >> s >> c1 >> c2;
int len = s.size();
for(int i = 0; i < len; ++i){
if(s[i] == c1) cnt1[i+1] = cnt1[i]+1;
else cnt1[i+1] = cnt1[i];
}
long long res = 0;
for(int i = k-1; i < len; ++i){
if(s[i] == c2) res += cnt1[i-k+2];
}
cout << res;
return 0;
}
不知道对不对
+1 一样的写法