Given a string s, partition s such that every substring of the partition is a palindrome.
Return the minimum cuts needed for a palindrome partitioning of s.
For example, given s =“aab”,
Return1since the palindrome partitioning[“aa”,“b”]could be produced using 1 cut.
//复用palindrome-partitioning 时间过久
class Solution {
public:
int minCut(string s)
{
int num = INT_MAX;
vector<vector<string> > res;
map<string, bool> flag;
vector<string> tmp;
if (s.size() == 0)
return 0;
part(s,flag, res, tmp);
for (int i=0;i<res.size();i++)
{
if (num>res[i].size())
num = res[i].size();
}
return num-1;
}
void part(string s, map<string, bool> &flag, vector<vector<string> > &res, vector<string> &tmp)
{
for(int i=0;i<s.size();i++)
{
string strtmp = s.substr(0,i+1);
if (ifpalindrome(strtmp, flag))
{
tmp.push_back(strtmp);
if (i == s.size()-1)
{
if(res.empty() || tmp.size()<res[0].size())
{
res.clear();
res.push_back(tmp);
}
}
else
part(s.substr(i+1),flag, res, tmp);
tmp.pop_back();
}
}
}
bool ifpalindrome (string str, map<string, bool> &flag)
{
if (flag.find(str) != flag.end())
return flag[str];
for(int i=0,j=str.size()-1;i<j;i++,j--)
{
if (str[i] != str[j])
{
flag.insert(pair<string, bool>(str, false));
return false;
}
}
flag.insert(pair<string, bool>(str, true));
return true;
}
};
// 1 i == j
//p[i][j] = s[i] == s[j] j=i+1
// 1 j>i+1 && all is palindrome
//min(p[i][i]+p[i+1][j], p[i][i+1]+p[i+2][j], ...p[i][j-1]+p[j][j]) j>i+1
//递归 动态规划 时间太久
class Solution {
public:
int minCut(string s) {
return fun(s,0,s.size()-1)-1;
}
int fun(string &s, int x, int y)
{
string key("");
int res = INT_MAX;
key += char(x+'0');
key += ",";
key += char(y+'0');
if (mmap.find(key) != mmap.end())
return mmap[key];
if (x == y)
res = 1;
else if (y == x+1)
res = s[x] == s[y]?1:2;
else
{
if (ifpalindrome(s,key, x,y))
res = 1;
else
{
for(int i=0;i<y-x;i++)
{
int tmp = fun(s, x, x+i) + fun(s, x+i+1, y);
res = tmp<res?tmp:res;
}
}
}
mmap.insert(pair<string, int>(key,res));
return res;
}
bool ifpalindrome (string &s, string &key, int x, int y)
{
for(int i=x,j=y;i<j;i++,j--)
{
string Key("");
Key += char(i+'0');
Key += ",";
Key += char(j+'0');
if (bmap.find(Key) != bmap.end())
{
bmap.insert(pair<string, bool>(key, bmap[Key]));
return bmap[key];
}
if (s[i] != s[j])
{
bmap.insert(pair<string, bool>(key, false));
return false;
}
}
bmap.insert(pair<string, bool>(key, true));
return true;
}
map<string, int> mmap;
map<string, bool>bmap;
};
//p[][] = false (n*n) //p[i][j] 代表i-》j是否回文
//p[i][j] = s[i] == s[j] j<i+2
//s[i] == s[j]&&p[i+1][j-1] j>=i+2
//动态规划
class Solution {
public:
int minCut(string s) {
int n = s.size();
vector<vector<bool> > p(n, vector<bool> (n, false));
vector<int> f(n+1, 0);// 从i到末尾最小次数
for (int i=0;i<=n;i++)
f[i] = n-i;
for (int i=n-1;i>=0;--i)
{
for (int j=i;j<n;++j)
{
if (s[i] == s[j] && (j<i+2 || p[i+1][j-1]))
{
f[i] = f[i] < (f[j+1]+1) ? f[i]: (f[j+1]+1);
p[i][j] = true;
}
}
}
return f[0]-1;
}
};
测试
#include<iostream>
#include<string>
#include<vector>
#include<map>
#include<limits.h>
using namespace std;
class Solution {
public:
int minCut(string s)
{
int num = INT_MAX;
vector<vector<string> > res;
map<string, bool> flag;
vector<string> tmp;
if (s.size() == 0)
return 0;
part(s,flag, res, tmp);
for (int i=0;i<res.size();i++)
{
if (num>res[i].size())
num = res[i].size();
}
return num-1;
}
void part(string s, map<string, bool> &flag, vector<vector<string> > &res, vector<string> &tmp)
{
for(int i=0;i<s.size();i++)
{
string strtmp = s.substr(0,i+1);
if (ifpalindrome(strtmp, flag))
{
tmp.push_back(strtmp);
if (i == s.size()-1)
{
if(res.empty() || tmp.size()<res[0].size())
{
res.clear();
res.push_back(tmp);
}
}
else
part(s.substr(i+1),flag, res, tmp);
tmp.pop_back();
}
}
}
bool ifpalindrome (string str, map<string, bool> &flag)
{
if (flag.find(str) != flag.end())
return flag[str];
for(int i=0,j=str.size()-1;i<j;i++,j--)
{
if (str[i] != str[j])
{
flag.insert(pair<string, bool>(str, false));
return false;
}
}
flag.insert(pair<string, bool>(str, true));
return true;
}
};
class Solution1 {
public:
int minCut(string s) {
return fun(s,0,s.size()-1)-1;
}
int fun(string &s, int x, int y)
{
string key("");
int res = INT_MAX;
key += char(x+'0');
key += ",";
key += char(y+'0');
if (mmap.find(key) != mmap.end())
return mmap[key];
if (x == y)
res = 1;
else if (y == x+1)
res = s[x] == s[y]?1:2;
else
{
if (ifpalindrome(s,key, x,y))
res = 1;
else
{
for(int i=0;i<y-x;i++)
{
int tmp = fun(s, x, x+i) + fun(s, x+i+1, y);
res = tmp<res?tmp:res;
}
}
}
mmap.insert(pair<string, int>(key,res));
return res;
}
bool ifpalindrome (string &s, string &key, int x, int y)
{
for(int i=x,j=y;i<j;i++,j--)
{
string Key("");
Key += char(i+'0');
Key += ",";
Key += char(j+'0');
if (bmap.find(Key) != bmap.end())
{
bmap.insert(pair<string, bool>(key, bmap[Key]));
return bmap[key];
}
if (s[i] != s[j])
{
bmap.insert(pair<string, bool>(key, false));
return false;
}
}
bmap.insert(pair<string, bool>(key, true));
return true;
}
map<string, int> mmap;
map<string, bool>bmap;
};
class Solution2 {
public:
int minCut(string s) {
int n = s.size();
vector<vector<bool> > p(n, vector<bool> (n, false));
vector<int> f(n+1, 0);// 从i到末尾最小次数
for (int i=0;i<=n;i++)
f[i] = n-i;
for (int i=n-1;i>=0;--i)
{
for (int j=i;j<n;++j)
{
if (s[i] == s[j] && (j<i+2 || p[i+1][j-1]))
{
f[i] = f[i] < (f[j+1]+1) ? f[i]: (f[j+1]+1);
p[i][j] = true;
}
}
}
return f[0]-1;
}
};
class Solution3 {
public:
int minCut(string s) {
int n = s.size();
vector<vector<bool> > p(n, vector<bool> (n, false));
for (int i=n-1;i>=0;--i)
{
for (int j=i;j<n;++j)
{
if (s[i] == s[j] && (j<i+2 || p[i+1][j-1]))
{
p[i][j] = true;
}
}
}
for (int i=0;i<n;++i)
{
for (int j=0;j<n;++j)
{
if (p[i][j])
cout<<"true ";
else
cout<<"false ";
}
cout<<endl;
}
return 0;
}
};
int main()
{
Solution s;
Solution1 s1;
Solution2 s2;
int res = s.minCut("babccbab");
int res1= s1.minCut("babccbab");
int res2= s2.minCut("babccbab");
cout<<res<<endl;
cout<<res1<<endl;
cout<<res2<<endl;
}