博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
求一个字符串中连续出现次数最多的子串
阅读量:7283 次
发布时间:2019-06-30

本文共 1243 字,大约阅读时间需要 4 分钟。

vector向量中的插入值如上图所示:

#include <iostream>

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

pair<int ,string> fun(const string &str)

{

vector<string> substrs;

int maxcount=1,count=1;

string substr;

int i,len=str.length();

for(i=0;i<len;i++)

{

 substrs.push_back(str.substr(i,len-i));   //把str字符串中的子串按每次把头部减少一个的方式插入到vector向量中

 cout<<substrs[i]<<endl;//输出vector中的值 如上图
}

for(i=0;i<len;i++) //先从第一个子串开始直到所有的遍历完所有的子串

{

for(int j=i+1;j<len;j++) //从下一个子串开始 寻找连续出现的子串

{

      count=1;

      if(substrs[i].substr(0, j-i)==substrs[ j].substr(0, j-i))  //寻找以a开头的子串(对于本题的输入而言)下面依次为b开头的子串,一直到c开头的子串

     {

        ++count;

       for(int k=j+(j-i);k<len;k+=j-i)

        {

         if(substrs[i].substr(0, j-i)==substrs[k].substr(0, j-i)) ++count; //如果有连续一个子串出现就继续遍历vector的下一个子串中的和现在出现相同子串的地方的下一个或几个字符

        else   break;

        }

   if(count>maxcount)  //maxcount  记录所有遍历中的最大连续子串出现的次数

   {

     maxcount=count;

      substr=substrs[i].substr(0, j-i);

   }

       }

}

}

return make_pair(maxcount ,substr);   //把maxcount 和 找到的子串做成pair<>返回

}

int main()

{
pair<int,string> result;
string str="abcbcbcabc";
result=fun(str);
cout<<result.first<<" "<<result.second<<endl; //输出为:2 ab
return 0;
}
 总体思路是:首先把给出的字符串按每次减少一个头部字符的方式存入vector中,然后开始在vector[0]中遍历以a开头的子串连续出现的次数。比较的方式为那vector[0]以a开头的一个字符和vector[1]内的比较 如果有 继续 没有 比较以ab开头的子串 。依次类推。下面是vector[1]中以b开头的的 步骤如上。

转载地址:http://fvzjm.baihongyu.com/

你可能感兴趣的文章
关于VMware HA的一些文章链接
查看>>
我的友情链接
查看>>
IDC简报:7月上旬国外最佳虚拟主机提供商Top5
查看>>
Beginning of my wonderful career
查看>>
uml 类图
查看>>
cocos2dx中的核心类
查看>>
Radius简介及ACS的安装配置(案例)
查看>>
mysql导出导入数据
查看>>
GreenPlum中自定义时间转换函数
查看>>
我的友情链接
查看>>
tcp建连部分参数
查看>>
windows2003搭建ntp服务器
查看>>
keepalived基础配置详解(一)
查看>>
我的友情链接
查看>>
Linux 磁盘存在旧RAID信息,清除raid信息
查看>>
window服务器下监控tomcat服务脚本
查看>>
CentOS 6防火墙的关闭
查看>>
vim 快速删除指定的一段字符
查看>>
Trunk配置实例
查看>>
Linux 系统安装
查看>>