Identify Sentences by Consonant Runs
National Olympiad — Regional round (2013 season)
Submission name: tststsbreh.cpp
Problem summary
Classify each sentence: one language never has four consecutive consonants in a word, while the other has at least one such word. Count the two languages, treating a,e,i,o,u,y as vowels.
Input
The sentence count followed by that many complete lines of lowercase Latin words, spaces, and a final period.
Output
The count without a consonant run of length 4, then the count with such a run.
Constraints
At most 1000 sentences; at most 1000 characters per sentence.
Worked example
Input and output for the worked example discussed below.
Input
3 tststsbreh kvassssi hubvlka. miyyy takvuz sum si em ubava em gizdava pa i umna i fruclavu. tststs shte si htshtschup glvta s tttttteb.
Output
1 2
Solution analysis
- Read whole lines because a sentence contains multiple words. Maintain the current run of consonant letters; increment it on a consonant and reset it on a vowel or a word separator.
- Once the run reaches 4, the sentence belongs to the consonant-heavy language. Continue scanning or remember a flag; later vowels cannot undo the existence of that earlier run.
- The first sample sentence begins with a long tststs consonant sequence, and the third does as well. The second has no four-consonant block, so the output is 1 2. The letter y explicitly counts as a vowel here.
Why it works
The running counter is exactly the number of consecutive consonants ending at the current character, because every non-consonant resets it. A flag becomes true if and only if some such run has length at least four, exactly the stated classification criterion.
Watch out. Do not forget y in the vowel set, join consonants across spaces, or use operator>> to read only the first word. Consume the newline after the initial count.
Commented C++17 solution
An independently written complete program with standard headers.
#include<iostream>
#include<string>
using namespace std;
int main(){
int n;
cin>>n;
string sentence;
getline(cin,sentence);
int light=0;
int heavy=0;
const string vowels="aeiouy";
for(int i=0;i<n;i++){
getline(cin,sentence);
int run=0;
bool found=false;
for(char letter:sentence){
if(letter>='a'&&letter<='z'&&vowels.find(letter)==string::npos){
run++;
// A later separator cannot cancel a consonant run already found.
found=found||run>=4;
}
else{
run=0;
}
}
if(found){
heavy++;
}
else{
light++;
}
}
cout<<light<<' '<<heavy<<'\n';
}