Victor and String HDU - 5421 双向回文树
题意:
有n种操作,开始给你一个空串,给你4中操作
1 c 在字符串的首部添加字符c
2 c 在字符串的尾部添加字符c
3 询问字符中的本质不同的回文串的个数
4 询问字符串中回文串的个数
思路:last[0]表示首部的操作的位置,last[1]表示尾部的操作的位置
模板提,用上双向的回文树就好了。
#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 = 1e5 + ;
const int maxm = 8e6 + ;
const int INF = 0x3f3f3f3f;
const int mod = 1e9 + ;
char s[maxn];
LL sum[maxn]; struct Palindrome_Automaton {
int len[maxn * ], next[maxn * ][], fail[maxn * ], cnt[maxn * ];
int num[maxn * ], S[maxn * ], sz, n[], last[]; int newnode(int l) {
for (int i = ; i < ; ++i)next[sz][i] = ;
cnt[sz] = num[sz] = , len[sz] = l;
return sz++;
} void init() {
sz = ;
newnode();
newnode(-);
S[] = -;
fail[] = ;
last[] = last[] = ;
n[] = maxn - , n[] = maxn - ;
} int get_fail(int x, int k) {
S[n[] - ] = -, S[n[] + ] = -;
while (S[n[k] - (k ? : -) * (len[x] + )] != S[n[k]])x = fail[x];
return x;
} int add(int c, int k) {
c -= 'a';
S[n[k] += (k ? : -)] = c;
int cur = get_fail(last[k],k);
if (!(last[k] = next[cur][c])) {
int now = newnode(len[cur] + );
fail[now] = next[get_fail(fail[cur],k)][c];
next[cur][c] = now;
num[now] = num[fail[now]] + ;
last[k]=now;
if (len[last[k]]==n[]-n[]+) last[k^]=last[k];
}
//last[k] = next[cur][c];
//cnt[last]++;
return num[last[k]];
} void count()//统计本质相同的回文串的出现次数
{
for (int i = sz - ; i >= ; --i)cnt[fail[i]] += cnt[i];
//逆序累加,保证每个点都会比它的父亲节点先算完,于是父亲节点能加到所有子孙
}
} pam;
char op[];
int n;
int main() {
//FIN;
while(~sfi(n)){
pam.init();
LL ans=;
for (int i = ,x; i < n; ++i) {
sfi(x);
if (x==) {
sfs(op);
ans+=pam.add(op[],);
}else if (x==) {
sfs(op);
ans+=pam.add(op[],);
}else if (x==) printf("%d\n",pam.sz-);
else printf("%lld\n",ans);
}
}
return ;
}
Victor and String HDU - 5421 双向回文树的更多相关文章
- Victor and String[Bestcoder #52 1004](回文树)
题目描述 Victor喜欢玩字符串.他认为一个字符串是迷人的,当且仅当字符串是回文的.Victor想玩n次.每次他都会做以下四种操作中的一种.操作1:在字符串的开头添加一个字符 c.操作2:在字符串的 ...
- HDU 5421 Victor and String(回文树)
Victor and String Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 524288/262144 K (Java/Othe ...
- 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 题意:多组输入,开始字符 ...
- 【HDU5421】Victor and String(回文树)
[HDU5421]Victor and String(回文树) 题面 Vjudge 大意: 你需要支持以下操作: 动态在前端插入一个字符 动态在后端插入一个字符 回答当前本质不同的回文串个数 回答当前 ...
- 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 6599 I Love Palindrome String (回文树+hash)
题意 找如下子串的个数: (l,r)是回文串,并且(l,(l+r)/2)也是回文串 思路 本来写了个回文树+dfs+hash,由于用了map所以T了 后来发现既然该子串和该子串的前半部分都是回文串,所 ...
- 杭电多校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 (回文树,求多少对不相交的回文串)
Sample Input aca aaaa Sample Output 3 15 题意: 多组输入,每次给定字符串S(|S|<1e5),求多少对不相交的回文串. 思路:可以用回文树求出以每个位置 ...
- hdu5421 Victor and String 回文树(前后插入)
题目传送门 题意:对一个字符串支持四种操作,前插入字符,后插入字符,询问本质不同的回文串数量和所有回文串的数量. 思路: 就是在普通回文树的基础上,维护suf(最长回文后缀)的同时再维护一个pre(最 ...
随机推荐
- 拾遗:Qemu/KVM
WinXP: #!/bin/bash name=winxp qemu-system-x86_64 \ -enable-kvm \ -cpu host -smp ,sockets=,cores=,thr ...
- 前端(十九)—— Bootstrap框架
Bootstrap Bootstrap中文文档 一.简介 Bootstrap是美国Twitter公司的设计师Mark Otto和Jacob Thornton合作基于HTML.CSS.JavaScrip ...
- python3 使用int函数将浮点数转换成整数
int函数将浮点数转换成整数需要注意的地方 >>> int(153)153>>> int(153.4)153>>> int(153.5)153&g ...
- centos7下的nfs配置
NFS是Network File System的缩写,即网络文件系统.客户端通过挂载的方式将NFS服务器端共享的数据目录挂载到本地目录下. nfs为什么需要RPC?因为NFS支持的功能很多,不同功能会 ...
- hdu6354 /// 线段树
题目大意: 给定n m x y z 长度为n的序列初始为0 接下来m个操作 l r v 将l r区间内比v小的数都变成v l r v由x y z和给定的函数生成 线段树维护区间 最大值 最小值 再加 ...
- ie兼容小知识点
1.ie的滤镜特别针对ie使用css3的使用 (1)从css3渐变:filter: progid:DXImageTransform.Microsoft.gradient(startcolorstr=# ...
- SpringCloud服务消费有哪几种方式?
一.使用LoadBalancerClient LoadBalancerClient接口的命名中,可以看出这是一个负载均衡客户端的抽象定义,spring提供了一个实现 org.springframewo ...
- JUC 一 CountDownLatch(闭锁)
java.util.concurrent 介绍 CountDownLatch是一个同步辅助类,在完成一组正在其他线程中执行的操作之前,它允许一个或多个线程一直等待 CountDownLatch cou ...
- 官网下载 Linux 上需要的 MySQL的步骤
1.输入MySQL 官网地址 https://dev.mysql.com/ 选择 download ->Community->MySQL Community Server 点击 MyS ...
- java script 基本函数
Math.random() 是令系统随机选取大于等于 0.0 且小于 1.0 的伪随机 double 值. 日期时间函数(需要用变量调用):var b = new Date(); // ...