Problem:


Score : 400 points

Problem Statement

Takahashi has a string S consisting of lowercase English letters.

Starting with this string, he will produce a new one in the procedure given as follows.

The procedure consists of Q operations. In Operation i (1 \leq i \leq Q), an integer T_i is provided, which means the following:

If T_i = 1: reverse the string S.

If T_i = 2: An integer F_i and a lowercase English letter C_i are additionally provided.

If F_i = 1 : Add C_i to the beginning of the string S.

If F_i = 2 : Add C_i to the end of the string S.

Help Takahashi by finding the final string that results from the procedure.

Constraints

1 \leq |S| \leq 10^5

S consists of lowercase English letters.

1 \leq Q \leq 2 \times 10^5

T_i = 1 or 2.

F_i = 1 or 2, if provided.

C_i is a lowercase English letter, if provided.

Input

Input is given from Standard Input in the following format:

S
Q
Query_1
:
Query_Q

In the 3-rd through the (Q+2)-th lines, Query_i is one of the following:

1

which means T_i = 1, and:

2 F_i C_i

which means T_i = 2.

Output

Print the resulting string.

Sample Input 1

a
4
2 1 p
1
2 2 c
1

Sample Output 1

cpa

There will be Q = 4 operations. Initially, S is a.

Operation 1: Add p at the beginning of S. S becomes pa.

Operation 2: Reverse S. S becomes ap.

Operation 3: Add c at the end of S. S becomes apc.

Operation 4: Reverse S. S becomes cpa.

Thus, the resulting string is cpa.

Sample Input 2

a
6
2 2 a
2 1 b
1
2 2 c
1
1

Sample Output 2

aabc

There will be Q = 6 operations. Initially, S is a.

Operation 1: S becomes aa.

Operation 2: S becomes baa.

Operation 3: S becomes aab.

Operation 4: S becomes aabc.

Operation 5: S becomes cbaa.

Operation 6: S becomes aabc.

Thus, the resulting string is aabc.

Sample Input 3

y
1
2 1 x

Sample Output 3

xy



Sample input:
a
4
2 1 p
1
2 2 c
1


Sample output:
cpa

Submission status: Runtime Error

Solution code:
import sys
input = sys.stdin.readline.rstrip()
def main():
    moji=input()
    cycle=int(input())
    flont_or_back=0
    flont=""
    back=""
    for i in range(cycle):
        line=input()
        if line[0]=="1":
            flont_or_back+=1
        else:
            fi=line[2]
            ci=line[4]
            if flont_or_back%2==0:
                if fi=="1":
                    flont=ci+flont
                else:
                    back+=ci
            else:
                if fi=="1":
                    back+=ci
                else:
                    flont=ci+flont
    moji=flont+moji+back
    if flont_or_back%2==0:
        print(moji)
    else:
        print(moji[::-1])
    

if __name__ == '__main__':
    main()

Submission status: Runtime Error

Solution code:
# coding: utf-8
S = input().split()
Q = int(input())

def pop(left, right, rev, f, c):
    if rev == 1:
        if f == '1':
            left_ = [c] + left
        else:
            right.append(c)
    else:
        if f == '1':
            right.append(c)
        else:
            left_ = [c] + left
    return left_, right


left, right = [], []
rev = 1
for _ in range(Q):
    que = input().split()
    if que[0] == '1':
        rev *= -1
        continue
    if que[0] == '2':
        left, right = pop(left, right, rev, *que[1:])
print(''.join(left+S+right)[::rev])

Submission status: Runtime Error

Solution code:
s=input()
q=int(input())
l = [list(input().split()) for i in range(q)]
c=0
for i in range(q):
  if l[i][0]=="1":
    c+=1
  elif c%2=0 and l[i][1]=="1":
    s=l[i][2]+s
  elif c%2=0:
    s=s+l[i][2]
  elif c%2=1 and l[i][1]=="1":
    s=s+l[i][2]
  else:
    s=l[i][2]+s
if c%2=1:
  s=s[::-1]
print(s)

Submission status: Runtime Error

Solution code:
 import math

s = input()
i = int(input())

isR = False
for _ in range(i):
    q = input().split(" ")

    if q[0] == "1":
        isR = not isR
    else:
        if isR:
            if q[1] == "1":
                s = s + q[2]
            else:
                s = q[2] + s
        else:
            if q[1] == "1":
                s = q[2] + s
            else:
                s = s + q[2]

if isR:
    print(s[::-1])
else:
    print(s)

Submission status: Runtime Error

Solution code:
#include<stdio.h>
#include<stdbool.h>
#include<string.h>
#define MAX 300000
bool reverse = false;
int main(){
    char S[MAX];
    scanf("%s",S);
    int Q;
    int length=strlen(S);
    scanf("%d",&Q);
    for(int i=0;i<Q;i++){
        int q;
        scanf("%d",&q);
        if(q==1){
            if(reverse)
                reverse = false;
            else
                reverse = true;
        }
        else{
            int F;
            char C;
            scanf("%d %c",&F,&C);
            if((F==1 && !reverse) || (F==2 && reverse)){
                for(int j=length-1;j>=0;j--){
                    S[j+1] = S[j];
                }
                length++;
                S[0] = C;
            }
            else{
                S[length] = C;
                length++;
            }   
        }
    }
    if(!reverse){
         printf("f %s",S);
    }
    else{
        for(int i=length-1;i>=0;i--)
            printf("%c",S[i]);
    }
}

Submission status: Runtime Error

Solution code:
#たのん
s=input()
q=int(input())
head=[]
tail=[]
rev=False
for _ in range(q):
  tmp=list(input().split())
  if tmp[0]=='1': #文字列を反転させる操作のとき
    if rev==False: #反転していない状態であれば反転した状態にする
      rev=True
    else:
      rev=False #反転した状態であれば反転していない状態にする
  if tmp[0]=='2': #文字を追加する操作のとき
    if rev==False: 
     #反転していない状態であれば、先頭への追加はそのまま先頭に、末尾への追加もそのまま末尾に追加する
      if tmp[1]=='1':
        head.append(tmp[2])
      if tmp[1]=='2':#単純にelseとしてもOKだが、略さない方が間違えない
        tail.append(tmp[2])#↓Trueの場合、if revでOKだが、略さないがベター
  if rev==True:#反転した状態であれば、先頭への追加は末尾への追加に、末尾への追加は先頭への追加に置き換える
    if tmp[1]=='1':
      tail.append(tmp[2])
    if tmp[1]=='2':
      head.append(tmp[2])
head=''.join(map(str,head))#文字列として結合しておく
tail=''.join(map(str,tail))
ans=head[::-1]+s+tail #答えは(先頭に追加した文字列の逆順)+(元の文字列)+(末尾に追加した文字列)となる
if rev==True: #もし最終的に文字列が反転した状態なら答えの文字列を反転させる
  ans=ans[::-1]
print(ans)

Submission status: Runtime Error

Solution code:
#たのん
s=input()
q=int(input())
head=[]
tail=[]
rev=False
for _ in range(q):
  tmp=list(input().split())
  if tmp[0]=='1': #文字列を反転させる操作のとき
    if rev==False: #反転していない状態であれば反転した状態にする
      rev=True
    else:
      rev=False #反転した状態であれば反転していない状態にする
  if tmp[0]=='2': #文字を追加する操作のとき
    if rev==False: 
     #反転していない状態であれば、先頭への追加はそのまま先頭に、末尾への追加もそのまま末尾に追加する
     if tmp[1]=='1':
        head.append(tmp[2])
      if tmp[1]=='2':#単純にelseとしてもOKだが、略さない方が間違えない
        tail.append(tmp[2])#↓Trueの場合、if revでOKだが、略さないがベター
  if rev==True: 
    #反転した状態であれば、先頭への追加は末尾への追加に、末尾への追加は先頭への追加に置き換える
     if tmp[1]=='1':
        tail.append(tmp[2])
      if tmp[1]=='2':
        head.append(tmp[2])
head=''.join(map(str,head))#文字列として結合しておく
tail=''.join(map(str,tail))
ans=head[::-1]+s+tail #答えは(先頭に追加した文字列の逆順)+(元の文字列)+(末尾に追加した文字列)となる
if rev==True: #もし最終的に文字列が反転した状態なら答えの文字列を反転させる
  ans=ans[::-1]
print(ans)

Submission status: Runtime Error

Solution code:
S = input()
Q = int(input())
reverse = False 
for i in range(0,Q):
    q = int(input())
    if q == 1:
       reverse = not reverse
    elif q == 2:
        F = int(input())
        C = input()
        if (F==1 and reverse) or (F==2 and not reverse):
            S = S + C
        else:
            S = C + S

print(S)

Submission status: Runtime Error

Solution code:
import sys
input=sys.stdin.readline

s=input()
q=int(input())
for i in range(q):
  t=input()
  if t=='1':
    s=s[::-1]
  else:
    t, f, c = map(str, t.split())
    if f =='1':
      s=c+s
    else:
      s=s+c
print(s)

Submission status: Runtime Error

Solution code:
s = input()
q = int(input())

head = []
tail = []
flip = 0

for i in range(q):
	query = input().split()
	if query[0] == '1':
		flip ^= 1
	else:
		f = query[1]
		c = query[2]
		if (f ^ flip) == 0:
			head.append(c)
		else:
			tail.append(c)

if flip:
	head, tail = tail, head
	s = s[::-1]

head = ''.join(head)
tail = ''.join(tail)

print(head[::-1] + s + tail)

Submission status: Runtime Error

Solution code:
def main():
    from collections import deque
    S = input()
    Q = int(input())

    for _ in range(Q):
        query = deque(input().split())
        if query[0] == "1":
            S.reverse()
        elif query[1] == "1":
            S = query[2] + S
        elif query[1] == "2":
            S = S + query[2]

    print("".join(map(str, S)))

if __name__ == "__main__":
    main()

Submission status: Runtime Error

Solution code:
ans = input()
n = int(input())
for i in range(n):
    query = input()
    if query == "1":
        ans = ''.join(list(ans).reverse())
    else:
        _,option,str = query.split()
        if option == "1":
            ans = str + ans
        else:
            ans += str
print(ans)

Submission status: Runtime Error

Solution code:
from collections import deque
import copy
s = input()
q_ = int(input())
query = [input().split() for _ in range(q_)]
l = deque([s])

for q in query:
    if q[0] == '1':
        ll = copy.deepcopy(l)
        l = [''.join(ll)[::-1]]
    elif q[0] == '2':
        if q[1] == '1':
            l.appendleft(q[2])
        else:
            l.append(q[2])

print(''.join(l))

Submission status: Runtime Error

Solution code:
from collections import deque
import copy
s = input()
q_ = int(input())
query = [input().split() for _ in range(q_)]
l = deque([s])

for q in query:
    if q[0] == '1':
        l = copy.deepcopy([''.join(l)[::-1]])
    elif q[0] == '2':
        if q[1] == '1':
            l.appendleft(q[2])
        else:
            l.append(q[2])

print(''.join(l))

Submission status: Runtime Error

Solution code:
from collections import deque
s = input()
q_ = int(input())
query = [input().split() for _ in range(q_)]
l = deque([s])

for q in query:
    if q[0] == '1':
        l = [''.join(l)[::-1]]
    elif q[0] == '2':
        if q[1] == '1':
            l.appendleft(q[2])
        else:
            l.append(q[2])

print(''.join(l))

Submission status: Runtime Error

Solution code:
from collections import deque
s = input()
q_ = int(input())
query = [input().split() for _ in range(q_)]
l = deque([s])

for q in query:
    if q[0] == '1':
        s = ''.join(l)
        s = s[::-1]
        l = [s]
    elif q[0] == '2':
        if q[1] == '1':
            l.appendleft(q[2])
        else:
            l.append(q[2])

print(''.join(l))

Submission status: Runtime Error

Solution code:
  string = None
  def reverse(self):
    self.string=self.string[::-1]
  def insert(self,C):
    self.string = self.string.insert(C)
  def append(self,C):
    self.string = self.string.append(C)

def run():
  S=S_Takahashi()
  S.string =input()
  Q =int(input())
  for i in range(Q):
    input_list = input().split()
    T = input_list[0]
    if T ==1:
      S.reverse()
    else:
      F=input_list[1]
      C=input_list[2]
      if F==1:
        S.insert(C)
      else:
        S.append(C)
  return S.string
print(run())

Submission status: Runtime Error

Solution code:
from collections import deque


reverse = False

string = deque(input().rstrip())
operate_times = int(input().rstrip())

for i in range(operate_times):
    row = input().rstrip()
    if row[0]=='1':
        if reverse==True:
            reverse = False
        else:
            reverse = True
    
    if row[0]=='2':
        row = row.split()
        head_or_tail = row[1]
        add_string = row[2]

        if reverse==True:
            if head_or_tail==1:
                string.append(add_string)
            else:
                string.appendleft(add_string)
        
        else:
            if head_or_tail==2:
                string.append(add_string)
            else:
                string.appendleft(add_string)


if reverse==True:
    string = string[::-1]

print(''.join(string))

Submission status: Runtime Error

Solution code:
from collections import deque

d=deque()
s=input()
d.append(s)
q=int(input())
flag=1
t = [input().split() for l in range(q)]
for i in range(q):
  if t[i][0]=='1':
    flag=flag*(-1)
  else:
    if t[i][1]=='1' and flag==1:
      s=t[i][2]+s
    elif t[i][1]=='1' and flag==-1:
      s=s+t[i][2]
    elif t[i][1]=='2' and flag==1:
      s=s+t[i][2]
    elif t[i][1]=='2' and flag==-1:
      s=t[i][2]+s
if flag==-1:
  new_s = ''.join(list(reversed(s)))
print(new_s)

Submission status: Runtime Error

Solution code:
import sys
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines

from collections import deque

S = deque(readline().rstrip().decode())
Q = int(readline())
query = int(readlines())
rev = False
for q in query:
  if q[0] == ord('1'):
        rev = not rev
    else:
        _, f, c = q.decode().split()
        f = int(f)
        if rev ^ (f == 1):
            S.appendleft(c)
        else:
            S.append(c)
 
T = ''.join(S)
if rev:
    T = T[::-1]
print(T)

Submission status: Runtime Error

Solution code:

from scipy.sparse.csgraph import shortest_path, floyd_warshall, dijkstra, bellman_ford, johnson, minimum_spanning_tree
from scipy.sparse import csr_matrix, coo_matrix, lil_matrix
import numpy as np
from functools import reduce

from collections import deque, Counter, defaultdict
from itertools import product, permutations,combinations
from operator import itemgetter
from heapq import heappop, heappush
from bisect import bisect_left, bisect_right, bisect
from fractions import gcd
from math import ceil,floor, sqrt, cos, sin, pi, factorial
import statistics
import sys
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines
sys.setrecursionlimit(10**8)
INF = float('inf')
MOD = 10**9+7
def lcm(a, b):
  return a*b//gcd(a, b)

def main():
  S = input()
  Q = int(input())
  TFC = list(list(input().split()) for _ in range(Q))
  rev = 0
  q = deque(S)
  for i in range(Q):
    if TFC[i][0] == '1':
      rev ^= 1
    else:
      if TFC[i][1] == '1':
        if rev == 0:
          q.appendleft(TFC[i][2])
        else:
          q.append(FC[i][2])
      else:
        if rev == 0:
          q.append(TFC[i][2])
        else:
          q.appendleft(TFC[i][2])
  ans = ''.join(q)
  print(ans if rev==0 else ans[::-1])
  
if __name__ == '__main__':
  main()

Submission status: Runtime Error

Solution code:

from scipy.sparse.csgraph import shortest_path, floyd_warshall, dijkstra, bellman_ford, johnson, minimum_spanning_tree
from scipy.sparse import csr_matrix, coo_matrix, lil_matrix
import numpy as np
from functools import reduce

from collections import deque, Counter, defaultdict
from itertools import product, permutations,combinations
from operator import itemgetter
from heapq import heappop, heappush
from bisect import bisect_left, bisect_right, bisect
from fractions import gcd
from math import ceil,floor, sqrt, cos, sin, pi, factorial
import statistics
import sys
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines
sys.setrecursionlimit(10**8)
INF = float('inf')
MOD = 10**9+7
def lcm(a, b):
  return a*b//gcd(a, b)

def main():
  S = input()
  Q = int(input())
  TFC = list(list(input().split()) for _ in range(Q))
  rev = 0
  q = deque(S)
  for i in range(Q):
    if TFC[i][0] == '1':
      rev ^= 1
    else:
      if TFC[i][1] == '1':
        if rev == 0:
          q.appendleft(TFC[i][2])
        else:
          q.append(FC[i][2])
      else:
        if rev == 0:
          q.append(TFC[i][2])
        else:
          q.appendleft(TFC[i][2])
  ans = ''
  if rev==0:
    while q:
      ans += q.popleft()
  else:
    while q:
      ans += q.pop()
  print(ans)
  
if __name__ == '__main__':
  main()

Submission status: Runtime Error

Solution code:
from collections import deque
s=input()
Q=int(input())

ans=deque()
ans.append(s)
rev=False

for _ in range(Q):
    query=input().split()
    if query[0]=="1":
        rev = not rev
    else:
        if rev:
            query[1]=str(3-int(query[1]))
        if query[1]=="1":
            ans.appendleft(query[2])
        else:
            ans.append(query[2])

if not rev:
    for si in ans:
        print(si,end="")
else:
    for i in range(len(ans),0,-1):
        print(ans[i],end="")

Submission status: Runtime Error

Solution code:
s=[input()]
Q=int(input())

rev=-1
for i in range(Q):
    que=input().split()
    if que[0]=="1":
        rev*=-1
    else:
        if que[1]=="1":
            if rev>0:
                s.append(que[2])
            else:
                s=[que[2]]+s
        else:
            if rev<0:
                s.append(que[2])
            else:
                s=[que[2]]+s
if rev<0:
    for si in s:
        print(si,end="")
else:
    for i in range(len(s),0,-1):
        print(s[i],end="")

Submission status: Runtime Error

Solution code:
# coding: utf-8

S = input()
N = int(input())
Q = list(input().split() for _ in range(N))

skip = 0
for i in range(N):
    if skip == 0:
        if i == N - 1:
            if Q[i][0] == "1":
                S = S[::-1]
            else:
                if Q[i][1] == "1":
                    S = Q[i][2] + S
                else:
                    S = S + Q[i][2]
        else:
            if Q[i + 1][0] == "1":
                skip = 1
            else:
                if Q[i][1] == "1":
                    S = Q[i][2] + S
                else:
                    S = S + Q[i][2]
    else:
        skip = 0

print(S)

Submission status: Runtime Error

Solution code:
temp = input()
s = ["tikan"]
flag = "off"
q = int(input())
dummy = ""
for i in range(q):
  q_list = input().split()
  if q_list[0] == "1":
    if flag == "on":
      flag = "off"
    else:
      flag = "on"
  else:
    if q_list[1] == "1":
      if flag == "off":
        s.insert(0,q_list[2])
      else:
        s.append(q_list[2])
    else:
      if flag == "off":
        s.append(q_list[2])
      else:
        s.insert(0,q_list[2])
#  print(s)
ans = ""
for i in s:
  if i == "tikan":
    i = temp
    if flag == "on":
      i = list(i)
      i.reverse()
      i = "".join(i)
  ans += i
if flag == "on":
  ans = list(ans)
  ans.reverse()
  ans = "".join(ans)
print(ans)v

Submission status: Runtime Error

Solution code:
S=input()
n=int(input())
for i in range(0,n):
  a=list(map(input().split()))
  if len(a)==1:
    S=S[::-1]
  else:
    if a[1]=="1":
      S=a[2]+S
    else:
      S=S+a[2]
print(S)

Submission status: Runtime Error

Solution code:
from collections import deque
S = deque([i for i in input()])
Q = int(input())
list = [['1', "0", "0"] for i in range(Q) ]
for i in range(Q):
    a = input()
    if a[0] == '2':
        p, q, r = map(str, a.split())
        if q == "1":
            S.appendleft(r)
        else:
            S.append(r)
    else:
        tmp = S[0]
        S[0] = S[-1]
        S[-1] = tmp
ans = ""
N = len(S)
for i in N:
    ans = ans + S.popleft()
print(ans)

Submission status: Runtime Error

Solution code:
s = input()
q = int(input())

rev = False
head = []
tail = []

for i in range(q):
    query = input()
    if query == "1":
        rev = not rev
    else:
        _, f, c = query.split()
        if (not rev and f == "1") or (rev and f == "2"):
            head.append(c)
        else:
            tail.append(c)

if not rev:
    print(*head[::-1], s, *tail, sep="")
else:
    print(*tail[::-1], s, *head, sep="")

Submission status: Runtime Error

Solution code:
s = input()
q = int(input())

rev = False
head = []
tail = []

for _ in range(q):
    query = input()
    if query == "1":
        rev = not rev
    else:
        _, f, c = query.split()
        if (not rev and f == "1") or (rev and f == "2"):
            head.append(c)
        else:
            tail.append(c)

if not rev:
    print(*head[::-1], s, *tail, sep="")
else:
    print(*tail[::-1], s, *head, sep="")

Submission status: Runtime Error

Solution code:
def string():
    s = input()
    q = int(input())
    l = ''
    r = ''
    re = False
    while q > 0:
        t = list(input().strip().split())
        if t[0] == 1:
            re = not re
        else:
            if re:
                if t[1] == 2:
                    l = t[2] + l
                else:
                    r = r + t[2]
            else:
                if t[1] == 2:
                    l = t[2] + l
                else:
                    r = r + t[2]
        q -= 1
    s = l + s + r
    if re:
        s = s[::-1]
    print(s)
string()

Submission status: Runtime Error

Solution code:
import sys
sys.setrecursionlimit(10**6)
readline = sys.stdin.readline
read = sys.stdin.read
s = input()
u = ""
t = ""
q = int(input())
num = 0

for _ in range(q):
    a = readline.split()
    if len(a)==1:
        u,t = t,u
        num ^= 1
    else:
        f = int(a[1])
        c = a[2]
        if f==1:
            t += c
        else:
            u += c

if num:
    print(t[::-1]+s[::-1]+u)
else:
    print(t[::-1]+s+u)

Submission status: Runtime Error

Solution code:
s = input()
u = ""
t = ""
q = int(input())
num = 0

for _ in range(q):
    a = q.split()
    if len(a)==1:
        u,t = t,u
        num ^= 1
    else:
        f = int(a[1])
        c = a[2]
        if f==1:
            t += c
        else:
            u += c

if num:
    print(t[::-1]+s[::-1]+u)
else:
    print(t[::-1]+s+u)

Submission status: Runtime Error

Solution code:
S = input()
Q = int(input())

a = 0
for i in range(Q):
    t = input()
    if t == "1":
        if a == 0:
            a = 1
        else:
            a = 0
    else:
        t,f,c = t.split()
        if f == "1":
            if a == 0:
                S = c + S
            else
                S = S + c
        else:
            if a == 0:
                S = S + c
            else:
                S = c + S
            
if a == 1:
    S = S[::-1]
    
print(S)

Submission status: Runtime Error

Solution code:
#TLE
from collections import deque
s = deque(input())
q = int(input())
num = 0
for _ in range(q):
    t = input()
    if t == '1':
        if num == 0:
            num = 1
        else:
            num = 0
    else:
        t, f, c = t.split()
        c = deque(c)
        if num == 0:
            if f == '1':
                s = c + s
            else:
                s = s + c
        else:
            if f == '2':
                s = c + s
            else:
                s = s + c
s = ''.join(s)
if num == 1:
    s = s[::-1]
print(s)

Submission status: Runtime Error

Solution code:
S=input()
n=0
while n<int(input()):
    i=input()
    if i=='1':
        while i=='1':
            S=S[::-1];i=input();n+=1
    if i[0]=='2':
        x=''
        while i[2]=='1':
            x=i[4]+x;i=input();n+=1
        S=x+S;x=''
        while i[2]=='2':
            x+=i[4];i=input();n+=1
        S+=x
print(S)

Submission status: Runtime Error

Solution code:
S=input()
n=0
for n<int(input()):
    i=input()
    if i=='1':
        while i=='1':
            S=S[::-1];i=input();n+=1
    if i[0]=='2':
        x=''
        while i[2]=='1':
            x=i[4]+x;i=input();n+=1
        S=x+S;x=''
        while i[2]=='2':
            x+=i[4];i=input();n+=1
        S+=x
print(S)

Submission status: Runtime Error

Solution code:
S=input()
for i in range(int(input())):
    i=input()
    if i=='1':
        while i=='1':
            S=S[::-1];i=input()
    if i[0]=='2':
        x=''
        while i[2]=='1':
            x=i[4]+x;i=input()
        S=x+S;x=''
        while i[2]=='2':
            x+=i[4];i=input()
        S+=x
print(S)

Submission status: Runtime Error

Solution code:
S=input()
for i in range(int(input())):
    i=input()
    if i=='1':
        while i=='1':
        S=S[::-1];i=input()
    if i[0]=='2':
        x=''
        while i[2]=='1':
            x=i[4]+x;i=input()
        S=x+S;x=''
        while i[2]=='2':
            x+=i[4];i=input()
        S+=x
print(S)

Submission status: Runtime Error

Solution code:
S=input()
QN=int(input())
front=''
rear=''
rev=False

for i in range(QN):
    op=input()
    if op[0]=='1':
        rev=not(rev)
    else:
        if op[2]=='1':
            if rev:
                rear+=[4]
            else:
                front+=op[4]
        else:
            if rev:
                front+=op[4]
            else:
                rear+=op[4]

if rev:
    print(rear[::-1]+S+front)
else:
    print(front[::-1]+S+rear)

Submission status: Runtime Error

Solution code:
s=input()
q=int(input())
for i in range(q):
  t=int(input())
  if t==2:
    f=int(input())
    c=input()
    if f==1:     
      s=c+s
    else:
      s=s+c
  else:
    s=s[::-1]
print(s)

Submission status: Runtime Error

Solution code:
from collections import deque
S = deque(list(input()))
Q = int(input())
f = 0
for _ in range(Q):
  a, *b = input().split()
  f += int(a)
  if a == "2":
    if (int(b[0]) + f) % 2 == 1 :
      S.appendleft(b[1])
    else:
      S.append(b[1])
if  f % 2 == 0 :
  S = "".join(S)
else:
  S = "".join(S[::-1])
print (S)

Submission status: Runtime Error

Solution code:
from collections import deque
S = deque(list(input()))
Q = int(input())
f = 0
for _ in range(Q):
  a, *b = input().split()
  f += int(a)
  if a == "2":
    if (int(b[0]) + f) % 2 == 1 :
      S.appendleft(b[1])
    else:
      S.append(b[1])
if  f % 2 == 0 :
  S = "".join(S)
else:
  S = "".joins(S[::-1])
print (S)

Submission status: Runtime Error

Solution code:
s = input()
q = int(input())
querrys = []
answer = s
for i in range(0, q):
  querry = input().split()
  if querry[0] == '1':
    answer = reversed(answer)
  	# querrys.append(['1', '0', '0'])
  else:
    if querry[1] == '1':
      answer = querry[2] + answer
    # querrys.append(querry)
    else:
      answer += querry[2]
print(answer)

Submission status: Runtime Error

Solution code:
string = input()
num = int(input())
data = [input().split(' ') for i in range(kai)]

for i in range(num):
    if(data[i][0] == '1'):
        if(len(string) == 2):
            string = string[1] + string[0]
        elif(len(string) >= 3):
            string = string[-1] + string[1:-1] + string[0]
        
    else:
        if(data[i][1] == '1'):
            string = data[i][2] + string
        else:
            string = string + data[i][2]

print(string)

Submission status: Runtime Error

Solution code:
import sys
input = sys.stdin.readline
# lines = open("tmp_in").readlines()

# def _input():
#     for ss in lines:
#         yield ss.rstrip()
# input = _input().__next__

s = input()
q = int(input())
turn = 0
# ともにSに近い方から並べる
before = "" 
after = ""
for _ in range(q):
    tmp = input()
    if tmp=="1":
        turn += 1
    else:
        _,f,c = tmp.split()
        f = int(f)
        if (turn + f) % 2 == 1:
            before += c
        else:
            after += c
if turn%2==0:
    out = before[::-1] + s + after
else:
    out = after[::-1] + s[::-1] + before
print(out)

Submission status: Runtime Error

Solution code:
from collections import deque

s = deque(input())
n=int(input())
l = [input().split(" ") for _ in range(n)]

x = 0
for i in range(n):
    if l[i][0]=='1':
        if x == 0:
            x = 1
        else:
            x = 0
    elif l[i][0]=='2':
        if x == 0:
            if l[i][1]=='1':
                s.appendleft(l[i][2])
            else:
                s.append(l[i][2])
        else:
            if l[i][1]=='1':
                s.append(l[i][2])
            else:
                s.appendleft(l[i][2])

if x == 0:
    print(''.join(s))
else:
    print(''.join(s[::-1]))

Submission status: Runtime Error

Solution code:
from collections import deque

S = deque(input())
Q = int(input())

flag = 1
for i in range(Q):
    qry = input().split()

    if qry[0] == '1':
        if flag:
            flag = 0
        else:
            flag = 1
    
    elif qry[0] == '2':
        if flag:
            if qry[1] == '1':
                S.append(qry[2])
            else:
                S.appendleft(qry[2])
        else:
            if qry[1] == '1':
                S.append(qry[2])
            else:
                S.appendleft(qry[2])

if flag:
    print(ans)
else:
    print(ans[::-1])

Submission status: Runtime Error

Solution code:
S = input()
Q = int(input())

ans = S
flag = 1
for i in range(Q):
    qry = list(input().split())

    if qry[0] == '1' and flag:
        flag = 0
    elif qry[0] == '1' and not flag:
        flag = 1

    elif qry[0] == '2' and flag:
        if qry[1] == '1':
            ans = qry[2] + ans
        elif qry[1] == '2':
            ans += qry[2]
    elif qry[0] == '2' and not flag:
        if qry[1] == '1':
            ans += qry[2]
        elif qry[1] == '2':
            ans = qry[2] + ans

if flag:
    print(ans)
else:
    for i in range(len(ans)//2):
        ans[i], ans[-1-i] = ans[-1-i], ans[i]

    print(ans)

Submission status: Runtime Error

Solution code:
S = input()
Q = int(input())

ans = S
flag = 1
for i in range(Q):
    qry = list(input().split())

    if qry[0] == '1' and flag:
        flag = 0
    elif qry[0] == '1' and not flag:
        flag = 1

    elif qry[0] == '2' and flag:
        if qry[1] == '1':
            ans = qry[2] + ans
        elif qry[1] == '2':
            ans += qry[2]
    elif qry[0] == '2' and not flag:
        if qry[1] == '1':
            ans += qry[2]
        elif qry[1] == '2':
            ans = qry[2] + ans

if flag:
    print(ans)
else:
    for i in range(len(ans//2)):
        ans[i], ans[-1-i] = ans[-1-i], ans[i]

    print(ans)

Submission status: Runtime Error

Solution code:
from collections import deque
S=input()
Q=int(input())

X=deque(list(S))
f=1
for _ in range(Q):
    t,ff,c=input().split()
    if t=="1":
        f*=-1
    else:
        if ff=="1":
            if f==1:
                X.appendleft(c)
            else:
                X.append(c)
        else:
            if f==-1:
                X.appendleft(c)
            else:
                X.append(c)

if f==-1:
    X.reverse()

print("".join(X))

Submission status: Runtime Error

Solution code:
def change_fb(str_list):
    tmp = str_list[0]
    str_list[0] = str_list[-1]
    str_list[-1] = tmp

s = str(input())
q = int(input())
w = [input() for i in range(q)]

change = False
str_list = list(s)
for data in w:
    if int(data[0]) == 1:
        change = not change
    else:
        if change:
            change = False
            change_fb(str_list)
        t,f,c = data.split()
        if int(f) == 1:
            str_list.insert(0, c)
        else:
            str_list.append(c)
    print(str_list)

if change:
    change_fb(str_list)

print("".join(str_list))

Submission status: Runtime Error

Solution code:
from collections import deque

s = deque(input())
q = int(input())

query = []
rev=1

for i in range(q):
    query.append(input().split())

for i in range(q):
    if query[i][0]=="1":
        rev*=-1
    else:
        if rev==1:
            if query[i][1]=="1":
                s.appendleft(query[i][2])
            else:
                s.append(query[i][2])
        else:
            if query[i][1]=="2":
                s.appendleft(query[i][2])
            else:
                s.append(query[i][2])

if rev==-1:
    s = s[::-1]
print("".join(list(s)))

Submission status: Runtime Error

Solution code:
s = input()
q = int(input())

query = []
rev=1

for i in range(q):
    query.append(input().split())

for i in range(q):
    if query[i][0]=="1":
        rev*=-1
    else:
        if rev==1:
            if query[i][1]=="1":
                s.appendleft(query[i][2])
            else:
                s.append(query[i][2])
        else:
            if query[i][1]=="2":
                s.appendleft(query[i][2])
            else:
                s.append(query[i][2])

if rev==-1:
    s = s[::-1]
print(s)