CHRL2 - Editorial

Hmm i am using a ruby code for this…not sure why its breaking through

 #!/usr/bin/env ruby
    
    get_string=gets
    array_comp = ""
    chef_count = 0
    
    get_string.split('').each do |character|
      if character == "C" && array_comp == ""
        array_comp.concat("C")
      end
      if character == "H" && array_comp.length == 1
        array_comp.concat("H")
      end
      if character == "E" && array_comp.length == 2
        array_comp.concat("E")
      end
      if character == "F" && array_comp.length == 3
        array_comp.concat("F")
        chef_count += 1
        array_comp = ""
      end
    end
puts chef_count.to_i

Is there something I am missing here? https://www.codechef.com/submit/complete/8954982

hi

#include<bits/stdc++.h>
using namespace std;
#define pb push_back
#define mp make_pair
#define ff first
#define ss second
#define sc(a) scanf("%d",&a)
#define gc getchar_unlocked
#define scl(a) scanf("%lld",&a)
#define ll long long
#define all© c.begin(),c.end()
#define N 10010
#define mod 1000000007

int dp[26];

int main()
{
string s;
cin>>s;
int i;
memset(dp,0,sizeof(dp));

int ans=0,x=1000100;

for(i=0;i<s.length();i++)
{
	dp[s[i]-'A']++;
	if(s[i]=='F')
	{
		if(dp[2]!=0 && dp[4]!=0 && dp[5]!=0 && dp[7]!=0)
		{
			ans++;
			dp[2]--;
			dp[4]--;
			dp[5]--;
			dp[7]--;
		}
	}
	if(dp[2]==0)
		dp[4]=dp[5]=dp[7]=0;
	if(dp[7]==0)
		dp[4]=dp[5]=0;
	if(dp[4]==0)
		dp[5]=0;
}

cout<<ans<<endl;

return 0;

}

Please tell why I am getting WA in one of the test cases .
Which Test case is missing in my solution .

1 Like

#include<stdio.h>
#include<string.h>
void main()
{
char s[100000];
int i;
int c=0,h=0,e=0,f=0,l;
scanf("%s",s);
l=strlen(s);

for(i=0; i<l; i++)
{
    if(s[i]=='C')
    {
        c++;

    }
    else if(s[i]=='H')
    {
        if(c>h)
            h++;

    }
    else if(s[i]=='E')
    {
        if(h>e)
        {
            e++;
        }

    }
    else if(s[i]=='F')
    {
    if(e>f)

f++;
}

}

printf("%d",f);
}

is anything wrong with my code ?
it is giving run time error

1 Like

#include
#include<string.h>
using namespace std;
int main()
{
long int counte,counth,countc,countf,ans;
char ch[100001];
cin>>ch;
long int n=strlen(ch);
countc=counth=counte=countf=ans=0;
for(int i=0;i<n;i++)
{
if(ch[i]==‘C’)
{
//cout<<“STARTED FOUNDING CHEF . C LETTER FOUNDED\n”;
countc++;
}
else
if(ch[i]==‘H’&&countc>0)
{
//cout<<“H LETTER FOUND.\n”;
counth++;
}
else
if(ch[i]==‘E’&&counth>0&&countc>0)
{
//cout<<“E LETTER FOUND.\n”;
counte++;
}
else
if(ch[i]==‘F’&&counte>0&&counth>0&&countc>0)
{
//cout<<“F LAST LETTER FOUND.RESETTING ALL.\n”;
counte=counth=countc=0;
//cout<<“ANSWER NOW INCREMENTED.\n”;
ans++;
//cout<<“ANSWER NOW “<<ans<<” \n”;
}
}
cout<<ans<<"\n";
}
Please tell whats wrong in my code??

#include
#include<string.h>
using namespace std;
int main()
{

long int counte,counth,countc,countf,ans;

char ch[100001];
cin>>ch;
long int n=strlen(ch);
countc=counth=counte=countf=ans=0;
for(int i=0;i<n;i++)
{
	if(ch[i]=='C')
	{
		//cout<<"STARTED FOUNDING CHEF . C LETTER FOUNDED\n";
		countc++;
	}
	else
	if(ch[i]=='H'&&countc>0)
	{
		//cout<<"H LETTER FOUND.\n";
		counth++;
	}
	else
	if(ch[i]=='E'&&counth>0&&countc>0)
	{
		//cout<<"E LETTER FOUND.\n";
		counte++;
	}
	else
	if(ch[i]=='F'&&counte>0&&counth>0&&countc>0)
	{
		//cout<<"F LAST LETTER FOUND.RESETTING ALL.\n";
		counte=counth=countc=0;
		//cout<<"ANSWER NOW INCREMENTED.\n";
		ans++;
		//cout<<"ANSWER NOW  "<<ans<<" \n";
	}
}
cout<<ans<<"\n";

}
Please tell whats wrong in my code??

I don’t know whats wrong in my below code. Kindly help me to find out the error in that.

def string_manipulation(ipstring):
cond = ‘CHEF’
result = 0
alist = list(map(lambda x: x,ipstring))
if (len(ipstring) >= 1 and len(ipstring) <= 100000) and ipstring.isupper() == True:
ipstring_indexes = [x for x in range(len(alist)) if alist[x]==list(map(lambda x: x,cond))[0]]
for loop in range(0,len(ipstring_indexes)):
if len(ipstring_indexes)-1 != loop:
num = alist[ipstring_indexes[loop]:ipstring_indexes[loop+1]]
else:
num = alist[ipstring_indexes[loop]:len(alist)-1]
if “”.join([n for i, n in enumerate(num) if i==0 or n != num[i-1]]) == cond:
result = result+1
else:
result = 0
return result
print string_manipulation(raw_input(‘Enter String’))

I have the following solution. Can someone tell me what’s wrong with the logic?

<?php

$str = fgets(STDIN);

$chef = 'CHEF';
$pointer = 0;
$count = 0;

for ($i=0; $i<strlen($str); $i++)
{
	if ($str[$i] === $chef[$pointer])
	{
		if ($pointer === 3)
		{
			$pointer = 0;
			$count++;
		}
		else
		{
			$pointer++;
		}
	}
}

echo $count;

oh I had it like you but without c-- in the if str[i] == 'H', I had instead if str[i] == 'F' { c--; h--; e--; f++} which was wrong :stuck_out_tongue:

very neat code :wink:

glad u liked it… :smiley:

had exactly the same code (though the name of variables are different ) :slight_smile:
very good writing style … easy to understand…

consider , FEHCCHEF , ANS=1 but ur ANS=2

The above code written in Python

Can anyone explain why my code gets WA
I think it is right.

#include <bits/stdc++.h>
using namespace std;

int main() {

string s;
cin >> s;
int a[4] = {};
for(int i=0;i<s.size();i++)
{
    if(s[i]=='C')
        a[0]++;
    else if(s[i]=='H' && a[0]>a[1])
        a[1]++;
    else if(s[i]=='E' && a[0]>a[2])
        a[2]++;
    else if(s[i]=='F' && a[0]>a[3])
        a[3]++;
}

cout << a[3] <<endl;
return 0;

}

s=input()
d={}
for i in s:
if i==“C”:
if d.get(i)==None:
d[i]=1
else:
d[i]+=1
if d.get(“C”)!=None:
if d.get(i)==None:
d[i]=1
else:
if d[i]<d[“C”]:
d[i]+=1

print(min(d.values()))
i dont know where my code is failing.`s=input()
d={}
for i in s:
if i==“C”:
if d.get(i)==None:
d[i]=1
else:
d[i]+=1
if d.get(“C”)!=None:
if d.get(i)==None:
d[i]=1
else:
if d[i]<d[“C”]:
d[i]+=1
print(d)
print(min(d.values()))

`

ya we need the letter to be in same order as in CHEF

the route in chef is C > H > E > F
you are always chking possibility of route directly from
C to E in 1st else if block | correct
C to H in 2nd else if block | incorrect , change it
C to F in 3nd else if block | incorrect , change it

ONLY THESE MINUTE CHANGES AND YOUR CODE WILL BE PERFECT FOR ALL TEST DATA

GETTING CORRECT FOR SUBTASK 1 TASK 0 , SUBTASK 2 TASK 3 …PLS HELP ME
@usernameharsh @vlew @zygon @deepu_codes @goodwine
#include <bits/stdc++.h>
using namespace std;
int main()
{
string s,s2="";
cin>>s;
int cnt=0;
s2.append(s,0,1);
for(int i=1;i<s.length();i++)
{
if(s[i]==s[i-1]) {}
else
s2.append(s,i,1);
}
for(int i=0;i<s2.length();i++)
{
if(s2[i]==‘C’&&s2[i+1]==‘H’&&s2[i+2]==‘E’&&s2[i+3]==‘F’)
{
cnt++; i+=2;
}
}
//cout<<s2<<endl;
cout<<cnt<<endl;
return 0;
}

My code is pretty much equivalent to yours, and I get a WA (Wrong Answer) when I submit it. I can’t figure out which string would give me a wrong answer. Did you ever figure out what is wrong with yours?