Hy-phe-na-tion Rulez
Jump to navigation
Jump to search
This problem is actually mildly ambiguous in the "qu" case. Consider "equce"....should it be equ-ce or e-qu-ce? I think it's the former....but good to know.
Anyway, just do all the case work and apply the rules like it says.
import java.util.*;
public class a {
Scanner in=new Scanner(System.in);
public static void main(String[] args) {
new a().go();
}
String fakec3="str.*";
String fakec2="(qu|tr|br|st|sl|bl|cr|ph|ch).*";
String vowels="[aeiouy]";
public void go() {
while(true){
String ss=in.next();
if(ss.equals("==="))break;
String s=ss.toLowerCase();
Queue<Integer> h=new LinkedList<Integer>();
for(int i=0;i<s.length();i++){
if(!s.substring(i, i+1).matches(vowels))continue;//first must be a vowel
if(s.charAt(i)=='u'&&i>0&&s.charAt(i-1)=='q')continue;//qu combo special
//match first consonant
int on=i+1;
int firstmatch=0;
if(on>=s.length())continue;
if(s.substring(on).matches(fakec3)){
on+=3;
firstmatch=3;
} else if(s.substring(on).matches(fakec2)){
on+=2;
firstmatch=2;
} else if (s.substring(on,on+1).matches("[a-zA-Z&&[^aeiouAEIOU]]")){
on+=1;
firstmatch=1;
} else {
continue;//didn't match a consonant;
}
if(on>=s.length())continue;//out of letters
if(s.substring(on).matches("[aiouy]|([aeiouy].+)")){//Found a vcv match
h.add(i+1);
continue;
}
if(s.substring(on).matches(fakec3)){//match second consonant
on+=3;
} else if(s.substring(on).matches(fakec2)){
on+=2;
} else if (s.substring(on,on+1).matches("[a-zA-Z&&[^aeiouAEIOU]]")){
on+=1;
} else {
continue;//didn't match a consonant;
}
if(on>=s.length())continue;
if(s.substring(on).matches("[aeiouy].*")){//found vccv match
h.add(i+firstmatch+1);
}
//equce is undefined
}
int on=0;
StringBuilder b=new StringBuilder("");
while(!h.isEmpty()){
int next=h.poll();
b.append(ss.substring(on,next));
b.append("-");
on=next;
}
b.append(ss.substring(on));
System.out.println(b.toString());
}
}
}