Skip to content

Commit eb3eb3e

Browse files
authored
added method for counting of palindrome
- Fixed count of each palindrome - Added longest palindrome, count of longest palindrome - Added total palindrome count
1 parent 2acf56f commit eb3eb3e

File tree

1 file changed

+42
-7
lines changed

1 file changed

+42
-7
lines changed

palin.cpp

+42-7
Original file line numberDiff line numberDiff line change
@@ -16,34 +16,58 @@ struct N
1616
};
1717
unsigned int res =0;
1818
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+
1937
void display(N *node)
2038
{
2139
for (int i=0; i<26; i++)
2240
{
2341
N * t= node->node[i];
2442
if(t)
2543
{
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+
}
2849
if(t->count)
2950
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; }
3053
display(node->node[i]);
3154
}
3255
}
3356
}
3457
int main(int argc, char*argv[])
3558
{
36-
59+
longest_palin = 0;
60+
longest_palin_count =0;
3761
cin >> s;
3862
struct N *current_node =NULL;
3963
// Setup imaginary node node;
40-
struct N *Node0 = new N;
64+
Node0 = new N;
4165
memset(Node0, 0, sizeof(N));
4266
Node0->suffix_link = Node0;
4367
Node0->len =-1;
4468

4569
//Setup 0 node
46-
struct N *Node1 = new N;
70+
Node1 = new N;
4771
memset(Node1, 0, sizeof(N));
4872
Node1->suffix_link = Node0;
4973
Node1->len =0;
@@ -80,6 +104,14 @@ int main(int argc, char*argv[])
80104
{
81105
if(s[i]== s[i-start->len-1]) // Found node which can be extended
82106
{
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+
83115
start->node[s[i] -'a'] = new N;
84116
memset(start->node[s[i] -'a'], 0, sizeof(N));
85117
start->node[s[i] -'a']->len = 2 + start->len;
@@ -92,7 +124,7 @@ int main(int argc, char*argv[])
92124
//If suffix link point to itself, first check if a node of that characters exists , if yes use that else use Node0
93125
if(link->suffix_link==link)
94126
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']);}
96128
else
97129
start->node[s[i] -'a']->suffix_link = Node1;
98130
else
@@ -104,7 +136,7 @@ int main(int argc, char*argv[])
104136
break;
105137
}
106138
if(link->node[s[i]-'a'])
107-
link = link->node[s[i]-'a'];
139+
{link = link->node[s[i]-'a'];updateCount(link);}
108140
else
109141
link = Node1;
110142
start->node[s[i] -'a']->suffix_link=link;
@@ -124,6 +156,7 @@ int main(int argc, char*argv[])
124156
if(DEBUG)cout <<"Zero Node"<<endl;
125157
display(Node1);
126158
cout << "Total palindromes are: "<<res<<endl;
159+
cout <<"Longest Palindrom is of length: "<<longest_palin<<" Count: "<<longest_palin_count<<endl<<endl;
127160
}
128161

129162
/* Sample Test Vectors:
@@ -132,4 +165,6 @@ int main(int argc, char*argv[])
132165
* abaaa
133166
* abbaab
134167
* aaba
168+
* malayalam
169+
* abatxaba
135170
* */

0 commit comments

Comments
 (0)