-
Notifications
You must be signed in to change notification settings - Fork 0
/
palindrome.h
96 lines (81 loc) · 2.51 KB
/
palindrome.h
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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
/*
* palindrome.h --
*
* algorithms related to the calculation of the palindromeimum
*
* Wed, Jan 23, 2013 2:15:58 PM: initial release by Todd Xue
*/
#ifndef _palindrome_h
#define _palindrome_h
namespace palindrome {
inline bool is_palindrome(char const* s, char const* s_end) {
for (; s < s_end; ++s, --s_end)
if (*s != *s_end)
return false;
return true;
}
/**
* max length sub string which is a palindrome
* output goes into [pb, pe)
*/
void max_substr_simple(int n, char const* s, char const*& pb, char const*& pe);
void max_substr_center(int n, char const* s, char const*& pb, char const*& pe);
void max_substr_Manacher(int n, char const* s, char const*& pb, char const*& pe, int* buffer);
/**
* integer related palindrome
*/
namespace integer {
typedef unsigned int uint;
inline char* get_digits(uint n, char* digits) {
if (n == 0) {
*digits++ = 0;
return digits;
}
while (n > 0) {
*digits++ = n % 10;
n /= 10;
}
return digits;
}
inline int get_number(char const* digits, char const* end) {
int n = 0;
for (--end; end >= digits; --end) {
n *= 10;
n += *end;
}
return n;
}
/**
* - determine if is palindrom
*/
inline bool is_palindrome(uint n) {
if (n < 0)
return false;
char digits[20];
char* end = get_digits(n, digits);
return palindrome::is_palindrome(digits, end - 1);
}
/**
* - given an uinteger, find the closest number that is palindrome
*
* hash table looks up doesn't work in this case, based on reflection rule compose,
* there are huge number of uintegers which are palindrome
*
* but got some hints first:
*
* 123 --> 121, closest, always adjust the last digits
* 13 --> 11
*
* so seems pretty obvious to get this actually :)
*/
inline uint closest_palindrome(uint n) {
char digits[20];
char* end = get_digits(n, digits);
for (char *b = digits, *e = end-1; b < e; ++b, --e) {
*b = *e;
}
return get_number(digits, end);
}
}
}
#endif