洛谷 P6832 [Cnoi2020]子弦

思路

直接找出现最多次数的字母即可

因为串越短,可能的出现次数就会越多,所以直接找出现次数最多的字母,输出出现次数即可

代码

#include <cmath>
#include <queue>
#include <cstdio>
#include <vector>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;

const int A = 1e7 + 11;
const int B = 1e6 + 11;
const int mod = 1e9 + 7;
const int inf = 0x3f3f3f3f;

inline int read() {
  char c = getchar();
  int x = 0, f = 1;
  for ( ; !isdigit(c); c = getchar()) if (c == '-') f = -1;
  for ( ; isdigit(c); c = getchar()) x = x * 10 + (c ^ 48);
  return x * f;
}

char s[A];
int ans, a[A];

int main() {
  scanf("%s", s + 1);
  int n = strlen(s + 1);
  for (int i = 1; i <= n; i++) a[s[i] - 'a']++;
  for (int i = 0; i < 26; i++) ans = max(ans, a[i]);
  cout << ans << '
';
  return 0;
}
原文地址:https://www.cnblogs.com/loceaner/p/13706116.html