#include <vector> #include <string> #include "message.h" #include "palindromy.h" using namespace std; vector<int> Palindromes(string str) { vector<int> v; int sz = str.size(); int i = 0; int palsz = 0; while (i < sz) { if ((i > palsz) && (str[i - palsz - 1] == str[i])) { palsz += 2; ++i; continue; } v.push_back(palsz); int s = v.size() - 2; int e = s - palsz; if (s <= e) { palsz = 1; i += 1; continue; } for (int j = s; s > e; --j) { int d = j - e - 1; if (v[j] == d) { palsz = d; break; } v.push_back(min(d, v[j])); } } v.push_back(palsz); int vLen = v.size(); int s = vLen - 2; int e = s - (2 * sz + 1 - vLen); for (int i = s; i > e; --i) { int d = i - e - 1; v.push_back(min(d, v[i])); } return v; } int main (void) { if (MyNodeId()) return 0; int sz = GetLength(); string s; s.reserve(sz); for (int i = 0; i < sz; ++i) s.push_back(GetLetter(i)); vector<int> z = Palindromes(s); long long sum = 0; for (auto pl : z) sum += (pl + 1) / 2; printf("%lld\n", sum); }
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 | #include <vector> #include <string> #include "message.h" #include "palindromy.h" using namespace std; vector<int> Palindromes(string str) { vector<int> v; int sz = str.size(); int i = 0; int palsz = 0; while (i < sz) { if ((i > palsz) && (str[i - palsz - 1] == str[i])) { palsz += 2; ++i; continue; } v.push_back(palsz); int s = v.size() - 2; int e = s - palsz; if (s <= e) { palsz = 1; i += 1; continue; } for (int j = s; s > e; --j) { int d = j - e - 1; if (v[j] == d) { palsz = d; break; } v.push_back(min(d, v[j])); } } v.push_back(palsz); int vLen = v.size(); int s = vLen - 2; int e = s - (2 * sz + 1 - vLen); for (int i = s; i > e; --i) { int d = i - e - 1; v.push_back(min(d, v[i])); } return v; } int main (void) { if (MyNodeId()) return 0; int sz = GetLength(); string s; s.reserve(sz); for (int i = 0; i < sz; ++i) s.push_back(GetLetter(i)); vector<int> z = Palindromes(s); long long sum = 0; for (auto pl : z) sum += (pl + 1) / 2; printf("%lld\n", sum); } |