def find_word(line):
for step in xrange(1, len(line)):
for start in xrange(step):
if len(set(line[(start or None)::step])) != 1:
break
else:
return line[:step]
return line
print find_word('HELLOHELLOHELLOHEL')
print find_word('HHHELLOHHHELLOHHHELLOHHH')
print find_word('_HHELLOHHELLOHHELLOHHELLOHHELLOHHE')
HELLO
HHHELLO
_HHELLOHHELLOHHELLOHHELLOHHELLOHHE
int maxrepword(char *A){
int *ref=new int[strlen(A)];
int b=0,s=1;
ref[0]=-1;
while(A[s]){
if(A[s]==A[b]) ref[s++]=b++;
else if(b==0) ref[s++]=-1;
else b=ref[b-1]+1;
}
delete[] ref;
return s-b;
}
str = "HHELLOHHELLOHHELLOHHELLOHHELLOHHE"
word = ""
resword = ""
found = False
print 'String: ' + str
for i in range(len(str)/2):
word += str[i]
print 'Testing subword: ' + word
found = True
wordlen = len(word)
for k in range(len(str)):
if str[k] != word[k % wordlen]:
found = False
print str[k] + ' != ' + word[k % wordlen]
break
else:
print str[k] + ' == ' + word[k % wordlen]
if found == True:
resword = word
break
if found:
print 'Found: ' + resword
else:
print 'Found: ' + str