aboutsummaryrefslogtreecommitdiff
path: root/challenge-145/khalid-anwar/cpp/ch-2.cpp
blob: 7d3b46117817565fe54e992052072a9fa163f543 (plain)
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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
// C++ program palindromic tree by khalid anwar 
#include "bits/stdc++.h"
using namespace std;
#define MAXN 1000

struct Node 
{
  
    // store start and end indexes of current
    // Node inclusively
  int start, end;
   

    // stores length of substring
  int length;
   

    // stores insertion Node for all characters a-z
  int insertEdg[26];
   

    // stores the Maximum Palindromic Suffix Node for
    // the current Node
  int suffixEdg;
 
};

// two special dummy Nodes as explained above
  Node root1, root2;

// stores Node information for constant time access
  Node tree[MAXN];

// Keeps track the current Node while insertion
int currNode;

string s;

int ptr;

 
void
insert (int idx) 
{
  
//STEP 1//
    
    /* search for Node X such that s[idx] X S[idx]
       is maximum palindrome ending at position idx
       iterate down the suffix link of currNode to
       find X */ 
  int tmp = currNode;
  
while (true)
    
    {
      
int curLength = tree[tmp].length;
      
if (idx - curLength >= 1 and s[idx] == s[idx - curLength - 1])
	
break;
      
tmp = tree[tmp].suffixEdg;
    
}
  
 
    /* Now we have found X ....
     * X = string at Node tmp
     * Check : if s[idx] X s[idx] already exists or not*/ 
    if (tree[tmp].insertEdg[s[idx] - 'a'] != 0)
    
    {
      
	// s[idx] X s[idx] already exists in the tree
	currNode = tree[tmp].insertEdg[s[idx] - 'a'];
      
return;
    
}
  
 
    // creating new Node
    ptr++;
  
 
    // making new Node as child of X with
    // weight as s[idx]
    tree[tmp].insertEdg[s[idx] - 'a'] = ptr;
  
 
    // calculating length of new Node
    tree[ptr].length = tree[tmp].length + 2;
  
 
    // updating end point for new Node
    tree[ptr].end = idx;
  
 
    // updating the start for new Node
    tree[ptr].start = idx - tree[ptr].length + 1;
  
 
 
//STEP 2//
    
    /* Setting the suffix edge for the newly created
       Node tree[ptr]. Finding some String Y such that
       s[idx] + Y + s[idx] is longest possible
       palindromic suffix for newly created Node. */ 
    
tmp = tree[tmp].suffixEdg;
  
 
    // making new Node as current Node
    currNode = ptr;
  
if (tree[currNode].length == 1)
    
    {
      
	// if new palindrome's length is 1
	// making its suffix link to be null string
	tree[currNode].suffixEdg = 2;
      
return;
    
}
  
while (true)
    
    {
      
int curLength = tree[tmp].length;
      
if (idx - curLength >= 1 and s[idx] == s[idx - curLength - 1])
	
break;
      
tmp = tree[tmp].suffixEdg;
    
}
  
 
    // Now we have found string Y
    // linking current Nodes suffix link with s[idx]+Y+s[idx]
    tree[currNode].suffixEdg = tree[tmp].insertEdg[s[idx] - 'a'];

}


 
// driver program
  int
main () 
{
  
    // initializing the tree
    root1.length = -1;
  
root1.suffixEdg = 1;
  
root2.length = 0;
  
root2.suffixEdg = 1;
  
 
tree[1] = root1;
  
tree[2] = root2;
  
ptr = 2;
  
currNode = 1;
  
 
    // given string
    s = "challenge";
  
int l = s.length ();
  
 
for (int i = 0; i < l; i++)
    
insert (i);
  
 
    // printing all of its distinct palindromic
    // substring
    cout << "All distinct palindromic substring for " 
 <<s << " : \n";
  
for (int i = 3; i <= ptr; i++)
    
    {
      
cout << i - 2 << ") ";
      
for (int j = tree[i].start; j <= tree[i].end; j++)
	
cout << s[j];
      
cout << endl;
    
} 
 
return 0;

}