Skip to content

Instantly share code, notes, and snippets.

@chancancode
Created September 19, 2009 23:18
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save chancancode/189628 to your computer and use it in GitHub Desktop.
Save chancancode/189628 to your computer and use it in GitHub Desktop.
import java.util.Arrays;
import java.util.HashSet;
import java.util.Scanner;
public class C {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
int i=0;
Scanner in = new Scanner(System.in);
while(in.hasNextLine()){
i++;
if(i != 1) System.out.println();
System.out.println("Case "+i+":");
String s = in.nextLine();
HashSet<String> result = new HashSet<String>();
result.add(s);
HashSet<String> newWords = new HashSet<String>();
newWords.add(s);
while(newWords.size() > 0){
HashSet<String> oldNewWords = (HashSet<String>) newWords.clone();
newWords = new HashSet<String>();
for(String ss : oldNewWords){
for(String sss : toAckanese(ss)){
for(String ssss : toArgharian(sss)){
if(result.contains(ssss))
continue;
else{
result.add(ssss);
newWords.add(ssss);
}
}
}
}
}
String[] finalResult = result.toArray(new String[0]);
Arrays.sort(finalResult);
for(String sssssssssssssss : finalResult){
System.out.println(sssssssssssssss);
}
}
}
static HashSet<String> toAckanese(String s){
HashSet<String> rtn = new HashSet<String>();
for(int i=0;i<s.length();i++){
try{
// Rule 1
if(i == s.length()-1 && s.charAt(i) == 'e'){
rtn.add(s.substring(0,i-1)+"e"+s.substring(i-1,i));
}
if(s.charAt(i) == 'r'){
// Rule 2
if(s.charAt(i+1) == 'g' && s.charAt(i+2) == 'e')
if(i+3>=s.length())
rtn.add(s.substring(0,i)+"che");
else
rtn.add(s.substring(0,i)+"che"+s.substring(i+3));
// Rule 3
if(s.charAt(i+1) == 'e')
if(i+2>=s.length())
rtn.add(s.substring(0,i)+"rec");
else
rtn.add(s.substring(0,i)+"rec"+s.substring(i+2));
}
// Rule 4
if(s.charAt(i) == 'c' && s.charAt(i+1) == 'e')
rtn.add(s.substring(0,i)+"ec"+s.substring(i+2));
}catch(Exception e){}
}
return rtn;
}
static HashSet<String> toArgharian(String s) {
HashSet<String> rtn = new HashSet<String>();
for(int i=0;i<s.length();i++){
try{
// Rule 1
if(i == s.length()-2 && s.charAt(i) == 'e'){
rtn.add(s.substring(0,i)+s.substring(i+1)+"e");
}
// Rule 2
if(s.charAt(i) == 'c'){
// Rule 2
if(s.charAt(i+1) == 'h' && s.charAt(i+2) == 'e')
if(i+3>=s.length())
rtn.add(s.substring(0,i)+"rge");
else
rtn.add(s.substring(0,i)+"rge"+s.substring(i+3));
}
// Rule 3
if(s.charAt(i) == 'r'){
if(s.charAt(i+1) == 'e' && s.charAt(i+2) == 'c')
if(i+3>=s.length())
rtn.add(s.substring(0,i)+"re");
else
rtn.add(s.substring(0,i)+"re"+s.substring(i+3));
}
// Rule 4
if(s.charAt(i) == 'e' && s.charAt(i+1) == 'c')
rtn.add(s.substring(0,i)+"ce"+s.substring(i+2));
}catch(Exception e){}
}
HashSet<String> realRtn = new HashSet<String>();
for(String ss : rtn){
if(ss.length() >= 3 && ss.length() <= 12)
realRtn.add(ss);
}
return realRtn;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment