题解 | #子串计算#(map+substr)

子串计算

https://www.nowcoder.com/practice/bcad754c91a54994be31a239996e7c11

#include <iostream>
#include <map>
using namespace std;

map<string, int> cnt; //自动初始化

int main() {
    string str;
    while(cin >> str) {
        for(int i = 1;i <= str.length(); i++) {
            for(int j = 0;j < i; j++) {
                string s = str.substr(j, i - j);
                cnt[s]++;
            } //获取子串:外层i是子串长度,内层j是子串开始的位置。i - j + j == i
        }
        for(auto iter = cnt.begin(); iter != cnt.end(); iter++) {
            if(iter->second > 1){
                cout << iter->first << ' ' << iter->second << endl;
            }
        }
    }
    
    return 0;
}

全部评论

相关推荐

今天要投简历吗?&nbsp;国庆期间真的不投比较好吗?&nbsp;emmmmmm
想要offer的每一...:可以去投,谁回你,你就拉黑他,国庆还上班,贼黑心
我的秋招日记
点赞 评论 收藏
分享
09-01 11:31
门头沟学院 Java
buul:七牛云的吧,感觉想法是好的,但是大家没那么多时间弄他这个啊。。。不知道的还以为他是顶尖大厂呢还搞比赛抢hc,只能说应试者的痛苦考察方是无法理解的,他们只会想一出是一出
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务