利用映射,将子串当做关键字,将其出现次数当做映射值
#include <iostream>
#include <cstdio>
#include <string>
#include <map>
using namespace std;
int main(int argc, char** argv) {
string str;
while(cin >> str){
map<string, int> number;
for(int i = 0; i <= str.size(); i++){
for(int j = 0; j < i; j++){
string key = str.substr(j, i-j); //每个子串
number[key]++;
}
}
map<string, int>::iterator it;
for(it = number.begin(); it != number.end(); it++){
if(1 < it->second){
cout << it->first << " " << it->second << endl;
}
}
}
return 0;
}