I Love Palindrome String HDU - 6599 回文树+hash
题意:
输出每个长度下的回文串(这些回文串的左半边也需要是回文串)
题解:
直接套上回文树,然后每找到一个回文串就将他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的更多相关文章
- HDU 6599 I Love Palindrome String (回文树+hash)
题意 找如下子串的个数: (l,r)是回文串,并且(l,(l+r)/2)也是回文串 思路 本来写了个回文树+dfs+hash,由于用了map所以T了 后来发现既然该子串和该子串的前半部分都是回文串,所 ...
- Palindrome Partition CodeForces - 932G 回文树+DP+(回文后缀的等差性质)
题意: 给出一个长度为偶数的字符串S,要求把S分成k部分,其中k为任意偶数,设为a[1..k],且满足对于任意的i,有a[i]=a[k-i+1].问划分的方案数. n<=1000000 题解: ...
- Interesting HDU - 5785 回文树
题意: 找出所有[i,j]为回文串[j+1,k]也为回文串的i*k乘积之和. 题解: 设sum1[i] 为正着插入,到 i 的所有回文串的起始位置的前缀和,sum2[i] 表示反正插入的前缀和 ans ...
- 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 ...
- 杭电多校HDU 6599 I Love Palindrome String (回文树)题解
题意: 定义一个串为\(super\)回文串为: \(\bullet\) 串s为主串str的一个子串,即\(s = str_lstr_{l + 1} \cdots str_r\) \(\bullet\ ...
- HDU 5157 Harry and magic string(回文树)
Harry and magic string Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/O ...
- HDU 5421 Victor and String(回文树)
Victor and String Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 524288/262144 K (Java/Othe ...
- Victor and String HDU - 5421 双向回文树
题意: 有n种操作,开始给你一个空串,给你4中操作 1 c 在字符串的首部添加字符c 2 c 在字符串的尾部添加字符c 3 询问字符中的本质不同的回文串的个数 4 询问字符串中回文串的个数 思路 ...
- 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 题意:多组输入,开始字符 ...
随机推荐
- python 调用redis
#!/usr/bin/env python #_*_ coding:UTF-8 _*_ import redis import pickle #普通连接 ''' db="db1" ...
- Git 学习第三天(二)
默认情况下, Git合并是采用"fast forward"模式,但这种模式下,如果删除分支,会丢掉分支信息 禁用 fast forward 模式: git merge --no-f ...
- error C3867: “std::basic_string<char,std::char_traits<char>,std::allocator<char>>::c_str”: 函数调用缺少参数列表;请使用“&std::basic_string<char,std::char_traits<char>,std::allocator<char>>::c_str”创建指向成员的指针
这个问题找了很多没有找到满意的答案.仔细看了一下,是使用了c_str的问题. 我直接把使用string.c_str的地方使用char*代替即解决问题.
- Centos光盘ISO安装过程再理解
ISO启动时的基本流程 vmlinuz -> 加载initrd.img -> 加载内核基本驱动 -> 挂载光盘至/run/install/repo -> 启动anaconda ...
- python调用tushare获取上市公司管理层人员薪酬和持股
接口:stk_rewards 描述:获取上市公司管理层薪酬和持股 积分:用户需要2000积分才可以调取,具体请参阅本文最下方积分获取办法 注:tushare包下载和初始化教程,请查阅我之前的文章 输入 ...
- springfox-swagger原理解析与使用过程中遇到的坑
swagger简介 swagger确实是个好东西,可以跟据业务代码自动生成相关的api接口文档,尤其用于restful风格中的项目,开发人员几乎可以不用专门去维护rest api,这个框架可以自动为你 ...
- 关于js 重载
拜读js忍者修炼一书 读到关于js函数重载内容这个模块 主要是介绍通过js的访问argument这个参数来实现js函数的重载 通过在函数内部进行判断js argument参数的长度 代码如下所示 va ...
- http://localhost:8080 is requesting your username and password
after you startup your tomcat, you type a concrete request url in broswer, the tomcat probably wil ...
- vue实现京东动态楼层效果
页面效果如下 <template> <div> <h1>首页</h1> <section class="floor-nav" ...
- MLE极大似然估计和EM最大期望算法
机器学习十大算法之一:EM算法.能评得上十大之一,让人听起来觉得挺NB的.什么是NB啊,我们一般说某个人很NB,是因为他能解决一些别人解决不了的问题.神为什么是神,因为神能做很多人做不了的事.那么EM ...