Skip to content

Instantly share code, notes, and snippets.

@jcchurch
Created October 11, 2011 14:26
Show Gist options
  • Save jcchurch/1278214 to your computer and use it in GitHub Desktop.
Save jcchurch/1278214 to your computer and use it in GitHub Desktop.
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