Codeforces Round #533(Div. 2) B.Zuhair and Strings

柔情只为你懂 2021-12-24 05:05 308阅读 0赞

链接:https://codeforces.com/contest/1105/problem/B

题意:

给一个字符串和k,连续k个相同的字符,可使等级x加1,

例:8 2 aaacaabb

则有aa aa 即x=2。

求最大的k

思路:

第一眼想的是诶个查找,但是绝对会T,就没做,过一个小时才想到可以直接遍历,记录每个字符对应的最大x即可。

代码:

  1. #include <bits/stdc++.h>
  2. using namespace std;
  3. const int MAXN = 200000+10;
  4. char s[MAXN];
  5. int vis[26];
  6. int main()
  7. {
  8. int n,k;
  9. scanf("%d%d",&n,&k);
  10. scanf("%s",s+1);
  11. int now = 0;
  12. for (int i = 1;i<=n;i++)
  13. {
  14. if (s[i] == s[i-1] || i == 1)
  15. {
  16. now++;
  17. if (now == k)
  18. {
  19. vis[s[i] - 97]++;
  20. now = 0;
  21. }
  22. }
  23. else
  24. {
  25. now = 1;
  26. if (now == k)
  27. {
  28. vis[s[i] - 97]++;
  29. now = 0;
  30. }
  31. }
  32. }
  33. int sum = 0;
  34. for (int i = 0;i<26;i++)
  35. sum = max(sum,vis[i]);
  36. cout << sum << endl;
  37. return 0;
  38. }

  

转载于:https://www.cnblogs.com/YDDDD/p/10297531.html

发表评论

表情:
评论列表 (有 0 条评论,308人围观)

还没有评论,来说两句吧...

相关阅读