algorithm/problem solving

acmicpc.net 10828(스택), 10845(큐), 1874(스택), 9933

qkqhxla1 2016. 8. 24. 18:13

https://www.acmicpc.net/problem/10828

stack = []
for i in xrange(input()):
    com = raw_input()
    if com[1]=='u':
        stack.append(int(com.split()[1]))
    elif com[0]=='p' and com[1]=='o':
        print -1 if len(stack)==0 else stack.pop()
    elif com[1]=='i':
        print len(stack)
    elif com[1]=='m':
        print 1 if len(stack)==0 else 0
    elif com[1]=='o':
        print -1 if len(stack)==0 else stack[-1]

https://www.acmicpc.net/problem/10845

q = []

for i in xrange(input()):
    c = raw_input()
    if c[1]=='u':
        q.append(int(c.split()[1]))
    elif c[1]=='o':
        print -1 if len(q)==0 else q.pop(0)
    elif c[1]=='i':
        print len(q)
    elif c[0]=='e':
        print 1 if len(q)==0 else 0
    elif c[0]=='f':
        print -1 if len(q)==0 else q[0]
    elif c[0]=='b':
        print -1 if len(q)==0 else q[-1]

https://www.acmicpc.net/problem/1874

# -*- encoding: cp949 -*-
stack = []
n = int(raw_input())
_print = ''
index = 1
flag = False
for i in xrange(n):
    p = int(raw_input())
    if p > n or (len(stack)>0 and stack[-1] > p):
        flag = True
        break
    while True:
        if index > p:
            break
        stack.append(index)
        _print += '+\n'
        index += 1

    stack.pop()
    _print += '-\n'
    
if flag:
    print 'NO'
else:
    print _print[:-1]

https://www.acmicpc.net/problem/9933

# -*- encoding: cp949 -*-
answer = ''
name = [raw_input() for i in xrange(int(raw_input()))]
flag = False
for i in xrange(len(name)):
    for j in xrange(len(name)):
        if name[i][::-1]==name[j]:
            answer += str(len(name[i]))+' '+name[i][len(name[i])/2]
            print answer
            flag = True
            break
    if flag:
        break