题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=6599

题目大意为求字符串S有多少个子串S[l,r]满足回文串的定义,并且S[l,(l+r)/2]也满足回文串的定义。

可以直接建回文自动机,然后再统计出每种回文串的个数,然后再枚举状态,判断该状态所表示的回文串它的一半是否满足回文串的定义,判断用hash来判断即可。

 #include<cstdio>
#include<iostream>
#include<cstring>
#include<string>
#include<algorithm>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
const int maxn = 3e5 + ;
ull Hash[maxn], xp[maxn];
void init() {
xp[] = ;
for (int i = ; i <= 3e5 + ; i++)
xp[i] = xp[i - ] * ;
}
ull getH(int l, int r) {
if (l == )return Hash[r];
return Hash[r] - Hash[l - ] * xp[r - l + ];
}
bool check(int l, int r) {
int len = r - l + ;
int mid = l + r >> ;
if (len & )return getH(l, mid) == getH(mid, r);
else return getH(l, mid) == getH(mid + , r);
}
ll ans[maxn], id[maxn];
struct Palindromic_Tree {
int next[maxn][],fail[maxn], cnt[maxn],len[maxn],S[maxn];
int last, n,p;
int newnode(int l) {
for (int i = ; i < ; ++i) next[p][i] = ;
cnt[p] = ;
len[p] = l;
return p++;
}
void init() {
p = ;
newnode();
newnode(-);
last = ;
n = ;
S[n] = -;
fail[] = ;
} int get_fail(int x) {
while (S[n - len[x] - ] != S[n]) x = fail[x];
return x;
}
void add(int c) {
c -= 'a';
S[++n] = c;
int cur = get_fail(last);
if (!next[cur][c]) {
int now = newnode(len[cur] + );
fail[now] = next[get_fail(fail[cur])][c];
next[cur][c] = now;
}
last = next[cur][c];
cnt[last]++;
id[last] = n;
}
void count() {
for (int i = p - ; i >= ; --i) cnt[fail[i]] += cnt[i];
for (int i = ; i < p; i++)
if (check(id[i] - len[i], id[i] - ))
ans[len[i]] += cnt[i];
}
}a;
char s[maxn];
int main() {
init();
while (~scanf("%s", s)) {
int n = strlen(s);
Hash[] = s[];
memset(ans, , sizeof(ans));
for (int i = ; i < n; i++)
Hash[i] = Hash[i - ] * + s[i];
a.init();
for (int i = ; i < n; i++)
a.add(s[i]);
a.count();
for (int i = ; i <= n; i++)
printf("%lld%c", ans[i], (i == n ? '\n' : ' '));
}
}

最新文章

  1. 冰球项目日志2-yjw
  2. Java_通过反射调用类中的方法
  3. K910 升级Android 4.4.2可用的Google Service Framework
  4. [C#]AES加密算法实现
  5. linux下Nginx服务器安装教程
  6. bzoj1011 [HNOI2008]遥远的行星
  7. Split字符串分割函数
  8. Hashmap in java
  9. [c#]如何在form的webbrowser控件中获得鼠标坐标
  10. 监听UITabBarItem来拦截是否要跳转
  11. 阵列卡,组成的磁盘组就像是一个硬盘,pci-e扩展出sata3.0
  12. connectVisualVMtoTomcat
  13. Linux 配置VNC远程桌面
  14. 从壹开始微服务 [ DDD ] 之一 ║ D3模式设计初探 与 我的计划书
  15. Chapter 5 Blood Type——15
  16. js如何获取url参数
  17. 【java】static的应用场景
  18. Linux的mv 命令
  19. 洛谷 P1852 [国家集训队]跳跳棋 解题报告
  20. Intellij IDEA 配置Subversion插件时效解决方法

热门文章

  1. CSP2019 题解
  2. QQ群文件未通过安全检查,禁止下载该文件
  3. Python---进阶---常用模块os、jso
  4. 【GDOI 2016 Day1】疯狂动物城
  5. [洛谷P3243] 菜肴制作
  6. 【leetcode】313. Super Ugly Number
  7. plt.imshow()为什么不能显示同时显两张照片
  8. JavaScript modularity with RequireJS (from spaghetti code to ravioli code)
  9. leetcode-mid-Linked list- 230 Kth Smallest Element in a BST
  10. whu 1581 Union of cubes