Created
October 11, 2011 14:26
-
-
Save jcchurch/1278214 to your computer and use it in GitHub Desktop.
Solution for a 2008 South Central ACM Problem, seen here: http://livearchive.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=339&page=show_problem&problem=2157
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.util.*; | |
public class Tribute { | |
public static void main(String[] args) { | |
Scanner scan = new Scanner(System.in); | |
int datasets = scan.nextInt(); | |
scan.nextLine(); | |
for (int i = 0; i < datasets; i++) { | |
String original = scan.nextLine(); | |
String out = ""; | |
String buffer = ""; | |
int cursor = 0; | |
for (int j = 0; j < original.length(); j++) { | |
char op = original.charAt(j); | |
switch (op) { | |
case 'x': | |
if (cursor > 0) { | |
out = out.substring(0, cursor-1) + out.substring(cursor); | |
cursor--; | |
} | |
break; | |
case 'K': | |
while (cursor > 0 && out.charAt(cursor-1) != ' ') { | |
out = out.substring(0, cursor-1) + out.substring(cursor); | |
cursor--; | |
} | |
break; | |
case 'c': | |
if (cursor > 0) { | |
out = out.substring(0, cursor) + out.charAt(cursor-1) + out.substring(cursor); | |
cursor++; | |
} | |
break; | |
case 'D': | |
buffer = ""; | |
int c = cursor; | |
while (c > 0 && out.charAt(c-1) != ' ') { | |
c--; | |
} | |
while (c < cursor) { | |
buffer += out.charAt(c); | |
c++; | |
} | |
out = out.substring(0, cursor) + buffer + out.substring(cursor); | |
cursor += buffer.length(); | |
break; | |
case 'R': | |
buffer = ""; | |
while (cursor > 0 && out.charAt(cursor-1) != ' ') { | |
buffer += out.charAt(cursor-1); | |
out = out.substring(0, cursor-1) + out.substring(cursor); | |
cursor--; | |
} | |
out = out.substring(0, cursor) + buffer + out.substring(cursor); | |
cursor += buffer.length(); | |
break; | |
case 'p': | |
out = out.substring(cursor); | |
cursor = 0; | |
break; | |
case 'W': | |
out = out.substring(0, cursor); | |
break; | |
case 'h': | |
if (cursor > 0) cursor--; | |
break; | |
case 'L': | |
if (cursor < out.length()) cursor++; | |
break; | |
case 'f': | |
cursor = 0; | |
break; | |
case 'G': | |
cursor = out.length(); | |
break; | |
default: | |
out = out.substring(0, cursor) + op + out.substring(cursor); | |
cursor++; | |
} | |
} | |
out = out.substring(0, cursor) + "^" + out.substring(cursor); | |
System.out.println(out); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment