Harry and magic string HDU - 5157 记录不相交的回文串对数
题意:
记录不相交的回文串对数
题解:
正着反着都来一遍回文树
用sum1【i】 表示到 i 位置,出现的回文串个数的前缀和
sun2【i】表示反着的个数
ans+=sum1【i-1】*sum2【i】
#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];
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 = n = last = ;
newnode();
newnode(-);
S[] = -;
fail[] = ;
} int get_fail(int x) {
while (S[n - len[x] - ] != S[n])x = fail[x];
return x;
} void add(int c, int pos) {
c -= 'a';
S[++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]] + ;
}
last = next[cur][c];
cnt[last]++;
} void count()//统计本质相同的回文串的出现次数
{
for (int i = sz - ; i >= ; --i)cnt[fail[i]] += cnt[i];
//逆序累加,保证每个点都会比它的父亲节点先算完,于是父亲节点能加到所有子孙
}
} pam; int main() {
// FIN;
while (~sfs(s + )) {
pam.init();
int n = strlen(s + );
for (int i = ; i <= n; i++) {
pam.add(s[i], i);
sum[i] = sum[i - ] + pam.num[pam.last];
}
pam.init();
LL ans = ;
for (int i = n; i >= ; --i) {
pam.add(s[i], i);
ans += pam.num[pam.last] * sum[i - ];
}
printf("%lld\n", ans);
}
return ;
}
Harry and magic string HDU - 5157 记录不相交的回文串对数的更多相关文章
- HDU - 5157 :Harry and magic string (回文树,求多少对不相交的回文串)
Sample Input aca aaaa Sample Output 3 15 题意: 多组输入,每次给定字符串S(|S|<1e5),求多少对不相交的回文串. 思路:可以用回文树求出以每个位置 ...
- Girls' research - HDU 3294 (Manacher处理回文串)
题目大意:给以一个字符串,求出来这个字符串的最长回文串,不过这个字符串不是原串,而是转换过的,转换的原则就是先给一个字符 例如 'b' 意思就是字符把字符b转换成字符 a,那么c->b, d-& ...
- HDU 5651 计算回文串个数问题(有重复的全排列、乘法逆元、费马小定理)
原题: http://acm.hdu.edu.cn/showproblem.php?pid=5651 很容易看出来的是,如果一个字符串中,多于一个字母出现奇数次,则该字符串无法形成回文串,因为不能删减 ...
- (回文串 )Best Reward -- hdu -- 3613
http://acm.hdu.edu.cn/showproblem.php?pid=3613 Best Reward Time Limit: 2000/1000 MS (Java/Others) ...
- Manacher回文串算法学习记录
FROM: http://hi.baidu.com/chenwenwen0210/item/482c84396476f0e02f8ec230 #include<stdio.h> #inc ...
- HDU 5371(2015多校7)-Hotaru's problem(Manacher算法求回文串)
题目地址:HDU 5371 题意:给你一个具有n个元素的整数序列,问你是否存在这样一个子序列.该子序列分为三部分,第一部分与第三部分同样,第一部分与第二部分对称.假设存在求最长的符合这样的条件的序列. ...
- hdu 1159 Palindrome(回文串) 动态规划
题意:输入一个字符串,至少插入几个字符可以变成回文串(左右对称的字符串) 分析:f[x][y]代表x与y个字符间至少插入f[x][y]个字符可以变成回文串,可以利用动态规划的思想,求解 状态转化方程: ...
- YZOI Easy Round 2_回文串 string
原文链接:http://laphets1.gotoip3.com/?id=18 Description 给出一个由小写字母组成的字符串,其中一些字母被染黑了,用?表示.已知原来的串不是 一个回文串,现 ...
- (最长回文串 模板) 最长回文 -- hdu -- 3068
http://acm.hdu.edu.cn/showproblem.php?pid=3068 最长回文 Time Limit: 4000/2000 MS (Java/Others) Memory ...
随机推荐
- 23. Jmeter使用ServerAgent对服务器进行性能监控
我们在做服务器性能测试的时候,往往会考虑四个点:CPU.网络.磁盘.内存.一般情况下是使用Linux命令进行监控,那么jmeter可否做到呢?答案是可以的,闲话不多说,进入正题. 环境准备 jmete ...
- apach hadoop2.6 集群利用Phoenix 4.6-hbase 批量导入并自动创建索引
基础环境: 1.安装apach 版本hadoop2.6 2.部署hbase1.0.0 3.下载phoenix-4.6.0-HBase-1.0.下载地址(http://mirror.nus.edu.sg ...
- leetcode.数组.16最接近的三数之和-java
1. 具体题目 给定一个包括 n 个整数的数组 nums 和 一个目标值 target.找出 nums 中的三个整数,使得它们的和与 target 最接近.返回这三个数的和.假定每组输入只存在唯一答案 ...
- java-day20
注解:说明程序的,给计算机看的 注释:用文字描述程序的,给程序员看的 定义:注解(Annotation),也叫元数据.一种代码级别的说明.它是JDK1.5及以后版本引入的一个特性.与类.接口.枚举是在 ...
- 39-Ubuntu-用户管理-04-usermod设置主组和附加组
主组:通常在新建用户时指定,在/etc/passwd的第4列GID显示主组名. 附加组:在/etc/group中最后一列显示该组的用户列表,用于指定用户的附加权限. 1.修改用户的主组 sudo us ...
- json传参报错
restful接口报错: com.fasterxml.jackson.core.JsonParseException: Unexpected character ('e' (code 101)): w ...
- Javascript原型对象中的toString
<script> //tostring function Person(name,age,gender){ this.name=name; this.age=age; this.gende ...
- webpack4.x的使用历程
第一次接触的webpack是在一个3.x的资料中 在4.x的运用中遇到了好多的坑,我就以小白的身份把我使用webpaxk的过程分享出来,其中很多不足欢迎大佬们指正 node安装不再赘述 一.安装 np ...
- python异常整理
一.操作excel 时异常 1.PermissionError: [Errno 13] Permission denied (1)原因:权限错误:[Errno 13] 权限被拒绝 错误产生的原因是文件 ...
- vue 学习五 深入了解components(父子组件之间的传值)
上一章记录了 如何在父组件中向子组件传值,但在实际应用中,往往子组件也要向父组件中传递数据,那么此时我们应该怎么办呢 1.在父组件内使用v-on监听子组件事件,并在子组件中使用$emit传递数据 // ...