dotcpp0715281


私信TA

用户名:dotcpp0715281

访问量:112

签 名:

等  级
排  名 23524
经  验 590
参赛次数 0
文章发表 3
年  龄 0
在职情况 学生
学  校 河南大学
专  业

  自我简介:

解题思路:
分别记录首字母和尾字母出现的位置,然后遍历数组得出结果。
注意事项:
降低时间复杂度。
参考代码:

#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 人评分

看不懂代码?想转换其他语言的代码? 或者想问其他问题? 试试问问AI编程助手,随时响应你的问题:

编程语言转换

万能编程问答  

代码解释器

代码纠错

SQL生成与解释

  评论区