Skip to content
Programmingoneonone
Programmingoneonone
  • Engineering Subjects
    • Internet of Things (IoT)
    • Digital Communication
    • Human Values
  • Programming Tutorials
    • C Programming
    • Data structures and Algorithms
    • 100+ Java Programs
    • 100+ C Programs
    • 100+ C++ Programs
  • Solutions
    • HackerRank
      • Algorithms Solutions
      • C solutions
      • C++ solutions
      • Java solutions
      • Python solutions
    • Leetcode Solutions
    • HackerEarth Solutions
  • Work with US
Programmingoneonone
Programmingoneonone

HackerEarth Cats Substrings problem solution

YASH PAL, 31 July 202414 February 2026
In this HackerEarth Cats Substrings problem solution There are two cats playing, and each of them has a set of strings consisted of lower case English letters.
 
The first cat has N strings, while the second one has M strings. Both the first and the second cat will choose one of it’s strings and give it to you. After receiving two strings, you will count the number of pairs of equal substrings that you can choose in both strings, and send this number back to the cats. (Note that two occurences of the same substring are considered different. For example, strings “bab” and “aba” have 6 such pairs.)
 
The cats are going to give you all N * M possible pairs. They are interested in the total sum of numbers received from you. Your task is to find this number.
 
 
HackerEarth Cats Substrings problem solution

 

 

HackerEarth Cats Substrings problem solution.

#include<bits/stdc++.h>
#define bs 1000000007

const int N = 100005;

using namespace std;

int n;
string st1[N];
string st2[N];
long long ans;
int m;
string Z;
int cp;
vector<int> v;
vector<pair<int, int> > events;
int l[N*5],r[N*5];
int c1[N*5],c2[N*5];
int last;
long long TTL;
long long pw[1<<21];
long long s[1<<21];

void remove_block(int l,int r)
{
TTL-=1ll*c1[l]*c2[l];
}

void add_block(int l,int r)
{
TTL+=1ll*c1[l]*c2[l];
}

long long eval(int ps,int span)
{
return (s[ps+span]-s[ps])*pw[(1<<20)-ps];
}

int lcp(int a,int b)
{
if (a<b)
swap(a,b);
int l,r;
l=0;
r=Z.size()-b;
while (l<r)
{
int mid=l+r+1;
mid/=2;
if (eval(a,mid)==eval(b,mid))
l=mid;
else
r=mid-1;
}
return l;
}

bool cmp(int a,int b)
{
int l=lcp(a,b);
if (a+l==Z.size())
return true;
if (b+l==Z.size())
return false;
return (Z[a+l]<Z[b+l]);
}

int sz1,sz2;

int main(){
//freopen("beavers.in","r",stdin);
//freopen("beavers.out","w",stdout);
//freopen("F:/in.txt","r",stdin);
//freopen("F:/output.txt","w",stdout);
//ios_base::sync_with_stdio(0);
//cin.tie(0);

pw[0]=1;
for (int i=1;i<=(1<<20);i++)
pw[i]=pw[i-1]*173;

cin>>n;
for (int i=1;i<=n;i++)
cin>>st1[i];
cin>>m;
for (int i=1;i<=m;i++)
cin>>st2[i];

for (int i=1;i<=n;i++)
{
Z+=st1[i];
sz1+=st1[i].size();
Z+="*";
}

cp=Z.size();

for (int i=1;i<=m;i++)
{
Z+=st2[i];
sz2+=st2[i].size();
Z+="#";
}

for (int i=1;i<=Z.size();i++)
s[i]=s[i-1]+Z[i-1]*pw[i];

for (int i=0;i<Z.size();i++)
{
v.push_back(i);
}


sort(v.begin(),v.end(),cmp);

for (int i=0;i<v.size();i++)
{
l[i]=i;
r[i]=i;
if (Z[v[i]]>='a'&&Z[v[i]]<='z')
{
if (v[i]<cp)
c1[i]=1;
else
c2[i]=1;
}
}

for (int i=0;i+1<v.size();i++)
{
int val=lcp(v[i],v[i+1]);
events.push_back(make_pair(val,i));
}

sort(events.begin(),events.end());
reverse(events.begin(),events.end());

last=1e9;
for (int i=0;i<events.size();i++)
{
int val=events[i].first;
ans+=TTL*1ll*(last-val);
last=val;
int ps=events[i].second;
remove_block(l[ps],ps);
remove_block(ps+1,r[ps+1]);
l[r[ps+1]]=l[ps];
r[l[ps]]=r[ps+1];
c1[l[ps]]+=c1[ps+1];
c2[l[ps]]+=c2[ps+1];
add_block(l[ps],r[l[ps]]);
}

ans+=1ll*last*TTL;
cout<<ans<<endl;

return 0;
}
 
 
coding problems solutions HackerEarth HackerEarth

Post navigation

Previous post
Next post

Leave a Reply

Your email address will not be published. Required fields are marked *

Programmingoneonone

We at Programmingoneonone, also known as Programming101 is a learning hub of programming and other related stuff. We provide free learning tutorials/articles related to programming and other technical stuff to people who are eager to learn about it.

Pages

  • About US
  • Contact US
  • Privacy Policy

Practice

  • Java
  • C++
  • C

Follow US

  • YouTube
  • LinkedIn
  • Facebook
  • Pinterest
  • Instagram
©2026 Programmingoneonone | WordPress Theme by SuperbThemes