C++ 字符串子串计数:寻找出现次数最多的 k-String
#include
using namespace std;
int main() { string s; cin >> s; int k; cin >> k;
unordered_map<string, int> substrCount;
// Count the occurrences of substrings of length k
for (int i = 0; i <= s.length() - k; i++) {
string substr = s.substr(i, k);
substrCount[substr]++;
}
int maxCount = 0;
// Find the maximum count of a substring
for (auto it = substrCount.begin(); it != substrCount.end(); it++) {
if (it->second > maxCount) {
maxCount = it->second;
}
}
cout << maxCount << endl;
return 0;
}
原文地址: https://www.cveoy.top/t/topic/bVPG 著作权归作者所有。请勿转载和采集!