Skip to content

Instantly share code, notes, and snippets.

@ryanthegiantlion
Last active June 22, 2016 18:07
Show Gist options
  • Save ryanthegiantlion/b8ea19a0befa417f5ddc6d3edaa941e4 to your computer and use it in GitHub Desktop.
Save ryanthegiantlion/b8ea19a0befa417f5ddc6d3edaa941e4 to your computer and use it in GitHub Desktop.
## solution 1 using arrays
# import sys
# line = sys.stdin.readline().strip()
# linearr = []
# for c in line:
# linearr.append(c)
# # print linearr
# def iso2(s1l, s1u, s2l, s2u):
# global linearr
# # print s1l, s1u, s2l, s2u
# for i in xrange(s1u - s1l-1):
# for j in xrange(i+1, s1u - s1l):
# if (linearr[s1l+i] == linearr[s1l+j] and linearr[s2l+i] != linearr[s2l+j]) or (linearr[s1l+i] != linearr[s1l+j] and linearr[s2l+i] == linearr[s2l+j]):
# return False
# return True
# subsets = [[] for i in xrange(len(line)+1)]
# count = 0
# for i in xrange(len(line)):
# for j in xrange(0, i+1):
# set_has_isomorph = False
# same_sized_subsets = subsets[i+1-j]
# for k in same_sized_subsets:
# if iso2(k[0], k[1], j, i+1):
# set_has_isomorph = True
# break
# if not set_has_isomorph:
# count = count + 1
# same_sized_subsets.append((j,i+1))
# # print '****'
# # print same_sized_subsets
# print count
# # solution 2 using strings
# import sys
# line = sys.stdin.readline().strip()
# def iso2(s1, s2):
# for i in xrange(len(s1)-1):
# for j in xrange(i+1, len(s2)):
# if (s1[i] == s1[j] and s2[i] != s2[j]) or (s1[i] != s1[j] and s2[i] == s2[j]):
# return False
# return True
# subsets = [[] for i in xrange(len(line)+1)]
# count = 0
# for i in xrange(len(line)):
# for j in xrange(0, i+1):
# set_has_isomorph = False
# same_sized_subsets = subsets[i+1-j]
# for k in same_sized_subsets:
# if iso2(k, line[j:i+1]):
# set_has_isomorph = True
# break
# if not set_has_isomorph:
# count = count + 1
# same_sized_subsets.append(line[j:i+1])
# print count
## solution 3 using string with transformation
# import sys
# line = sys.stdin.readline().strip()
# def canonicalIsomorph(s):
# mapping = {}
# currentOrdinal = 97
# for i in s:
# if i not in mapping:
# mapping[i] = chr(currentOrdinal)
# currentOrdinal = currentOrdinal + 1
# return ''.join([mapping[i] for i in s])
# subsets = [set() for i in xrange(len(line)+1)]
# count = 0
# # print subsets
# for i in xrange(len(line)):
# for j in xrange(0, i+1):
# set_has_isomorph = False
# same_sized_subsets = subsets[i+1-j]
# cm = canonicalIsomorph(line[j:i+1])
# if cm in same_sized_subsets:
# # for k in same_sized_subsets:
# # if iso2(k, line[j:i+1]):
# set_has_isomorph = True
# break
# if not set_has_isomorph:
# count = count + 1
# # same_sized_subsets.append(line[j:i+1])
# same_sized_subsets.add(cm)
# # print same_sized_subsets
# print count
# # solution 4 we improve on 3 to only do mapping once
import sys
line = sys.stdin.readline().strip()
def getMappingForCanonicalForm(s):
mapping = {}
currentOrdinal = 97
for i in s:
if i not in mapping:
mapping[i] = chr(currentOrdinal)
currentOrdinal = currentOrdinal + 1
return ''.join([mapping[i] for i in s])
mappings = []
for i in xrange(len(line)):
mappings.append(getMappingForCanonicalForm(line[i:]))
subsets = [set() for i in xrange(len(line)+1)]
count = 0
# print subsets
for i in xrange(len(line)):
for j in xrange(0, i+1):
set_has_isomorph = False
same_sized_subsets = subsets[i+1-j]
# cm = canonicalIsomorph(line[j:i+1])
# cm = getCanonicalFormFromMapping(line[j:i+1], mappings[j])
cm = mappings[j][0:i+1-j]
if cm in same_sized_subsets:
# for k in same_sized_subsets:
# if iso2(k, line[j:i+1]):
set_has_isomorph = True
break
if not set_has_isomorph:
count = count + 1
# same_sized_subsets.append(line[j:i+1])
same_sized_subsets.add(cm)
# print same_sized_subsets
print count
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment