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 题意:多组输入,开始字符 ...
随机推荐
- mac 堡垒机传文件
安装zssh brew install zssh 上传文件 zssh登陆上跳板机 在跳板机上ssh到相应服务器 在服务器上cd至相应要放上传文件的目录 rz -bye //在远程服务器的相应目录上运行 ...
- 剑指offer——61平衡二叉树
题目描述 输入一棵二叉树,判断该二叉树是否是平衡二叉树. 题解: 方法一:使用深度遍历,判断每个节点是不是平衡二叉树,这种从上至下的方法会导致底层的节点重复判断多次 方法二:使用后序遍历判断,这种 ...
- 剑指offer——47把数组排成最小的数
题目描述 输入一个正整数数组,把数组里所有数字拼接起来排成一个数,打印能拼接出的所有数字中最小的一个.例如输入数组{3,32,321},则打印出这三个数字能排成的最小数字为321323. 题解: ...
- 反射与类加载之ClassLoader与类加载器(二)
更多Android高级架构进阶视频学习请点击:https://space.bilibili.com/474380680本篇文章将从以下几个内容来阐述反射与类加载: [动态代理模式] [Android ...
- 50-Ubuntu-其他命令-2-软硬链接示意图
在Linux中,文件名和文件的数据是分开存储的. 使用ls -lh可以查看一个文件的硬链接数. 在日常工作中,几乎不会建立文件的硬链接数. 在Linux中,只有文件的硬链接数等于0才会被删除.如下图第 ...
- 【洛谷】P1288 取数游戏II
题目链接:https://www.luogu.org/problemnew/show/P1288 题意:中文题面不赘述啦. 题解:代码很好写,其实就是判断边数是否为偶数.先手确定方向其实都是一样的,但 ...
- ios网络学习------2 用非代理方法实现同步post请求
#pragma mark - 这是私有方法,尽量不要再方法中直接使用属性,由于一般来说属性都是和界面关联的,我们能够通过參数的方式来使用属性 #pragma mark post登录方法 -(void) ...
- Spring-boot整合Redis,遇到的问题
1.通过set进redis中的数据,get不到 String cityKey ="city_"+id; ValueOperations<String,City> ope ...
- Jmeter+ant
1.下载 ant,解压到非中文目录,并配置环境变量,不会的自行 google 2.将 jmeter 中 extras 子目录里的 ant-jmeter-1.1.1.jar 复制到 ant 中的 lib ...
- HTTP状态码(转)
转自菜鸟教程:https://www.runoob.com/http/http-status-codes.html HTTP状态码共分为5种类型: HTTP状态码分类 分类 分类描述 1** 信息,服 ...