i m trying this problem PRHYME spoj.pl/problems/PRHYME/
I m using a trie with reverse strings.
Checked it for many test cases. But getting WA 
Please help me in debugging 
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
#define MAXA 28
#define sentinal_char char('z'+1)
struct node {
int ct;
int flg;
struct node *next[MAXA];
};
struct node* create_node(){
struct node *new_node = new node;
new_node -> ct = 0;
new_node -> flg = 1;
for(int i = 0; i < MAXA; i++){
new_node->next[i] = NULL;
}
return new_node;
}
void insert_trie ( struct node *root, string w, int in ) {
if ( in < 0 ) {
// cout << "Return\n";
return;
}
// cout << "Current : " << w[in] << "\tInserting At: " << w[in] - 'a'<< endl;
if ( root -> next[w[in] - 'a'] == NULL) {
root -> next[w[in] - 'a'] = create_node();
// cout << "New Node Allocated\n";
}
root -> next [w[in] - 'a'] -> ct ++;
insert_trie( root -> next[w[in] - 'a'], w, in - 1 );
}
vector<string> vans;
int mxs = 0;
string matched;
int gflg = 0;
void find_ans(struct node *root, string curs){
if(root == NULL){
vans.push_back(curs);
return;
}
int fl = 1;
for(int i = 0; i < MAXA; i++){
if ( root -> next[i] != NULL && root -> next[i] -> flg == 1 ) {
fl = 0;
find_ans( root -> next[i], char(i + 'a') + curs);
}
}
if( fl ){
vans.push_back(curs);
}
}
void enable ( struct node * root, string w, int in ) {
if(root == NULL){
find_ans(root, "");
return ;
}
if(0 > in){
return ;
}
root -> flg = 1;
enable(root -> next [w[in] - 'a'], w, in-1);
}
void disable ( struct node * root, string w, int in ) {
if(root == NULL){
return ;
}
if(0 > in){
return ;
}
if( root -> ct > 1)
gflg = 1;
if(root -> ct == 1 && gflg == 1)
root -> flg = 0;
disable(root -> next [w[in] - 'a'], w, in-1);
}
void search_trie ( struct node * root, string w, int in ) {
// cout << w.size() << "\t" << in << endl;
if(root == NULL){
// cout << "Null Found\n";
find_ans(root, "");
return ;
}
if(0 > in){
// cout << "String Finished\n";
find_ans(root, "");
return ;
}
// cout << "Current : " << w[in] << "\tFinding At: " << w[in] - 'a'<< endl;
if( root -> next [w[in] - 'a'] -> flg == 0 ) {
find_ans(root, "");
return;
}
matched = w[in] + matched;
search_trie(root -> next [w[in] - 'a'], w, in-1);
}
int main(){
int i, j;
int n;
struct node* root = create_node();
string str;
getline( cin, str );
while( str != "" ) {
str = str;
insert_trie(root, str, str.size()-1);
getline( cin, str );
}
//cout << "\nFirst Part Over\n";
while(getline(cin, str)){
// cout << "Finding Ans For : " << str << "\n";
vans.clear();
matched = "";
gflg = 0;
disable(root, str, str.size() - 1);
search_trie( root, str, str.size() - 1 );
enable(root, str, str.size() - 1);
sort(vans.begin(), vans.end());
// for(i = 0; i < vans.size(); i++){
// cout << vans[i] << " ";
// }
cout << vans[0] << matched << endl;
}
return 0;
}