Блог пользователя codeDominus

Автор codeDominus, история, 6 лет назад, По-английски

I am trying to solve this problem using rabin-karp maching algorithm.

My hash function is

ll hash = 0;
ll P = 3; // only 3 characters are considered here 'a' ,'b', 'c'; 
ll MOD = 1e9+7;
string s; // hash of this string is calculated 
for(int i=0; i<s.size();i++)
{
    hash = (hash + (pow(P,i)*(s[i]-'a'+1))%MOD)%MOD;
}

Is there any better way to calculate the hash function to avoid conflict?

I tried to change (s[i] — 'a' + 1) by s[i]. Still getting conflict.

Thanks in Advance.

Полный текст и комментарии »

  • Проголосовать: нравится
  • +5
  • Проголосовать: не нравится

Автор codeDominus, история, 7 лет назад, По-английски

link to problem

I solved the problem using DSU; My logic: let suppose there is two disjoint set of size M and N where each member is a friend of every other in the set. now suppose one member of 1st set become friend of a member of 2nd set. we need total of m*n such friendship for every member of 1st set to become friends with every other member of 2nd set.

is there any mistake in my logic ?(posting me solution just for logic not for debugging)

my solution

thanks in advance

Полный текст и комментарии »

Теги dsu
  • Проголосовать: нравится
  • +1
  • Проголосовать: не нравится

Автор codeDominus, история, 7 лет назад, По-английски

LINK for the problem I am learning DSU and came up with this question but could not apply DSU. It will be of great help if someone tell the logic for solving this problem.

UPD 1 :solved the question LINK for answer

Полный текст и комментарии »

Теги dsu
  • Проголосовать: нравится
  • +4
  • Проголосовать: не нравится