求字符串里出现最多的子串及次数

本文介绍了一个C++程序,用于统计给定字符串中所有子串出现的次数,并找出出现次数最多的子串。通过使用标准库中的vector和pair来存储子串及其对应的出现频率。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

练手的小程序,不写注释了微笑

#include<iostream>

#include<assert.h>

#include<string>
#include<utility>
#include<vector>
using namespace std;

void main()
{
bool equal = false;
unsigned long int i,j,max = 0;
string str = "abcdcbababccabccddemmmmmcdfabedsb",v_str;
vector<pair<string,unsigned long int>> vec;
vector<pair<string,unsigned long int>>::iterator iter;
for(i = 0; i < str.size();i++)
{
for(j = 1;i+j <= str.size();j++)
{
v_str = str.substr(i,j);
equal = false;
for(iter = vec.begin();iter != vec.end();iter++)
{
if( iter->first == v_str )
{
equal = true;
(iter->second)++;
break;
}

}
if(equal == false)
{
vec.push_back(make_pair(v_str,1));
iter = vec.end() - 1;
}
if(max < iter->second)
{
max = iter->second;
}
cout << iter->first << endl;
cout << iter->second << endl;
}
}
for(iter = vec.begin();iter != vec.end();iter++)
{
if(iter->second == max)
{
cout << " the biggest substring is " << endl;
cout << iter->first << endl;
cout << iter->second << "totally times " << endl;
}
}
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值