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 ...
随机推荐
- 分析无法进入Linux系统的原因
上文:http://www.cnblogs.com/long123king/p/3549701.html 1: static int __init kernel_init(void * unused) ...
- Bagging vs Boosting vs Stacking
原文地址:https://www.jianshu.com/p/9dacdc88d3ec Bagging 用于减小方差. 使用训练数据的不同随机子集(大小常等于训练数据,采用不放回的方式进行抽取)来训练 ...
- OSPF中DR和BDR到底是谁先选举出来的?
在OSPF的DRBDR选举的过程中,DR的选举依靠的是hello报文,在two-way之后,交互hello报文完成DR/BDR的选举. 那么在每台路由器根据收到的所有hello报文,会构建自己接口的数 ...
- exe自启动的几种方式
1 注册表启动项目RUN (注册路径 HKEY_LOCAL_MACHINE\SOFTWARE\microsoft\Windows\CurrentVersion\Run) 2 计划任务 比较少见这种方式 ...
- MySQL慢查询日志分割
mysql> set global slow_query_log=0; Query OK, 0 rows affected (0.00 sec) mysql> set global s ...
- ECMS清除挂马以及后台升级实战(从ecms6.6~ecms7.0)
当时状况 Windows Server 2008 R2 Enterprise + 帝国CMS6.6 + MySql server软件: Microsoft-IIS/7.5 操作系统: WINNT ...
- Excel_VBA 常用代码
单元格编辑后改变背景色(6号,355832828) Dim oldvalue As Variant Private Sub Worksheet_Change(ByVal Target As Range ...
- SB般的“WE战队”输掉了比赛
事实再一次证明,对于LOL这种游戏,国服选手是根本就不能有一点期待的, 国服环境太好了,赢了可以吹,输了不能骂,自信心极度膨胀,估计WE俱乐部都没有个心理咨询师, 下岗了还可以再卖卖脸,卖卖饼, 国服 ...
- 多flavor导致的源码依赖问题-- Cannot choose between the following configurations of project :sample:
一.背景: 当我们在源码依赖的时候经常会导致一些问题. 我们的主工程有如下配置,它依赖了一个sample的本地工程 flavorDimensions "demo" productF ...
- GarsiaWachs算法
解决石子问题: 题目描述如下: 有n堆石子排成一列,每堆石子有一个重量w[i], 每次合并可以合并相邻的两堆石子,一次合并的代价为两堆石子的重量和w[i]+w[i+1].问安排怎样的合并顺序,能够使得 ...