首先我们重新定义0 ≠ 0, 0 = 1, 1 = 0, 1 ≠ 1,然后跑一边manacher就好啦~
然后去现学了manacher。。。
1 /************************************************************** 2 Problem: 2084 3 User: rausen 4 Language: C++ 5 Result: Accepted 6 Time:24 ms 7 Memory:6176 kb 8 ****************************************************************/ 9 10 #include <cstdio> 11 #include <algorithm> 12 13 using namespace std; 14 typedef long long ll; 15 const int N = 5e5 + 5; 16 17 int n; 18 char s[N]; 19 20 inline bool check(char a, char b) { 21 return min(a, b) == '0' && max(a, b) == '1'; 22 } 23 24 ll manacher(char st[], int n) { 25 static char s[N << 1]; 26 static int rad[N << 1]; 27 int mx = 1, id = 1, i; 28 ll res = 0; 29 for (s[0] = '$', s[i = 1] = '#'; i <= n; ++i) 30 s[i << 1] = st[i], s[i << 1 | 1] = '#'; 31 n = n << 1 | 1; 32 for (i = 1; i <= n; ++i) { 33 rad[i] = max(min(rad[id + id - i], mx - i), 0); 34 while ((s[i + rad[i]] == '#' && s[i - rad[i]] == '#') || (check(s[i + rad[i]], s[i - rad[i]]))) ++rad[i]; 35 if (i + rad[i] > mx) mx = i + rad[i], id = i; 36 res += rad[i] >> 1; 37 } 38 return res; 39 } 40 41 int main() { 42 scanf("%d", &n); 43 scanf("%s", s + 1); 44 printf("%lld\n", manacher(s, n)); 45 return 0; 46 }
By Xs酱~ 转载请说明
博客地址:http://www.cnblogs.com/rausen