LOC JULY SCHAR Need Help

easy
locjuly17

#1

I passed all test cases except one(TLE).

int main() {

int t;scanf("%d",&t);
while(t--){
   char s[100000];
  scanf("%s",s);
    int len=strlen(s);
  //  cout<<len<<endl;
 //   for(int i=0;i<100000;i++)if(s*-'a'>=0 && s*-'a'<=25)len++;else break;
    
    int cost[len];
    for(int i=0;i<len;i++)cost*=1000000000;
    cost[0]=0;
    vector <int> g[26];
    for(int i=0;i<len;i++)g[s*-'a'].pb(i);
    
    for(int i=0;i<len-1;i++){
        g[s*-'a'].erase(g[s*-'a'].begin());
        for(auto it=0;it<g[s*-'a'].size();it++)if(cost[g[s*-'a'][it]]>cost*)cost[g[s*-'a'][it]]=cost*;
        int temp=abs(s[i+1]-s*);
        if(cost[i+1]>cost*+temp)cost[i+1]=cost*+temp;
    }
    printf("%d

",cost[len-1]);
}
return 0;
}


#2

It seems obviously TLE to me :confused:

Take a look at the nested for-loops you used. I think that makes your code O(N^2). Doesn’t it? In case I am wrong, can you explain further what you have tried to do?


#3

A more elegant way of what you tried to do is like this-

       int arr[26],i,j;
	   for(i=0;i<26;i++)arr*=1000000;

	   int dp[n];
	   dp[0]=0;
	   arr[s[0]-'a']=0;
	   for(i=1;i<n;i++)
	   {
	       dp*=min(arr[s*-'a'],dp[i-1]+abs(s*-s[i-1]));
	       arr[s*-'a']=min(arr[s*-'a'],dp*);
	   }
	   //for(i=0;i<n;i++)cout<<dp*<<" ";cout<<endl;
	   cout<<dp[n-1]<<endl;

arr is serving the purpose of, minimum cost of reaching at letter i if we use jumps in between.


#4

A very similar problem to this one is DIGJUMP. Maybe its editorial will help you much more :slight_smile:


#5

I dont think we can see your code because LoC solutions not being public. Can you somehow share your code?


#6

I have edited question.


#7

I know it has to be TLE. I am not asking about how can I optimize my code. I want to know how to solve it efficiently.


#8

Ohk, I thought you wanted to know why your code is wrong.


#9

I didnt include comments because i thought we both are on same both in terms of thinking. Do you need comments as well?


#10

No comments needed. Vow!!! I just had to use dp. Thanks.


#11

You did use dp, its just that you could have done better. Wish you best :slight_smile:


#12

Nice solution @vijju123 :slight_smile:
I guess you have used shortest path algo in some way (denoted by arr[]).


#13

Thanks dear :slight_smile: . Yes, I used arr for that. Its just visualization. Take it as, we are at some character ‘ch’ present at index i, we can reach it in two ways, either by jumping from previous char, or jumping to this char directly from same-character checkpost. Jumping from previous same-char checkpost is already stored in arr[s*-‘a’], and cost to reach here from previous element is in dp*.

For this case, imagine that we are filling a general index i , then proceed for start and end of table.

The best thing is synergy, dp* helps in determining arr, while arr helps in finding dp*.


#14

@vijju123 I have a doubt what if there is no check point of that character but it gives the smallest path i.e. acdefb.
So here a to b is shortest path Right?. But the algorithm is looking for check points which does not exist. So it will go for a-f i.e 5 then f-b that is 4. Because arr[b-‘a’] is 1000000. Can you please explain how the algo will handle this.


#15

In the question we are starting from a, and have to reach b. We cannot reach b before we reach f, since jumping isnt possible and as given in Q, we have to go linearly from a to c, then d,e,f and finally b. Answer is same in both cases.


#16

I interpreted the question in wrong way. Need to develop patience to read the question carefully. Lesson learned:)