Here you will find solutions of many problems on spoj. If you want solution of some problem which is not listed in blog or have doubt regarding any spoj problem (which i have solved) or any programming concept (data structure) you can mail me @ raj.nishant360@gmail.com

And my humble request to you all that don't copy the code only try to understand the logic and algorithm behind the code. I have started this because if you tried as hard as you can and still can't find any solution to the problem then you can refer to this.
You can read my answer how to start competitive programming CLICK HERE

Wednesday, February 5, 2014

IITWPC4H-Maggu and Cuteness of Strings

Maggu and Cuteness of Strings

below given code is for iitwpc4h spoj or Maggu and Cuteness of Strings spoj.
Here simple logic is behind this question at first you should know what is Subsequence .Then it is very simple you have to just find the maximum length of consecutive equal characters in both strings and add them up.

#include <iostream>
#include <cstring>
#include <cstdlib>
using namespace std;
int main()
{
 int d,i,j,lent,lens,maxlen,curlen,max;
 char t[200000],s[200000],cur;
 scanf("%d",&d);
 while(d--)
 {
  //cin>>t>>s;
scanf("%s",t);
scanf("%s",s);
  lent=strlen(t);
  lens=strlen(s);
  int *tlist=(int*)calloc(26,sizeof(int));
  int *slist=(int*)calloc(26,sizeof(int));
  cur=t[0];
  curlen=1;
  tlist[cur-97]=1;
  for (i=1; i<lent; i++)
  {
   if (t[i]==cur)
   {
    curlen++;
    if (curlen>tlist[cur-97])
    {
     tlist[cur-97]=curlen;
    }
   }
   else
   {
    cur=t[i];
    curlen=1;
    if (curlen>tlist[cur-97])
    {
     tlist[cur-97]=curlen;
    }

   }
  }
  cur=s[0];
  curlen=1;
  slist[cur-97]=1;
  for (i=1; i<lens; i++)
  {
   if (s[i]==cur)
   {
    curlen++;
    if (curlen>slist[cur-97])
    {
     slist[cur-97]=curlen;
    }
   }
   else
   {
    cur=s[i];
    curlen=1;
    if (curlen>slist[cur-97])
    {
     slist[cur-97]=curlen;
    }

   }
  }
  int *array=(int*)calloc(26,sizeof(int));
  for (i=0; i<26; i++)
   array[i]=tlist[i]+slist[i];
  max=-1;
  for (i=0; i<26; i++)
   if (max<array[i])
    max=array[i];
  printf("%d\n",max);
 }
 return 0;
}


No comments:

Post a Comment

Your comment is valuable to us