Problem:


Score : 700 points

Problem Statement

Limak can repeatedly remove one of the first two characters of a string,
    for example abcxyx \rightarrow acxyx \rightarrow cxyx \rightarrow cyx.

You are given N different strings S_1, S_2, \ldots, S_N.
Among N \cdot (N-1) / 2 pairs (S_i, S_j),
    in how many pairs could Limak obtain one string from the other?

Constraints

2 \leq N \leq 200\,000

S_i consists of lowercase English letters a-z.

S_i \neq S_j

1 \leq |S_i|

|S_1| + |S_2| + \ldots + |S_N| \leq 10^6

Input

Input is given from Standard Input in the following format.

N
S_1
S_2
\vdots
S_N

Output

Print the number of unordered pairs (S_i, S_j)
    where i \neq j and Limak can obtain one string from the other.

Sample Input 1

3
abcxyx
cyx
abc

Sample Output 1

1

The only good pair is (abcxyx, cyx).

Sample Input 2

6
b
a
abc
c
d
ab

Sample Output 2

5

There are five good pairs: (b, abc), (a, abc), (abc, c), (b, ab), (a, ab).



Sample input:
3
abcxyx
cyx
abc


Sample output:
1

Submission status: Runtime Error

Solution code:
n=int(input())
G=[[0]*20 for i in range(25)]
A=[0,0,0,0]
for i in range(n):
  S=list(input())
  s=""
  f=0
  a=0
  for j in S:
    if j==".":
      f=1
    else:
      s=s+j
      if f==1:
        a=a+1
  A[0]=int(s)
  A[1]=a
  m=int(s)
  c=0
  while m%10==0:
    c=c+1
    m=m//10
  A[0]=m
  A[1]-=c
  m=A[0]
  c=0
  while m%2==0:
    c=c+1
    m=m//2
  A[2]=c
  c=0
  m=A[0]
  while m%5==0:
    c=c+1
    m=m//5
  A[3]=c
  G[A[2]-A[1]+9][A[3]-A[1]+9]+=1
B=[[0]*21 for i in range(26)]
for i in range(24,-1,-1):
  for j in range(19,-1,-1):
    B[i][j]=B[i+1][j]+B[i][j+1]-B[i+1][j+1]+G[i][j]
ans=0
for i in range(25):
  for j in range(20):
    ans=ans+G[i][j]*B[max(0,18-i)][max(0,18-j)]
    if i>=max(0,18-i) and j>=max(0,18-j):
      ans=ans-G[i][j]
print(ans//2)

Submission status: Runtime Error

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

n = int(readline())
s = read().split()
s.sort(key= lambda x: len(x))

words = dict()
# for si in 'abcdefghijklmnopqrstuvmxyz':
#     words[si] = dict()

ans = 0
for si in s:
    if(len(si)==1):
        head = si[0]
        tail = ''
    else:
        head = si[0]
        tail = si[1:]

    heads = set()
    heads.add(head)

    now = ''
    stack = ['']
    for ti in tail[::-1]:
        now += ti
        stack.append(now)

    for ti in stack[::-1]:
        if(ti in words):
            for j in heads:
                if(j in words[ti]):
                    ans += words[ti][j]
        if(ti==''):
            break
        heads.add(ti[-1])

    tail = stack[-1]

    if(not tail in words):
        words[tail] = dict()
    if(not head in words[tail]):
        words[tail][head] = 1
    else:
        words[tail][head] += 1

    # print(si,ans)
    # print(stack)

print(ans)

Submission status: Runtime Error

Solution code:
import numpy as np
n = int(input())
a = np.array([input() for _ in range(n)], dtype=np.float)


ans = 0
temp = a[0]

for i in range(1, n):
    temp = temp * a[i]
    if temp.is_integer():
        ans += 1


print(ans)

Submission status: Runtime Error

Solution code:
def func1(str1, str2):
  if str1 == str2:
    return 1
  elif len(str1) > 0 and len(str2) > 0:
    str2_pop0 = str2
    str2_pop0 = str2_pop0.pop(0)
    func(str1, str2_pop0)
    str2_pop1 = str2
    str2_pop1 = str2_pop1.remove(str2[1])
    func(str1, str2_pop1)
  else:
    return 0
def func2(str1, str2):
  if str1 == str2:
    return 1
  elif len(str1) > 0 and len(str2) > 0:
    str1_pop0 = str1
    str1_pop0 = str1_pop0.pop(0)
    func(str1_pop0, str2)
    str1_pop1 = str1
    str1_pop1 = str1_pop1.remove(str1[1])
    func(str1_pop1, str2)
  else:
    return 0
input_n = input()
N = int(input_n)
a = []
for i in range(N):
  a[i] = input()
counter = 0
for i in range(N):
  for j in range(N-i):
    if i != j+i:
      result = func1(a[i], a[j+i])
      if result:
        counter += 1
      else:
        result = func2(a[i], a[j+i])
        if result:
          counter += 1
print(counter)