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(最 ...
随机推荐
- LIBRARY_PATH是编译时候用的,LD_LIBRARY_PATH是程序运行是使用的
LD_LIBRARY_PATH与LIBRARY_PATH的区别 看起来很像,但是完全是两码事. LIBRARY_PATH is used by gcc before compilation to se ...
- 关于RF中元素定位问题
今天碰到一个定位元素的问题,用CLASS定位. 调试后是这样的情况: 显示定位正确,字体被覆盖了.完了,在RF中跑脚本的时候,报错,说没有找到元素 . 郁闷,各种试,还是没有定位到. 最好问前端,教我 ...
- Python中使用item()方法遍历字典的例子
Python中使用item()方法遍历字典的例子 这篇文章主要介绍了Python中使用item()方法遍历字典的例子,for...in这种是Python中最常用的遍历字典的方法了,需要的朋友可以参考下 ...
- 使用poi实现生成excel文件
import java.util.ArrayList; import javax.servlet.ServletOutputStream; import org.apache.poi.hssf.use ...
- Java jar文件
JAR(Java Archive)是基于ZIP文件格式的文件格式. 它用于捆绑Java应用程序或小程序的资源,类文件,声音文件,图像等. 它还提供数据压缩.一个JAR文件作为一种特殊类型的ZIP文件. ...
- python初学者学习工具安装教程&安装步骤详解
一.python安装: 版本:3.6.8 下载地址:https://www.python.org/downloads/ 安装步骤截图: 1.点击python安装包,出现下图所示界面,注意勾选A ...
- Avito Cool Challenge 2018 B - Farewell Party
题目大意: 有n个人 接下来一行n个数a[i] 表示第i个人描述其他人有a[i]个的帽子跟他不一样 帽子编号为1~n 如果所有的描述都是正确的 输出possible 再输出一行b[i] 表示第i个人的 ...
- 1 新增硬盘挂载home文件夹。 2 修理扇区
一 挂载新硬盘 主机磁盘容量不够大时,想新增一颗新磁盘的时候.并将磁盘全部分割成单一分割槽,且将该分割槽挂载到/home目录,你该怎么做呢? 1 安装硬盘. 2 磁盘分区. 3 格式化磁盘 4 将 ...
- Vue-cli开发笔记三----------引入外部插件
(一)绝对路径直接引入: (1)主入口页面index.html中头部script标签引入: <script type="text/javascript" src=" ...
- 在GitHub上编辑README.md排版样式
如何在github的readme.md编辑出好看的样式排版文章呢...? 参考:https://blog.csdn.net/u012067966/article/details/50736647