字符串中连续子串出现次数统计,获取连续出现次数最多的子串

// STR.cpp : 定义控制台应用程序的入口点。
//

#include "stdio.h"
#include "stdlib.h"
#include "iostream"
#include "string"
#include "vector"
#include "map"

using namespace std;
vector<string> FindAllSubString(string str);
map<string, int> SubStrCount(string str);
map<string, int> FindAllSuccessSubString(string str);

int main(int argc, char * argv[])
{
	//string name = "wei song";

	//string subName = name.substr(0,5);
	//string sub2 = name.substr(0, 0);
	//cout<<sub2<<endl;
	//cout<<subName<<endl;
	//cout<<name<<endl;

	// test
	string test = "abcbcbcabc";
	vector<string> result = FindAllSubString(test);

	for(int i = 0; i < result.size(); i++)
	{
		cout<<result[i]<<endl;
	}

	map<string, int> m_result = SubStrCount(test);
	cout<<""<<endl;
	for(map<string, int>::iterator iter = m_result.begin(); iter != m_result.end(); iter++)
	{
		cout<<iter->first<<"    "<<iter->second<<endl;
	}

	// 
	cout<<""<<endl;
	string test2 = "aaabcaa";
	map<string, int> m_map = FindAllSuccessSubString(test);

	string maxsub;
	int max = 0;

	for(map<string, int>::iterator iter = m_map.begin(); iter != m_map.end(); iter++)
	{
		if(iter->second > max)
		{
			max = iter->second;
			maxsub = iter->first;
		}
	}

	cout<<maxsub<<":"<<max<<endl;
	system("pause");
	return 0;
}

// 统计所有可能出现的子串
vector<string> FindAllSubString(string str)
{
	vector<string> result;
	int length = str.length();

	// 获得所有子串
	for(int i = 0; i <= length; i++)
	{
		// i 为子串的长度
		// 从原字符串的不同的位置获取子串
		for(int j = 0; j <= length - i; j++)
		{
			// 获取在偏移j 长度为 i的子串
			string sub = str.substr(j, i);
			bool flag = false;
			for(vector<string>::iterator iter = result.begin(); iter != result.end(); iter++)
			{
				if(*iter == sub)
				{
					flag = true;
					break;
				}
			}
			if(flag == false)
			{
				result.push_back(sub);
			}
		}
	}
	return result;
}


// 统计每个子串出现的次数
map<string, int> SubStrCount(string str)
{
	map<string, int> result;
	int length = str.length();
	for(int i = 1; i <= length; i++)
	{
		// i 为子串长度
		for(int j = 0; j<= length - i; j++)
		{
			// j 为计算子串的偏移量
			string sub = str.substr(j, i);
			result[sub]++;
		}
	}

	return result;
}

// 统计所有可能出现的子串
// 计算连续出现的子串的数量
map<string, int> FindAllSuccessSubString(string str)
{
	map<string, int> s_map;
	int length = str.length();

	// 获得所有子串
	for(int i = 1; i <= length; i++)
	{
		// i 为子串的长度
		// 从原字符串的不同的位置获取子串
		for(int j = 0; j <= length - i; j++)
		{
			// 获取在偏移j 长度为 i的子串
			string sub = str.substr(j, i);
			int subLength = sub.length();
			int count = 1; // 子串的连续出现的次数
			// 统计连续子串的出现次数
			int k;
			for(k = j + subLength; k <= length - subLength; k = k + subLength)
			{
				if(str.substr(k, subLength) == sub)
					count++;
				else
					break;
			}
			if(count > s_map[sub])
				s_map[sub] = count;
		}
	}
	return s_map;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值