题意:

输出每个长度下的回文串(这些回文串的左半边也需要是回文串)

题解:

直接套上回文树,然后每找到一个回文串就将他hash。

因为符合要求的回文串本身是回文串,左半边也是回文串,所以它左半边也右半边相同,

自己画个图很容易发现的

每次hash判断一下就好了

 #include <set>
#include <map>
#include <stack>
#include <queue>
#include <cmath>
#include <ctime>
#include <cstdio>
#include <string>
#include <vector>
#include <cstring>
#include <iostream>
#include <algorithm>
#include <unordered_map> #define pi acos(-1.0)
#define eps 1e-9
#define fi first
#define se second
#define rtl rt<<1
#define rtr rt<<1|1
#define bug printf("******\n")
#define mem(a, b) memset(a,b,sizeof(a))
#define name2str(x) #x
#define fuck(x) cout<<#x" = "<<x<<endl
#define sfi(a) scanf("%d", &a)
#define sffi(a, b) scanf("%d %d", &a, &b)
#define sfffi(a, b, c) scanf("%d %d %d", &a, &b, &c)
#define sffffi(a, b, c, d) scanf("%d %d %d %d", &a, &b, &c, &d)
#define sfL(a) scanf("%lld", &a)
#define sffL(a, b) scanf("%lld %lld", &a, &b)
#define sfffL(a, b, c) scanf("%lld %lld %lld", &a, &b, &c)
#define sffffL(a, b, c, d) scanf("%lld %lld %lld %lld", &a, &b, &c, &d)
#define sfs(a) scanf("%s", a)
#define sffs(a, b) scanf("%s %s", a, b)
#define sfffs(a, b, c) scanf("%s %s %s", a, b, c)
#define sffffs(a, b, c, d) scanf("%s %s %s %s", a, b,c, d)
#define FIN freopen("../in.txt","r",stdin)
#define gcd(a, b) __gcd(a,b)
#define lowbit(x) x&-x
#define IO iOS::sync_with_stdio(false) using namespace std;
typedef long long LL;
typedef unsigned long long ULL;
const ULL seed = ;
const LL INFLL = 0x3f3f3f3f3f3f3f3fLL;
const int maxn = 1e6 + ;
const int maxm = 8e6 + ;
const int INF = 0x3f3f3f3f;
const int mod = 1e9 + ;
char s[maxn];
ULL p[maxn], HA[maxn];
int ans[maxn]; ULL get_HASH(int l, int r) {
return HA[r] - HA[l - ] * p[r - l + ];
} struct Palindrome_Automaton {
int len[maxn], next[maxn][], fail[maxn], cnt[maxn];
int num[maxn], str[maxn], sz, n, last;
int vis[maxn]; int newnode(int l) {
for (int i = ; i < ; ++i)next[sz][i] = ;
cnt[sz] = num[sz] = , len[sz] = l;
return sz++;
} void init() {
sz = n = last = ;
newnode();
newnode(-);
str[] = -;
fail[] = ;
mem(vis, );
} int get_fail(int x) {
while (str[n - len[x] - ] != str[n])x = fail[x];
return x;
} void add(int c, int pos) {
c -= 'a';
str[++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;
num[now] = num[fail[now]] + ;
int temp = (len[now] + ) / ;
// fuck(n - len[now] + 1),fuck(n - len[now] + temp),fuck(n - temp),fuck(n);
if (len[now] == ||
get_HASH(n - len[now] + , n - len[now] + temp) == get_HASH(n - temp+, n))
vis[now] = ;
else vis[now] = ;
}
last = next[cur][c];
cnt[last]++;
} void count()//统计本质相同的回文串的出现次数
{
for (int i = sz - ; i >= ; --i) {
cnt[fail[i]] += cnt[i];
if (vis[i]) ans[len[i]] += cnt[i];
}
//逆序累加,保证每个点都会比它的父亲节点先算完,于是父亲节点能加到所有子孙
}
} pam; int main() {
// FIN;
p[] = ;
for (int i = ; i < maxn; ++i) p[i] = p[i - ] * seed;
while (~sfs(s + )) {
int n = strlen(s + );
pam.init();
for (int i = ; i <= n; ++i) {
HA[i] = HA[i - ] * seed + s[i];
pam.add(s[i], i);
ans[i] = ;
}
pam.count();
for (int i = ; i <= n; ++i) printf("%d%c", ans[i], (i == n ? '\n' : ' '));
}
return ;
}

I Love Palindrome String HDU - 6599 回文树+hash的更多相关文章

  1. HDU 6599 I Love Palindrome String (回文树+hash)

    题意 找如下子串的个数: (l,r)是回文串,并且(l,(l+r)/2)也是回文串 思路 本来写了个回文树+dfs+hash,由于用了map所以T了 后来发现既然该子串和该子串的前半部分都是回文串,所 ...

  2. Palindrome Partition CodeForces - 932G 回文树+DP+(回文后缀的等差性质)

    题意: 给出一个长度为偶数的字符串S,要求把S分成k部分,其中k为任意偶数,设为a[1..k],且满足对于任意的i,有a[i]=a[k-i+1].问划分的方案数. n<=1000000 题解: ...

  3. Interesting HDU - 5785 回文树

    题意: 找出所有[i,j]为回文串[j+1,k]也为回文串的i*k乘积之和. 题解: 设sum1[i] 为正着插入,到 i 的所有回文串的起始位置的前缀和,sum2[i] 表示反正插入的前缀和 ans ...

  4. Gym - 101981M:(南京) Mediocre String Problem(回文树+exkmp)

    #include<bits/stdc++.h> #define ll long long #define rep(i,a,b) for(int i=a;i<=b;i++) using ...

  5. 杭电多校HDU 6599 I Love Palindrome String (回文树)题解

    题意: 定义一个串为\(super\)回文串为: \(\bullet\) 串s为主串str的一个子串,即\(s = str_lstr_{l + 1} \cdots str_r\) \(\bullet\ ...

  6. HDU 5157 Harry and magic string(回文树)

    Harry and magic string Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/O ...

  7. HDU 5421 Victor and String(回文树)

    Victor and String Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 524288/262144 K (Java/Othe ...

  8. Victor and String HDU - 5421 双向回文树

    题意: 有n种操作,开始给你一个空串,给你4中操作 1 c  在字符串的首部添加字符c 2 c  在字符串的尾部添加字符c 3  询问字符中的本质不同的回文串的个数 4 询问字符串中回文串的个数 思路 ...

  9. HDU - 5421:Victor and String (回文树,支持首尾插入新字符)

    Sample Input 6 1 a 1 b 2 a 2 c 3 4 8 1 a 2 a 2 a 1 a 3 1 b 3 4 Sample Output 4 5 4 5 11 题意:多组输入,开始字符 ...

随机推荐

  1. 3. Python基础语法

    注释 我们在文言文中经常会看到注释,注释可以帮助读者对文章的理解.代码中的注释也是一样,优秀的代码注释可以帮助读者对代码的理解.当然在代码编写过程中,注释的使用不一定只是描述一段代码,也可能的是对代码 ...

  2. python学习9—文件基本操作与高级操作

    python学习9—文件基本操作与高级操作 1. 文件基本操作 打开文件,获得文件句柄:f = open('filename',encoding='utf-8'),open会查询操作系统的编码方式,并 ...

  3. zabbix--监控的组件和进程介绍

    上图是zabbix的架构,zabbix proxy(代理),可以减小IO并发. zabbix web GUI是用php写的画图工具,从数据库抓取数据. zabbix database zabbix获取 ...

  4. 认识AppDomain类

    原文:认识AppDomain类 表示应用程序域,它是一个应用程序在其中执行的独立环境. 创建新的 AppDomain,在该新建 AppDomain 中实例化类型,以及与该类型的对象通信. usingn ...

  5. GP下CalculateField的用法

    以前用过这个类做字段计算,许久不用有些忘却,记录一下使用方式 public static void CalculateField(IFeatureLayer featureLayer,IField f ...

  6. javascript基础入门之js中的结构分支与循环语句

    javascript基础入门之js中的结构分支与循环语句 程序的结构①顺序结构:自上而下:②选择(分支)结构:多条路径,根据不同的条件,只执行其中一个:③循环结构:重复某些代码④配合特定的语句实现选择 ...

  7. mysql 查询正在执行的sql

    select * from information_schema.`PROCESSLIST` where info is not null; 或者 -- use information_schema; ...

  8. [转]在WPF中自定义控件 UserControl

    在这里我们将将打造一个UserControl(用户控件)来逐步讲解如何在WPF中自定义控件,并将WPF的一些新特性引入到自定义控件中来.我们制作了一个带语音报时功能的钟表控件, 效果如下: 在VS中右 ...

  9. MySQL 刷题知识点整理

    1. left join on 与 right join on, inner join on 的区别: left join on 把左表中的行全部展示,而将寻找右表中符合的行展示: right joi ...

  10. Zookeeper_ZAB协议

    ZAB协议 ZAB协议简介 ZAB:(Zookeeper Atomic Broadcast),zk原子消息广播协议,是专为ZK设计的一中支持崩溃恢复的原子广播协议,是一种Paxos协议的优化算法,在Z ...