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

Submit Status

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

Added by: The quick brown fox jumps over the lazy dog
Date: 2010-10-18
Time limit: 0.100s-0.170s
Source limit: 50000B
Memory limit: 1536MB
Cluster: Cube (Intel G860)
Languages: All
Resource: Udit Agarwal

回文树:
回文树是一种处理回文串的强大工具
#include <iostream>
#include <string.h>
#include <stdlib.h>
#include <algorithm>
#include <math.h>
#include <stdio.h> using namespace std;
#define MAX 1005
struct 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;
}

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

Submit Status

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

Added by: The quick brown fox jumps over the lazy dog
Date: 2010-10-18
Time limit: 0.100s-0.170s
Source limit: 50000B
Memory limit: 1536MB
Cluster: Cube (Intel G860)
Languages: All
Resource: Udit Agarwal

SPOJ Number of Palindromes(回文树)的更多相关文章

  1. SP7586 NUMOFPAL - Number of Palindromes(回文树)

    题意翻译 求一个串中包含几个回文串 题目描述 Each palindrome can be always created from the other palindromes, if a single ...

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

    [SPOJ]NUMOFPAL - Number of Palindromes(Manacher,回文树) 题面 洛谷 求一个串中包含几个回文串 题解 Manacher傻逼题 只是用回文树写写而已.. ...

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

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

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

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

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

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

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

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

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

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

  8. Gym - 101806Q:QueryreuQ(回文树)

    A string is palindrome, if the string reads the same backward and forward. For example, strings like ...

  9. HDU 5157 Harry and magic string(回文树)

    Harry and magic string Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/O ...

随机推荐

  1. word2016打开2007文档出错

    不知从何时起,我电脑上的一部分office文件打开时频频报错!!! 有一段时间发现,通过iis发布的网站上下载excel文件的时候,通过localhost下载的文件能够正常打开,但是通过IP和端口下载 ...

  2. jms、amqp、mqtt区别与联系

    消息传递作为基本通信机制已经在全世界成功运用.无论是人与人.机器与人还是机器与机器之间,消息传递一直都是唯一常用的通信方式.在双方(或更多)之间交换消息有两种基本机制. 同步消息传递 异步消息传递 同 ...

  3. SSDB(网络LevelDB)-- 实际遇到的问题

    简介 SSDB -- 支持网络的LevelDB 站点:https://github.com/ideawu/ssdb 我实际使用了SSDB支持网络+持久化特性,完成了一个集群 1.句柄数 ulimit ...

  4. Linux系统编程之----》信号

    "===信号========================================================================================= ...

  5. Linux SWAP 深度解读

    概述 本文讨论的swap基于Linux4.4内核代码.Linux内存管理是一套非常复杂的系统,而swap只是其中一个很小的处理逻辑. 希望本文能让读者了解Linux对swap的使用大概是什么样子.阅读 ...

  6. 正常断开连接情况下,判断非阻塞模式socket连接是否断开

    摘自:http://blog.chinaunix.net/uid-15014334-id-3429627.html 在UNIX/LINUX下, 1,对于主动关闭的SOCKET, recv返回-1,而且 ...

  7. ORACLE / PLSQL 插入或更新数据的几种方式

    第一种,直接用values 插入 ---日期需要用TO_DATE操作 INSERT INTO EMP (EMPNO,ENAME ,JOB,MGR, HIREDATE , SAL ,COMM, DEPT ...

  8. Jquery js框架使用

    jquery  众所周知 ,强大的 js框架 自己使用的一些笔记 //1.json格式定义方法 var product_obj={    check_init:function(){          ...

  9. 在Ubuntu中安装mongodb

    # 下载密钥文件 sudo apt-key adv --keyserver hkp://keyserver.ubuntu.com:80 --recv 7F0CEB10 gpg: 下载密钥‘7F0CEB ...

  10. 在Linux下搭建git服务器

    http://www.cnblogs.com/dee0912/p/5815267.html 步骤很详细,很受用