@@ -16,34 +16,58 @@ struct N
16
16
};
17
17
unsigned int res =0 ;
18
18
string s;
19
+ unsigned int longest_palin =0 ;
20
+ unsigned int longest_palin_count =0 ;
21
+ struct N *Node0, *Node1;
22
+ /*
23
+ * whenever a sufflink is referred by a node, all the smaller suffix palindromes count are updated
24
+ *
25
+ */
26
+ void updateCount (N *node)
27
+ {
28
+ if (DEBUG)cout<<" updateCount" <<endl;
29
+ while (node && node != Node1)
30
+ {
31
+ node->count ++;
32
+ if (DEBUG)cout<<" updateCount done" <<endl;
33
+ node = node->suffix_link ;
34
+ }
35
+ }
36
+
19
37
void display (N *node)
20
38
{
21
39
for (int i=0 ; i<26 ; i++)
22
40
{
23
41
N * t= node->node [i];
24
42
if (t)
25
43
{
26
- if (DEBUG)cout <<" index: " << t->index <<" len: " <<t->len <<" count: " <<t->count <<endl;
27
- cout << s.substr (t->index ,t->len )<<endl;
44
+ if (DEBUG)
45
+ {
46
+ cout <<" index: " << t->index <<" len: " <<t->len <<" count: " <<t->count <<endl;
47
+ cout << s.substr (t->index ,t->len )<<endl;
48
+ }
28
49
if (t->count )
29
50
res += t->count ;
51
+ if (t->len >= longest_palin)
52
+ {if (t->len !=longest_palin)longest_palin_count=t->count ;else longest_palin_count++; longest_palin = t->len ; }
30
53
display (node->node [i]);
31
54
}
32
55
}
33
56
}
34
57
int main (int argc, char *argv[])
35
58
{
36
-
59
+ longest_palin = 0 ;
60
+ longest_palin_count =0 ;
37
61
cin >> s;
38
62
struct N *current_node =NULL ;
39
63
// Setup imaginary node node;
40
- struct N * Node0 = new N;
64
+ Node0 = new N;
41
65
memset (Node0, 0 , sizeof (N));
42
66
Node0->suffix_link = Node0;
43
67
Node0->len =-1 ;
44
68
45
69
// Setup 0 node
46
- struct N * Node1 = new N;
70
+ Node1 = new N;
47
71
memset (Node1, 0 , sizeof (N));
48
72
Node1->suffix_link = Node0;
49
73
Node1->len =0 ;
@@ -80,6 +104,14 @@ int main(int argc, char*argv[])
80
104
{
81
105
if (s[i]== s[i-start->len -1 ]) // Found node which can be extended
82
106
{
107
+ if (start->node [s[i] -' a' ])
108
+ {
109
+ start->node [s[i] -' a' ]->count ++;
110
+ current_node = start->node [s[i] -' a' ];
111
+ found =true ;
112
+ continue ;
113
+ }
114
+
83
115
start->node [s[i] -' a' ] = new N;
84
116
memset (start->node [s[i] -' a' ], 0 , sizeof (N));
85
117
start->node [s[i] -' a' ]->len = 2 + start->len ;
@@ -92,7 +124,7 @@ int main(int argc, char*argv[])
92
124
// If suffix link point to itself, first check if a node of that characters exists , if yes use that else use Node0
93
125
if (link ->suffix_link ==link )
94
126
if (link ->node [s[i]-' a' ])
95
- start->node [s[i] -' a' ]->suffix_link = link ->node [s[i]-' a' ];
127
+ { start->node [s[i] -' a' ]->suffix_link = link ->node [s[i]-' a' ];updateCount ( link -> node [s[i]- ' a ' ]);}
96
128
else
97
129
start->node [s[i] -' a' ]->suffix_link = Node1;
98
130
else
@@ -104,7 +136,7 @@ int main(int argc, char*argv[])
104
136
break ;
105
137
}
106
138
if (link ->node [s[i]-' a' ])
107
- link = link ->node [s[i]-' a' ];
139
+ { link = link ->node [s[i]-' a' ];updateCount ( link );}
108
140
else
109
141
link = Node1;
110
142
start->node [s[i] -' a' ]->suffix_link =link ;
@@ -124,6 +156,7 @@ int main(int argc, char*argv[])
124
156
if (DEBUG)cout <<" Zero Node" <<endl;
125
157
display (Node1);
126
158
cout << " Total palindromes are: " <<res<<endl;
159
+ cout <<" Longest Palindrom is of length: " <<longest_palin<<" Count: " <<longest_palin_count<<endl<<endl;
127
160
}
128
161
129
162
/* Sample Test Vectors:
@@ -132,4 +165,6 @@ int main(int argc, char*argv[])
132
165
* abaaa
133
166
* abbaab
134
167
* aaba
168
+ * malayalam
169
+ * abatxaba
135
170
* */
0 commit comments