解题思路:
分别记录首字母和尾字母出现的位置,然后遍历数组得出结果。
注意事项:
降低时间复杂度。
参考代码:
#include <stdio.h> #include <iostream> #include <string> #include <vector> using namespace std; int main(){ int k; scanf("%d", &k); string s; cin >> s; char a,b; getchar(); scanf("%c", &a); getchar(); scanf("%c", &b); long long res = 0; vector<int> start, end; for(int i = 0; i < s.size(); i++){ if(s[i] == a){ start.push_back(i); } if(s[i] == b){ end.push_back(i); } } int tmp = 0; for(int i = 0; i < start.size(); i++){ for(int j = tmp; j < end.size(); j++){ if(end[j] - start[i] < k-1){ continue; }else{ res += end.size() - j; tmp = j; break; } } } printf("%lld", res); return 0; }
0.0分
0 人评分
wu-理财计划 (C++代码)浏览:835 |
C语言程序设计教程(第三版)课后习题9.6 (C语言代码)浏览:585 |
C语言程序设计教程(第三版)课后习题6.1 (C语言代码)浏览:548 |
C语言程序设计教程(第三版)课后习题5.6 (C语言代码)浏览:902 |
C语言程序设计教程(第三版)课后习题9.10 (C语言代码)浏览:834 |
C语言程序设计教程(第三版)课后习题5.4 (C语言代码)浏览:810 |
1048题解(读入回车问题)浏览:565 |
C语言程序设计教程(第三版)课后习题3.7 (C语言代码)浏览:706 |
演讲大赛评分 (C语言代码)浏览:1636 |
字符逆序 (C语言代码)浏览:621 |