-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathKMP.py
More file actions
79 lines (71 loc) · 1.77 KB
/
KMP.py
File metadata and controls
79 lines (71 loc) · 1.77 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
# -*- coding: utf-8 -*-
# @Time : 2019/2/25 11:46
# @Author : xulzee
# @Email : xulzee@163.com
# @File : KMP.py
# @Software: PyCharm
def get_next(s: 'str'):
next_list = [-1] * len(s)
i, j = 0, -1
while i < len(s) - 1:
if j == -1 or s[i] == s[j]:
i += 1
j += 1
if s[i] == s[j]:
next_list[i] = next_list[j]
else:
next_list[i] = j
else:
j = next_list[j]
return next_list
def index_kmp(s: 'str', t: 'str'):
i, j = 0, 0
next_list = get_next(t)
while i < len(s) and j < len(t):
if j == -1 or s[i] == t[j]:
j += 1
i += 1
else:
j = next_list[j]
if j >= len(t):
return i - len(t)
else:
return -1
def isPalindrome(s):
if not len(s):
return True
s = "".join(filter(str.isalnum, s)).lower()
return s == s[::-1]
if __name__ == '__main__':
s = input()
if len(s) % 2 != 0:
print(len(s))
else:
slow = 0
fast = len(s) // 2
while fast < len(s):
if s[fast] != s[slow]:
break
fast += 1
slow += 1
if fast < len(s) - 1:
print(len(s))
else:
print(len(s) // 2)
# s = input()
# t = input()
# n = int(input())
# for i in range(n):
# str_in = input().split()
# left, right = int(str_in[0]) - 1, int(str_in[1])
# count = 0
# temp = s[left:right]
# while (True):
# ret = index_kmp(temp, t)
# if ret != -1:
# count += 1
# left = ret + len(t)
# temp = temp[left:]
# else:
# break
# print(count)