SPOJ Number of Palindromes(回文树)
来源:互联网 发布:最好的建筑设计软件 编辑:程序博客网 时间:2024/04/30 00:55
Description
Each palindrome can be always created from the other palindromes, if a single character is also a palindrome. For example, the string "malayalam" can be created by some ways:
* malayalam = m + ala + y + ala + m
* malayalam = m + a + l + aya + l + a + m
We want to take the value of function NumPal(s) which is the number of different palindromes that can be created using the string S by the above method. If the same palindrome occurs more than once then all of them should be counted separately.
Input
The string S.
Output
The value of function NumPal(s).
Limitations
0 < |s| <= 1000
Example
Input:
malayalam
Output:
15
Hint
#include <iostream>#include <string.h>#include <stdlib.h>#include <algorithm>#include <math.h>#include <stdio.h>using namespace std;#define MAX 1005struct Node{ int next[26]; int len; int sufflink; int num;}tree[MAX];char s[MAX];int num;int suff;bool addLetter(int pos){ int cur=suff,curlen=0; int let=s[pos]-'a'; while(1) { curlen=tree[cur].len; if(pos-1-curlen>=0&&s[pos-1-curlen]==s[pos]) break; cur=tree[cur].sufflink; } if(tree[cur].next[let]) { suff=tree[cur].next[let]; return false; } num++; suff=num; tree[num].len=tree[cur].len+2; tree[cur].next[let]=num; if(tree[num].len==1) { tree[num].sufflink=2; tree[num].num=1; return true; } while(1) { cur=tree[cur].sufflink; curlen=tree[cur].len; if(pos-1-curlen>=0&&s[pos-1-curlen]==s[pos]) { tree[num].sufflink=tree[cur].next[let]; break; } } tree[num].num=1+tree[tree[num].sufflink].num; return true;}void initTree(){ num=2;suff=2; tree[1].len=-1;tree[1].sufflink=1; tree[2].len=0;tree[2].sufflink=1;}int main(){ scanf("%s",s); int len=strlen(s); initTree(); long long int ans=0; for(int i=0;i<len;i++) { addLetter(i); ans+=tree[suff].num; } printf("%d\n",ans); return 0;}
Description
Each palindrome can be always created from the other palindromes, if a single character is also a palindrome. For example, the string "malayalam" can be created by some ways:
* malayalam = m + ala + y + ala + m
* malayalam = m + a + l + aya + l + a + m
We want to take the value of function NumPal(s) which is the number of different palindromes that can be created using the string S by the above method. If the same palindrome occurs more than once then all of them should be counted separately.
Input
The string S.
Output
The value of function NumPal(s).
Limitations
0 < |s| <= 1000
Example
Input:
malayalam
Output:
15
Hint
- SPOJ Number of Palindromes(回文树)
- SPOJ NUMOFPAL - Number of Palindromes 水题 (Palindromic Tree 练习)
- HDOJ 3948 The Number of Palindromes 回文串自动机
- CF245H:Queries for Number of Palindromes(dp & 区间回文串数量)
- SPOJ NUMOFPAL 回文树
- H. Queries for Number of Palindromes
- hdu 3948 The Number of Palindromes
- 【CF245H】【Queries for Number of Palindromes】
- HDU 3948The Number of Palindromes
- codeforces 245H Queries for Number of Palindromes 区间DP
- CodeForces 245H Queries for Number of Palindromes (区间DP)
- 1.Minimal Number of Palindromes on a String(dp)
- Codeforces 245H Queries for Number of Palindromes
- codeforces Queries for Number of Palindromes 区间dp
- CF 245H Queries for Number of Palindromes
- HDOJ 3948 The Number of Palindromes 后缀数组
- HDU 3948 The Number of Palindromes(后缀数组)
- CodeForces245H - Queries for Number of Palindromes(区间dp)
- ZOJ1003 Crashing Balloon
- HDU-3750-Guess Game
- HDU2255 KM优化(n^3)模板
- Markdown的常用语法(个人总结)
- BZOJ 2055:80人环游世界
- SPOJ Number of Palindromes(回文树)
- ipyhon 与 ipython notebook
- c语言内嵌汇编语法
- java反射机制详解 及 Method.invoke解释 getMethod
- 后缀数组学习
- HDU5274 Dylans loves tree(树链剖分+异或)
- centos6.7 搭建 redis集群
- Linux驱动LCD driver学习总结
- linux命令大全(4)