CHEFCHR - Editorial

PROBLEM LINK:

Practice
Contest

Author: Shivam Rathore
Tester: Hanlin Ren
Editorialist: Hanlin Ren

DIFFICULTY:

CAKEWALK

PREREQUISITES:

None

PROBLEM:

Chef has a string consisting of only lowercase letters a-z. He wants to pick 4 consecutive letters from the string such that the 4 letters can be arranged into the word chef. Find the number of ways to do this.

EXPLANATION:

The solution is straightforward. Let the string be s[0\dots(n-1)]. Let’s enumerate the start point of these letters, say i. Then 0\le i\le n-4. The four letters are s[i],s[i+1],s[i+2],s[i+3]. To check if they can be arranged into chef, we can sort these letters and check if they form cefh in order. Let me explain in details.

Checking four letters

First, we need a subprocedure which given 4 letters s1, s2, s3, s4, determine if they can be reordered into chef. Let’s call it check(s1, s2, s3, s4). A method to do this is to sort the letters, and the result of sorting should be cefh. C++ code:

bool check(char s1, char s2, char s3, char s4) {
  char s[5] = {s1, s2, s3, s4, 0};
  sort(s, s + 4);
  return strcmp(s, "cefh") == 0;//strcmp returns 0 if two strings are equal
}

The 0 at the end of array s is necessary, since strcmp recognizes it as the end of string. Or, if you don’t like strcmp, the last line can be written as

return s[0]=='c' && s[1]=='e' && s[2]=='f' && s[3]=='h';

The original problem

Given the subprocedure check, the rest is easy:

  • We enumerate the start i of the four letters. Let n be the length of string, we have 0\le i\le n-4, and four letters are s[i], s[i+1], s[i+2], s[i+3];
  • If check(s[i],s[i+1],s[i+2],s[i+3]), the answer is increased by 1;
  • At last, if answer is 0, output normal; otherwise output lovely and the answer.

Code:

n = strlen(s); //s[0..n-1]
answer = 0;
//check all possible i's
for (i = 0; i <= n - 4; i++)
  if (check(s[i], s[i + 1], s[i + 2], s[i + 3]))
    answer += 1;
//output answer
if (answer == 0) printf("normal\n");
else printf("lovely %d\n", answer);

AUTHOR’S AND TESTER’S SOLUTIONS:

Author’s solution can be found here.
Tester’s solution can be found here.

https://pastebin.com/aq7mK1cJ

Can anyone tell me why I am getting wrong answer? Not able to figure out.

https://pastebin.com/9LtzsuAd
Can anyone tell what is wrong?


can anybody tell me the problem in the code please

#include<string.h>
#include
using namespace std;
int main(){
long long int t;
cin>>t;
while(t–){
char str[500005];
long long int l=0,i=0;
cin.getline(str,500005);
l=strlen(str);

long long int count=0;
	for(int i=0;i<l;i++){
	 	
if(str[i]=='c'||str[i]=='h'||str[i]=='e'||str[i]=='f')
            if((str[i+1]=='c'||str[i+1]=='h'||str[i+1]=='e'||str[i+1]=='f')&&(str[i]!=str[i+1]))
                if((str[i+2]=='c'||str[i+2]=='h'||str[i+2]=='e'||str[i+2]=='f')&&(str[i]!=str[i+2])&&(str[i+1]!=str[i+2]))
                    if((str[i+3]=='c'||str[i+3]=='h'||str[i+3]=='e'||str[i+3]=='f')&&(str[i]!=str[i+3])&&(str[i+1]!=str[i+3])&&(str[i+2]!=str[i+3]))
count++;

}
if(count>0){

	cout<<"lovely "<<count<<endl;
	 }
	else
		cout<<"normal"<<endl;

}
return 0;

}

I am getting a segmentation Error in this problem

 #include<iostream>
#include<algorithm>
#include<vector>
#include<string>
#include <stdio.h>
using namespace std;
void meth( char *s,int &m){
    vector<char> vec;
    for(int i=0;i<4;i++){
        if(s[i]=='c'||s[i]=='h'||s[i]=='e'||s[i]=='f'){
          
         if( std::find(vec.begin(), vec.end(),s[i])==vec.end()){
             vec.push_back(s[i]);
        }
    }
} 
if(vec.size()==4){   
    m=m+1;
}
}


int main(){
	int t=0;
	cin>>t;
	while(t-->0){
   string s1;
   cin>>s1;
   const char *c=s1.c_str();
   char s[5];
   int ans=0;
   for(int i=0;i<s1.length()-3;i++){
	   	int x=i;
   		int count=0;
		   //cout<<c[i]<<" "<<endl;
   	if(c[i]=='c'||c[i]=='h'||c[i]=='e'||c[i]=='f'){
   		 while(count!=4){
   		 		s[count]=c[x];
   		 		count++;
   		 		x++;			
		    }
			  
	   }
	
	 meth(s,ans);
	}
	if(ans>0){
		cout<<"lovely"<<" "<<ans;
	}
	else{
		cout<<"normal"<<endl;
	}
	cout<<"\n";
	 //  delete[] s;
	
	 // cout<<ans<<endl;
	}
}

Can anyone help me out because i am getting segmentation fault when i am submitting my solution
https://www.codechef.com/viewsolution/22576965

can anyone help me find the errors in this solution:
https://www.codechef.com/viewsolution/24887947

@r_64 @shivam010

for _ in range(int(input())):
count=0
s=input()
i=0
while i!=(len(s)-3):
k=s[i]+s[i+1]+s[i+2]+s[i+3]
if ‘c’ in k and ‘e’ in k and 'h’in k and ‘f’ in k:
count+=1
i+=1
else:
i+=1
if count>=1:
print(‘lovely’,count)
else:
print(‘normal’)
#whats the problem in this code bro please tell it’s running fine for me

I used this strategy:
If we add and multiply the numeric value of characters c, h, e and f i.e.sum = 'c' + 'h' + 'e' + 'f' and multiply them mul = 'c' * 'h' * 'e' * 'f'.
Then we only need to check if s[i], s[i+1], s[i+2], s[i+3] evaluates to same thing or not.
My code:

#include <iostream>
#include <string>
using namespace std;

int main(){
    int t, n, i, sum, mul, k;
    cin >> t;
    string s;
    sum = ('c') + ('h') + ('e') + ('f');
    mul = ('c') * ('h') * ('e') * ('f');
    while(t-->0){
        cin >> s;
        k = 0;
        n = s.length();
        for(i=0; i<n-3; ++i){
            if(s[i]*s[i+1]*s[i+2]*s[i+3]==mul && s[i]+s[i+1]+s[i+2]+s[i+3]==sum) ++k;
        }
        if(k==0) cout << "normal\n";
        else cout << "lovely " << k << endl;
    }
    return 0;
}

Of course not the right way because there can be another combination satisfying sum and mul, but fastest and passed all the cases.

This is a test case where your code fails
1
chefchefchef
It is happening because find function will give you the first occurence of that permutation but there is possibility of multiple occurences of the same substring

You have asked an amazing doubt. It took me a lot of time to find the mistake
See the corrected solution here
https://www.codechef.com/viewsolution/32998956

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

int main()
{
int t;
cin>>t;
while(t–)
{
string s,temp;
cin>>s;
int c=0;

    for(int i=0;i<s.size();i++)
    {
       set<char>ourset;
       ourset.insert(s[i]);
       ourset.insert(s[i+1]);
       ourset.insert(s[i+2]);
       ourset.insert(s[i+3]);
       if(ourset.size()==4)
       {
         temp=s.substr(i,4);

         sort(temp.begin(),temp.end());
         if(temp=="cefh")
            c++;
       }
    }
    if(c>0)
        cout<<"lovely "<<c<<"\n";
    else
        cout<<"normal\n";
}
return 0;

}

How is my solution ??
Execution time - 0.68

#include
using namespace std;

int main() {
// your code goes here
int t;
cin>>t;
while(t–)
{
string str;
cin>>str;
int k=0;

    for(int i=0;i<=(str.size()-4);i++)
    {
        bool c,h,e,f;
        c=false;
        h=false;
        e=false;
        f=false;
        for(int j=i;j<=(i+3);j++)
        {
            if(str.at(j)=='c')
            c=true;
            if(str.at(j)=='h')
            h=true;
            if(str.at(j)=='e')
            e=true;
            if(str.at(j)=='f')
            f=true;
        }
        if((c==true)&&(h==true)&&(e==true)&&(f==true))
        k++;
    }
    
    
    if(k==0)
    cout<<"normal"<<endl;
    else
    cout<<"lovely "<<k<<endl;
}
return 0;

}

Can anybody tell me what is the error in this code
while submitting, it is giving SIGSEV error