XYSTR - Editorial

cakewalk

Don’t know why its has DP tag.

DP is overkilling, when simple traversal is suffucient

i j k l m:

if (i,j) forms a pair we can expect (k,l) to form other pair
else next probable pair would be (j,k)

https://www.codechef.com/viewsolution/34196632

3 Likes

June long challenge editorial beginner friendly video explanation and code

Delicious cake (CONTAIN) : Codechef June Long Challenge||The Delicious Cake(CONTAIN) - YouTube
Tom and jerry (EOEO) : CodeChef June Long Challenge||The Tom and Jerry Game!||EOEO - YouTube
Even matrix (EVENM) : CodeChef June Long Challenge||Even Matrix|| EVENM - YouTube

Python Solution Using ASCII Value of x and y:

T = int(input())
for _ in range(T):
	s = str(input())
	p = i =0
	while i < len(s)-1:
		if abs(ord(s[i])-ord(s[i+1])) == 1:
			p += 1
			i += 1
		i += 1
	print(p)
1 Like

can anyone explain this in setter’s code?
if(prev_taken){
prev_taken=0;
continue;
}

prev_taken is a flag.

if this is active means the last element is already paired with the second last element otherwise we can choose to pair with it.

last ???

video Explanation>>>>

PYTHON code of chef and string…
for _ in range(int(input())):
arr = input()
count = 0
i = 0
while i < len(arr)-1:
if (arr[i] == “x” and arr[i+1] == “y”) or (arr[i]==“y” and arr[i+1]==“x”):
count+=1
i+=1
i+=1
print (count)

2 Likes

char last = ‘z’ declare in while loop
because every test cases last will be initially ‘z’ but in your case last may be ‘x’, ‘y’ or ‘z’

Thanks

bro is anything wrong with dis concept

Can you tell me the problem in this code?
#include <bits/stdc++.h>
using namespace std;

int main() {
int t;
cin>>t;
while(t–){
string s;
cin>>s;
int flag=0,flag1=0,result,len;
len=s.length();
for(int i=0;i<len;i++){
if(s[i]==‘x’)
flag++;
if(s[i]==‘y’)
flag1++;
}
result=min(flag,flag1);
cout<<result<<endl;
}
return 0;
}

Can you please tell me what’s the problem in this approach it’s giving a WA-
https://www.codechef.com/viewsolution/34515962

Test string “xxxxyyyy”, your code gives 4. Expected is 1.

Also you have to put a “\n” or endl after printing the result. (For every question, on any CP website).

[EDIT]: Also, flags are generally used to check some condition. If your variable is counting something it’s better to name it countx and county. Naming it flag is not wrong, but it’s just easier for others to understand your code if you use the conventional meaning of flag and count.

1 Like

Ok, I understood what the problem with my code is and I have also made flag as countx and county. Thank you so much.

1 Like

Can you please also check what’s wrong in this code? I get the correct outputs during custom run but it shows runtime error
https://www.codechef.com/viewsolution/34532525

The runtime error that you are getting is SIGSEGV a.k.a Segmentation Fault. In layman terms, you’re accessing out of bounds. In C/C++ arrays are 0 indexed, so for A[n] you can access A[0]…A[n-1]. But A[n] throws a runtime error. It might’ve run smoothly in your compiler because accessing A[n] is undefined behavior and it’s different for different compilers.

A Simple Fix

Write your for loop as

for(int i = 0; i <n; i++)
{
    // your code
}
Or

Declare the array as
int ar[n + 1]

I strongly suggest the first fix.

[EDIT]: Also, you’re declaring ar[n] before taking the input! Before taking the input for n, the variable will be initialize with a junk value (if nothing is specified). You have to initialize your array after taking input for n. I guess this is why it ran smoothly in your compiler. Generally junk values are like 4576 or something. So for small inputs it’s fine as you have declared an array of size 4576. But when the given input to n is greater than the junk value, it throws an error.

1 Like

Thank you so much :blush:. You taught me something new today.

1 Like

Input :
1
xxxyyyy

expected output :
1
(their is only one pair of xy)

Your output :
3

1 Like