 # Sending file

Practice

Author: Aryan KD
Tester: Aryan KD
Editorialist: Aryan KD

# DIFFICULTY:

CAKEWALK, SIMPLE, EASY.

Math .

# PROBLEM:

Chef wants to send a file to social network , then he encountered unexpected problem . If the name of the file contains three or more “x” (lowercase Latin letters “x”) in a row, the system considers that the file content does not correspond to the social network topic. In this case, the file is not sent and an error message is displayed.

Determine the minimum number of characters to remove from the file name so after that the name does not contain “xxx” as a substring. Print 0 if the file name does not initially contain a forbidden substring “xxx”.

You can delete characters in arbitrary positions (not necessarily consecutive). If you delete a character, then the length of a string is reduced by 1. For example, if you delete the character in the position 2 from the string “txxxnpl”, then the resulting string is “txxnpl”.

# EXPLANATION:

Determine the minimum number of characters to remove from the file name so after that the name does not contain “xxx” as a substring. Print 0 if the file name does not initially contain a forbidden substring “xxx”.

You can delete characters in arbitrary positions (not necessarily consecutive). If you delete a character, then the length of a string is reduced by 1. For example, if you delete the character in the position 2 from the string “txxxnpl”, then the resulting string is “txxnpl”.

simply meaning of it we have to just count number of x at a place is more than two and print the number suppose the given string is ghxxxxxhx
in given string 5 ‘x’ are together means here more than 2 x are together here if we want to send file we have remove 3 ‘x’ for sending a file there for ans is 3

# SOLUTIONS:

Setter's Solution

#include<bits/stdc++.h>
using namespace std;
void testcase()
{
string s1;
cin>>s1;
int cnt=0,sum=0;
for(int i=0;i<s1.length();i++){
if(s1[i]==‘x’ && s1[i+1]== ‘x’){
cnt++;
}
else if((s1[i]!=s1[i+1])&&cnt>1){
sum+=(cnt-1);
cnt=0;
}
else{
cnt=0;
}

}
cout<<sum;
}
int main()
{
int t;
cin>>t;
while(t–)
{
testcase();
cout<<endl;
}
return 0;
}

Tester's Solution

#include<bits/stdc++.h>
using namespace std;
void testcase()
{
string s1;
cin>>s1;
int cnt=0,sum=0;
for(int i=0;i<s1.length();i++){
if(s1[i]==‘x’ && s1[i+1]== ‘x’){
cnt++;
}
else if((s1[i]!=s1[i+1])&&cnt>1){
sum+=(cnt-1);
cnt=0;
}
else{
cnt=0;
}

}
cout<<sum;
}
int main()
{
int t;
cin>>t;
while(t–)
{
testcase();
cout<<endl;
}
return 0;
}

Editorialist's Solution

#include<bits/stdc++.h>
using namespace std;
void testcase()
{
string s1;
cin>>s1;
int cnt=0,sum=0;
for(int i=0;i<s1.length();i++){
if(s1[i]==‘x’ && s1[i+1]== ‘x’){
cnt++;
}
else if((s1[i]!=s1[i+1])&&cnt>1){
sum+=(cnt-1);
cnt=0;
}
else{
cnt=0;
}

}
cout<<sum;
}
int main()
{
int t;
cin>>t;
while(t–)
{
testcase();
cout<<endl;
}
return 0;
}