Problem:
A Simple Offline Text Editor A text editor is a useful software tool that can help people in various situations including writing and programming. Your job in this problem is to construct an offline text editor, i.e., to write a program that first reads a given text and a sequence of editing commands and finally reports the text obtained by performing successively the commands in the given sequence. The editor has a text buffer and a cursor. The target text is stored in the text buffer and most editing commands are performed around the cursor. The cursor has its position that is either the beginning of the text, the end of the text, or between two consecutive characters in the text. The initial cursor position (i.e., the cursor position just after reading the initial text) is the beginning of the text. A text manipulated by the editor is a single line consisting of a sequence of characters, each of which must be one of the following: 'a' through 'z', 'A' through 'Z', '0' through '9', '.' (period), ',' (comma), and ' ' (blank). You can assume that any other characters never occur in the text buffer. You can also assume that the target text consists of at most 1,000 characters at any time. The definition of words in this problem is a little strange: a word is a non-empty character sequence delimited by not only blank characters but also the cursor. For instance, in the following text with a cursor represented as '^',   He^llo, World.  the words are the following.   He  llo,  World.  Notice that punctuation characters may appear in words as shown in this example. The editor accepts the following set of commands. In the command list, " any-text " represents any text surrounded by a pair of double quotation marks such as "abc" and "Co., Ltd.".   Command   Descriptions    forward char   Move the cursor by one character to the right, unless the cursor is already at the end of the text.    forward word   Move the cursor to the end of the leftmost word in the right. If no words occur in the right, move it to the end of the text.    backward char   Move the cursor by one character to the left, unless the cursor is already at the beginning of the text.    backward word   Move the cursor to the beginning of the rightmost word in the left. If no words occur in the left, move it to the beginning of the text.    insert " any-text "   Insert any-text (excluding double quotation marks) at the position specified by the cursor. After performing this command, the new cursor position is at the end of the inserted text. The length of any-text is less than or equal to 100.    delete char   Delete the character that is right next to the cursor, if it exists.    delete word   Delete the leftmost word in the right of the cursor. If one or more blank characters occur between the cursor and the word before performing this command, delete these blanks, too.  If no words occur in the right, delete no characters in the text buffer.    Input The first input line contains a positive integer, which represents the number of texts the editor will edit. For each text, the input contains the following descriptions:   The first line is an initial text whose length is at most 100.  The second line contains an integer M representing the number of editing commands.  Each of the third through the M +2nd lines contains an editing command.  You can assume that every input line is in a proper format or has no syntax errors. You can also assume that every input line has no leading or trailing spaces and that just a single blank character occurs between a command name (e.g., forward) and its argument (e.g., char). Output For each input text, print the final text with a character '^' representing the cursor position.  Each output line shall contain exactly a single text with a character '^'. Sample Input  3
A sample input
9
forward word
delete char
forward word
delete char
forward word
delete char
backward word
backward word
forward word
Hallow, Word.
7
forward char
delete word
insert "ello, "
forward word
backward char
backward char
insert "l"

3
forward word
backward word
delete word  Output for the Sample Input  Asampleinput^
Hello,  Worl^d.
^

Solution (Python):
for _ in [0]*int(input()):
    s=input()
    cur=0
    for _ in [0]*int(input()):
        c=input()
        if c=='forward word':
            while cur<len(s) and s[cur]==' ':cur+=1
            while cur<len(s) and s[cur]!=' ':cur+=1
        elif c=='delete char' and cur!=len(s):s=s[:cur]+s[cur+1:]
        elif c=='backward word':
            while cur>0 and s[cur-1]==' ':cur-=1
            while cur>0 and s[cur-1]!=' ':cur-=1
        elif c=='forward char':cur=min(len(s),cur+1)
        elif c=='delete word':
            if s[cur:].count(' ') == len(s[cur:]): continue
            while cur<len(s) and s[cur]==' ':s=s[:cur]+s[cur+1:]
            while cur<len(s) and s[cur]!=' ':s=s[:cur]+s[cur+1:]
        elif c[0]=='i':
            c=c.split('"')[1]
            s=s[:cur]+c+s[cur:]
            cur+=len(c)
        elif 'backward char':cur=max(0,cur-1)
    print(s[:cur]+'^'+s[cur:])

Solution (Python):
for _ in [0]*int(input()):
    s=input()
    cur=0
    for _ in [0]*int(input()):
        c=input()
        if c=='forward word':
            while cur<len(s) and s[cur]==' ':cur+=1
            while cur<len(s) and s[cur]!=' ':cur+=1
        elif c=='delete char':s=s[:cur]+s[cur+1:]
        elif c=='backward word':
            while cur>0 and s[cur-1]==' ':cur-=1
            while cur>0 and s[cur-1]!=' ':cur-=1
        elif c=='forward char':cur=min(len(s),cur+1)
        elif c=='delete word':
            if s[cur:].count(' ') == len(s[cur:]): continue
            while cur<len(s) and s[cur]==' ':s=s[:cur]+s[cur+1:]
            while cur<len(s) and s[cur]!=' ':s=s[:cur]+s[cur+1:]
        elif c[0]=='i':
            c=c.split('"')[1]
            s=s[:cur]+c+s[cur:]
            cur+=len(c)
        elif 'backward char':cur=max(0,cur-1)
    print(s[:cur]+'^'+s[cur:])

Solution (Python):
for _ in [0]*int(input()):
    s=input()
    cur=0
    for _ in [0]*int(input()):
        c=input()
        if c[0]=='i':
            c=c.split('"')[1]
            s=s[:cur]+c+s[cur:]
            cur+=len(c)
        elif c[8]=='w':
            while cur<len(s) and s[cur]==' ':cur+=1
            while cur<len(s) and s[cur]!=' ':cur+=1
        elif c[7]=='c':s=s[:cur]+s[cur+1:]
        elif c[9]=='w':
            while cur>0 and s[cur-1]==' ':cur-=1
            while cur>0 and s[cur-1]!=' ':cur-=1
        elif c[8]=='c':cur=min(len(s),cur+1)
        elif c[7]=='w':
            if s[cur:].count(' ') == len(s[cur:]): continue
            while cur<len(s) and s[cur]==' ':s=s[:cur]+s[cur+1:]
            while cur<len(s) and s[cur]!=' ':s=s[:cur]+s[cur+1:]
        else:cur=max(0,cur-1)
    print(s[:cur]+'^'+s[cur:])

Solution (Python):
class TextEditor:
    cur_w = 0
    cur_c = 0

    def __init__(self, txt):
        self.words = txt.split(' ')
        self.queries = {
            'forward char': self.forward_char,
            'forward word': self.forward_word,
            'backward char': self.backward_char,
            'backward word': self.backward_word,
            'delete char': self.delete_char,
            'delete word': self.delete_word
        }

    def query(self, q):
        if q[0] == 'i':
            txt = q.split(maxsplit=1)[1][1:-1]
            self.insert(txt)
        else:
            self.queries[q]()

    def forward_char(self):
        if self.cur_c < len(self.words[self.cur_w]):
            self.cur_c += 1
        elif self.cur_w < len(self.words) - 1:
            self.cur_w += 1
            self.cur_c = 0
        else:
            pass

    def forward_word(self):
        if self.cur_c < len(self.words[self.cur_w]):
            self.cur_c = len(self.words[self.cur_w])
        elif self.cur_w < len(self.words) - 1:
            self.cur_w += 1
            while not len(self.words[self.cur_w]) and self.cur_w < len(self.words) - 1:
                self.cur_w += 1
            self.cur_c = len(self.words[self.cur_w])
        else:
            pass

    def backward_char(self):
        if self.cur_c > 0:
            self.cur_c -= 1
        elif self.cur_w > 0:
            self.cur_w -= 1
            self.cur_c = len(self.words[self.cur_w])
        else:
            pass

    def backward_word(self):
        if self.cur_c > 0:
            self.cur_c = 0
        elif self.cur_w > 0:
            self.cur_w -= 1
            while not len(self.words[self.cur_w]) and self.cur_w > 0:
                self.cur_w -= 1
        else:
            pass

    def insert(self, txt):
        st = txt.split(' ')
        new_words = self.words[:self.cur_w]
        if len(st) > 1:
            cw = self.words[self.cur_w]
            new_words.append(cw[:self.cur_c] + st[0])
            new_words.extend(st[1:-1])
            new_words.append(st[-1] + cw[self.cur_c:])
            self.cur_c = len(st[-1])
        else:
            cw = self.words[self.cur_w]
            new_words.append(cw[:self.cur_c] + st[0] + cw[self.cur_c:])
            self.cur_c = self.cur_c + len(st[0])
        new_words.extend(self.words[self.cur_w + 1:])
        self.cur_w = self.cur_w + len(st) - 1
        self.words = new_words

    def delete_char(self):
        cw = self.words[self.cur_w]
        if self.cur_c < len(cw):
            self.words[self.cur_w] = cw[:self.cur_c] + cw[self.cur_c + 1:]
        elif self.cur_w < len(self.words) - 1:
            if len(cw):
                nw = self.words.pop(self.cur_w + 1)
                self.words[self.cur_w] = cw + nw
            else:
                self.words.pop(self.cur_w)
        else:
            pass

    def delete_word(self):
        if self.cur_c < len(self.words[self.cur_w]):
            self.words[self.cur_w] = self.words[self.cur_w][:self.cur_c]
        elif self.cur_w < len(self.words) - 1:
            tmp_w = self.cur_w + 1
            while tmp_w < len(self.words) and not len(self.words[tmp_w]):
                tmp_w += 1
            if tmp_w < len(self.words):
                del self.words[self.cur_w + 1:tmp_w + 1]

    def output(self):
        words = self.words.copy()
        words[self.cur_w] = self.words[self.cur_w][:self.cur_c] + '^' + \
                            self.words[self.cur_w][self.cur_c:]
        print(*words)


n = int(input())
for _ in range(n):
    te = TextEditor(input().strip())
    q = int(input())
    for _ in range(q):
        te.query(input().strip())
    te.output()

Solution (Python):
for i in range(input()):
    s=raw_input()
    cur=0
    for j in range(input()):
        cmd=raw_input()
        if cmd=="forward char":
            cur=min(cur+1,len(s))
        elif cmd=="forward word":
            while cur<len(s) and s[cur]==" ":
                cur+=1
            while cur<len(s) and s[cur]!=" ":
                cur+=1
        elif cmd=="backward char":
            cur=max(cur-1,0)
        elif cmd=="backward word":
            while cur>0 and s[cur-1]==" ":
                cur-=1
            while cur>0 and s[cur-1]!=" ":
                cur-=1
        elif cmd=="delete char":
            if cur!=len(s):
                s=s[:cur]+s[cur+1:]
        elif cmd=="delete word":
            temp=cur
            if temp>=len(s):
                continue
            while temp<len(s) and s[temp]==" ":
                temp+=1
            if temp>=len(s):
                continue
            while temp<len(s) and s[temp]!=" ":
                temp+=1
            s=s[:cur]+s[temp:]
        elif "insert" in cmd:
            idx1=cmd.find('"')
            idx2=cmd.find('"',idx1+1)
            s=s[:cur]+cmd[idx1+1:-1]+s[cur:]
            cur+=idx2-idx1-1
    print s[:cur]+"^"+s[cur:]

Solution (Python):
def operate( cmd, text, cur ):
    lentxt = len(text)
    if cmd == "forward char":
        if cur < lentxt: cur += 1
    elif cmd == "backward char":
        if cur > 0: cur -= 1
    elif cmd == "forward word":
        while cur < lentxt and text[cur] == ' ': cur += 1
        while cur < lentxt and text[cur] != ' ': cur += 1
    elif cmd == "backward word":
        while cur > 0 and text[cur - 1] == ' ': cur -= 1
        while cur > 0 and text[cur - 1] != ' ': cur -= 1
    elif cmd == "delete char":
        text = text[:cur] + text[cur + 1:]
    elif cmd == "delete word":
        d = 0
        while cur + d < lentxt and text[cur + d] == ' ': d += 1
        if cur + d != lentxt:
            while cur + d < lentxt and text[cur + d] != ' ': d += 1
            text = text[:cur] + text[cur + d:]
    else:
        ins = cmd[8:-1]
        text = text[:cur] + ins + text[cur:]
        cur += len(ins)
    return cur, text

if __name__ == "__main__":
    a = int(raw_input())
    for i in range(a):
        text = raw_input()
        cnt = int(raw_input())
        cursor = 0
        for j in range(cnt):
            command = raw_input()
            cursor, text = operate( command, text, cursor )
        print (text[:cursor] + '^' + text[cursor:])

Solution (Python):
for _ in range(input()):
	s = raw_input()
	cur = 0
	for __ in range(input()):
		cmd = raw_input()
		if   cmd == "forward char":
			cur = min(len(s),cur+1)
			
		elif cmd == "forward word":
			while cur < len(s) and s[cur] == " ": cur+=1
			while cur < len(s) and s[cur] != " ": cur+=1
				
		elif cmd == "backward char":
			cur = max(0,cur-1)
			
		elif cmd == "backward word":
			while cur > 0 and s[cur-1] == " ": cur-=1
			while cur > 0 and s[cur-1] != " ": cur-=1
			
		elif cmd == "delete char":
			s = s[:cur] + s[cur+1:]
			
		elif cmd == "delete word":
			temp=cur
			if temp>=len(s):
				continue
			while temp<len(s) and s[temp]==" ":
				temp+=1
			if temp>=len(s):
				continue
			while temp<len(s) and s[temp]!=" ":
				temp+=1
			s = s[:cur] + s[temp:]
		elif cmd[:6] == "insert":
			s = s[:cur] + cmd[8:-1] + s[cur:]
			cur += len(cmd[8:-1])

	print s[:cur] + "^" + s[cur:]

Solution (Python):
for _ in range(input()):
	s = raw_input()
	cur = 0
	for __ in range(input()):
		cmd = raw_input()
		if   cmd == "forward char":
			cur = min(len(s),cur+1)
			
		elif cmd == "forward word":
			while cur < len(s) and s[cur] == " ": cur+=1
			while cur < len(s) and s[cur] != " ": cur+=1
				
		elif cmd == "backward char":
			cur = max(0,cur-1)
			
		elif cmd == "backward word":
			while cur > 0 and s[cur-1] == " ": cur-=1
			while cur > 0 and s[cur-1] != " ": cur-=1
			
		elif cmd == "delete char":
			s = s[:cur] + s[cur+1:]
			
		elif cmd == "delete word":
			if s[cur:].count(" ") == len(s[cur:]): continue
			while cur < len(s) and s[cur] == " ": s = s[:cur] + s[cur+1:]
			while cur < len(s) and s[cur] != " ": s = s[:cur] + s[cur+1:]
			
		elif cmd[:6] == "insert":
			s = s[:cur] + cmd[8:-1] + s[cur:]
			cur += len(cmd[8:-1])

	print s[:cur] + "^" + s[cur:]

Solution (Python):
for _ in [0]*int(input()):
    s=input()
    char=0
    for _ in [0]*int(input()):
        c=input()
        if c[0]=='i':
            c=c.split('"')[1]
            s=s[:char]+c+s[char:]
            char += len(c)
            
        elif c[8]=='w':
            while char <len(s) and s[char]==' ':
                char += 1
            while char < len(s) and s[char]!=' ':
                char += 1
                
        elif c[7]=='c':
            s=s[:char]+s[char+1:]
            
        elif c[9]=='w':
            while char > 0 and s[char-1]==' ':
                char-=1
            while char > 0 and s[char-1]!=' ':
                char-=1
                
        elif c[8]=='c':
            char=min(len(s),char+1)
            
        elif c[7]=='w':
            if s[char:].count(' ') == len(s[char:]): 
                continue
            while char<len(s) and s[char]==' ':
                s=s[:char]+s[char+1:]
                
            while char<len(s) and s[char]!=' ':
                s=s[:char]+s[char+1:]
                
        else:
            char=max(0,char-1)
            
    print(s[:char]+'^'+s[char:])