HDU 5056 - Boring count

题意

统计一个字符串中子串每个字符最多重复k次的子串。

思路

用pos记录位置,当碰到cnt>k的时候就往前移

代码

  
  
  
  
  1. #include <cstdio>
  2. #include <stack>
  3. #include <set>
  4. #include <iostream>
  5. #include <string>
  6. #include <vector>
  7. #include <queue>
  8. #include <functional>
  9. #include <cstring>
  10. #include <algorithm>
  11. #include <cctype>
  12. #include <ctime>
  13. #include <cstdlib>
  14. #include <fstream>
  15. #include <string>
  16. #include <sstream>
  17. #include <map>
  18. #include <cmath>
  19. #define LL long long
  20. #define lowbit(x) ((x) & (-x))
  21. #define MP(a, b) make_pair(a, b)
  22. #define MS(arr, num) memset(arr, num, sizeof(arr))
  23. #define PB push_back
  24. #define F first
  25. #define S second
  26. #define ROP freopen("input.txt", "r", stdin);
  27. #define BitCount(x) __builtin_popcount(x)
  28. const double PI = acos(-1.0);
  29. const int INF = 0x3f3f3f3f;
  30. using namespace std;
  31. const int MAXN = 1e5 + 100;
  32. const int MOD = 20071027;
  33. typedef pair<int, int> pii;
  34. typedef vector<int>::iterator viti;
  35. typedef vector<pii>::iterator vitii;
  36. char str[MAXN];
  37. int cnt[30];
  38. int main()
  39. {
  40. //ROP;
  41. int n, i, j, last, T, k;
  42. scanf("%d", &T);
  43. while (T--)
  44. {
  45. MS(cnt, 0);
  46. int pos = 1;
  47. scanf("%s", str + 1);
  48. scanf("%d", &k);
  49. int len = strlen(str + 1);
  50. LL ans = 0;
  51. for (i = 1; i <= len; i++)
  52. {
  53. cnt[str[i] - 'a']++;
  54. while (cnt[str[i] - 'a'] > k)
  55. {
  56. cnt[str[pos] - 'a']--;
  57. pos++;
  58. }
  59. ans += i - pos + 1;
  60. }
  61. cout << ans << endl;
  62. }
  63. return 0;
  64. }

你可能感兴趣的:(ACM,HDU)