題目:http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=1056
import java.util.*; import java.lang.*; public class Main{ public static int times; public static void main(String[] args) { Scanner keyin = new Scanner(System.in); while(keyin.hasNext()){ int rulesNum = keyin.nextInt(); keyin.nextLine(); Rules[] stRules = new Rules[20]; if(rulesNum==0) break; Rules rules ; for(int i=0 ; i<rulesNum ;i++){ String find = keyin.nextLine(); String replace = keyin.nextLine(); rules = new Rules(find,replace); stRules[i] = rules; } String give = keyin.nextLine(); System.out.println(search(stRules,give,rulesNum)); } } public static String search(Rules[] stRule,String give,int rulesNum){ StringBuilder editedText = new StringBuilder(give); for(int i=0 ; i<rulesNum ; i++){ int findIndex = editedText.indexOf(stRule[i].find); while(findIndex!=-1){ editedText.delete(findIndex, findIndex+(stRule[i].find).length()); editedText.insert(findIndex,stRule[i].replace); findIndex = editedText.indexOf(stRule[i].find); } } return editedText.toString(); } } class Rules { public String find=""; public String replace=""; public Rules(String find,String replace){ this.find = find; this.replace=replace; } }
沒有留言:
張貼留言