【SPOJ】NUMOFPAL - Number of Palindromes(Manacher,回文树)

题面

洛谷

求一个串中包含几个回文串

题解

Manacher傻逼题

只是用回文树写写而已。。

  1. #include<iostream>
  2. #include<cstdio>
  3. #include<cstdlib>
  4. #include<cstring>
  5. #include<cmath>
  6. #include<algorithm>
  7. #include<set>
  8. #include<map>
  9. #include<vector>
  10. #include<queue>
  11. using namespace std;
  12. #define ll long long
  13. #define RG register
  14. #define MAX 10000
  15. inline int read()
  16. {
  17. RG int x=0,t=1;RG char ch=getchar();
  18. while((ch<'0'||ch>'9')&&ch!='-')ch=getchar();
  19. if(ch=='-')t=-1,ch=getchar();
  20. while(ch<='9'&&ch>='0')x=x*10+ch-48,ch=getchar();
  21. return x*t;
  22. }
  23. int n;
  24. char s[MAX];
  25. int size[MAX];
  26. struct PT
  27. {
  28. struct Node
  29. {
  30. int son[26];
  31. int ff,len;
  32. }t[MAX];
  33. int last,tot;
  34. void init()
  35. {
  36. t[tot=1].len=-1;
  37. t[0].ff=t[1].ff=1;
  38. }
  39. void extend(int c,int n,char *s)
  40. {
  41. int p=last;
  42. while(s[n-t[p].len-1]!=s[n])p=t[p].ff;
  43. if(!t[p].son[c])
  44. {
  45. int v=++tot,k=t[p].ff;
  46. while(s[n-t[k].len-1]!=s[n])k=t[k].ff;
  47. t[v].len=t[p].len+2;
  48. t[v].ff=t[k].son[c];
  49. t[p].son[c]=v;
  50. }
  51. ++size[last=t[p].son[c]];
  52. }
  53. void Calc()
  54. {
  55. for(int i=tot;i;--i)size[t[i].ff]+=size[i];
  56. }
  57. }PT;
  58. int ans;
  59. int main()
  60. {
  61. PT.init();
  62. scanf("%s",s+1);
  63. n=strlen(s+1);
  64. for(int i=1;i<=n;++i)PT.extend(s[i]-97,i,s);
  65. PT.Calc();
  66. for(int i=2;i<=PT.tot;++i)ans+=size[i];
  67. printf("%d\n",ans);
  68. return 0;
  69. }

【SPOJ】NUMOFPAL - Number of Palindromes(Manacher,回文树)的更多相关文章

  1. 【CF245H】Queries for Number of Palindromes(回文树)

    [CF245H]Queries for Number of Palindromes(回文树) 题面 洛谷 题解 回文树,很类似原来一道后缀自动机的题目 后缀自动机那道题 看到\(n\)的范围很小,但是 ...

  2. CF245H Queries for Number of Palindromes(回文树)

    题意翻译 题目描述 给你一个字符串s由小写字母组成,有q组询问,每组询问给你两个数,l和r,问在字符串区间l到r的字串中,包含多少回文串. 输入格式 第1行,给出s,s的长度小于5000 第2行给出q ...

  3. 【Aizu2292】Common Palindromes(回文树)

    [Aizu2292]Common Palindromes(回文树) 题面 Vjudge 神TMD日语 翻译: 给定两个字符串\(S,T\),询问\((i,j,k,l)\)这样的四元组个数 满足\(S[ ...

  4. CF17E Palisection(manacher/回文树)

    CF17E Palisection(manacher/回文树) Luogu 题解时间 直接正难则反改成求不相交的对数. manacher求出半径之后就可以差分搞出以某个位置为开头/结尾的回文串个数. ...

  5. BZOJ.2565.[国家集训队]最长双回文串(Manacher/回文树)

    BZOJ 洛谷 求给定串的最长双回文串. \(n\leq10^5\). Manacher: 记\(R_i\)表示以\(i\)位置为结尾的最长回文串长度,\(L_i\)表示以\(i\)开头的最长回文串长 ...

  6. SPOJ Number of Palindromes(回文树)

    Number of Palindromes Time Limit: 100MS   Memory Limit: 1572864KB   64bit IO Format: %lld & %llu ...

  7. SP7586 NUMOFPAL - Number of Palindromes 解题报告

    SP7586 NUMOFPAL - Number of Palindromes 题意翻译 求一个串中包含几个回文串 输入输出格式 输入格式: The string S. 输出格式: The value ...

  8. URAL 2040 Palindromes and Super Abilities 2(回文树)

    Palindromes and Super Abilities 2 Time Limit: 1MS   Memory Limit: 102400KB   64bit IO Format: %I64d ...

  9. 回文树(回文自动机) - URAL 1960 Palindromes and Super Abilities

     Palindromes and Super Abilities Problem's Link: http://acm.timus.ru/problem.aspx?space=1&num=19 ...

随机推荐

  1. 【Oracle】-初识PL/SQL

    在最近的工作中要用到存储过程和函数,索性把PL/SQL整体的看一下.之前看过基本书和园子里的博文,在这里将所学简单总结. 一.基本语句 1.大小写 2.分隔符  --  : 3.引用字符串  --   ...

  2. mysql必知必会

    春节放假没事,找了本电子书mysql必知必会敲了下.用的工具是有道笔记的markdown文档类型. 下面是根据大纲已经敲完的章节,可复制到有道笔记的查看,更美观. # 第一章 了解SQL## 什么是S ...

  3. laravel服务容器-----深入理解控制反转(IoC)和依赖注入(DI)

    首先大家想一想什么是容器,字面意思就是盛放东西的东西,常见的变量,对象属性都是容器,一个容器能够装什么东西,完全在于你对这个容器的定义.有的容器不仅仅只是存文本,变量,而是对象,属性,那么我们通过这种 ...

  4. mysql 密码过期问题

    问题描述: Your password has expired. To log in you must change it using a client that supports expired p ...

  5. dedecms在任意页面调用任意栏目文章

    dedecms在任意页面调用任意栏目文章,我们用arclist标签即可实现.如果是调用多个栏目文章可以给typeid多个值. 我们以调用ID为1和ID为30的两个栏目下5篇文章为例: {dede:ar ...

  6. tomcat使用cookies缓存的时候中文报错解决办法 java.lang.IllegalArgumentException: Control character in cookie value or attribute.

    报错出现 java.lang.IllegalArgumentException: Control character in cookie value or attribute. at org.apac ...

  7. Java线程的六种状态

    java线程有很多种状态,最主要的有六种,被创建.运行.睡眠.等待.阻塞以及消亡六种,也有很多归结为5种,把睡眠以及等待归结为冻结: 被创建:就是线程被创建,就是new thread()之后就是创建一 ...

  8. Android开发之组件

    Android应用程序由组件组成,组件是可以解决被调用的基本功能模块.Android系统利用组件实现程序内部或程序间的模块调用,以解决代码复用问题,这是Android系统非常重要的特性.在程序设计时, ...

  9. linux虚拟化概述

    虚拟化硬件虚拟化:一台物理机虚拟出多台逻辑上的计算机cpu,内存可分配给多个虚拟机软件虚拟化:一个LAMP平台支撑多个网站桌面虚拟化...... 虚拟机:通过软件平台模拟出的计算机对最终用户来说,感受 ...

  10. 编写DAO,通过JdbcTemplate操作数据库的实践

    目的:编写DAO,通过Spring中的JdbcTemplate,对数据库中的学生数据进行增删改查操作. 要操作的数据库表结构为: 一.大体框架 1.要利用JdbcTemplate,首先要添加Sprin ...