链接:https://ac.nowcoder.com/acm/contest/3665/B
来源:牛客网
题目描述
Every problem maker has a flower in their heart out of love for ACM. As a famous problem maker, hery also has a flower.
Hery define string T as flower-string if T appears more than twice in string S and |T| = k. Hery wants you to find how many flower-string in S.
输入描述:
The first line contains a string S consist of ’f’, ’l’, ’o’, ’w’, ’e’, ’r’ and an integer k.
(1 ≤ |S| ≤ 10^5
, 1 ≤ k ≤ 100
)(1≤∣S∣≤10
5
,1≤k≤100)
输出描述:
Output an integer m in the first line, the number of flower − string in S.
Next m lines, each line contains a flower-string and the lexicographical order of them should be from small to large
示例1
输入
flowerflowerflower 3
输出
4
flo
low
owe
wer
题目分析: 典型的map用法,利用map的Key - value的对应,再利用set不重复的特性,暴力计算即可。
代码如下:
#include<iostream>
#include<map>
#include<set>
using namespace std;
set<string> st;
map<string, int> mp;
int main(){
string str;
int k;
cin >>str >> k;
if(k<=str.size()){
for(int i=0; i<=str.size()-k; i++){
string now = str.substr(i,k);
mp[now]++;
if(mp[now]==3)
st.insert(now);
}
}
cout <<st.size()<<endl;
set<string>::iterator it;
for(it=st.begin();it!=st.end();it++){
cout <<*it<<endl;
}
return 0;
}