-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMinimum_Palidrom_Divid.cpp
More file actions
92 lines (84 loc) · 1.73 KB
/
Copy pathMinimum_Palidrom_Divid.cpp
File metadata and controls
92 lines (84 loc) · 1.73 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
#include "stdafx.h"
#include "iostream"
#include "vector"
#include "string"
#include "map"
#include <algorithm>
#include <stdlib.h>
#include <time.h>
#include <stack>
#include <sstream>
#include <iomanip>
#include <random>
#include <assert.h>
#include <iterator>
#include <queue>
#include <math.h>
#include <vector>
#include <set>
using namespace std;
vector<vector<bool> > getPalidromMatrix(string s)
{
int n=s.size();
vector<vector<bool> > matrix(n, vector<bool>(n, false));
for(int i=0;i<n;i++)
{
for(int j=0;j<=i;j++)
{
if(i==j)matrix[j][i]=true;
else if(j+1==i)matrix[j][i]=s[i]==s[j];
else matrix[j][i]=s[i]==s[j]&&matrix[j+1][i-1];
}
}
return matrix;
}
vector<string> dividString(vector<pair<int, int> > &divid, string s)
{
int n=s.size();
int j=n-1;
vector<string> result;
while(j>=0)
{
int prev=divid[j].second;
result.push_back(s.substr(prev, j-prev+1));
j=prev-1;
}
reverse(result.begin(), result.end());
return result;
}
vector<string> minmalPalidrom(string s)
{
vector<vector<bool> > palidromMatrix=getPalidromMatrix(s);
int n=s.size();
vector<pair<int, int> > divid(n, pair<int, int>(INT_MAX, 0));
for(int i=0;i<n;i++)
{
if(palidromMatrix[0][i])
{
divid[0].first=0;
divid[0].second=0;
}
else
{
for(int k=0;k<i;k++)
{
if(palidromMatrix[k+1][i])
{
if(divid[k].first+1<divid[i].first)
{
divid[i].first=divid[k].first+1;
divid[i].second=k+1;
}
}
}
}
}
return dividString(divid, s);
}
int _tmain(int argc, _TCHAR* argv[])
{
vector<string> result=minmalPalidrom("aabaa");
for(int i=0;i<result.size();i++)
cout<<result[i]<<endl;
return 0;
}