C++算法:【较难131】分割回文串

LeetCode :131分割回文串

给你一个字符串 s,请你将 s 分割成一些子串,使每个子串都是回文。
返回符合要求的 最少分割次数 。
示例 1:
输入:s = “aab”
输出:1
解释:只需一次分割就可将 s 分割成 [“aa”,“b”] 这样两个回文子串。
示例 2:
输入:s = “a”
输出:0
示例 3:
输入:s = “ab”
输出:1
提示:
1 <= s.length <= 2000
s 仅由小写英文字母组成

2023年1月1日解答

class Solution {
public:
int minCut(string s) {
m_c = s.length();
vector<vector> is;
is.assign(m_c, vector(m_c+1));
for (int c = 0; c < m_c; c++)
{
//长度为1的字符串一定是回文
is[c][1] = true;
}
for (int c = 0; c + 1 < m_c; c++)
{
is[c][2] = (s[c] == s[c + 1]);
}

	 for (int len = 3; len <= m_c; len++)
	 {
		 for (int c = 0; c + len - 1 < m_c; c++)
		 {
			 is[c][len] = is[c + 1][len - 2] && (s[c] == s[c + len - 1]);
		 }
	 }

	 //最少多少个回文构成
	 vector<int> dp(m_c + 1,INT_MAX);
	 dp[0] = 0;
	 for (int c = 0; c < m_c; c++)
	 {
		 for (int len = 1; len <= m_c; len++)
		 {
			 if (is[c][len] && (c+len <= m_c ))
			 {
				 dp[c + len] = min(dp[c + len], dp[c] + 1);
			 }
		 }
	 }
	 return dp[m_c] - 1;
 }
 int m_c;

};

2023年8月

//马拉车计算回文回文
class CPalindrome
{
public:
//vOddHalfLen[i]表示 以s[i]为中心,且长度为奇数的最长回文的半长,包括s[i]
//比如:“aba” vOddHalfLen[1]为2 “abba” vEvenHalfLen[1]为2
static void Do(vector& vOddHalfLen, vector& vEvenHalfLen,const string& s)
{
vector v;
for (const auto& ch : s)
{
v.emplace_back(ch);
v.emplace_back(‘*’);
}
v.pop_back();

	const int len = v.size();
	vector<int> vHalfLen(len);
	int center = -1, r = -1;
	//center是对称中心,r是其右边界(闭)
	for (int i = 0; i < len; i++)
	{
		int tmp = 1;
		if (i <= r)
		{
			int pre = center - (i - center);
			tmp = min(vHalfLen[pre], r - i + 1);
		}
		for (tmp++; (i + tmp - 1 < len) && (i - tmp + 1 >= 0) && (v[i + tmp - 1] == v[i - tmp + 1]); tmp++);
		vHalfLen[i] = --tmp;
		const int iNewR = i + tmp - 1;
		if (iNewR > r)
		{
			r = iNewR;
			center = i;
		}
	}

	vOddHalfLen.resize(s.length());
	vEvenHalfLen.resize(s.length());
	for (int i = 0; i < len; i++)
	{
		if (i & 1)
		{
			vEvenHalfLen[i / 2] = vHalfLen[i] / 2;
			
		}
		else
		{
			vOddHalfLen[i / 2] = (vHalfLen[i]+1) / 2 ;				
		}
	}
}

};
class Solution {
public:
int minCut(string s) {
m_c = s.length();
vector vOddHalfLen, vEvenHalfLen;
CPalindrome::Do(vOddHalfLen, vEvenHalfLen,s);
//邻接表
vector<vector> vNeiBo(m_c+1);
for (int i = 0; i < m_c; i++)
{
for (int len = 1; len <= vOddHalfLen[i]; len++)
{
const int cur = i - len + 1;
const int next = i + len;
vNeiBo[cur].emplace_back(next);
}
for (int len = 1; len <= vEvenHalfLen[i]; len++)
{
const int cur = i - len + 1;
const int next = i + len+1;
vNeiBo[cur].emplace_back(next);
}
}

	queue<int> que;
	que.emplace(0);
	vector<int> vDis(m_c+1, -1);
	vDis[0] = 0;
	while (que.size())
	{
		const int cur = que.front();
		que.pop();
		const int curDis = vDis[cur];
		for (const auto& next : vNeiBo[cur])
		{
			if (-1 != vDis[next])
			{
				continue;
			}
			vDis[next] = curDis + 1;
			que.emplace(next);
		}
	}
	return vDis.back() - 1;
}
int m_c;

};

其它

视频课程

如果你觉得复杂,想从简单的算法开始,可以学习我的视频课程。
https://edu.csdn.net/course/detail/38771
我的其它课程
https://edu.csdn.net/lecturer/6176

测试环境

win7 VS2019 C++17

相关下载

doc版文档,排版好
https://download.csdn.net/download/he_zhidan/88348653

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论
动态规划方法可以用来解决分割文串的问题。可以根据给定的字符串s,使用动态规划找到所有可能的文子串。 首先,我们可以定义一个二维数组dp,其中dp[i][j]表示字符串s从索引i到j的子串是否是文串。对于任意的i和j,如果s的第i个字符和第j个字符相等,并且s的第i+1个字符到第j-1个字符是文串,则dp[i][j]为true。 然后,我们可以使用动态规划填充dp数组。我们可以从字符串s的末尾开始遍历,每次遍历一个字符。对于每个索引i,我们再从i开始向右遍历,直到字符串的末尾。对于每个索引i和j,我们检查字符串s从索引i到j是否是文串。如果是文串,则将这个子串添加到结果集中,并继续向右遍历,搜索下一个可能的文子串。 最后,当我们遍历完整个字符串s时,我们就可以得到所有可能的分割方案。每个分割方案都是由一组文子串组成的。 下面是一个使用动态规划的实现示例代码: ```cpp class Solution { public: vector<vector<string>> partition(string s) { vector<vector<string>> result; vector<string> path; vector<vector<bool>> dp(s.size(), vector<bool>(s.size(), false)); backtracking(s, 0, path, result, dp); return result; } void backtracking(string const& s, int startIndex, vector<string>& path, vector<vector<string>>& result, vector<vector<bool>>& dp) { if (startIndex >= s.size()) { result.push_back(path); return; } for (int i = startIndex; i < s.size(); i++) { if (s[startIndex == s[i && (i - startIndex <= 2 || dp[startIndex + 1][i - 1])) { dp[startIndex][i = true; path.push_back(s.substr(startIndex, i - startIndex + 1)); backtracking(s, i + 1, path, result, dp); path.pop_back(); } } } }; ``` 这是一个基于溯和动态规划的算法,它可以找到字符串s所有可能的分割方案,使得每个子串都是文串。你可以根据自己的需求使用这个算法来解决分割文串的问题。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* *2* [131. 分割文串 溯 c++](https://blog.csdn.net/qq_39993896/article/details/127132759)[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^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] - *3* [LeetCode-分割文串(C++)](https://blog.csdn.net/weixin_42817333/article/details/125468202)[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^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

闻缺陷则喜何志丹

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值