题意:

记录不相交的回文串对数

题解:

正着反着都来一遍回文树

用sum1【i】 表示到 i 位置,出现的回文串个数的前缀和

sun2【i】表示反着的个数

ans+=sum1【i-1】*sum2【i】

  1. #include <set>
  2. #include <map>
  3. #include <stack>
  4. #include <queue>
  5. #include <cmath>
  6. #include <ctime>
  7. #include <cstdio>
  8. #include <string>
  9. #include <vector>
  10. #include <cstring>
  11. #include <iostream>
  12. #include <algorithm>
  13. #include <unordered_map>
  14.  
  15. #define pi acos(-1.0)
  16. #define eps 1e-9
  17. #define fi first
  18. #define se second
  19. #define rtl rt<<1
  20. #define rtr rt<<1|1
  21. #define bug printf("******\n")
  22. #define mem(a, b) memset(a,b,sizeof(a))
  23. #define name2str(x) #x
  24. #define fuck(x) cout<<#x" = "<<x<<endl
  25. #define sfi(a) scanf("%d", &a)
  26. #define sffi(a, b) scanf("%d %d", &a, &b)
  27. #define sfffi(a, b, c) scanf("%d %d %d", &a, &b, &c)
  28. #define sffffi(a, b, c, d) scanf("%d %d %d %d", &a, &b, &c, &d)
  29. #define sfL(a) scanf("%lld", &a)
  30. #define sffL(a, b) scanf("%lld %lld", &a, &b)
  31. #define sfffL(a, b, c) scanf("%lld %lld %lld", &a, &b, &c)
  32. #define sffffL(a, b, c, d) scanf("%lld %lld %lld %lld", &a, &b, &c, &d)
  33. #define sfs(a) scanf("%s", a)
  34. #define sffs(a, b) scanf("%s %s", a, b)
  35. #define sfffs(a, b, c) scanf("%s %s %s", a, b, c)
  36. #define sffffs(a, b, c, d) scanf("%s %s %s %s", a, b,c, d)
  37. #define FIN freopen("../in.txt","r",stdin)
  38. #define gcd(a, b) __gcd(a,b)
  39. #define lowbit(x) x&-x
  40. #define IO iOS::sync_with_stdio(false)
  41.  
  42. using namespace std;
  43. typedef long long LL;
  44. typedef unsigned long long ULL;
  45. const ULL seed = ;
  46. const LL INFLL = 0x3f3f3f3f3f3f3f3fLL;
  47. const int maxn = 1e6 + ;
  48. const int maxm = 8e6 + ;
  49. const int INF = 0x3f3f3f3f;
  50. const int mod = 1e9 + ;
  51. char s[maxn];
  52. LL sum[maxn];
  53.  
  54. struct Palindrome_Automaton {
  55. int len[maxn], next[maxn][], fail[maxn], cnt[maxn];
  56. int num[maxn], S[maxn], sz, n, last;
  57.  
  58. int newnode(int l) {
  59. for (int i = ; i < ; ++i)next[sz][i] = ;
  60. cnt[sz] = num[sz] = , len[sz] = l;
  61. return sz++;
  62. }
  63.  
  64. void init() {
  65. sz = n = last = ;
  66. newnode();
  67. newnode(-);
  68. S[] = -;
  69. fail[] = ;
  70. }
  71.  
  72. int get_fail(int x) {
  73. while (S[n - len[x] - ] != S[n])x = fail[x];
  74. return x;
  75. }
  76.  
  77. void add(int c, int pos) {
  78. c -= 'a';
  79. S[++n] = c;
  80. int cur = get_fail(last);
  81. if (!next[cur][c]) {
  82. int now = newnode(len[cur] + );
  83. fail[now] = next[get_fail(fail[cur])][c];
  84. next[cur][c] = now;
  85. num[now] = num[fail[now]] + ;
  86. }
  87. last = next[cur][c];
  88. cnt[last]++;
  89. }
  90.  
  91. void count()//统计本质相同的回文串的出现次数
  92. {
  93. for (int i = sz - ; i >= ; --i)cnt[fail[i]] += cnt[i];
  94. //逆序累加,保证每个点都会比它的父亲节点先算完,于是父亲节点能加到所有子孙
  95. }
  96. } pam;
  97.  
  98. int main() {
  99. // FIN;
  100. while (~sfs(s + )) {
  101. pam.init();
  102. int n = strlen(s + );
  103. for (int i = ; i <= n; i++) {
  104. pam.add(s[i], i);
  105. sum[i] = sum[i - ] + pam.num[pam.last];
  106. }
  107. pam.init();
  108. LL ans = ;
  109. for (int i = n; i >= ; --i) {
  110. pam.add(s[i], i);
  111. ans += pam.num[pam.last] * sum[i - ];
  112. }
  113. printf("%lld\n", ans);
  114. }
  115. return ;
  116. }

Harry and magic string HDU - 5157 记录不相交的回文串对数的更多相关文章

  1. HDU - 5157 :Harry and magic string (回文树,求多少对不相交的回文串)

    Sample Input aca aaaa Sample Output 3 15 题意: 多组输入,每次给定字符串S(|S|<1e5),求多少对不相交的回文串. 思路:可以用回文树求出以每个位置 ...

  2. Girls' research - HDU 3294 (Manacher处理回文串)

    题目大意:给以一个字符串,求出来这个字符串的最长回文串,不过这个字符串不是原串,而是转换过的,转换的原则就是先给一个字符 例如 'b' 意思就是字符把字符b转换成字符 a,那么c->b, d-& ...

  3. HDU 5651 计算回文串个数问题(有重复的全排列、乘法逆元、费马小定理)

    原题: http://acm.hdu.edu.cn/showproblem.php?pid=5651 很容易看出来的是,如果一个字符串中,多于一个字母出现奇数次,则该字符串无法形成回文串,因为不能删减 ...

  4. (回文串 )Best Reward -- hdu -- 3613

    http://acm.hdu.edu.cn/showproblem.php?pid=3613 Best Reward Time Limit: 2000/1000 MS (Java/Others)    ...

  5. Manacher回文串算法学习记录

    FROM:  http://hi.baidu.com/chenwenwen0210/item/482c84396476f0e02f8ec230 #include<stdio.h> #inc ...

  6. HDU 5371(2015多校7)-Hotaru&#39;s problem(Manacher算法求回文串)

    题目地址:HDU 5371 题意:给你一个具有n个元素的整数序列,问你是否存在这样一个子序列.该子序列分为三部分,第一部分与第三部分同样,第一部分与第二部分对称.假设存在求最长的符合这样的条件的序列. ...

  7. hdu 1159 Palindrome(回文串) 动态规划

    题意:输入一个字符串,至少插入几个字符可以变成回文串(左右对称的字符串) 分析:f[x][y]代表x与y个字符间至少插入f[x][y]个字符可以变成回文串,可以利用动态规划的思想,求解 状态转化方程: ...

  8. YZOI Easy Round 2_回文串 string

    原文链接:http://laphets1.gotoip3.com/?id=18 Description 给出一个由小写字母组成的字符串,其中一些字母被染黑了,用?表示.已知原来的串不是 一个回文串,现 ...

  9. (最长回文串 模板) 最长回文 -- hdu -- 3068

    http://acm.hdu.edu.cn/showproblem.php?pid=3068 最长回文 Time Limit: 4000/2000 MS (Java/Others)    Memory ...

随机推荐

  1. 分析无法进入Linux系统的原因

    上文:http://www.cnblogs.com/long123king/p/3549701.html 1: static int __init kernel_init(void * unused) ...

  2. Bagging vs Boosting vs Stacking

    原文地址:https://www.jianshu.com/p/9dacdc88d3ec Bagging 用于减小方差. 使用训练数据的不同随机子集(大小常等于训练数据,采用不放回的方式进行抽取)来训练 ...

  3. OSPF中DR和BDR到底是谁先选举出来的?

    在OSPF的DRBDR选举的过程中,DR的选举依靠的是hello报文,在two-way之后,交互hello报文完成DR/BDR的选举. 那么在每台路由器根据收到的所有hello报文,会构建自己接口的数 ...

  4. exe自启动的几种方式

    1 注册表启动项目RUN (注册路径 HKEY_LOCAL_MACHINE\SOFTWARE\microsoft\Windows\CurrentVersion\Run) 2 计划任务 比较少见这种方式 ...

  5. MySQL慢查询日志分割

    mysql> set global slow_query_log=0; Query OK, 0 rows affected (0.00 sec)   mysql> set global s ...

  6. ECMS清除挂马以及后台升级实战(从ecms6.6~ecms7.0)

    当时状况 Windows Server 2008 R2 Enterprise + 帝国CMS6.6 + MySql   server软件: Microsoft-IIS/7.5 操作系统: WINNT ...

  7. Excel_VBA 常用代码

    单元格编辑后改变背景色(6号,355832828) Dim oldvalue As Variant Private Sub Worksheet_Change(ByVal Target As Range ...

  8. SB般的“WE战队”输掉了比赛

    事实再一次证明,对于LOL这种游戏,国服选手是根本就不能有一点期待的, 国服环境太好了,赢了可以吹,输了不能骂,自信心极度膨胀,估计WE俱乐部都没有个心理咨询师, 下岗了还可以再卖卖脸,卖卖饼, 国服 ...

  9. 多flavor导致的源码依赖问题-- Cannot choose between the following configurations of project :sample:

    一.背景: 当我们在源码依赖的时候经常会导致一些问题. 我们的主工程有如下配置,它依赖了一个sample的本地工程 flavorDimensions "demo" productF ...

  10. GarsiaWachs算法

    解决石子问题: 题目描述如下: 有n堆石子排成一列,每堆石子有一个重量w[i], 每次合并可以合并相邻的两堆石子,一次合并的代价为两堆石子的重量和w[i]+w[i+1].问安排怎样的合并顺序,能够使得 ...