-
题目描述:
-
给出一个01字符串(长度不超过100),求其每一个子串出现的次数。
-
输入:
-
输入包含多行,每行一个字符串。
-
输出:
-
对每个字符串,输出它所有出现次数在1次以上的子串和这个子串出现的次数,输出按字典序排序。
-
样例输入:
-
10101
-
样例输出:
-
0 2 01 2 1 3 10 2 101 2
借鉴了大牛的博文(http://blog.youkuaiyun.com/wxyfennie/article/details/52456863),依照此思路完成下列代码。
#include<stdio.h>
#include<string.h>
#include<iostream>
#include<map>
using namespace std;
map<string, int> m;
char buf[101];
int main() {
while (scanf("%s", buf) != EOF) {
m.clear();
string goal;
int len = strlen(buf);
for (int i = 0; i < len; i++) {
goal = buf[i];
if (m.find(goal) != m.end())
m[goal]++;
else
m[goal] = 1;
for (int j = i + 1; j < len; j++) {
goal += buf[j];
if (m.find(goal) != m.end())
m[goal]++;
else
m[goal] = 1;
}
}
for (map<string, int>::iterator ite = m.begin(); ite != m.end(); ite++) {
if (ite->second > 1)
cout << ite->first << " " << ite->second << endl;
}
}
return 0;
}
题目链接:
http://ac.jobdu.com/problem.php?pid=1149