|
| 1 | +/* |
| 2 | + Idea: |
| 3 | + - Using hashing we can check for each prefix if it is |
| 4 | + a palindrome or not. |
| 5 | + - From the information from the previous point we can |
| 6 | + calculate the result. |
| 7 | +*/ |
| 8 | + |
| 9 | +#include <bits/stdc++.h> |
| 10 | + |
| 11 | +using namespace std; |
| 12 | + |
| 13 | +int const N = 5e6 + 1; |
| 14 | +int const p = 67; |
| 15 | +int const m = 1e9 + 9; |
| 16 | +char s[N]; |
| 17 | +int n, res[N]; |
| 18 | +long long hash_value[N][2]; |
| 19 | +long long p_pow[N]; |
| 20 | + |
| 21 | +long long compute_hash() { |
| 22 | + p_pow[0] = 1; |
| 23 | + for(int i = 1; i < N; ++i) |
| 24 | + p_pow[i] = (p_pow[i - 1] * p) % m; |
| 25 | + |
| 26 | + if(islower(s[0])) |
| 27 | + hash_value[0][0] = ((s[0] - 'a' + 1) * p_pow[0]) % m; |
| 28 | + else if(isupper(s[0])) |
| 29 | + hash_value[0][0] = ((s[0] - 'A' + 27) * p_pow[0]) % m; |
| 30 | + else |
| 31 | + hash_value[0][0] = ((s[0] - '0' + 53) * p_pow[0]) % m; |
| 32 | + |
| 33 | + for(int i = 1; i < n; ++i) { |
| 34 | + if(islower(s[i])) |
| 35 | + hash_value[i][0] = (hash_value[i - 1][0] + (s[i] - 'a' + 1) * p_pow[i]) % m; |
| 36 | + else if(isupper(s[i])) |
| 37 | + hash_value[i][0] = (hash_value[i - 1][0] + (s[i] - 'A' + 27) * p_pow[i]) % m; |
| 38 | + else |
| 39 | + hash_value[i][0] = (hash_value[i - 1][0] + (s[i] - '0' + 53) * p_pow[i]) % m; |
| 40 | + } |
| 41 | + |
| 42 | + if(islower(s[n - 1])) |
| 43 | + hash_value[n - 1][1] = ((s[n - 1] - 'a' + 1) * p_pow[0]) % m; |
| 44 | + else if(isupper(s[n - 1])) |
| 45 | + hash_value[n - 1][1] = ((s[n - 1] - 'A' + 27) * p_pow[0]) % m; |
| 46 | + else |
| 47 | + hash_value[n - 1][1] = ((s[n - 1] - '0' + 53) * p_pow[0]) % m; |
| 48 | + |
| 49 | + for(int i = n - 2; i >= 0; --i) { |
| 50 | + if(islower(s[i])) |
| 51 | + hash_value[i][1] = (hash_value[i + 1][1] + (s[i] - 'a' + 1) * p_pow[n - i - 1]) % m; |
| 52 | + else if(isupper(s[i])) |
| 53 | + hash_value[i][1] = (hash_value[i + 1][1] + (s[i] - 'A' + 27) * p_pow[n - i - 1]) % m; |
| 54 | + else |
| 55 | + hash_value[i][1] = (hash_value[i + 1][1] + (s[i] - '0' + 53) * p_pow[n - i - 1]) % m; |
| 56 | + } |
| 57 | +} |
| 58 | + |
| 59 | +bool is_pal(int idx) { |
| 60 | + long long first_hash = hash_value[idx][0]; |
| 61 | + long long second_hash = (hash_value[0][1] - hash_value[idx + 1][1] + m) % m; |
| 62 | + |
| 63 | + first_hash = (first_hash * p_pow[(n - idx - 1)]) % m; |
| 64 | + |
| 65 | + return first_hash == second_hash; |
| 66 | +} |
| 67 | + |
| 68 | +int main() { |
| 69 | + scanf("%s", s); |
| 70 | + n = strlen(s); |
| 71 | + |
| 72 | + compute_hash(); |
| 73 | + |
| 74 | + res[0] = 1; |
| 75 | + for(int i = 1; i < n; ++i) |
| 76 | + if(is_pal(i)) |
| 77 | + res[i] = res[i / 2 - !(i & 1)] + 1; |
| 78 | + |
| 79 | + int to = 0; |
| 80 | + for(int i = 0; i < n; ++i) |
| 81 | + to += res[i]; |
| 82 | + |
| 83 | + printf("%d\n", to); |
| 84 | + |
| 85 | + return 0; |
| 86 | +} |
0 commit comments