This is the link to the SPOJ problem HASHIT : 
http://www.spoj.pl/problems/HASHIT/
i donno whts the mistake... i keep getting wrong answer even though
the Q is Straightforward :(

#include<iostream>
#include<string>
using namespace std;

int hash(string str)
{
    int sum = 0;
    int len = str.size();
    for(int i=0; i<len; i++)
            sum = (sum + (i+1)*str[i]) % 101;
    sum = (sum * 19) % 101;
    return sum;
}

void add(string *array, string str, int &count)
{
     //cout << "string received:" << str << endl;
     int key = hash(str), pos;
     for(int j=0; j<=19; j++)
     {
           pos = (key + j*j + 23*j) % 101;
           if(array[pos] == "")
           {
               //cout << "Added" << endl;
               array[pos] = str;
               count++;
               return;
           }
           else if(array[pos] == str)
               return;
     }
}

void d(string *array, string str, int &count)
{
     //cout << "string received:" << str << endl;
     int key = hash(str), pos;
     for(int j=0; j<=19; j++)
     {
           pos = (key + j*j + 23*j) % 101;
           if(array[pos] == str)
           {
               //cout << "Deleted" << endl;
               array[pos] = "";
               count--;
               return;
           }
     }
}

int main()
{

    freopen("input.txt", "r", stdin);
    freopen("output.txt", "w", stdout);

    int t;
    cin >> t;
    while(t--)
    {
         int n, count = 0;
         string array[101], temp;
         for(int i=0; i<101; i++)
              array[i] = "";
         cin >> n;

         while(n--)
         {
              cin >> temp;
              string opr = temp.substr(0, 3);
              if(opr == "ADD")
                  add(array, temp.substr(4, temp.size() - 4), count);
              else
                  d(array, temp.substr(4, temp.size() - 4), count);
         }

         cout << count << endl;

         for(int i=0; i<101; i++)
         {
               if(array[i] != "")
                   cout << i << ":" << array[i] << endl;
         }
    }
    //system("pause");
    return 0;
}

-- 
You received this message because you are subscribed to the Google Groups 
"Algorithm Geeks" group.
To post to this group, send email to algogeeks@googlegroups.com.
To unsubscribe from this group, send email to 
algogeeks+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/algogeeks?hl=en.

Reply via email to