PKU : 2803 - Defining Moment

問題概要

http://poj.org/problem?id=2803

問題文中の表のように, 文字列の接頭辞・接尾辞が, ある特定の文字列になったら, それを他のメッセージに置き換えるような問題.
接頭辞・接尾辞は, それぞれ最大でも1回しか出てこないものとしてよい.

アルゴリズム

文字列操作やるだけ.
Wordの前側につけるメッセージと, 後ろ側につけるメッセージを別で管理する.

プログラム

static String pre,suf,word;

static void prefix(String s){
  if(s.startsWith("anti")){
    pre += "against ";
    word = word.substring(4);
  }
  else if(s.startsWith("post")){
    pre += "after ";
    word = word.substring(4);
  }
  else if(s.startsWith("pre")){
    pre += "before ";
    word = word.substring(3);
  }
  else if(s.startsWith("re")){
    suf += " again";
    word = word.substring(2);
  }
  else if(s.startsWith("un")){
    pre += "not ";
    word = word.substring(2);
  }
}

static void suffix(String s){
  if(s.endsWith("er")){
    pre += "one who ";
    suf = "s" + suf;
    word = word.substring(0,word.length()-2);
  }
  else if(s.endsWith("ing")){
    pre += "to actively ";
    word = word.substring(0,word.length()-3);
  }
  else if(s.endsWith("ize")){
    pre += "change into ";
    word = word.substring(0,word.length()-3);
  }
  else if(s.endsWith("s")){
    pre += "multiple instances of ";
    word = word.substring(0,word.length()-1);
  }
  else if(s.endsWith("tion")){
    pre += "the process of ";
    suf = "ing" + suf;
    word = word.substring(0,word.length()-4);
  }
}

public static void main(String[] args)throws IOException{
  BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
  int T = Integer.parseInt(br.readLine());

  while(T-- > 0){
    String s = br.readLine();
    pre = suf = "";
    word = s;
    prefix(s);
    suffix(s);
    System.out.println(pre + word + suf);
  }
}