回文分区
给定一个字符串,如果该分区的每个子字符串都是回文,则该字符串的分区是回文分区。例如,“aba|b|bbabb|a|b|aba”是“ababbbabbababa”的回文分割。确定给定字符串的回文分割所需的最少切口。例如,“ababbbabbababa”至少需要3次切割。这三个切口是“a|babbbab|b|ababa”。如果字符串是回文,则至少需要 0 次切割。如果长度为 n 的字符串包含所有不同的字符,则需要最少 n-1 次剪切。
输入 : str = “geek”
输出 : 2
我们需要至少进行 2 次切割,即 “g ee k”
输入 : str = “aaaa”
输出 : 0
字符串已经是回文。
输入 : str = “abcde”
输出 : 4
输入 : str = “abbac”
输出 : 1
使用递归:
// C++ Code for Palindrome Partitioning
// Problem
#include <bits/stdc++.h>
using namespace std;
bool isPalindrome(string String, int i, int j)
{
while(i < j)
{
if(String[i] != String[j])
return false;
i++;
j--;
}
return true;
}
int minPalPartion(string String, int i, int j)
{
if( i >= j || isPalindrome(String, i, j) )
return 0;
int ans = INT_MAX, count;
for(int k = i; k < j; k++)
{
count = minPalPartion(String, i, k) +
minPalPartion(String, k + 1, j) + 1;
ans = min(ans, count);
}
return ans;
}
// Driver code
int main() {
string str = "ababbbabbababa";
cout << "Min cuts needed for " <<
"Palindrome Partitioning is " <<
minPalPartion(str, 0, str.length() - 1) << endl;
return 0;
}
// This code is contributed by rag2127
更多解法:
回文分区|DP-17 - 极客 (geeksforgeeks.org)
给定一个整数数组,您需要找到局部最大值
Example : [1 3 5 4 7 10 6] Output: 5 or 10 Explanation: Any of the local maxima can be the output. Here 5 is greater than 3 and 4, 10 is greater than 7 and 6.
使用遍历:
// A C++ program to find a peak element
#include <bits/stdc++.h>
using namespace std;
// Find the peak element in the array
int findPeak(int arr[], int n)
{
// first or last element is peak element
if (n == 1)
return 0;
if (arr[0] >= arr[1])
return 0;
if (arr[n - 1] >= arr[n - 2])
return n - 1;
// check for every other element
for (int i = 1; i < n - 1; i++) {
// check if the neighbors are smaller
if (arr[i] >= arr[i - 1] && arr[i] >= arr[i + 1])
return i;
}
}
// Driver Code
int main()
{
int arr[] = { 1, 3, 20, 4, 1, 0 };
int n = sizeof(arr) / sizeof(arr[0]);
cout << "Index of a peak point is " << findPeak(arr, n);
return 0;
}
更多解法:
峰元素|练习|极客为极客 (geeksforgeeks.org)
给定一系列括号,您将如何确定其是否有效
Example : ({[][]}) Output: Valid Explanation: Every opening bracket has a closing bracket. Example: ({[]]}) Output: Invalid Explanation : Every opening bracket does not have a closing bracket.
// CPP program to check for balanced brackets.
#include <bits/stdc++.h>
using namespace std;
// function to check if brackets are balanced
bool areBracketsBalanced(string expr)
{
stack<char> temp;
for(int i=0;i<expr.length();i++)
{
if(temp.empty())
{
temp.push(expr[i]);
}
else if((temp.top()=='('&& expr[i]==')') || (temp.top()=='{' && expr[i]=='}') || (temp.top()=='[' && expr[i]==']'))
{
temp.pop();
}
else
{
temp.push(expr[i]);
}
}
if(temp.empty())
{
return true;
}
return false;
}
// Driver code
int main()
{
string expr = "{()}[]";
// Function call
if (areBracketsBalanced(expr))
cout << "Balanced";
else
cout << "Not Balanced";
return 0;
}
使用Stack检查表达式中的平衡括号(良好格式) - GeeksforGeeks
给定一个具有整数值的二叉树,找到其中具有最大值的子路径
给定一个二叉树,找到最大路径和。路径可以在树中的任何节点开始和结束。
对于每个节点,最大路径可以通过四种方式通过节点:
1. 节点只有
2。通过左子节点 + 节点
3 的最大路径。通过右子节点 + 节点
4 的最大路径。通过左子级的最大路径 + 节点 + 通过右子项
的最大路径的想法是跟踪四个路径,并最终选择最大路径。需要注意的一件重要事情是,每个子树的根都需要返回最大路径总和,以便最多涉及根的一个子项。父函数调用需要此值。在下面的代码中,此总和存储在“max_single”中,并由递归函数返回。
Example : 1 / \ 2 3 / \ / \ 4 5 6 7 Output : Max path is 5, 2, 1, 3, 7 Explanation : 5+2+1+3+7=18 is the maximum value that can spanned. -100 / \ 2 3 / \ / \ 4 5 6 7 Output : Max path is 6, 3, 7 Explanation : 6+3+7=16 is the maximum value that can spanned.
// C/C++ program to find maximum path sum in Binary Tree
#include<bits/stdc++.h>
using namespace std;
// A binary tree node
struct Node
{
int data;
struct Node* left, *right;
};
// A utility function to allocate a new node
struct Node* newNode(int data)
{
struct Node* newNode = new Node;
newNode->data = data;
newNode->left = newNode->right = NULL;
return (newNode);
}
// This function returns overall maximum path sum in 'res'
// And returns max path sum going through root.
int findMaxUtil(Node* root, int &res)
{
//Base Case
if (root == NULL)
return 0;
// l and r store maximum path sum going through left and
// right child of root respectively
int l = findMaxUtil(root->left,res);
int r = findMaxUtil(root->right,res);
// Max path for parent call of root. This path must
// include at-most one child of root
int max_single = max(max(l, r) + root->data, root->data);
// Max Top represents the sum when the Node under
// consideration is the root of the maxsum path and no
// ancestors of root are there in max sum path
int max_top = max(max_single, l + r + root->data);
res = max(res, max_top); // Store the Maximum Result.
return max_single;
}
// Returns maximum path sum in tree with given root
int findMaxSum(Node *root)
{
// Initialize result
int res = INT_MIN;
// Compute and return result
findMaxUtil(root, res);
return res;
}
// Driver program
int main(void)
{
struct Node *root = newNode(10);
root->left = newNode(2);
root->right = newNode(10);
root->left->left = newNode(20);
root->left->right = newNode(1);
root->right->right = newNode(-25);
root->right->right->left = newNode(3);
root->right->right->right = newNode(4);
cout << "Max path sum is " << findMaxSum(root);
return 0;
}