-4

Here is the link to the problem: http://www.spoj.com/problems/PHONELST/

The judge gives wrong answer around the second set of test cases. Here is my code for the problem, please help me out.Thanks in advance.

#include<iostream>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdbool.h>
#include<vector>
using namespace std;

#define ARRAY_SIZE(a) sizeof(a)/sizeof(a[0])

// Alphabet size (# of symbols)
#define ALPHABET_SIZE (10)

// Converts key current character into index
// use only 'a' through 'z' and lower case 
#define CHAR_TO_INDEX(c) ((int)c - (int)'0')

// trie node
struct TrieNode
{
    struct TrieNode *children[ALPHABET_SIZE];

    // isLeaf is true if the node represents
    // end of a word
    bool isLeaf;
};

// Returns new trie node (initialized to NULLs)
struct TrieNode *getNode(void)
{
    struct TrieNode *pNode = NULL;

    pNode = (struct TrieNode *)malloc(sizeof(struct TrieNode));

    if (pNode)
    {
        int i;

        pNode->isLeaf = false;

        for (i = 0; i < ALPHABET_SIZE; i++)
            pNode->children[i] = NULL;
    }

    return pNode;
}

// If not present, inserts key into trie
// If the key is prefix of trie node, just marks leaf node
bool insert(struct TrieNode *root, string key)
{
    int level;
    int length = key.length();
    int index;

    struct TrieNode *pCrawl = root;

    for (level = 0; level < length; level++)
    {
        index = CHAR_TO_INDEX(key[level]);

        if(pCrawl->isLeaf)
        {
            return 0;
        } 
        else if (!pCrawl->children[index])
        {
            pCrawl->children[index] = getNode();
        }

        pCrawl = pCrawl->children[index];
    }

    // mark last node as leaf
    pCrawl->isLeaf = true;
    return 1;
}
int main()
{
   int t;
   cin>>t;
   while(t--)
   {
       int n;
       cin>>n;
       struct TrieNode *root = getNode();
       vector<string>v;
       bool ok=1;
       string keys;
       for(int z=0;z<n;z++)
       {

            cin>>keys;
            v.push_back(keys);      
       }
       for(int z=0;z<n&&ok;++z)
       {        
             ok=insert(root,v[z]);     
       }     
       if(ok)
            cout<<"YES"<<endl;
       else 
            cout<<"NO"<<endl;
       }
           return 0;
}
Vignesh
  • 21
  • 3
  • 1
    I guess SPOJ loves memory leaks. – PaulMcKenzie Jul 30 '16 at 15:16
  • Your program has memory leaks, and each time that loop in `main` is executed, the leaks just pile up -- you could be exhausting memory for all we know, and/or you're not resetting your structure to empty for each set of data. Second, identify the test data that gives the wrong results and hard-code it into the program you're posting here. There is no need for input loops if you know what the data is that is giving the issue. Once you have that, then debug your code to see where your answers differ. – PaulMcKenzie Jul 30 '16 at 15:56

1 Answers1

0

After inserting into the vector all the phone numbers, the vector needs to be sorted. The reason is that if insertion is done without sorting the array, for the test case below the code gives wrong answer.
2
91190
911
The judge accepts the solution after the change mentioned above is made.

Vignesh
  • 21
  • 3