-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathPalindromicTree.java
168 lines (151 loc) · 3.8 KB
/
PalindromicTree.java
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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
import java.util.Arrays;
class PalindromicTree
{
private final char START='a';
private char[] word;
private String wordS;
private int size;
private Node[] tree;
private int currentNode;
private int pointer;
private static class Node
{
private final int NUMBER=26;
private int start,end;
private int length;
private int insertionEdge[]=new int[NUMBER];
private int maximumPalindromicSuffixEdge;
public Node(int start,int end)
{
this.setStart(start);
this.setEnd(end);
this.setLength();
}
public void setStart(int start)
{
this.start=start;
}
public void setEnd(int end)
{
this.end=end;
}
public void setLength()
{
this.length=end-start+1;
}
public void setMaximumPalindromicSuffixEdge(int index)
{
this.maximumPalindromicSuffixEdge=index;
}
public int getValueOfInsertionEdgeAtIndex(int index) {
// TODO Auto-generated method stub
return insertionEdge[index];
}
public void setValueOfInsertionEdgeAtIndex(int index, int pointer) {
// TODO Auto-generated method stub
insertionEdge[index]=pointer;
}
public int getMaximumPalindromicSuffixEdge() {
// TODO Auto-generated method stub
return maximumPalindromicSuffixEdge;
}
public int getLength() {
// TODO Auto-generated method stub
return length;
}
public int getStart() {
// TODO Auto-generated method stub
return start;
}
public int getEnd() {
// TODO Auto-generated method stub
return end;
}
@Override
public String toString() {
// TODO Auto-generated method stub
return "Insertion Edge: "+Arrays.toString(insertionEdge)+" ,MaximumPailndromicSuffixEdge: "+maximumPalindromicSuffixEdge;
}
}
public PalindromicTree(String word)
{
this(word.toCharArray());
}
public PalindromicTree(char[] word)
{
this.wordS=String.valueOf(word);
this.word=word;
size=word.length;
tree=new Node[size+2+1];//2 dummy nodes, 1 indexed base , n possible palindrome
tree[1]=new Node(0,-2);
tree[2]=new Node(0,-1);
tree[1].setMaximumPalindromicSuffixEdge(1);
tree[2].setMaximumPalindromicSuffixEdge(1);
currentNode=1;
pointer=2;
initialize();
}
private void initialize()
{
for(int index=0;index<size;index++)
insert(index);
}
private boolean wasInsertionEdgeAlreadySet(int temp,int index)
{
if(tree[temp].getValueOfInsertionEdgeAtIndex(word[index]-START)!=0)
{
currentNode=tree[temp].getValueOfInsertionEdgeAtIndex(word[index]-START);
return true;
}
pointer++;
tree[temp].setValueOfInsertionEdgeAtIndex(word[index]-START,pointer);
tree[pointer]=new Node(index-tree[temp].length-1,index);
currentNode=pointer;
return false;
}
private void setSuffixEdge(int temp,int index)
{
temp=tree[temp].getMaximumPalindromicSuffixEdge();
//set suffix edge
if(tree[currentNode].getLength()==1)
{
tree[currentNode].setMaximumPalindromicSuffixEdge(2);
return;
}
temp=getPalindrome(temp,index);
tree[currentNode].setMaximumPalindromicSuffixEdge(tree[temp].getValueOfInsertionEdgeAtIndex(word[index]-START));
return;
}
private void insert(int index)
{
int temp=getPalindrome(currentNode,index);
if(wasInsertionEdgeAlreadySet(temp, index))
return;
setSuffixEdge(temp, index);
}
private int getPalindrome(int root,int index)
{
int temp=root;
int currentLength;
while(true)
{
currentLength=tree[temp].length;
if(index-currentLength>=1 && word[index]==word[index-currentLength-1])
return temp;
temp=tree[temp].getMaximumPalindromicSuffixEdge();
}
}
private String toString(int index) {
// TODO Auto-generated method stub
int start=tree[3+index].getStart();
int end=tree[3+index].getEnd();
return wordS.substring(start, end+1);
}
public String[] getAllPalindrome()
{
String palindrome[]=new String[size];
for(int i=0;i<size;i++)
palindrome[i]=toString(i);
return palindrome;
}
}