Playing with String (Codeforces 305E)
Tags: Others, games

Two people play the following string game. Initially the players have got some string s. The players move in turns, the player who cannot make a move loses. Before the game began, the string is written on a piece of paper, one letter per cell.  An example of the initial situation at s = "abacaba" A player's move is the sequence of actions:  The player chooses one of the available pieces of paper with some string written on it. Let's denote it is t. Note that initially, only one piece of paper is available.  The player chooses in the string t = t1t2... t|t| character in position i (1 ≤ i ≤ |t|) such that for some positive integer l (0 < i - l; i + l ≤ |t|) the following equations hold: ti - 1 = ti + 1, ti - 2 = ti + 2, ..., ti - l = ti + l.  Player cuts the cell with the chosen character. As a result of the operation, he gets three new pieces of paper, the first one will contain string t1t2... ti - 1, the second one will contain a string consisting of a single character ti, the third one contains string ti + 1ti + 2... t|t|.   An example of making action (i = 4) with string s = «abacaba» Your task is to determine the winner provided that both players play optimally well. If the first player wins, find the position of character that is optimal to cut in his first move. If there are multiple positions, print the minimal possible one.

Input: The first line contains string s (1 ≤ |s| ≤ 5000). It is guaranteed that string s only contains lowercase English letters.

Output: If the second player wins, print in the single line "Second" (without the quotes). Otherwise, print in the first line "First" (without the quotes), and in the second line print the minimal possible winning move — integer i (1 ≤ i ≤ |s|).

Note: In the first sample the first player has multiple winning moves. But the minimum one is to cut the character in position 2. In the second sample the first player has no available moves.

Sample Inputs:
abacaba

abcde

Sample Outputs:
First
2

Second

Solution (Python):
from collections import *
from itertools import *
from functools import *
from bisect import *
from heapq import *
import sys
IN = lambda : sys.stdin.readline().rstrip('\r\n')
PN = lambda x: sys.stdout.write(x)
I = lambda : int(IN())
S = lambda : IN().split()
M = lambda : map(int, IN().split())
L = lambda : list(map(int, IN().split()))
G = lambda : map(lambda x: int(x) - 1, IN().split())

def list(shape, value=0):
    if len(shape) == 0:
        return value
    else:
        return [list(shape[1:], value=value) for _ in range(shape[0])]
s = IN()
n = len(s)
f = [0 for _ in range(n + 1)]
segs = []
st = [0 for _ in range(n + 1)]
s = ' ' + s
for i in range(1, n + 1):
    S = set()
    for x in range(2, i):
        (L, R) = (x - 1, x + 1)
        S.add(f[L] ^ f[i - R + 1])
    while f[i] in S:
        f[i] += 1
for i in range(2, n):
    if s[i - 1] == s[i + 1]:
        st[i] = 1
i = 2
xorsum = 0
while i <= n:
    if st[i] == 0:
        i += 1
    else:
        j = i
        while st[j] == 1:
            j += 1
        segs.append((i, j - 1))
        xorsum ^= f[j - i + 2]
        i = j
if xorsum == 0:
    print('Second')
else:
    print('First')
    for p in segs:
        (l, r) = (p[0], p[1])
        x = r - l + 3
        for i in range(2, x):
            (L, R) = (i - 1, i + 1)
            if f[L] ^ f[x - R + 1] ^ f[x] == xorsum:
                print(l + i - 2)
                exit(0)

Solution (Python):
U = input()
memo = {}
DP = [0] * (len(U) + 1)
DP[0] = 0
for j in range(1, len(U) + 1):
    s = set()
    for i in range(j):
        s.add(DP[max(0, i - 1)] ^ DP[max(0, j - i - 2)])
    mex = 0
    while mex in s:
        mex += 1
    DP[j] = mex
memo2 = {}

def Grundy(S):
    """"""
    if S in memo2:
        return memo2[S]
    total = 0
    consec = 0
    for i in range(1, len(S) - 1):
        if S[i - 1] == S[i + 1]:
            consec += 1
        else:
            total ^= DP[consec]
            consec = 0
    total ^= DP[consec]
    memo2[S] = total
    return total
import sys
sys.setrecursionlimit(3000)
if Grundy(U) != 0:
    for i in range(1, len(U) - 1):
        if U[i - 1] == U[i + 1]:
            if Grundy(U[:i]) ^ Grundy(U[i + 1:]) == 0:
                print('First')
                print(i + 1)
                break
else:
    print('Second')

Solution (Python):
from collections import *
from itertools import *
from functools import *
from bisect import *
from heapq import *
import sys
IN = lambda : sys.stdin.readline().rstrip('\r\n')
PN = lambda x: sys.stdout.write(x)
I = lambda : int(IN())
S = lambda : IN().split()
M = lambda : map(int, IN().split())
L = lambda : list(map(int, IN().split()))
G = lambda : map(lambda x: int(x) - 1, IN().split())

def list(shape, value=0):
    if len(shape) == 0:
        return value
    else:
        return [list(shape[1:], value=value) for _ in range(shape[0])]
s = IN()
n = len(s)
f = [0 for _ in range(n + 1)]
segs = []
st = [0 for _ in range(n + 1)]
s = ' ' + s
for i in range(1, n + 1):
    S = set()
    for x in range(2, i):
        (L, R) = (x - 1, x + 1)
        S.add(f[L] ^ f[i - R + 1])
    while f[i] in S:
        f[i] += 1
for i in range(2, n):
    if s[i - 1] == s[i + 1]:
        st[i] = 1
i = 2
xorsum = 0
while i <= n:
    if st[i] == 0:
        i += 1
    else:
        j = i
        while st[j] == 1:
            j += 1
        segs.append((i, j - 1))
        xorsum ^= f[j - i + 2]
        i = j
if xorsum == 0:
    print('Second')
else:
    print('First')
    for p in segs:
        (l, r) = (p[0], p[1])
        x = r - l + 3
        for i in range(2, x):
            (L, R) = (i - 1, i + 1)
            if f[L] ^ f[x - R + 1] ^ f[x] == xorsum:
                print(l + i - 2)
                exit(0)