50010. Word Editor

I'm a slow walker, but I never walk backwards.

Problem description

Write a program to edit a word. First you will be given a word consisting of lower letters. Each line afterward is a command. The commands are as follow.

  • replace x y
    Both $x$ and $y$ are character. You need to replace all occurrence of $x$ by $y$.
  • remove x
    Remove all occurrence of $x$.
  • addhead x
    Add a character $x$ at the beginning of the word.
  • addtail x
    Add a character $x$ at the end of the word.
  • end
    Stop the computation. It is guarantee the last command is always an end.

For example, if you are given a word "pangfeng", then after "replace g d" you will have "pandfend". Then after "remove n" you will have "padfed". After "addhead a" you will have "apadfed". After "addtail s" you will have "apadfeds".

Subtask

  • 10pt. Implement replace and end. The input is always correct.
  • 20pt. Implement the replace, remove, and end. The input is always correct.
  • 30pt. Implement the replace, remove, addtail, and end. The input is always correct.
  • 30pt. Implement all commands. The input is always correct.
  • 10pt. Implement all commands. The input command maybe incorrect. If you encounter an invalid command "xyz", output "invalid command xyz" and stop the program immediately.

Input Format

It has a testdata set. First line contains a string $S$, then following many commands to edit the word.

  • $\vert S\vert \le 100000$
  • $\vert \text{#valid command}\vert \le 100000$
  • $\vert \text{invalid command}\vert \le 20$

Sample Input 1

pangfeng
replace g d
end

Sample Output 1

pandfend

Sample Input 2

pangfeng
replace g d
remove n
end

Sample Output 2

padfed

Sample Input 3

pangfeng
replace g d
remove n
addhead a
addtail s
end

Sample Output 3

apadfeds

Sample Input 3

pangfeng
replace g d
remove n
addhead a
addtail s
addpangfeng b
addhead a
end

Sample Output 3

invalid command addpangfeng

建議

//
scanf("%s", S); 取代 gets() / fgets()
//
scanf("%s %s %s", cmd, sx, sy), cx = sx[0], cy = sy[0]; 取代 scanf("%s %c %c", cmd, &cx, &cy);
//
int n = strlen(s);
for (int i = 0; i < n; i++) {
}
// TLE,不要用以下這種寫法。
for (int i = 0; i < strlen(s); i++) {
}

Discussion