题解 | #子串计算#

子串计算

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

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

map<string ,int> mp;  //定义键值对容器

int main() {
    string s;
    cin>>s;
    int n=s.size();
    for(int i=0;i<n;i++){  //遍历字符串 (子串头)
        string str;
        for(int j=i;j<n;j++){   //(子串尾)
            str+=s[j]; //子串
            mp[str]++;  //对应的键值对加1
        }
    }

    for(auto t:mp){  //遍历mp
        if(t.second>1){  
            cout<<t.first<<" "<<t.second<<endl;
        }
    }
}
// 64 位输出请用 printf("%lld")

#子串计算#
全部评论

相关推荐

点赞 评论 收藏
转发
点赞 收藏 评论
分享
牛客网
牛客企业服务