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: Time Limit Exceeded

Solution code:
#!/usr/bin/env python3
import collections
import itertools as it
import math
#import numpy as np
 
s  = input()
n  = int(input())
#  = map(int, input().split())
#  = list(map(int, input().split()))
#  = [int(input()) for i in range(N)]
#
# c = collections.Counter()

for _ in range(n):
    ipt = input()

    if ipt[0] == '1':
        s = s[::-1]
    else:
        _, id_, s_ = ipt.split()
        if id_ == '1':
            s = s_ + s
        else:
            s = s + s_

print(s)

Submission status: Time Limit Exceeded

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


def main():
    S = input().strip()
    Q = int(input())
    # Query = [list(input().split()) for _ in range(Q)]
    reverse = 1
    for _ in range(0, Q):
        T = list(input().split())
        if T[0] == "1":
            if reverse == 1:
                reverse = 2
            else:
                reverse = 1
        elif T[0] == "2":
            if int(T[1]) == reverse:
                S = T[2] + S
            else:
                S = S + T[2]
    print(S) if reverse == 1 else print(S[::-1])


if __name__ == '__main__':
    main()

Submission status: Time Limit Exceeded

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


def main():
    S = input().strip()
    Q = int(input())
    Query = [list(input().split()) for _ in range(Q)]
    reverse = 1
    for T in Query:
        if T[0] == "1":
            if reverse == 1:
                reverse = 2
            else:
                reverse = 1
        elif T[0] == "2":
            if int(T[1]) == reverse:
                S = T[2] + S
            else:
                S = S + T[2]
    print(S) if reverse == 1 else print(S[::-1])


if __name__ == '__main__':
    main()

Submission status: Time Limit Exceeded

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

d = 1
for i in range(q):
    t = input()
    if t == '1':
        d *= -1
#        print(d)
    else:
        _, f, c = t.split()
        f = int(f)
        if d == 1 and f == 1:
            s = c + s
        elif d == 1 and f == 2:
            s = s + c
        elif d == -1 and f == 1:
            s = s + c[::-1]
        elif d == -1 and f == 2:
            s = c[::-1] + s
#        print(d, f, c, s)

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

Submission status: Time Limit Exceeded

Solution code:
S = input()
Q = int(input())
for i in range(Q) :
    Query = input()
    q = Query.split(" ")
    if int(q[0]) == 1 :
        S = S[::-1]
    else:
        if int(q[1]) == 1 :
            S = str(q[2])+ S
        else:
            S = S + str(q[2])
print(S)

Submission status: Time Limit Exceeded

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

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

Submission status: Time Limit Exceeded

Solution code:
S = input()
Q = int(input())
Ss = S
for i in range(Q) :
    Query = input()
    q = Query.split(" ")
    if int(q[0]) == 1 :
        Ss = Ss[::-1]
    else:
        if int(q[1]) == 1 :
            Ss = str(q[2]) + Ss
        else:
            Ss = Ss + str(q[2])
print(Ss)

Submission status: Time Limit Exceeded

Solution code:
S = input()
Q = int(input())
Ss = S
q = []
for _ in range(Q):
    q.append(list(map(str,input().rsplit())))
for i in range(Q) :
    if int(q[i][0]) == 1 :
        Ss = Ss[::-1]
    else:
        if int(q[i][1]) == 1 :
            Ss = str(q[i][2]) + Ss
        else:
            Ss = Ss + str(q[i][2])
print(Ss)

Submission status: Time Limit Exceeded

Solution code:
S = input()
Q = int(input())
Ss = S
q = []
for _ in range(Q):
    q.append(list(map(str,input().rsplit())))

for i in range(Q) :
    if int(q[i][0]) == 1 :
        Ss = Ss[::-1]
    elif int(q[i][0]) == 2 :
        if int(q[i][1]) == 1 :
            Ss = str(q[i][2]) + Ss
        else:
            Ss = Ss + str(q[i][2])
print(Ss)

Submission status: Time Limit Exceeded

Solution code:
s = input()
q = int(input())
for i in range(q):
    q_i = input()
    if q_i == '1':
        s = ''.join(list(s)[::-1])
    else:
        a, f, c = q_i.split()
        if f == '1':
            s = c+s
        else:
            s = s+c
print(s)

Submission status: Time Limit Exceeded

Solution code:
s = input()
n = int(input())
cnt = 0
for i in range(n):
    a = input()
    if a == "1":
        cnt +=1
    else:
        if a[2] == "1" and cnt%2 ==0:
            s = a[4] + s
        elif a[2] == "1" and cnt%2 ==1:
            s = s + a[4]
        elif a[2] == "2" and cnt%2 ==0:
            s = s + a[4]
        elif a[2] == "2" and cnt%2 ==1:
            s = a[4] + s

if cnt%2 == 1:
    print(s[::-1])
else:
    print(s)

Submission status: Time Limit Exceeded

Solution code:
import math
import queue
from collections import defaultdict
 
def readInt():
	return int(input())
def readInts():
	return list(map(int, input().split()))
def readChar():
	return input()
def readChars():
	return input().split()
def factorization(n):
	res = []
	if n%2==0:
		res.append(2)
	for i in range(3,math.floor(n//2)+1,2):
		if n%i==0:
			c = 0
			for j in res:
				if i%j==0:
					c=1
			if c==0:
				res.append(i)
	return res
def fact2(n):
	p = factorization(n)
	res = []
	for i in p:
		c=0
		z=n
		while 1:
			if z%i==0:
				c+=1
				z/=i
			else:
				break
		res.append([i,c])
	return res
def fact(n):#階乗
	ans = 1
	m=n
	for _i in range(n-1):
		ans*=m
		m-=1
	return ans
def comb(n,r):#コンビネーション
	if n<r:
		return 0
	l = min(r,n-r)
	m=n
	u=1
	for _i in range(l):
		u*=m
		m-=1
	return u//fact(l)
def printQueue(q):
	r=q
	ans=[0]*r.qsize()
	for i in range(r.qsize()-1,-1,-1):
		ans[i] = r.get()
	print(ans)

s = readChar()
q = readInt()
qa = []
booh = 0
for i in range(q):
	x = input()
	if x=="1":
		qa.append([1,0,0])
	else:
		x = x.split()
		qa.append([int(x[0]),int(x[1]),x[2]])

def hanten():
	global booh
	booh = (booh+1)%2

def t1(s):
	return s[::-1]

def t2(f,c,s):
	if f==1:
		return c+s
	else:
		return s+c

for a in qa:
	if a[0]==1:
		hanten()
	else:
		s=t2((a[1]+booh)%2,a[2],s)
if booh==1:
	s = t1(s)

print(s)

Submission status: Time Limit Exceeded

Solution code:
import math
import queue
from collections import defaultdict
 
def readInt():
	return int(input())
def readInts():
	return list(map(int, input().split()))
def readChar():
	return input()
def readChars():
	return input().split()
def factorization(n):
	res = []
	if n%2==0:
		res.append(2)
	for i in range(3,math.floor(n//2)+1,2):
		if n%i==0:
			c = 0
			for j in res:
				if i%j==0:
					c=1
			if c==0:
				res.append(i)
	return res
def fact2(n):
	p = factorization(n)
	res = []
	for i in p:
		c=0
		z=n
		while 1:
			if z%i==0:
				c+=1
				z/=i
			else:
				break
		res.append([i,c])
	return res
def fact(n):#階乗
	ans = 1
	m=n
	for _i in range(n-1):
		ans*=m
		m-=1
	return ans
def comb(n,r):#コンビネーション
	if n<r:
		return 0
	l = min(r,n-r)
	m=n
	u=1
	for _i in range(l):
		u*=m
		m-=1
	return u//fact(l)
def printQueue(q):
	r=q
	ans=[0]*r.qsize()
	for i in range(r.qsize()-1,-1,-1):
		ans[i] = r.get()
	print(ans)

s = readChar()
q = readInt()
qa = []
for i in range(q):
	x = input()
	if x=="1":
		qa.append([1,0,0])
	else:
		x = x.split()
		qa.append([int(x[0]),int(x[1]),x[2]])

def t1(s):
	return s[::-1]

def t2(f,c,s):
	if f==1:
		return c+s
	else:
		return s+c

for a in qa:
	if a[0]==1:
		s=t1(s)
	else:
		s=t2(a[1],a[2],s)

print(s)

Submission status: Time Limit Exceeded

Solution code:
S = input()
Q = int(input())
flag = True
for i in range(Q):
  string = input().split()
  if string[0] == '1':
    flag = not flag
  elif flag ^ (string[1] == '1'):
      S = S + string[2]
  else:
      S = string[2] + S
if not flag:
  S = S[::-1]
print(S)

Submission status: Time Limit Exceeded

Solution code:
S = input()
Q = int(input())
for i in range(Q):
  string = input().split()
  if string[0] == '1':
    S = S[::-1]
  elif string[1] == '1':
      S = string[2] + S
  else:
      S = S + string[2]
print(S)

Submission status: Time Limit Exceeded

Solution code:
# D - String Formation

S = input()
Q = int(input())
rev = False

for _ in range(Q):
    T = [t for t in input().split()]
    t = int(T[0])
    if t == 1:
        rev = not rev
    if t == 2:
        f, c = int(T[1]), T[2]
        if f == 1:
            if rev:
                S = S + c
            else:
                S = c + S
        if f == 2:
            if rev:
                S = c + S
            else:
                S = S + c

print(S if not rev else S[::-1])

Submission status: Time Limit Exceeded

Solution code:
# D - String Formation

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

for _ in range(Q):
    T = [t for t in input().split()]
    if T[0] == '1':
        S = S[::-1]
    else:
        if T[1] == '1':
            S = T[2] + S
        else:
            S = S + T[2]

print(S)

Submission status: Time Limit Exceeded

Solution code:
# D - String Formation

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

for _ in range(Q):
    T = [t for t in input().split()]
    t = int(T[0])
    if t == 1:
        S = S[::-1]
    if t == 2:
        f, c = int(T[1]), T[2]
        if f == 1:
            S = c + S
        if f == 2:
            S = S + c

print(S)

Submission status: Time Limit Exceeded

Solution code:
# -*- coding: utf-8 -*-
s = input()
q = int(input())
q_list = []
for _ in range(q):
  q_list.append(input())

for query in q_list:
  if query == '1':
    s = s[::-1]
  else:
    _, f, c = query.split()
    if f == '1':
      s = c + s
    else:
      s = s + c
print(s)

Submission status: Time Limit Exceeded

Solution code:
s = input()
q = int(input())
m = 0
for i in range(q):
    Q = input().split()
    if(Q[0] == '1'):m = m ^ 1
    elif(Q[0] == '2'):
        if(Q[1] == '1'):
            if(m == 0):s = Q[2] + s
            else:s = s + Q[2]
        elif(Q[1] == '2'):
            if(m == 0):s = s + Q[2]
            else:s = Q[2] + s
if(m == 1):s = s[::-1]
print(s)

Submission status: Time Limit Exceeded

Solution code:
s = input()
q = int(input())
for i in range(q):
    Q = input().split()
    if(Q[0] == '1'):s = s[::-1]
    elif(Q[0] == '2'):
        if(Q[1] == '1'):s = Q[2] + s
        elif(Q[1] == '2'):s = s + Q[2]
print(s)

Submission status: Time Limit Exceeded

Solution code:
# coding: utf-8
# Your code here!
S = input()
Q = int(input())
Qlist = [list(input().split()) for _ in range(Q)]

#print(S)
i=0
while i<Q:
    if Qlist[i][0]=="1":
        S=S[::-1]
    else:
        check=Qlist[i][2]
        if Qlist[i][1]=="1":
            S=check+S
        else:
            S=S+check
    i+=1

print(S)

Submission status: Time Limit Exceeded

Solution code:
s = input()
q = int(input())
for i in range(q):
    query = list(input().split())
    if int(query[0])==1:
        s = s[::-1]
    elif int(query[0])==2:
        if int(query[1])==1:
            s = query[2] + s
        elif int(query[1])==2:
            s = s + query[2]

print(s)

Submission status: Time Limit Exceeded

Solution code:
s=input()
q=int(input())
cnt=0
for i in range(q):
  k=input()
  if k[0]=='1':
    cnt+=1
  else:
    if k[2]=='1':
      if cnt%2==0:
        s=k[4]+s
      else:
        s+=k[4]
    elif k[2]=='2':
      if cnt%2!=0:
        s=k[4]+s
      else:
        s+=k[4]
if cnt%2==0:
  pass
else:
  s=s[::-1]
print(s)

Submission status: Time Limit Exceeded

Solution code:
s=input()
q=int(input())
for i in range(q):
  k=input()
  if k[0]=='1':
    s=s[::-1]
  else:
    if k[2]=='1':
      s=k[4]+s
    elif k[2]=='2':
      s+=k[4]
print(s)

Submission status: Time Limit Exceeded

Solution code:
# Template 1.0
import sys, re
from collections import deque, defaultdict, Counter, OrderedDict
from math import ceil, sqrt, hypot, factorial, pi, sin, cos, radians
from heapq import heappush, heappop, heapify, nlargest, nsmallest
def STR(): return list(input())
def INT(): return int(input())
def MAP(): return map(int, input().split())
def LIST(): return list(map(int, input().split()))
def list2d(a, b, c): return [[c] * b for i in range(a)]
def sortListWithIndex(listOfTuples, idx):   return (sorted(listOfTuples, key=lambda x: x[idx]))
def sortDictWithVal(passedDic):
    temp = sorted(passedDic.items(), key=lambda kv: (kv[1], kv[0]))
    toret = {}
    for tup in temp:
        toret[tup[0]] = tup[1]
    return toret
def sortDictWithKey(passedDic):
    return dict(OrderedDict(sorted(passedDic.items())))
sys.setrecursionlimit(10 ** 9)
INF = float('inf')
mod = 10 ** 9 + 7

s = input()
q = INT()
rev = 0
for _ in range(q):
     temp = input()
     if(len(temp.split())==1):
         rev+=1

     else:
         t,f,c = temp.split()
         if(f=="1"):
             if(rev%2==0):
                 s = c + s
             else:
                 s = s + c
         else:
             if(rev%2==0):
                 s = s + c
             else:
                 s = c + s
if(rev%2==0):
    print(s)
else:
    print(s[::-1])

Submission status: Time Limit Exceeded

Solution code:
# Template 1.0
import sys, re
from collections import deque, defaultdict, Counter, OrderedDict
from math import ceil, sqrt, hypot, factorial, pi, sin, cos, radians
from heapq import heappush, heappop, heapify, nlargest, nsmallest
def STR(): return list(input())
def INT(): return int(input())
def MAP(): return map(int, input().split())
def LIST(): return list(map(int, input().split()))
def list2d(a, b, c): return [[c] * b for i in range(a)]
def sortListWithIndex(listOfTuples, idx):   return (sorted(listOfTuples, key=lambda x: x[idx]))
def sortDictWithVal(passedDic):
    temp = sorted(passedDic.items(), key=lambda kv: (kv[1], kv[0]))
    toret = {}
    for tup in temp:
        toret[tup[0]] = tup[1]
    return toret
def sortDictWithKey(passedDic):
    return dict(OrderedDict(sorted(passedDic.items())))
sys.setrecursionlimit(10 ** 9)
INF = float('inf')
mod = 10 ** 9 + 7

s = input()
q = INT()
for _ in range(q):
     temp = input()
     if(len(temp.split())==1):
         # print("here")
         s = s[::-1]
     else:
         t,f,c = temp.split()
         # print(t,f,c,_)
         if(f=="1"):
             s = c + s
         else:
             s = s + str(c)
print(s)

Submission status: Time Limit Exceeded

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

def exec_word(S,query):
  if query[0] == '1':
    return(S[::-1])
  elif query[0] == '2':
    if query[1] == '1':
      return (query[2]+S)
    else:
      return(S+query[2])

for i in range(Q):
  query = input().split()
  S = exec_word(S,query)
  
print(S)

Submission status: Time Limit Exceeded

Solution code:
from collections import deque

s=input()
s=deque(s)
q=int(input())
ans=''
flg=0
for _ in range(q):
	query=list(map(str,input().split()))
	if query[0]=='1':
		flg=1-flg
	else:
		if flg:
			s.reverse()
			#print(s)
			flg=0
		if query[1]=='1':
			s.appendleft(query[2])
		else:
			s.append(query[2])
if flg:
	t=[]
	for i in range(len(s)):
		t.append(s.pop())
	print(''.join(t))
else:
    print(''.join(s))

Submission status: Time Limit Exceeded

Solution code:
from sys import stdin
s = str(stdin.readline().rstrip())
#A, B, C = map(int, stdin.readline().rstrip().split())
#S = [list(map(int, stdin.readline().rstrip().split())) for _ in range(h)]#hの定義を忘れずに
q= int(stdin.readline().rstrip())
hanten = 0
for i in range(q):
    l = list(map(str, stdin.readline().rstrip().split()))
    if int(l[0]) == 1:
        hanten += 1
    else:
        if int(l[1])==1:
            if hanten%2 == 0:
                s = l[2]+s
            else:
                s = s+l[2]
        else:
            if hanten%2==0:
                s = s+l[2]
            else:
                s = l[2]+s

if hanten%2==0:
    print(s)
else:
    print(s[::-1])

Submission status: Time Limit Exceeded

Solution code:
def main():
    S = input()
    Q = int(input())
    reverse = False
    for _ in range(Q):
        query = input()
        if len(query) == 1:
            reverse = not reverse
        else:
            _, F, C = query.split()
            if (not reverse and F == '1') or (reverse and F == '2'):
                S = C + S
            else:
                S = S + C
    if reverse:
        S = S[::-1]
    print(S)


if __name__ == '__main__':
    main()

Submission status: Time Limit Exceeded

Solution code:
import collections
s = collections.deque(input())
q = int(input())

rev = 1

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

if rev != 1:
    s.reverse()

ans = ""

for i in range(len(s)):
    ans += s.popleft()

print(ans)

Submission status: Time Limit Exceeded

Solution code:
import collections
s = collections.deque(input())
q = int(input())

for i in range(q):
    t = tuple(input().split())
    if t[0] == "1":
        s.reverse()
    elif t[1] == "1":
        s.appendleft(t[2])
    else:
        s.append(t[2])

ans = ""

for i in range(len(s)):
    ans += s.popleft()

print(ans)

Submission status: Time Limit Exceeded

Solution code:
import collections
s = collections.deque(input())
q = int(input())

for i in range(q):
    t = tuple(input().split())
    if t[0] == "1":
        s.reverse()
    elif t[1] == "1":
        s.appendleft(t[2])
    else:
        s.append(t[2])

ans = ""

for i in s:
    ans += i

print(ans)

Submission status: Time Limit Exceeded

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

count = 0
top = bottom = ""

for i in range(q):
  query = list(input().split())
  if query[0] == "1":
    count = (count + 1) % 2  
  if query[0] == "2":    
    if query[1] == "1":      
      if count == 0:        
        top += query[2]       
      else:
        bottom += query[2]
    if query[1] == "2":      
      if count == 0:        
        bottom += query[2]        
      else:       
        top += query[2]
  
s = top[::-1] + s + bottom

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

Submission status: Time Limit Exceeded

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

for i in range(q):
    t = list(input().split())
    if t[0] == "1":
        s = s[::-1]
    elif t[1] == "1":
        s = t[2] + s
    else:
        s += t[2]
print(s)

Submission status: Time Limit Exceeded

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

count = 0
for i in range(q):
  query = list(input().split())
  
  if query[0] == "1":
    count = (count + 1) % 2
    
  if query[0] == "2":
    
    if query[1] == "1":
      
      if count == 0:
        
        s = query[2] + s
        
      else:
        s = s + query[2]
    if query[1] == "2":
      
      if count == 0:
        
        s = s + query[2]
        
      else:
        
        s = query[2] + s

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

Submission status: Time Limit Exceeded

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

for i in range(q):
  query = list(input().split())
  if query[0] == "1":
    s = s[::-1]
  if query[0] == "2":
    if query[1] == "1":
      s = query[2] + s
    if query[1] == "2":
      s = s + query[2]
      
print(s)

Submission status: Time Limit Exceeded

Solution code:
S = input()
Q = int(input())
query = [0]*Q

for i in range(Q):
    inp = input()
    if inp == "1":
        query[i] = "1"
    else:
        query[i] = list(inp.split())

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

print(S)

Submission status: Time Limit Exceeded

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

u = []
for i in range(q):
    u.append(list(map(str,input().split())))

l = ''
r = ''

x = 1
for i in range(q):
    if int(u[i][0]) == 1:
        x = x*(-1)
    else:
        if int(u[i][1])*x == 1 or int(u[i][1])*x == -2:
            l += u[i][2]
        else:
            r += u[i][2]
if x == 1:
    print(l[::-1]+s[::x]+r)
else:
    print(r[::-1]+s[::x]+l)

Submission status: Time Limit Exceeded

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

u = []
for i in range(q):
    u.append(list(map(str,input().split())))

for i in range(q):
    if int(u[i][0]) == 1:
        s = s[::-1]
    else:
        if int(u[i][1]) == 1:
            s = u[i][2]+s
        else:
            s = s + u[i][2]

print(s)

Submission status: Time Limit Exceeded

Solution code:
S = input()
Q = int(input())
query = []
for _ in range(Q):
    query.append(input().split(" "))

def calc(q, S):
    if q[0] == "1":
        S = S[::-1]
    else:
        if q[1] == "1":
            S = q[2] + S
        else:
            S = S + q[2]
    return S

for q in query:
    S = calc(q, S)
print(S)

Submission status: Time Limit Exceeded

Solution code:
S = input()
Q = int( input() )
inv = False
for ii in range( Q ):
    query = input()
    if( query[0] == '1' ):
        if( inv ):
            inv = False
        else:
            inv = True
    else:
        if( query[2] == '1' ):
            if( inv ):
                S = S+query[4]
            else:
                S = query[4]+S
        else:
            if( inv ):
                S = query[4]+S
            else:
                S = S+query[4]
if(inv):
    S = S[::-1]                

print(S)

Submission status: Time Limit Exceeded

Solution code:
S = input()
Q = int( input() )
for ii in range( Q ):
    query = input()
    if( query[0] == '1' ):
        S = S[::-1]
    else:
        if( query[2] == '1' ):
            S = query[4]+S
        else:
            S = S+query[4]
print(S)

Submission status: Time Limit Exceeded

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

inn = []
jun = 0

for i in range(q):
    inn = list(input().split())
    if inn[0] == "1":
        if jun == 0:
            jun = 1
        else:
            jun = 0#jun = 0はそのまま　jun = 1は逆をする
    elif inn[0] == "2":
        if inn[1] == "1" and jun == 0:
            s.insert(0,inn[2])
        elif inn[1] == "2" and jun == 0:
            s.append(inn[2])
        if inn[1] == "1" and jun == 1:
            s.append(inn[2])
        elif inn[1] == "2" and jun == 1:
            s.insert(0,inn[2])
        
if jun == 0:
    news = ''.join(s)
else:
    s = s[::-1]
    news = ''.join(s)

print(news)

Submission status: Time Limit Exceeded

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

inn = []

for i in range(q):
    inn = list(input().split())
    if inn[0] == "1":
        s = s[::-1]
    elif inn[0] == "2":
        if inn[1] == "1":
            s.insert(0,inn[2])
        elif inn[1] == "2":
            s.append(inn[2])

news = ''.join(s)
    
print(news)

Submission status: Time Limit Exceeded

Solution code:
S = input()
Q = int(input())
for i in range(Q):
    Qx = input().split()
    if int(Qx[0]) == 1:
        S = S[::-1]
    else:
        if int(Qx[1]) == 1:
            S = Qx[2] + S
        else:
            S = S + Qx[2]
print(S)

Submission status: Time Limit Exceeded

Solution code:
S = input()
Q = int(input())
Switch = False
Query = []
for i in range(Q):
    Query.append(input())
    
head = ''
tail = ''
for s in Query:
    if s[0] == '1':
        if Switch:
            Switch = False
        else:
            Switch = True
    if s[0] == '2':
        if s[2] == '1' and Switch==False:
            head = s[4]+head
        elif s[2]=='1' and Switch==True:
            tail = tail+s[4]
        elif s[2] == '2' and Switch==False:
            tail = tail+s[4]
        else:
            head = s[4]+head
S = head+S+tail
if Switch == False:
    print(S)
else:
    print(S[::-1])

Submission status: Time Limit Exceeded

Solution code:
S = input()
Q = int(input())
Switch = False
Query = []
for i in range(Q):
    Query.append(input())
for s in Query:
    if s[0] == '1':
        if Switch:
            Switch = False
        else:
            Switch = True
    if s[0] == '2':
        if s[2] == '1' and Switch==False:
            S = s[4]+S
        elif s[2]=='1' and Switch==True:
            S = S+s[4]
        elif s[2] == '2' and Switch==False:
            S = S+s[4]
        else:
            S = s[4]+S
if Switch == False:
    print(S)
else:
    print(S[::-1])

Submission status: Time Limit Exceeded

Solution code:
S = input()
Q = int(input())
Switch = False
for i in range(Q):
    s = input()
    if s[0] == '1':
        if Switch:
            Switch = False
        else:
            Switch = True
    if s[0] == '2':
        if s[2] == '1' and Switch==False:
            S = s[4]+S
        elif s[2]=='1' and Switch==True:
            S = S+s[4]
        elif s[2] == '2' and Switch==False:
            S = S+s[4]
        else:
            S = s[4]+S
if Switch == False:
    print(S)
else:
    print(S[::-1])

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

Solution code:
S = input()
Q = int(input())
List = []
Str = []
isReverse = 0
for i in range(Q):
  List.append((input().split()))
for i in S:
  Str.append(i)
  
for i in range(Q):
  if List[i][0] == "1":
    isReverse = isReverse + 1
  if List[i][0] == "2":
    if List[i][1] == "1":
      if isReverse %2 == 0:
        Str.insert(0, List[i][2])
      else:
        Str.append(List[i][2])
    if List[i][1] == "2":
      if isReverse %2 == 0:
        Str.append(List[i][2])
      else:
        Str.insert(0, List[i][2])
if isReverse%2 == 1:
  Str.reverse()
print("".join(Str))

Submission status: Time Limit Exceeded

Solution code:
S = input()
Q = int(input())
List = []
Str = []
for i in range(Q):
  List.append((input().split()))
for i in S:
  Str.append(i)
  
for i in range(Q):
  if List[i][0] == "1":
    Str.reverse()
  if List[i][0] == "2":
    if List[i][1] == "1":
      Str.insert(0, List[i][2])
    if List[i][1] == "2":
      Str.append(List[i][2])

print("".join(Str))

Submission status: Time Limit Exceeded

Solution code:
s = list(input())
q = int(input())
q_lists = [input().split() for _ in range(q)]
for i in q_lists:
    if len(i)==1:
        s = s[::-1]
    else:
        f = i[1]
        c = i[2]
        if f=='1':
            s = s[::-1]
            s.append(c)
            s = s[::-1]
        else:
            s.append(c)
        
print(''.join(s))

Submission status: Time Limit Exceeded

Solution code:
s = list(input())
q = int(input())
q_lists = [input().split() for _ in range(q)]
hanten = 0
for i in q_lists:
    if len(i)==1:
        hanten += 1
    else:
        f = i[1]
        c = i[2]
        if hanten%2==0:
            if f=='1':
                s =[c] + s
            else:
                s.append(c)
        else:
            if f=="1":
                s.append(c)
            else:
                s = [c] + s
print(''.join(s) if hanten%2==0 else ''.join(s[::-1]))

Submission status: Time Limit Exceeded

Solution code:
s = input()
q = int(input())
q_lists = [input().split() for _ in range(q)]
hanten = 0
for i in q_lists:
    if len(i)==1:
        hanten += 1
    else:
        f = i[1]
        c = i[2]
        if hanten%2==0:
            if f=='1':
                s = c + s
            else:
                s = s + c
        else:
            if f=="1":
                s = s + c
            else:
                s = c + s
print(s if hanten%2==0 else s[::-1])

Submission status: Time Limit Exceeded

Solution code:
s = input()
q = int(input())
q_lists = [input().split() for _ in range(q)]
for i in q_lists:
    if len(i)==1:
        s = s[::-1]
    else:
        f = i[1]
        c = i[2]
        if f=='1':
            s = c + s
        else:
            s = s + c
print(s)

Submission status: Time Limit Exceeded

Solution code:
word = input().rstrip()
n = int(input())
hanten = 0
lhanten = ""
r = ""
for i in range(n):
    a = input().rstrip().split(" ")
    if a[0] == "1":
        hanten = (hanten + 1) % 2
        # word = word[::-1]
        # print(word)
    else:
        if (a[1] == "2" and hanten == 0) or (a[1] == "1" and hanten == 1):
            r += a[2]
            # print("rtuika")
        else:
            lhanten += a[2]
            # print("ltuika")

if hanten == 0:
    print(lhanten[::-1] + word + r)
else:
    print(r[::-1] + word[::-1] + lhanten)

Submission status: Time Limit Exceeded

Solution code:
word = input().rstrip()
n = int(input())
for i in range(n):
    a = input().rstrip().split(" ")
    if a[0] == "1":
        if len(word) == 1:
            pass
        else:
            word = word[::-1]
            # print(word)
    else:
        if a[1] == "2":
            word = word + a[2]
            # print("rtuika")
        else:
            word = a[2] + word
            # print("ltuika")
print(word)

Submission status: Time Limit Exceeded

Solution code:
s=input()
n=int(input())
cnt=0
for i in range(n):
  a=list(input())
  if a[0]=="1":
    cnt+=1
  else:
    if cnt%2==0 and a[2]=="1":
      s=a[4]+s
 
    elif cnt%2==0 and a[2]=="2":
      s=s+a[4]
     
    elif cnt%2==1 and a[2]=="1":
      s=s+a[4][::-1]

    elif cnt%2==1 and a[2]=="2":
      s=a[4][::-1]+s
    
if cnt%2==0:
  print(s)
else:
  print(s[::-1])

Submission status: Time Limit Exceeded

Solution code:
S=input()
Q=int(input())
query=[]
reverseflg=False

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

for q in range(Q):
  if query[q][0]=="1":
    reverseflg= not reverseflg
  
  if query[q][0]=="2":
    if query[q][1]=="1":
      if reverseflg== False:
        S=query[q][2]+S
      else:
        S=S+query[q][2]
    
    else:
      if reverseflg== False:
        S=S+query[q][2]
      else:
        S=query[q][2]+S
        
if reverseflg==False:
  print(S)
else:
  print(S[::-1])

Submission status: Time Limit Exceeded

Solution code:
S=input()
Q=int(input())
query=[]

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

for q in range(Q):
  if query[q][0]=="1":
    S = S[::-1]
  
  if query[q][0]=="2":
    if query[q][1]=="1":
      S=query[q][2]+S
    else:
      S=S+query[q][2]

print(S)