标签:can AC amp \n bit pre += ant target
【题目链接】
https://www.lydsy.com/JudgeOnline/problem.php?id=2084
【算法】
manacher
【代码】
#include<bits/stdc++.h> using namespace std; #define MAXN 500010 int n; char s[MAXN]; inline void Manacher() { int i,len,mx = 0,pos = 0; long long ans = 0; static char tmp[MAXN<<1]; static int p[MAXN<<1]; len = strlen(s+1); for (i = 1; i <= len; i++) { tmp[2*i-1] = ‘#‘; tmp[2*i] = s[i]; } tmp[len = 2 * len + 1] = ‘#‘; for (i = 1; i <= len; i++) { if (tmp[i] != ‘#‘) { p[i] = 1; continue; } if (mx > i) p[i] = min(p[2*pos-i],mx-i); else p[i] = 1; while (i - p[i] >= 1 && i + p[i] <= len && ((tmp[i-p[i]] == ‘#‘ && tmp[i+p[i]] == ‘#‘) || (tmp[i-p[i]] != tmp[i+p[i]]))) p[i]++; if (i + p[i] - 1 > mx) { mx = i + p[i] - 1; pos = i; } } for (i = 1; i <= len; i++) { if (tmp[i] == ‘#‘) ans += (long long)(p[i] - 1) / 2; } printf("%lld\n",ans); } int main() { scanf("%d%s",&n,s+1); Manacher(); return 0; }
标签:can AC amp \n bit pre += ant target
原文地址:https://www.cnblogs.com/evenbao/p/9248951.html