The June long challenge ended today been able to solve 4 questions the fourth one digjumps just being a modification of B.F.S. algorithm special thanks to my freind Ansh Khanna for motivating me to do the 4th problem and update this blog :) Ansh if you are reading this a big thanks man . Now talking about the readability of the problem it was not very good i thought i-1(Si-1) meant the the difference of the index and the value of the sequence at i-1 index. So now thinking of framing a similar question for codechef :P not in near future though :) anyways here is the problem link
QUESTION 4 JUNELONG DIGITJUMPS
my solution
#include <cstring>
#include <vector>
#include <list>
#include <map>
#include <set>
#include <deque>
#include <stack>
#include <bitset>
#include <algorithm>
#include <functional>
#include <numeric>
#include <utility>
#include <sstream>
#include <iostream>
#include <iomanip>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <ctime>
#include <memory.h>
#include <queue>
#include <climits>
using namespace std;
#define FOR(i,a) for(int i = 0;i < a;i++)
#define REP(i,a,b) for(int i = a;i < b;i++)
#define vi vector<int>
int main()
{
int arr[100001],len;/*the dp matrix storing the min moves taken to reach the element and the length of input string*/
char str[100001];/*the string*/
int allnum[10][100001];/*all the numbers position stored from 0-9*/
int allcnt[10];/*counts of all the numbers*/
bool truth[10];/*checks whethers the numbers encountered before or not*/
memset(arr,-1,100001*sizeof(int));/*intitialisation of the dp matrix*/
memset(str,'\0',100001*sizeof(char));/*sting init*/
memset(allcnt,0,10*sizeof(int));/*zero elements initially*/
memset(truth,false,10*sizeof(bool));
std::queue<int> q;/*the queue for storing positions*/
scanf("%s",str);
len=strlen(str);
for(int i=0;i<len;i++)
{
allnum[str[i]-'0'][allcnt[str[i]-'0']]=i;/*storing in the 2D array allnum the positions of numbers*/
allcnt[str[i]-'0']++;/*increase the size*/
}
arr[0]=0;/*no moves required to reach the index 0*/
q.push(0);/*beginning insert the zero*/
while(!q.empty())
{
if(!truth[str[q.front()]-'0'])/*if all the elements of the front type are not pushed*/
{
for(int i=0;i<allcnt[str[q.front()]-'0'];i++)/*i< allcount or total size of the particular element of the front type*/
{if(allnum[str[q.front()]-'0'][i]!=0)
q.push(allnum[str[q.front()]-'0'][i]);/*push the positions of elements in the queue*/
if(arr[q.front()]+1 < arr[allnum[str[q.front()]-'0'][i]] &&arr[allnum[str[q.front()]-'0'][i]]!=-1 )
{
arr[allnum[str[q.front()]-'0'][i]]=arr[q.front()]+1;/*transferring to the dp arr the lowest possible reach*/
}
if(arr[allnum[str[q.front()]-'0'][i]]==-1)
{
arr[allnum[str[q.front()]-'0'][i]]=arr[q.front()]+1;
}
}
truth[str[q.front()]-'0']=true;/*not repeating it next time*/
}
if(q.front()-1>=0)
{
if(arr[q.front()-1]>arr[q.front()]+1&&arr[q.front()-1]!=-1)
{
arr[q.front()-1]=arr[q.front()]+1;
q.push(q.front()-1);
}
if(arr[q.front()-1]==-1)
{
arr[q.front()-1]=arr[q.front()]+1;
q.push(q.front()-1);
}
}
if (q.front()+1<len)
{
if(arr[q.front()+1]>arr[q.front()]+1&&arr[q.front()+1]!=-1)
{
arr[q.front()+1]=arr[q.front()]+1;
q.push(q.front()+1);
}
if(arr[q.front()+1]==-1)
{
arr[q.front()+1]=arr[q.front()]+1;0
q.push(q.front()+1);
}
}
q.pop();
}
printf("%d\n",arr[len-1]);
return 0;
}
by the way it got AC in one go with a runtime of 0.00s memory of 7MB B-) but i have also seen far easier approaches from other people's code and editorials will try to learn them up as well and if possible write them here.
QUESTION 4 JUNELONG DIGITJUMPS
my solution
#include <cstring>
#include <vector>
#include <list>
#include <map>
#include <set>
#include <deque>
#include <stack>
#include <bitset>
#include <algorithm>
#include <functional>
#include <numeric>
#include <utility>
#include <sstream>
#include <iostream>
#include <iomanip>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <ctime>
#include <memory.h>
#include <queue>
#include <climits>
using namespace std;
#define FOR(i,a) for(int i = 0;i < a;i++)
#define REP(i,a,b) for(int i = a;i < b;i++)
#define vi vector<int>
int main()
{
int arr[100001],len;/*the dp matrix storing the min moves taken to reach the element and the length of input string*/
char str[100001];/*the string*/
int allnum[10][100001];/*all the numbers position stored from 0-9*/
int allcnt[10];/*counts of all the numbers*/
bool truth[10];/*checks whethers the numbers encountered before or not*/
memset(arr,-1,100001*sizeof(int));/*intitialisation of the dp matrix*/
memset(str,'\0',100001*sizeof(char));/*sting init*/
memset(allcnt,0,10*sizeof(int));/*zero elements initially*/
memset(truth,false,10*sizeof(bool));
std::queue<int> q;/*the queue for storing positions*/
scanf("%s",str);
len=strlen(str);
for(int i=0;i<len;i++)
{
allnum[str[i]-'0'][allcnt[str[i]-'0']]=i;/*storing in the 2D array allnum the positions of numbers*/
allcnt[str[i]-'0']++;/*increase the size*/
}
arr[0]=0;/*no moves required to reach the index 0*/
q.push(0);/*beginning insert the zero*/
while(!q.empty())
{
if(!truth[str[q.front()]-'0'])/*if all the elements of the front type are not pushed*/
{
for(int i=0;i<allcnt[str[q.front()]-'0'];i++)/*i< allcount or total size of the particular element of the front type*/
{if(allnum[str[q.front()]-'0'][i]!=0)
q.push(allnum[str[q.front()]-'0'][i]);/*push the positions of elements in the queue*/
if(arr[q.front()]+1 < arr[allnum[str[q.front()]-'0'][i]] &&arr[allnum[str[q.front()]-'0'][i]]!=-1 )
{
arr[allnum[str[q.front()]-'0'][i]]=arr[q.front()]+1;/*transferring to the dp arr the lowest possible reach*/
}
if(arr[allnum[str[q.front()]-'0'][i]]==-1)
{
arr[allnum[str[q.front()]-'0'][i]]=arr[q.front()]+1;
}
}
truth[str[q.front()]-'0']=true;/*not repeating it next time*/
}
if(q.front()-1>=0)
{
if(arr[q.front()-1]>arr[q.front()]+1&&arr[q.front()-1]!=-1)
{
arr[q.front()-1]=arr[q.front()]+1;
q.push(q.front()-1);
}
if(arr[q.front()-1]==-1)
{
arr[q.front()-1]=arr[q.front()]+1;
q.push(q.front()-1);
}
}
if (q.front()+1<len)
{
if(arr[q.front()+1]>arr[q.front()]+1&&arr[q.front()+1]!=-1)
{
arr[q.front()+1]=arr[q.front()]+1;
q.push(q.front()+1);
}
if(arr[q.front()+1]==-1)
{
arr[q.front()+1]=arr[q.front()]+1;0
q.push(q.front()+1);
}
}
q.pop();
}
printf("%d\n",arr[len-1]);
return 0;
}
by the way it got AC in one go with a runtime of 0.00s memory of 7MB B-) but i have also seen far easier approaches from other people's code and editorials will try to learn them up as well and if possible write them here.
No comments:
Post a Comment