题解 | #子串计算#
子串计算
https://www.nowcoder.com/practice/bcad754c91a54994be31a239996e7c11
#include<bits/stdc++.h> using namespace std; int main() { string str; cin >> str; //用有序哈希表记录字串及出现次数 map<string, int> count; //遍历字符串,获取所有子串及出现次数 for (int i = 0; i < str.length(); i++) { for (int j = 1; j <= str.length() - i; j++) { //j为截取长度 string sub_str = str.substr(i, j); //用substr函数截取子串 count[sub_str]++; } } for (auto iter = count.begin(); iter != count.end(); iter++) { if (iter->second > 1) { //若某个子串出现的次数大于1 cout << iter->first << ' ' << iter->second << endl; //输出 } } }