lanqiao3514 字串简写
This commit is contained in:
33
14lanqiao/test7-1.cpp
Normal file
33
14lanqiao/test7-1.cpp
Normal file
@@ -0,0 +1,33 @@
|
|||||||
|
// lanqiao3514 字串简写(枚举)
|
||||||
|
#include<bits/stdc++.h>
|
||||||
|
using namespace std;
|
||||||
|
#define int long long
|
||||||
|
signed main(){
|
||||||
|
int k;
|
||||||
|
string s;
|
||||||
|
char a, b;
|
||||||
|
cin >> k >> s >> a >> b;
|
||||||
|
vector<int> ft, bk;
|
||||||
|
for(int i = 0; i < s.size(); i++){
|
||||||
|
if(a == s[i]) ft.push_back(i);
|
||||||
|
}
|
||||||
|
for(int i = 0; i < s.size(); i++){
|
||||||
|
if(b == s[i]) bk.push_back(i);
|
||||||
|
}
|
||||||
|
int ans = 0;
|
||||||
|
for(int i = 0; i < ft.size(); i++){
|
||||||
|
for(int j = 0; j < bk.size(); j++){
|
||||||
|
if( bk[j] - ft[i] + 1 < k) continue;
|
||||||
|
else{
|
||||||
|
ans += bk.size() - j; // 1 2 1 1 1
|
||||||
|
break; // 找到一个符合条件的,直接把后面全加上,然后跳出此次循环
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
cout << ans << endl;
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
/* samples -> 6
|
||||||
|
4
|
||||||
|
abababdb a b
|
||||||
|
*/
|
||||||
21
14lanqiao/test7-2.cpp
Normal file
21
14lanqiao/test7-2.cpp
Normal file
@@ -0,0 +1,21 @@
|
|||||||
|
// lanqiao3514 字串简写(前缀和优化)
|
||||||
|
#include<bits/stdc++.h>
|
||||||
|
using namespace std;
|
||||||
|
#define int long long
|
||||||
|
signed main(){
|
||||||
|
int k;
|
||||||
|
string s;
|
||||||
|
char a, b;
|
||||||
|
cin >> k >> s >> a >> b;
|
||||||
|
int ft = 0;
|
||||||
|
int bk = 0;
|
||||||
|
for(int i = 0, j = k - 1; j < s.size(); i++, j++){
|
||||||
|
if(s[i] == a) ft++;
|
||||||
|
if(s[j] == b) bk += ft;
|
||||||
|
}
|
||||||
|
cout << bk << endl;
|
||||||
|
}
|
||||||
|
/* samples -> 6
|
||||||
|
4
|
||||||
|
abababdb a b
|
||||||
|
*/
|
||||||
Reference in New Issue
Block a user