利用map特性解题

链接: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`

#include<bits/stdc++.h>
using namespace std;
vector<string> p;
set<string> q;
map<string,int> w;
int main() {
    string s;
    cin>>s;
    int k;
    scanf("%d",&k);
    if(k>s.length()) {
        puts("0");
        return 0;
    }
    for(int i=0;i<=s.length()-k;i++) {
        string z=s.substr(i,k);
        w[z]++;
        if(w[z]>=3) {
            if(!q.count(z)) {
                p.push_back(z);/*不知道set怎么输出,所以用set的count的函数来将答案写入vector*/
                q.insert(z);
            }
        }
    }
    sort(p.begin(),p.end());
    printf("%d\n",p.size());
    for(int i=0;i<p.size();i++)
        cout<<p[i]<<endl;/*vector可以用数组的方式输出*/
    return 0;
}

宗旨:利用map的特性来统计字符串出现的次数;

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值