leetcode 子串(列出所有子串,无重复子串,重复子串)

1、列出字符串的所有子串

void getAllSubstring(string s,vector<string> &res){
	for (int i = 0; i < s.size(); i++){
		for (int j = 1; j <= s.size()-i; j++){
			res.emplace(s.substr(i, j));
		}
	}
}
int main()
{
	string s = "abc";
	
	set<string>strs;
	getAllSubstring(s,strs);
	for (auto a : strs)
		cout << a << " ";
	cout << endl;
	
	system("pause");
}

输出:

 a ab abc b bc c

 

2、列出字符串的无重复字符的子串

问题描述:给定一个字符串,找到最长子字符串的长度而不重复字符

例子:abcabcbb”字母的最长子字符串是“abc”,其长度为3.

           “bbbbb”,最长的子字符串是“b”,长度为1。

解题思路:类似滑动窗口(见8.23日滑动窗口的博客)

对字符串进行从左向右扫描,当扫描到abca时将a删除得到bca,继续向右移动,

每新加一个char,在左边检查有无重复的char,如果没有,正常添加

如果有,就删除左边部分(从最左边到重复char的位置)在这个过程中记录无重复子串的长度的最大值和子串。

代码实现:map容器里面存放的是当前扫描到char和对应的下标为止(实时对下标进行更新)

                  pre是指当前扫描char在扫描过的char中的位置

                  max是存储最长无重复子串的长度的最大值

                   i-pre当前扫描到的无重复子串的长度(因此当前扫描无重复子串的位置是从pre+1开始,长度为i-pre,

                           将其存储到res中)

#include<iostream>
#include<queue>
#include<vector>
#include<map>
#include<string>
using namespace std;

int lengthOfLongestSubstring(string s,vector<string> &res) {
	map<char, int> book;
	int i, Max = 0, pre = -1;
	for (i = 0; i<s.length(); i++) 
		book[s[i]] = -1;
	for (i = 0; i<s.length(); i++)
	{
		pre = max(pre, book[s[i]]);
		Max = max(Max, i - pre);
		res.push_back(s.substr(pre + 1, i-pre));
		book[s[i]] = i;
	}
	return Max;
}
int main()
{	
	string s="abcabcbb";
	vector<string>res;
	int max = lengthOfLongestSubstring(s,res);
	for (auto a : res)
		cout << a << endl;
	cout << max << endl;
	system("pause");
	return 0;
}

参考:https://www.nowcoder.com/questionTerminal/5947ddcc17cb4f09909efa7342780048

3、重复子串(华为2019年研发题)

题目描述:字符串中找到有重复子串的长度最大的子串

例子:AGCTAGCTBB 最长重复子串为AGCT,长度为4

解题思路:时间复杂度o(n^3)

string lsubstr_2(const string & str)
{
	string maxstr;
	for (int i = 0; i < str.size(); i++)
	for (int j = (str.size() - i); j >= 1; j--)
	{
		string subs = str.substr(i, j);
		int front = str.find(subs);
		int back = str.rfind(subs);
		if (front != back && subs.size() > maxstr.size())
			maxstr = subs;
	}
	return maxstr;
}

参考:https://www.nowcoder.com/questionTerminal/859d3e13ebb24e73861e03141bbe9cfb?source=relative

 

链接:https://www.nowcoder.com/questionTerminal/859d3e13ebb24e73861e03141bbe9cfb?source=relative

 

举例: ask not what your country  can do for you ,but what you can do for your  country

最长的重复子序列:can do for you

思路:使用后缀数组解决

分析:

1、由于要求最长公共子序列,则需要 找到字符串的所有子序列 ,即通过产生字符串的后缀数组实现。

2、由于要求最长的重复子序列,则需要对所有子序列进行排序,这样可以把 相同的字符串排在一起

3、 比较 相邻字符串 ,找出两个子串中,相同的字符的个数。

注意,对于一个子串,一个与其重复最多的字符串肯定是紧挨着自己的两个字符串。

步骤:

1、对待处理的字符串 产生后缀数组

2、 对后缀数组排序

3、依次 检测相邻两个后缀的公共长度

4、 取出最大 公共长度 的前缀

 

举例: 输入字符串 banana

1、字符串产生的后缀数组:
a[0]:banana
a[1]:anana
a[2]:nana
a[3]:ana
a[4]:na
a[5]:a

2、对后缀数组进行快速排序,以将后缀相近的(变位词)子串集中在一起

a[0]:a
a[1]:ana
a[2]:anana
a[3]:banana
a[4]:na
a[5]:nana

之后可以依次检测相邻两个后缀的公共长度并取出最大公共的前缀

#include<iostream>
#include<algorithm>
#include<string>
const int MaxCharNum = 500;
using namespace std;

bool StrCmp(char * str1, char * str2)
{
	return (*str1) < (*str2);
}

/*为字符串产生其后缀数组,并存放到数组suffixStr中*/
void GenSuffixArray(char* str, char* suffixStr[])
{
	int len = strlen(str);
	for (int i = 0; i < len; i++)
	{
		suffixStr[i] = &str[i];
	}
}

/*返回str1和str2的共同前缀的长度*/
int ComStrLen(char* str1, char* str2)
{
	int comLen = 0;
	while (*str1 && *str2)
	{
		if (*str1 == *str2)
		{
			comLen++;
		}
		str1++;
		str2++;
	}
	return comLen;
}



void GenMaxReStr(char* str)
{
	int len = strlen(str);
	int comReStrLen = 0;
	int maxLoc = 0;
	int maxLen = 0;
	char* suffixStr[MaxCharNum];
	GenSuffixArray(str, suffixStr);//产生后缀数组
	//对后缀数组进行排序
	sort(suffixStr, suffixStr + len, StrCmp);
	//统计相邻单词中相同的字符数,并输出结果
	for (int i = 0; i < len - 1; i++)
	{
		comReStrLen = ComStrLen(suffixStr[i], suffixStr[i + 1]);
		if (comReStrLen > maxLen)
		{
			maxLoc = i;
			maxLen = comReStrLen;
		}
	}
	//输出结果
	for (int i = 0; i < maxLen; i++)
	{
		cout << suffixStr[maxLoc][i];
	}
	cout << endl;
}

int main()
{
	char str[MaxCharNum];
	cin.getline(str, MaxCharNum);//遇到回车结束
	GenMaxReStr(str);
	system("pause");
	return 1;
}

 

  • 1
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
根据提供的引用内容,有三种方法可以解决LeetCode上的最长回文子串问题。 方法一是使用扩展中心法优化,即从左向右遍历字符串,找到连续相同字符组成的子串作为扩展中心,然后从该中心向左右扩展,找到最长的回文子串。这个方法的时间复杂度为O(n²)。\[1\] 方法二是直接循环字符串,判断子串是否是回文子串,然后得到最长回文子串。这个方法的时间复杂度为O(n³),效率较低。\[2\] 方法三是双层for循环遍历所有子串可能,然后再对比是否反向和正向是一样的。这个方法的时间复杂度也为O(n³),效率较低。\[3\] 综上所述,方法一是解决LeetCode最长回文子串问题的最优解法。 #### 引用[.reference_title] - *1* [LeetCode_5_最长回文子串](https://blog.csdn.net/qq_38975553/article/details/109222153)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^insertT0,239^v3^insert_chatgpt"}} ] [.reference_item] - *2* [Leetcode-最长回文子串](https://blog.csdn.net/duffon_ze/article/details/86691293)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^insertT0,239^v3^insert_chatgpt"}} ] [.reference_item] - *3* [LeetCode 第5题:最长回文子串(Python3解法)](https://blog.csdn.net/weixin_43490422/article/details/126479629)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^insertT0,239^v3^insert_chatgpt"}} ] [.reference_item] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值