题目描述:
找出字符串中重复的子串,如输入abcab,输出a ab b。
核心代码如下:
#include <iostream>
#include <vector>
#include <algorithm>
#include <cstring>
#include <string>
#include <unordered_map>
#include <cstring>
int main() {
//找出字符串中重复的子串,如abcab,输出a, ab, b。
unordered_map<string, int> map;
string str;
cin >> str;
for (int i = 0; i < str.size(); i++) {
for (int j = i; j < str.size(); j++) {
string ss;
for (int k = i; k <= j; k++) {
ss += str[k];
}
map[ss]++;
}
}
bool flag = true;
for (unordered_map<string, int>::iterator it = map.begin(); it != map.end(); it++) {
if (it->second > 1) {
if (flag) {
cout << it->first;
flag = false;
}
else
{
cout << " " << it->first;
}
}
}
cout << endl;
return 0;
}